Module:TNT: Difference between revisions

754 bytes added ,  2 years ago
m
16 revisions imported from wikipedia:Module:TNT
(fix doc translation link)
m (16 revisions imported from wikipedia:Module:TNT)
 
(17 intermediate revisions by 10 users not shown)
Line 36:
local p = {}
local i18nDataset = 'I18n/Module:TNT.tab'
local checkType = require('libraryUtil').checkType
 
-- Forward declaration of the local functions
Line 51 ⟶ 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)
Line 60 ⟶ 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)
checkType('format', 4, lang, 'string', true)
return formatMessage(dataset, key, params, lang)
end
 
 
-- Converts first parameter to a interwiki-ready link. For example, it converts
-- Identical to p.msg() above, but used from other lua modules with the language param
-- "Sandbox/Sample.tab" -> 'commons:Data:Sandbox/Sample.tab'
-- 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
 
-- Obsolete function that adds a 'c:' prefix to the first param.
-- "Sandbox/Sample.tab" -> 'commonsc:Data:Sandbox/Sample.tab'
function p.link(frame)
return link(frame.args[1])
Line 84 ⟶ 96:
local data = loadData(dataset)
local names = {}
for _, field in pairsipairs(data.schema.fields) do
table.insert(names, field.name)
end
Line 90 ⟶ 102:
local params = {}
local paramOrder = {}
for _, row in pairsipairs(data.data) do
local newVal = {}
local name = nil
for pos, val in pairsipairs(row) do
local columnName = names[pos]
if columnName == 'name' then
Line 141 ⟶ 153:
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('Missing JsonConfig extension; Cannot load https://commons.wikimedia.org/wiki/Data:' .. dataset)
end
 
Line 158 ⟶ 175:
-- Given a dataset name, convert it to a title with the 'commons:data:' prefix
link = function(dataset)
dataset =return 'c:Data:' .. mw.text.trim(dataset or '')
if mw.site.siteName == 'Wikimedia Commons' then
return dataset
else
return 'commons:' .. dataset
end
end