มอดูล:Infobox power station

--
Power supply units
Custom module to autofill six 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 MW1 × 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 MW1 × 160 MW
ps_units_decommissioned
	→ The number of generation units decommissioned
	→ Example: 1 × 75 MW1 × 70 MW
ps_units_planned
ps_units_cancelled
--
local p = {}
local i18n = {
	"langcode" = "en",
	"op_lbl" = "Units operational",
	"mm_lbl" = "Make and model",
	"uc_lbl" = "Units under const.",
	"dc_lbl" = "Units decommissioned",
	"pl_lbl" = "Units planned",
	"ca_lbl" = "Units cancelled",
}
-- numerically sort sequential tables whose values contain a number, like "350 MW"
-- sort on first number found
local function numcomp1( x, y )
	x = tonumber( tostring(x):match("%d+") ) or 0
	y = tonumber( tostring(y):match("%d+") ) or 0
	return x  test for best
	a = a:match("%%(.*)") or a:match("%%(.*)") or a
	b = b:match("%%(.*)") or b:match("%%(.*)") or b
	return a  0 then
		for i1, v1 in ipairs(props516) do
			-- set default count of this engine to 1
			numi1 = 1
			-- set default status of this engine to planned
			statusi1 = "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 v1.mainsnak.datavalue.value.id
			if mdlqid then
				-- look for a shortname to use for model display label, otherwise use model label
				local mdl = linkedname(mdlqid, langcode)
				local mfr
				local props176snak = mw.wikibase.getBestStatements(mdlqid, "P176")1
				if props176snak then
					-- model has a manufacturer
					props176snak = props176snak and props176snak.mainsnak
					local mfrqid = (props176snak.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
				mmi1 = 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
				capi1 = renderqty(props2109snak, langcode)
			elseif v1.mainsnak.snaktype == "somevalue" then
				mmi1 = "Unknown"
				-- set default capacity
				capi1 = 0
			end
			local quals = v1.qualifiers
			if quals then
				-- determine status from service retirement/entry/inception
				local dcsnak = quals.P730 and quals.P7301.snaktype
				local opsnak = quals.P729 and quals.P7291.snaktype
				local ucsnak = quals.P571 and quals.P5711.snaktype
				if dcsnak == "value" or dcsnak == "somevalue" then
					statusi1 = "dc"
				elseif opsnak == "value" or opsnak == "somevalue" then
					statusi1 = "op"
				elseif ucsnak == "value" or ucsnak == "somevalue" then
					statusi1 = "uc"
				end
				-- override if state of use (P5817) is cancelled-abandoned (Q30108381)
				if quals.P5817
					and quals.P58171.snaktype == "value"
					and quals.P58171.datavalue.value.id == "Q30108381" then
					statusi1 = "ca"
				end
				-- override default capacity from qualifier P2109 if available
				if quals.P2109 and quals.P21091.snaktype == "value" then
					capi1 = renderqty(quals.P21091, langcode)
				end
				-- if quantity (P1114) is given, replace num value
				if quals.P1114 and quals.P11141.snaktype == "value" then
					numi1 = tonumber(quals.P11141.datavalue.value.amount) or 1
				end
			end
			-- convert capacity in kW to MW
			if (capi1 or ""):sub(-2) == "kW" then
				local c = tonumber(capi1:match("%d+"))
				capi1 = c/1000 .. " " .. "MW"
			end
		end
	end
	-- 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 == "op" and mmi then opmmmmi = (opmmmmi or 0) + numi end
	end
	-- construct html string from the set of manufacturers and models
	-- first make a sequential table
	local opmmseq = {}
	for k, v in pairs(opmm) do
		opmmseq#opmmseq+1 = k .. " (" .. v .. ")"
	end
	table.sort(opmmseq, linkcomp)
	if psu_mm == "" 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 == "uc" and capi then uccapcapi = (uccapcapi or 0) + numi end
		if v == "op" and capi then opcapcapi = (opcapcapi or 0) + numi end
		if v == "dc" and capi then dccapcapi = (dccapcapi or 0) + numi end
		if v == "pl" and capi then plcapcapi = (plcapcapi or 0) + numi end
		if v == "ca" and capi then cacapcapi = (cacapcapi or 0) + numi end
	end
	-- construct html strings from the sets of capacities
	-- first make a sequential table
	-- under construction
	local uccapseq = {}
	for k, v in pairs(uccap) do
		uccapseq#uccapseq+1 = v .. " × " .. k
	end
	table.sort(uccapseq, numcomp2)
	if psu_uc == "" 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_op = table.concat(opcapseq, "") end
	-- decommissioned
	local dccapseq = {}
	for k, v in pairs(dccap) do
		dccapseq#dccapseq+1 = v .. " × " .. k
	end
	table.sort(dccapseq, numcomp2)
	if psu_dc == "" 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_pl = table.concat(plcapseq, "") end
	-- cancelled
	local cacapseq = {}
	for k, v in pairs(cacap) do
		cacapseq#cacapseq+1 = v .. " × " .. k
	end
	table.sort(cacapseq, numcomp2)
	if psu_ca == "" then psu_ca = table.concat(cacapseq, "") end
	-- construct table rows
	local out = ""
	-- operational
	if psu_op ~= "" then
		out = out ..  "" .. i18n.op_lbl
		.. "" .. psu_op .. ""
	end
	-- make & model
	if psu_mm ~= "" then
		out = out ..  "" .. i18n.mm_lbl
		.. "" .. psu_mm .. ""
	end
	-- planned
	if psu_pl ~= "" then
		out = out ..  "" .. i18n.pl_lbl
		.. "" .. psu_pl .. ""
	end
	-- cancelled
	if psu_ca ~= "" then
		out = out ..  "" .. i18n.ca_lbl
		.. "" .. psu_ca .. ""
	end
	-- under const.
	if psu_uc ~= "" then
		out = out ..  "" .. i18n.uc_lbl
		.. "" .. psu_uc .. ""
	end
	-- decommissioned
	if psu_dc ~= "" then
		out = out ..  "" .. i18n.dc_lbl
		.. "" .. psu_dc .. ""
	end
	if args.dbug and args.dbug ~= "" then
		local debugstr = "debug info"
		for i, v in pairs(status) do
			debugstr = debugstr .. i .. " - " .. v .. " - " .. (capi or "") .. " - " .. (mmi or "") .. " x " .. (numi or "") .. ""
		end
		local count = 0
		for k, v in pairs(opmm) do
			count = count +1
		end
		debugstr = debugstr .. "opmm size = " .. count
		out = out  ..  "" .. debugstr .. ""
	end
	-- Construct html hack to fit in when passed to Template:Infobox, which prefixes the data with
	--  and suffixes it with 
	if #out > 0 then
		out = "" .. out:sub(1,-11)
	end
	return out
end
return p