Jump to content

Module:CountryData

Permanently protected module
From Wikipedia, the free encyclopedia

localp={}localmostUsed=mw.loadData('Module:CountryData/summary')localfunctiongetcontents(frame,country,params)returnframe:expandTemplate({title="Country data "..country;args=params})endfunctionp.getcachedtable(frame,country,params)country=mostUsed.redirects[country]orcountryifparamsandnext(params)thenreturnp.gettable(frame,country,params)end-- Uses mw.loadData to cache data for the most-used templatesifmostUsed.pages[country]thenlocalcache=mw.loadData('Module:CountryData/cache'..mostUsed.pages[country])ifcache.data[country]thenreturncache.data[country]endend-- if not in cachereturnp.gettable(frame,country,params)endfunctionp.gettable(frame,country,params)--Returns the parameters of a country data template as a Lua table--If not a valid data template, return empty tablelocalbool,s=pcall(getcontents,frame,country,paramsor{})ifbooland(string.find(s,"^%{%{ *%{%{%{1")orstring.find(s,"^%{%{safesubst: *%{%{%{1"))then--Replace parameter delimiters with arbitrary control characters--to avoid clashes if param values contain equals/pipe signss=string.gsub(s,"|([^|=]-)=","\1\1%1\2")s=string.gsub(s,"}}%s*$","\1")--Loop over string and add params to tablelocalpart={}forparinstring.gmatch(s,"\1[^\1\2]-\2[^\1\2]-\1")dolocalk=string.match(par,"\1%s*(.-)%s*\2")localv=string.match(par,"\2%s*(.-)%s*\1")ifvandnot(v==""andstring.find(k,"^flag alias"))thenpart[k]=vendendreturnpartelsereturn{}endendfunctionp.getalias(frame)--Returns a single parameter value from a data templatelocalpart=p.gettable(frame,frame.args[1])ifframe.args.variantthenreturntostring(part[frame.args[2].."-"..frame.args.variant]orpart[frame.args[2]]orframe.args.def)elsereturntostring(part[frame.args[2]]orframe.args.def)endendfunctionp.gettemplate(frame)--For testing, recreates the country data from the created Lua table--Get data tablelocaldata=p.gettable(frame,frame.args[1])--Concatenate fields into a template-like stringlocalout="{{ {{{1}}}"fork,vinpairs(data)doout=out.."\n| "..k.." = "..vendreturnout.."\n}}"endreturnp
close