--
local p =
local i18n =
-- numerically sort sequential tables whose values contain a number, like "350 MW"-- sort on first number foundlocal function numcomp1(x, y) x = tonumber(tostring(x):match("%d+")) or 0 y = tonumber(tostring(y):match("%d+")) or 0 return x < yend
-- numerically sort sequential tables whose values contain two numbers, like "1 x 350 MW"-- sort on second number foundlocal function numcomp2(x, y) x = tonumber(tostring(x):match("%d+%D+(%d+)")) or 0 y = tonumber(tostring(y):match("%d+%D+(%d+)")) or 0 return x < yend
-- alphabetically sort sequential tables whose values may contain wikilinks.-- Formats: "Text" or "Link" or "Text"local function linkcomp(a, b) -- a = a:gsub("%[%[.*|", ""):gsub("%[%[", ""):gsub("]]","") -> test for best a = a:match("%[%[.*|(.*)]]") or a:match("%[%[(.*)]]") or a b = b:match("%[%[.*|(.*)]]") or b:match("%[%[(.*)]]") or b return a < bend
--Render monolingual textlocal function rendermlt(props, langcode) for k, v in ipairs(props) do v = v.mainsnak or v if v.snaktype
langcode then return v.datavalue.value.text end endend
-- Render quantity from snaklocal function renderqty(snak, langcode) if snak and snak.snaktype
"value" and v2.mainsnak.datavalue.value.language
-- Take a qid and return the shortname (P1813) or label, linked to an article if possiblelocal function linkedname(qid, langcode) local props1813 = mw.wikibase.getBestStatements(qid, "P1813") -- may have to use mw.wikibase.getLabelByLang(qid, langcode) on multi-lingual wikis: local lbl = rendermlt(props1813, langcode) or mw.wikibase.getLabel(qid) local lnk = mw.wikibase.getSitelink(qid) return lnk and lbl and ("" .. lbl .."") or lnk and ("" .. lnk .. "") or lblend
p.psunits = function(frame) local args = frame.args local psu_op = args.ps_units_operational or "" local psu_mm = args.ps_units_manu_model or "" local psu_uc = args.ps_units_uc or "" local psu_dc = args.ps_units_decommissioned or "" local psu_pl = args.ps_units_planned or "" local psu_ca = args.ps_units_cancelled or "" local qid = args.qid or "" if qid
local langcode = args.lang or "" if langcode
local status = local mm = local cap = local num =
local props516 = mw.wikibase.getBestStatements(qid, "P516") if #props516 > 0 then for i1, v1 in ipairs(props516) do -- set default count of this engine to 1 num[i1] = 1 -- set default status of this engine to planned status[i1] = "pl" -- model should be value of P516, get manufacturer from the linked P176 and capacity from linked P2109 -- if there is a value that isn't a model, just use the value local mdlqid = (v1.mainsnak.snaktype
"value") and props176snak.datavalue.value.id if mfrqid then -- look for a shortname to use for manufacturer display label, otherwise use manufacturer label mfr = linkedname(mfrqid, langcode) end end mm[i1] = mfr and mdl and (mfr .. " " .. mdl) or mfr or mdl -- get default capacity local props2109snak = mw.wikibase.getBestStatements(mdlqid, "P2109")[1] props2109snak = props2109snak and props2109snak.mainsnak cap[i1] = renderqty(props2109snak, langcode) elseif v1.mainsnak.snaktype
local quals = v1.qualifiers if quals then -- determine status from service retirement/entry/inception local dcsnak = quals.P730 and quals.P730[1].snaktype local opsnak = quals.P729 and quals.P729[1].snaktype local ucsnak = quals.P571 and quals.P571[1].snaktype if dcsnak
"somevalue" then status[i1] = "dc" elseif opsnak
"somevalue" then status[i1] = "op" elseif ucsnak
"somevalue" then status[i1] = "uc" end -- override if state of use (P5817) is cancelled-abandoned (Q30108381) if quals.P5817 and quals.P5817[1].snaktype
"Q30108381" then status[i1] = "ca" end
-- override default capacity from qualifier P2109 if available if quals.P2109 and quals.P2109[1].snaktype
-- if quantity (P1114) is given, replace num value if quals.P1114 and quals.P1114[1].snaktype
-- convert capacity in kW to MW if (cap[i1] or ""):sub(-2)
-- construct set of manufacturers and models of operational units -- key is the manufacturer + model and value is count of that local opmm = for i, v in ipairs(status) do if v
"" then psu_mm = table.concat(opmmseq, "
") end
-- construct sets of capacities of operational units (opcap), -- units under construction (uccap), decommissioned (dccap)], -- planned (plcap) and cancelled (cacap) -- key is the capacity and value is count of that capacity. local opcap, uccap, dccap, plcap, cacap =,,,, for i, v in ipairs(status) do if v
"op" and cap[i] then opcap[cap[i]] = (opcap[cap[i]] or 0) + num[i] end if v
"pl" and cap[i] then plcap[cap[i]] = (plcap[cap[i]] or 0) + num[i] end if v
"" then psu_uc = table.concat(uccapseq, "
") end -- operational local opcapseq = for k, v in pairs(opcap) do opcapseq[#opcapseq+1] = v .. " × " .. k end table.sort(opcapseq, numcomp2) if psu_op
"" then psu_dc = table.concat(dccapseq, "
") end -- planned local plcapseq = for k, v in pairs(plcap) do plcapseq[#plcapseq+1] = v .. " × " .. k end table.sort(plcapseq, numcomp2) if psu_pl
"" then psu_ca = table.concat(cacapseq, "
") end
-- construct table rows local out = "" -- operational if psu_op ~= "" then out = out .. "
if args.dbug and args.dbug ~= "" then local debugstr = "debug info
" for i, v in pairs(status) do debugstr = debugstr .. i .. " - " .. v .. " - " .. (cap[i] or "") .. " - " .. (mm[i] or "") .. " x " .. (num[i] or "") .. "
" end
local count = 0 for k, v in pairs(opmm) do count = count +1 end
debugstr = debugstr .. "opmm size = " .. count out = out .. "
-- Construct html hack to fit in when passed to Template:Infobox, which prefixes the data with --
return outend
return p