Module:TNT and Module:TNT/sandbox: Difference between pages

From Bonkipedia
(Difference between pages)
Page 1
Page 2
m (1 revision imported: Module Import)
 
m (1 revision imported: Module Import)
 
Line 42: Line 42:
function p.msg(frame)
function p.msg(frame)
local dataset, id
local dataset, id
local params = {}
local params = { n = 0 }
local lang = nil
local lang = nil
for k, v in pairs(frame.args) do
for k, v in pairs(frame.args) do
Line 50: 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)
local i = k - 2
params[i] = mw.text.trim(v)
params.n = math.max(params.n, i)
elseif k == 'lang' and v ~= '_' then
elseif k == 'lang' and v ~= '_' then
lang = mw.text.trim(v)
lang = mw.text.trim(v)
Line 65: Line 67:
checkType('format', 1, dataset, 'string')
checkType('format', 1, dataset, 'string')
checkType('format', 2, key, 'string')
checkType('format', 2, key, 'string')
return formatMessage(dataset, key, {...})
return formatMessage(dataset, key, {n = select('#', ...), ...})
end
end


Line 77: Line 79:
checkType('formatInLanguage', 2, dataset, 'string')
checkType('formatInLanguage', 2, dataset, 'string')
checkType('formatInLanguage', 3, key, 'string')
checkType('formatInLanguage', 3, key, 'string')
return formatMessage(dataset, key, {...}, lang)
return formatMessage(dataset, key, {n = select('#', ...), ...}, lang)
end
end


Line 88: Line 90:
function p.doc(frame)
function p.doc(frame)
local dataset = 'Templatedata/' .. sanitizeDataset(frame.args[1])
local dataset = 'Templatedata/' .. sanitizeDataset(frame.args[1])
return frame:extensionTag('templatedata', p.getTemplateData(dataset)) ..
return frame:extensionTag('templatedata', p.getTemplateData(dataset, frame.args)) ..
  formatMessage(i18nDataset, 'edit_doc', {link(dataset)})
  formatMessage(i18nDataset, 'edit_doc', {link(dataset)})
end
end


function p.getTemplateData(dataset)
function p.getTemplateData(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 = loadData(dataset)
Line 100: Line 102:
end
end


local numOnly = true
local params = {}
local params = {}
local paramOrder = {}
local paramOrder = {}
Line 113: Line 116:
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 131:


-- 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
Line 156: Line 174:
-- 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


Line 183: Line 199:
local id, msg = unpack(row)
local id, msg = unpack(row)
if id == key then
if id == key then
local result = mw.message.newRawMessage(msg, unpack(params or {}))
local result = mw.message.newRawMessage(msg, unpack(params or {}, 1, params and params.n or nil))
return result:plain()
return result:plain()
end
end