Module:JSONPageList

-- Demo for T332484 require( 'strict' ) local r={} --local lang="en" local site="enwiki" local siteprefix="en:"

local function get_link(qid, label) local sitelink=mw.wikibase.getSitelink(qid,site) label=label or mw.wikibase.getLabel(qid) if sitelink then return ..label.. else return ..label.. end end

local function get_heading(field) if field:match(":(.*)$") then return field:match(":(.*)$") elseif field=='@link' then return 'item' elseif field:sub(1,1)=='$' then local entity_id=field:sub(2):match("([PQpq]%d+)$") return mw.wikibase.getLabel(entity_id) else return field end end

local function get_cell(field, item, data_fields_map) if field:match("(.*):.*$") then field=field:match("(.*):.*$") end local qid=item._qid if field=='@link' then if qid then return get_link(qid, item._name) else return ..item._name.. end elseif field:sub(1,1)=='$' then local entity_path=field:sub(2) if entity_path:match("^([PQpq]%d+)$") then return mw.getCurrentFrame:callParserFunction("#statements",{entity_path, from=qid}) end else if item[field] then return tostring(item[field]) elseif qid and data_fields_map[field] and data_fields_map[field].property then local vals=mw.wikibase.getBestStatements(qid, data_fields_map[field].property) if data_fields_map[field].enum_map and vals and vals[1] and vals[1].mainsnak.datavalue.value and data_fields_map[field].enum_map[vals[1].mainsnak.datavalue.value.id] then return tostring(data_fields_map[field].enum_map[vals[1].mainsnak.datavalue.value.id]) end end if data_fields_map[field] and data_fields_map[field].default then return tostring(data_fields_map[field].default) end end return '...' end

function r.table(pagename, result_fields) if type(pagename) == "table" then pagename, result_fields = pagename.args[1], pagename.args[2] end local content if mw.title.new( pagename ).namespace==828 then content=require(pagename) else content=mw.title.new(pagename):getContent content=mw.text.jsonDecode(content) end site=content.site or site siteprefix=content.siteprefix or siteprefix local data_fields=content.fields or {} local data_fields_map={} for _, v in ipairs( data_fields ) do       if type(v)=="table" then data_fields_map[v[1]]=v end if v.enum then v.enum_map={} for _, p in ipairs( v.enum ) do               if type(p)=='table' then v.enum_map[p[2]]=p[1] end end end end if not result_fields or mw.text.trim(result_fields)=='' then result_fields={'@link'} for _, v in ipairs( data_fields ) do           table.insert(result_fields,v[1] .. (v.display_name and (":"..v.display_name) or '')) end else result_fields=mw.text.split(result_fields,',') end local items=content.items for i, v in ipairs( items ) do       if type(v) == "string" then items[i]={_qid=v} end end local result=mw.html.create("table"):attr( 'class', 'wikitable' ) local heading=result:tag("tr") for i, field in ipairs( result_fields ) do       field=mw.text.trim(field) result_fields[i]=field heading:tag("th"):wikitext(get_heading(field)) end for _, item in ipairs( items ) do       local row=result:tag("tr") for _, field in ipairs( result_fields ) do           row:tag("td"):wikitext(get_cell(field,item,data_fields_map)) end end return tostring( result ) end

return r