Module:Data: Difference between revisions
Appearance
Content deleted Content added
MusikAnimal (talk | contribs) m Protected Module:Data: Highly visible template ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite)) |
Jackmcbarn (talk | contribs) Improve error messages |
||
Line 4: | Line 4: | ||
return function(frame) |
return function(frame) |
||
local data = mw.loadData(k) |
local data = mw.loadData(k) |
||
local i = 1 |
|||
for _,v in ipairs(frame.args) do |
for _,v in ipairs(frame.args) do |
||
local ty = type(data) |
|||
if ty ~= 'table' then |
|||
if frame.args.softfail then |
|||
return '<span class="error">[[Category:Pages with failed Module:Data lookups]]Error: Tried to read index "' .. mw.text.nowiki(v) .. '" of mw.loadData("' .. mw.text.nowiki(k) .. '").' .. mw.text.nowiki(table.concat(frame.args, '.', 1, i - 1)) .. ', which is a ' .. ty .. '</span>' |
|||
else |
|||
error('Tried to read index "' .. v .. '" of mw.loadData("' .. k .. '").' .. table.concat(frame.args, '.', 1, i - 1) .. ', which is a ' .. ty) |
|||
end |
|||
end |
|||
data = data[v] |
data = data[v] |
||
i = i + 1 |
|||
end |
end |
||
return data |
return data |
Revision as of 00:34, 15 January 2021
![]() | This Lua module is used on approximately 153,000 pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This module allows wikitext to read values from data modules.
Usage
{{#invoke:Data|Module:ModuleName|key1|key2|key3|...}}
Read data value key1.key2.key3 from Module:ModuleName.
local mt = {}
function mt.__index(t, k)
return function(frame)
local data = mw.loadData(k)
local i = 1
for _,v in ipairs(frame.args) do
local ty = type(data)
if ty ~= 'table' then
if frame.args.softfail then
return '<span class="error">[[Category:Pages with failed Module:Data lookups]]Error: Tried to read index "' .. mw.text.nowiki(v) .. '" of mw.loadData("' .. mw.text.nowiki(k) .. '").' .. mw.text.nowiki(table.concat(frame.args, '.', 1, i - 1)) .. ', which is a ' .. ty .. '</span>'
else
error('Tried to read index "' .. v .. '" of mw.loadData("' .. k .. '").' .. table.concat(frame.args, '.', 1, i - 1) .. ', which is a ' .. ty)
end
end
data = data[v]
i = i + 1
end
return data
end
end
return setmetatable({}, mt)