Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:TNT: Difference between revisions

From WikiOasis Meta
m 1 revision imported from metawikimedia:Module:TNT: Importing Template:Sockpuppet from the Meta Wiki Wikimedia project wiki
m 21 revisions imported
 
(22 intermediate revisions by 11 users not shown)
Line 33: Line 33:
--        if the current page is Template:Graph:Lines/doc
--        if the current page is Template:Graph:Lines/doc
--
--
local config = (function()
local ok, res = pcall(mw.loadData, "Module:TNT/config");
return ok and res or {};
end)();


local p = {}
local p = {}
Line 86: Line 91:
end
end


local implGetTemplateData;
function p.doc(frame)
function p.doc(frame)
local dataset = 'Templatedata/' .. sanitizeDataset(frame.args[1])
local dataset = sanitizeDataset(frame.args[1])
return frame:extensionTag('templatedata', p.getTemplateData(dataset)) ..
local json, dataPage, categories = implGetTemplateData(nil, dataset, frame.args)
  formatMessage(i18nDataset, 'edit_doc', {link(dataset)})
return frame:extensionTag('templatedata', json) ..
formatMessage(i18nDataset, 'edit_doc', {link(dataPage)}) ..
(categories or "");
end
end


function p.getTemplateData(dataset)
function p.getTemplateData(dataset)
local data = implGetTemplateData(true, dataset);
return data;
end
function p.getTemplateDataNew(...)
return implGetTemplateData(nil, ...);
end
function implGetTemplateData(legacy, dataset, args)
-- TODO: add '_' parameter once lua starts reindexing properly for "all" languages
-- TODO: add '_' parameter once lua starts reindexing properly for "all" languages
local data = loadData(dataset)
local data, dataPage, categories = loadData(
dataset, nil, not legacy and 'TemplateData' or nil);
local names = {}
local names = {}
for _, field in ipairs(data.schema.fields) do
for _, field in ipairs(data.schema.fields) do
Line 100: Line 118:
end
end


local numOnly = true
local params = {}
local params = {}
local paramOrder = {}
local paramOrder = {}
Line 113: Line 132:
end
end
if name then
if name then
if (
(type(name) ~= "number")
and (
(type(name) ~= "string")
or not string.match(name, "^%d+$")
)
) then
numOnly = false
end
params[name] = newVal
params[name] = newVal
table.insert(paramOrder, name)
table.insert(paramOrder, name)
Line 119: Line 147:


-- Work around json encoding treating {"1":{...}} as an [{...}]
-- Work around json encoding treating {"1":{...}} as an [{...}]
params['zzz123']=''
if numOnly then
params['zzz123']=''
end


local json = mw.text.jsonEncode({
local json = mw.text.jsonEncode({
params=params,
params=params,
paramOrder=paramOrder,
paramOrder=paramOrder,
description=data.description
description=data.description,
-- TODO: Store this in a dataset:
format = (args and args.format or nil),
})
})


json = string.gsub(json,'"zzz123":"",?', "")
if numOnly then
json = string.gsub(json,'"zzz123":"",?', "")
end


return json
return json, dataPage, categories;
end
end


Line 148: Line 182:
end
end


loadData = function(dataset, lang)
loadData = function(dataset, lang, dataType)
dataset = sanitizeDataset(dataset)
dataset = sanitizeDataset(dataset)
if not dataset then
if not dataset then
Line 156: Line 190:
-- Give helpful error to thirdparties who try and copy this module.
-- Give helpful error to thirdparties who try and copy this module.
if not mw.ext or not mw.ext.data or not mw.ext.data.get then
if not mw.ext or not mw.ext.data or not mw.ext.data.get then
error('Missing JsonConfig extension; Cannot load https://commons.wikimedia.org/wiki/Data:' .. dataset)
error(string.format([['''Missing JsonConfig extension, or not properly configured;
Cannot load https://commons.wikimedia.org/wiki/Data:%s.
See https://www.mediawiki.org/wiki/Extension:JsonConfig#Supporting_Wikimedia_templates''']], dataset))
end
end


local data = mw.ext.data.get(dataset, lang)
local dataPage = dataset;
local data, categories;
if dataType == 'TemplateData' then
dataPage = 'TemplateData/' .. dataset;
data = mw.ext.data.get(dataPage, lang);
if data == false then
data = mw.ext.data.get('Templatedata/' .. dataset, lang);
if data ~= false then
local legacyTemplateDataCategoryName = config.legacyTemplateDataCategoryName;
if legacyTemplateDataCategoryName ~= false then
categories = string.format(
'[[Category:%s%s]]',
legacyTemplateDataCategoryName or "Templates using legacy global TemplateData table name",
config.translatableCategoryLink and mw.getCurrentFrame():callParserFunction("#translation:") or ""
);
end
dataPage = 'Templatedata/' .. dataset;
end
end
else
data = mw.ext.data.get(dataset, lang)
end


if data == false then
if data == false then
Line 166: Line 223:
error('Missing Commons dataset ' .. i18nDataset)
error('Missing Commons dataset ' .. i18nDataset)
else
else
error(formatMessage(i18nDataset, 'error_bad_dataset', {link(dataset)}))
error(formatMessage(i18nDataset, 'error_bad_dataset', {link(dataPage)}))
end
end
end
end
return data
return data, dataPage, categories;
end
end


Cookies help us deliver our services. By using our services, you agree to our use of cookies.