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 and not item._page 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

local function parse_params(pagename, result_fields, params) if type(pagename) == "table" then pagename, result_fields, params = pagename.args[1], pagename.args[2], pagename end if params:getParent and params:getParent.args[1] then pagename, result_fields, params = params:getParent.args[1], params:getParent.args[2], params:getParent end return pagename, result_fields, params end

local function get_content(pagename) 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 return content end

local function set_site(content) site=content.site or site siteprefix=content.siteprefix or siteprefix end

local function get_fields_and_items(content) 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 local items=content.items for i, v in ipairs( items ) do       if type(v) == "string" then items[i]={_qid=v} end end return data_fields, data_fields_map, items end

local function populate_result_fields(result_fields, data_fields) 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 return result_fields end

local function parse_data(pagename, result_fields, params) pagename, result_fields, params = parse_params(pagename, result_fields, params) local content=get_content(pagename) set_site(content) local data_fields, data_fields_map, items=get_fields_and_items(content) result_fields=populate_result_fields(result_fields, data_fields) return pagename, result_fields, params, content, data_fields, data_fields_map, items end

function r.table(pagename, result_fields, params) local content, data_fields, data_fields_map, items pagename, result_fields, params, content, data_fields, data_fields_map, items=parse_data(pagename, result_fields, params) 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

function r.plainlist(pagename, result_fields, params) local content, data_fields, data_fields_map, items pagename, result_fields, params, content, data_fields, data_fields_map, items=parse_data(pagename, result_fields, params) local result={} for _, item in ipairs( items ) do       table.insert(result,get_cell("@link",item,data_fields_map)) end return table.concat(result,',') end

function r.navbox(pagename, result_fields, params) local content, data_fields, data_fields_map, items pagename, result_fields, params, content, data_fields, data_fields_map, items=parse_data(pagename, result_fields, params) local result={} for _, item in ipairs( items ) do       table.insert(result,'* '..get_cell("@link",item,data_fields_map)..'\n') end return require('Module:Navbox')._navbox({       title='Navbox',        list1=table.concat(result)    }) end return r