Module:TNT: Difference between revisions

From Test Wiki
Content deleted Content added
m Changed protection level for "Module:TNT": High-risk Lua module: per request at WP:RFPP to match Module:Excerpt ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))
 
m 1 revision imported
 
(13 intermediate revisions by 9 users not shown)
Line 50: Line 50:
id = mw.text.trim(v)
id = mw.text.trim(v)
elseif type(k) == 'number' then
elseif type(k) == 'number' then
table.insert(params, mw.text.trim(v))
params[k - 2] = mw.text.trim(v)
elseif k == 'lang' and v ~= '_' then
elseif k == 'lang' and v ~= '_' then
lang = mw.text.trim(v)
lang = mw.text.trim(v)
Line 86: Line 86:
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])
local json, dataPage, categories = implGetTemplateData(dataset)
return frame:extensionTag('templatedata', p.getTemplateData(dataset)) ..
return frame:extensionTag('templatedata', json) ..
formatMessage(i18nDataset, 'edit_doc', {link(dataset)})
formatMessage(i18nDataset, 'edit_doc', {link(dataPage)}) ..
(categories or "");
end
end


function p.getTemplateData(dataset)
function p.getTemplateData(dataset)
local data = implGetTemplateData(dataset, true);
return data;
end

function implGetTemplateData(dataset, legacy)
-- 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 pairs(data.schema.fields) do
for _, field in ipairs(data.schema.fields) do
table.insert(names, field.name)
table.insert(names, field.name)
end
end


local numOnly = true
local params = {}
local params = {}
local paramOrder = {}
local paramOrder = {}
for _, row in pairs(data.data) do
for _, row in ipairs(data.data) do
local newVal = {}
local newVal = {}
local name = nil
local name = nil
for pos, val in pairs(row) do
for pos, columnName in ipairs(names) do
local columnName = names[pos]
if columnName == 'name' then
if columnName == 'name' then
name = val
name = row[pos]
else
else
newVal[columnName] = val
newVal[columnName] = row[pos]
end
end
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 120: Line 138:


-- Work around json encoding treating {"1":{...}} as an [{...}]
-- Work around json encoding treating {"1":{...}} as an [{...}]
if numOnly then
params['zzz123']=''
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,
})
})


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


return json
return json, dataPage, categories;
end
end


Line 149: Line 171:
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 157: Line 179:
-- 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
categories = '[[Category:Templates using legacy global TemplateData table name]]';
dataPage = 'Templatedata/' .. dataset;
end
end
else
data = mw.ext.data.get(dataset, lang)
end


if data == false then
if data == false then
Line 170: Line 208:
end
end
end
end
return data
return data, dataPage, categories
end
end