Module:Sandbox/Trappist the monk/party
require ('strict');
---------- DEBUG
--local party_qid = 'Q13564543' -- neos
--local party_qid = 'Q533384' -- Mouvement Réformateur
----------
--[[--------------------------< P A R T Y _ C O L O R _ G E T >------------------------------------------------
wikidata is inconsistent in how party color is provided. Sometimes the party qid has 'color' (P462), sometimes
it has 'sRGB color hex triplet' (P465), sometiems it has both.
For those that have P462, we can fetch the color's qid and from that fetch P465.
For those that have P465 we fetch P465 from the party qid.
For those that have both P462 and P465, we fetch P465 preferentially.
Returns the color when found; nil else
]]
local function party_color_get (party_qid)
local party_color_qid;
if mw.wikibase.getBestStatements (party_qid, 'P465')[1] then -- some parties have both P462 (color) and P465 (hex); look for P465 first
return mw.wikibase.getBestStatements (party_qid, 'P465')[1].mainsnak.datavalue.value; -- party_qid has the color hex triplet
end
if mw.wikibase.getBestStatements (party_qid, 'P462')[1] then -- is there a 'color' property?
party_color_qid = mw.wikibase.getBestStatements (party_qid, 'P462')[1].mainsnak.datavalue.value.id; -- attempt to get party's color qid
end
if party_color_qid then
return mw.wikibase.getBestStatements (party_color_qid, 'P465')[1].mainsnak.datavalue.value; -- fetch the color hex triplet from the color's qid
end
end
--[[--------------------------< P A R T Y _ D A T A _ G E T >--------------------------------------------------
fills the sequence
[1]: party name in the local language
[2]: party country in the local language
[3]: party's sRGB color hex triplet color (without leading '#')
[4]: first house: sequence with:
[1]: house name in the local language
[2]: house description in English
[3]: number of occupied seats
[4]: number of seats in the house
[5]: second house: sequence with:
[1]: house name in the local language
[2]: house description in English
[3]: number of occupied seats
[4]: number of seats in the house
['qid']: party's qid
two parties shown here but there can be more; Austrian People's Party has three
]]
local function party_data_get (party_qid, party_data_t)
party_data_t.qid = party_qid; -- store the party_qid for reference
table.insert (party_data_t, mw.wikibase.getLabel (party_qid)); -- party name in local language
local party_country_qid = mw.wikibase.getBestStatements (party_qid, 'P17')[1].mainsnak.datavalue.value.id;
table.insert (party_data_t, mw.wikibase.getLabel (party_country_qid)); -- country name in local language
table.insert (party_data_t, party_color_get (party_qid)); -- get the party's sRGB color hex triplet (without leading '#')
for i, raw_house_t in ipairs (mw.wikibase.getBestStatements (party_qid, 'P1410')) do
local house_t = {};
local house_qid = mw.wikibase.getBestStatements (party_qid, 'P1410')[i].qualifiers.P194[1].datavalue.value.id; -- get qid for house name
house_t[1] = mw.wikibase.getLabel (house_qid); -- get the house name
house_t[2] = mw.wikibase.getDescriptionByLang (house_qid, 'en');
house_t[3] = mw.wikibase.getBestStatements (party_qid, 'P1410')[i].mainsnak.datavalue.value.amount; -- get number of seats occupied by party in this house
house_t[4] = mw.wikibase.getBestStatements (house_qid, 'P1342')[1].mainsnak.datavalue.value.amount; -- get number of seats in this house
table.insert (party_data_t, house_t); -- add this house to the sequence
end
end
--[[--------------------------< M A I N >----------------------------------------------------------------------
test probe
{{#invoke:Sandbox/Trappist the monk/party|
from the debug console:
=p.main ({args = {'Q13564543'}})
]]
local function main (frame)
local party_data_t = {}; -- sequence that holds party name, country, color, houses and numbers of seats
party_data_get (frame.args[1], party_data_t);
return mw.dumpObject (party_data_t);
end
--[[--------------------------< T E S T >----------------------------------------------------------------------
temporary test function
build a list of upper/lower house descriptions from wikidata. Source for the qids is a commons tabular data file.
this list needs needs to be prettified after the fact.
]]
local function test()
local tab_data_t = mw.ext.data.get ('Lower and upper houses of eu member states.tab').data
local out_t = {};
for _, data_t in ipairs (tab_data_t) do
if data_t[3] then
table.insert (out_t, data_t[3] .. ': ' .. mw.wikibase.getDescriptionByLang (data_t[3], 'en'));
end
if data_t[5] then
table.insert (out_t, data_t[5] .. ': ' .. mw.wikibase.getDescriptionByLang (data_t[5], 'en'));
end
end
return mw.dumpObject (out_t);
end
--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]
return {
main = main,
test = test
}