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

Editing Module:TNT

From WikiOasis Meta
Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
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 55: Line 50:
id = mw.text.trim(v)
id = mw.text.trim(v)
elseif type(k) == 'number' then
elseif type(k) == 'number' then
params[k - 2] = mw.text.trim(v)
table.insert(params, 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 91: Line 86:
end
end


local implGetTemplateData;
function p.doc(frame)
function p.doc(frame)
local dataset = sanitizeDataset(frame.args[1])
local dataset = 'Templatedata/' .. sanitizeDataset(frame.args[1])
local json, dataPage, categories = implGetTemplateData(nil, dataset, frame.args)
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(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, dataPage, categories = loadData(
local data = loadData(dataset)
dataset, nil, not legacy and 'TemplateData' or nil);
local names = {}
local names = {}
for _, field in ipairs(data.schema.fields) do
for _, field in pairs(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 ipairs(data.data) do
for _, row in pairs(data.data) do
local newVal = {}
local newVal = {}
local name = nil
local name = nil
for pos, columnName in ipairs(names) do
for pos, val in pairs(row) do
local columnName = names[pos]
if columnName == 'name' then
if columnName == 'name' then
name = row[pos]
name = val
else
else
newVal[columnName] = row[pos]
newVal[columnName] = val
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 147: Line 120:


-- 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
-- TODO: Store this in a dataset:
format = (args and args.format or nil),
})
})


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


return json, dataPage, categories;
return json
end
end


Line 182: Line 149:
end
end


loadData = function(dataset, lang, dataType)
loadData = function(dataset, lang)
dataset = sanitizeDataset(dataset)
dataset = sanitizeDataset(dataset)
if not dataset then
if not dataset then
Line 190: Line 157:
-- 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(string.format([['''Missing JsonConfig extension, or not properly configured;
error('Missing JsonConfig extension; Cannot load https://commons.wikimedia.org/wiki/Data:' .. dataset)
Cannot load https://commons.wikimedia.org/wiki/Data:%s.
See https://www.mediawiki.org/wiki/Extension:JsonConfig#Supporting_Wikimedia_templates''']], dataset))
end
end


local dataPage = dataset;
local data = mw.ext.data.get(dataset, lang)
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 223: Line 167:
error('Missing Commons dataset ' .. i18nDataset)
error('Missing Commons dataset ' .. i18nDataset)
else
else
error(formatMessage(i18nDataset, 'error_bad_dataset', {link(dataPage)}))
error(formatMessage(i18nDataset, 'error_bad_dataset', {link(dataset)}))
end
end
end
end
return data, dataPage, categories;
return data
end
end


Please note that all contributions to WikiOasis Meta are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) (see Meta:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)

 

Page included on this page:

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