Module:Data/sandbox
Appearance
From Wikipedia, the free encyclopedia
Warning 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.
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.
[画像:Ready for use] This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing.
[画像:Protected] 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
[edit ]{{#invoke:Data|Module:ModuleName|key1|key2|key3|...}}
Read data value key1.key2.key3 from Module:ModuleName.
The above documentation is transcluded from Module:Data/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
local mt = {} function mt.__index(t, k) return function(frame) local success, data = pcall(mw.loadData, k) if not success then success, data = pcall(mw.loadJsonData, k) if not success then error("'" .. k .. "' is not a valid data page") end end for i, v in ipairs(frame.args) do local ty = type(data) if ty ~= 'table' then local args = {} for j = 1, i - 1 do args[j] = frame.args[j] end 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(args, '.')) .. ', which is a ' .. ty .. '</span>' else error('Tried to read index "' .. v .. '" of mw.loadData("' .. k .. '").' .. table.concat(args, '.') .. ', which is a ' .. ty) end end local nextdata = data[v] if nextdata == nil and tonumber(v) then data = data[tonumber(v)] else data = nextdata end end return data end end return setmetatable({}, mt)