Modulo:TNT: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m Copying 4 changes by Yurik: "optimize libraryUtil loading only when needed, fix templatedata doc having just the positional parameters, make i18n/ prefix optional, support for usage from modules, no auto I18n/ prefix, breaks some graphs" from mw:Module:TNT. (docs, translate)
Update from master using #Synchronizer
 
(2 versioni intermedie di un altro utente non mostrate)
Riga 50:
id = mw.text.trim(v)
elseif type(k) == 'number' then
table.insert(params,[k - 2] = mw.text.trim(v))
elseif k == 'lang' and v ~= '_' then
lang = mw.text.trim(v)
Riga 59:
 
-- Identical to p.msg() above, but used from other lua modules
function-- Parameters: name of p.format(dataset, message key, params,optional lang)arguments
-- Example with 2 params: format('I18n/Module:TNT', 'error_bad_msgkey', 'my-key', 'my-dataset')
function p.format(dataset, key, ...)
local checkType = require('libraryUtil').checkType
checkType('format', 1, dataset, 'string')
checkType('format', 2, key, 'string')
return formatMessage(dataset, key, params, lang{...})
checkType('format', 3, params, 'table', true)
end
checkType('format', 4, lang, 'string', true)
 
return formatMessage(dataset, key, params, lang)
 
-- Identical to p.msg() above, but used from other lua modules with the language param
-- Parameters: language code, name of dataset, message key, optional arguments
-- Example with 2 params: formatInLanguage('es', I18n/Module:TNT', 'error_bad_msgkey', 'my-key', 'my-dataset')
function p.formatInLanguage(lang, dataset, key, ...)
local checkType = require('libraryUtil').checkType
checkType('formatformatInLanguage', 41, lang, 'string', true)
checkType('formatInLanguage', 2, dataset, 'string')
checkType('formatformatInLanguage', 3, paramskey, 'tablestring', true)
return formatMessage(dataset, key, {...}, lang)
end
 
Riga 84 ⟶ 96:
local data = loadData(dataset)
local names = {}
for _, field in pairsipairs(data.schema.fields) do
table.insert(names, field.name)
end
 
local numOnly = true
local params = {}
local paramOrder = {}
for _, row in pairsipairs(data.data) do
local newVal = {}
local name = nil
for pos, valcolumnName in pairsipairs(rownames) do
local columnName = names[pos]
if columnName == 'name' then
name = valrow[pos]
else
newVal[columnName] = valrow[pos]
end
end
if name then
if (
(type(name) ~= "number")
and (
(type(name) ~= "string")
or not string.match(name, "^%d+$")
)
) then
numOnly = false
end
params[name] = newVal
table.insert(paramOrder, name)
Riga 108 ⟶ 129:
 
-- Work around json encoding treating {"1":{...}} as an [{...}]
if numOnly then
params['zzz123']=''
end
 
local json = mw.text.jsonEncode({
params=params,
paramOrder=paramOrder,
description=data.description,
})
 
if numOnly then
json = string.gsub(json,'"zzz123":"",?', "")
end
 
return json
Riga 141 ⟶ 166:
if not dataset then
error(formatMessage(i18nDataset, 'error_no_dataset', {}))
end
 
-- 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
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