Jump to content

Module:Infobox power station

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by RexxS (talk | contribs) at 17:40, 15 March 2019 (create testing module for Power supply units). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

--[[
Power supply units
Custom module to autofill four parameters for use in Template:Infobox power station
Parameters are:
ps_units_operational
	→ The number of generation units operational and their nameplate capacity
	→ Example: 3 × 100 MW<br>1 × 110 MW
ps_units_manu_model
	→ The manufacturer and model of the generation units
	→ Example: Vestas V164
ps_units_uc
	→ The number of generation units under construction
	→ Example: 2 × 150 MW<br>1 × 160 MW
ps_units_decommissioned
	→ The number of generation units decommissioned
	→ Example: 1 × 75 MW<br>1 × 70 MW
--]]

local p= {}

local i18n = {
	["langcode"] = "en",
	["op_lbl"] = "Units operational",
	["mm_lbl"] = "Make and model",
	["uc_lbl"] = "Units under const.",
	["dc_lbl"] = "Units decommissioned",
}

local cleanstring = function(s)
	return mw.text.trim(s or "")
end

local wdib = require("Module:WikidataIB")

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 qid = args.qid or mw.wikibase.getEntityIdForCurrentPage()
	if not qid then return nil end

	local langcode = args.lang or ""
	if langcode == "" then langcode = i18n.langcode end

	local props516 = mw.wikibase.getBestStatements(qid, "P516")
	if #props516 < 1 then return nil end

	local status = {}
	local mm = {}
	local cap = {}
	for i1, v1 in ipairs(props516) do
		local quals = v1.qualifiers
		if quals then
			-- determine status from service retirement/entry/inception
			if quals.P730 and quals.P730[1].snaktype == "value" then
				status[i1] = "dc"
			elseif quals.P729 and quals.P729[1].snaktype == "value" then
				status[i1] = "op"
			elseif quals.P571 and quals.P571[1].snaktype == "value" then
				status[i1] = "uc"
			else
				status[i1] = "unknown"
			end
			-- get manufacturers and models from codename (P1638)
			if quals.P1638 and quals.P1638.snaktype == "value" then
				-- scan the monolingual text for a language match
				for i2, v2 in ipairs(quals.P1638) do
					if v2.snaktype == "value" and v2.datavalue.value.language == langcode then
						mm[i1] = v2.datavalue.value.text
						break
					end
				end
			end
			-- get capacity from P2109
			if quals.P2109 and quals.P2109[1].snaktype == "value" then
				cap[i1] = tonumber(quals.P2109[1].datavalue.value.amount) or ""
				-- get qid of unit
				local uqid = (quals.P2109[1].datavalue.value.unit or ""):match("(Q%d+)")
				-- scan table of unit symbols
				local usym = ""
				for i2, v2 in ipairs( mw.wikibase.getAllStatements(uqid, "P5061") ) do
					if v2.snaktype == "value" and v2.datavalue.value.language == langcode then
						usym = v2.datavalue.value.text
						break
					end
				end
				cap[i1] = capi1 .. usym
			end
		end
	end

	-- count under construction, operational, decomissioned and unknown
	local count = { uc=0, op=0, dc=0, unknown=0 }
	for k2, v2 in pairs(status) do
		count[v2] = count[v2] +1
	end

	-- we need to allow local values to override Wikidata
	if psu_op == "" and count.op > 0 then psu_op = count.op end
	if psu_uc == "" and count.uc > 0 then psu_uc = count.uc end
	if psu_dc == "" and count.dc > 0 then psu_dc = count.dc end
	if psu_mm == "" and next(mm) then
		for k, v in pairs(mm) do
			psu_mm = psu_mm .. v .. "<br>"
		end
		psu_mm = psu_mm:sub(1, -5)
	end

	-- construct table rows
	local out = ""
	if psu_op ~= "" then
		out = out ..  "<tr><th>" .. i18n.op_lbl .. "</th><td>" .. psu_op .. "</td></tr>"
	end
	if psu_mm ~= "" then
		out = out ..  "<tr><th>" .. i18n.mm_lbl .. "</th><td>" .. psu_mm .. "</td></tr>"
	end
	if psu_uc ~= "" then
		out = out ..  "<tr><th>" .. i18n.uc_lbl .. "</th><td>" .. psu_uc .. "</td></tr>"
	end
	if psu_dc ~= "" then
		out = out ..  "<tr><th>" .. i18n.dc_lbl .. "</th><td>" .. psu_dc .. "</td></tr>"
	end

	return out
end

return p