Module:LoadData/sandbox
Appearance
From Wikipedia, the free encyclopedia
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
A more complex version of Module:Data with more options and the ability to select indexes with an inequality operator.
Usage
[edit ]{{#invoke:LoadData|Module name|index1|2 lteq=index2 limit|...|}}
- Zeroth parameter is the name of the data module to read, without Module:, e.g.
Example/data
- Next parameters, for an index N starting at one, are called
|N=
for the exact index you need, coerced to number if possible,|N lteq=
to select the highest numerical index less than or equal to the argument, or|N gteq=
for the opposite. |template=
is a printf-style string to interpolate the resulting value(s) into, e.g.<b>%s</b>
.|preprocess=
is like|template=
except thatframe:preprocess
is run on it; this makes e.g. template transclusions work.|if nil=
is the string to return if the result is nil. Default is nil, which comes out as the empty string.
The above documentation is transcluded from Module:LoadData/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.
localp={} -- Finds the next key key <= or >= the given i. -- operator is ±1 localfunctionfindItemRange(data,i,operator) localbestIndex=nil i=i*operator fork,vinpairs(data)do localkop=type(k)=='number'andk*operator ifkopandkop<=iand(bestIndex==nilorkop>bestIndex*operator)then bestIndex=k end end ifbestIndexthenreturndata[bestIndex]elsereturnnilend end localfunctionload(datamodule,frame) localargs=frame.args localdata=mw.loadData(datamodule) fori=1,20do ifargs[i]thendata=data[tonumber(args[i])orargs[i]] elseifargs[i..' lteq']then data=findItemRange(data,tonumber(args[i..' lteq']),1) elseifargs[i..' gteq']then data=findItemRange(data,tonumber(args[i..' gteq']),-1) elsebreakend end ifdata==nilthen returnargs['if_nil']-- not a required argument, OK to return nil here. end iftype(data)=='table'then -- Put the table into another table because the return value of loadData -- is a "fake" table that only has certain metamethods. localrealdata={} fork,vinpairs(data)do realdata[k]=v end data=realdata else data={data} end ifargs['template']then returnmw.text.unstripNoWiki(args['template']):format(unpack(data)) elseifargs['preprocess']then returnframe:preprocess(mw.text.unstripNoWiki(args['preprocess']):format(unpack(data))) else returntable.concat(data) end end returnsetmetatable({},{ __index=function(t,k) returnfunction(frame) ifk:sub(-5)=='.json'then returnload(mw.text.jsonDecode(mw.title.new(k).getContent()),frame) else returnload(mw.loadData('Module:'..k),frame) end end end })