Module:Sandbox/RexxS/Iscountry

From blackwiki
Jump to navigation Jump to search

{{Module rating }} Demo routine to determine whether a Wikidata entity is a country.

It takes an entity-id as the parameter |qid=.

Usage

From wiki-text:

  • {{#invoke:Sandbox/RexxS|iscountry |qid=entity-id}}

From another module:

local iscountry = require("Module:Sandbox/RexxS/Iscountry)._iscountry"
if iscountry(qid) then
    ...
end

Examples

For Lua error in Module:Wd at line 196: attempt to index field 'wikibase' (a nil value). (Q12130)

  • {{#invoke:Sandbox/RexxS/Iscountry |iscountry |qid=Q12130}}Lua error at line 59: attempt to index field 'wikibase' (a nil value).

For Lua error in Module:Wd at line 196: attempt to index field 'wikibase' (a nil value). (Q142)

  • {{#invoke:Sandbox/RexxS/Iscountry |iscountry |qid=Q142}}Lua error at line 59: attempt to index field 'wikibase' (a nil value).



local p = {}


--[[ https://w.wiki/aXJ
SELECT ?item ?itemLabel WHERE {
  ?item wdt:P279 wd:Q6256.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
--]]

local types_of_country = {
	"Q6256",	-- country
	"Q112099",	-- island nation
	"Q123480",	-- landlocked country
	"Q132453",	-- developed country
	"Q160381",	-- Western world
	"Q177323",	-- developing country
	"Q188800",	-- personal union
	"Q331644",	-- Khanate
	"Q831048",	-- Nassau-Hadamar
	"Q899706",	-- Peru–Bolivian Confederation
	"Q1093720",	-- real union
	"Q1151616",	-- stateless nation
	"Q1323642",	-- transcontinental country
	"Q1501897",	-- Generality Lands
	"Q1538989",	-- megadiverse country
	"Q1541668",	-- County of Diez
	"Q1641089",	-- Multinational state
	"Q2281140",	-- safe country of origin
	"Q2639564",	-- low-wage country
	"Q3024240",	-- historical country
	"Q3373662",	-- French-speaking Country
	"Q3500085",	-- periphery countries
	"Q3624078",	-- sovereign state
	"Q5170193",	-- core countries
	"Q5367717",	-- shinkoku
	"Q6726158",	-- Independent country
	"Q7449317",	-- semi-periphery countries
	"Q11396118",	-- divided country
	"Q11495981",	-- victorious country
	"Q25449457",	-- continental country
	"Q28864179",	-- proposed country
	"Q51576574",	-- Mediterranean country
	"Q66724388",	-- autonomous country within the Kingdom of Denmark
	"Q66903706",	-- banana producing countries
}

local isCountry = {}
for i, v in ipairs(types_of_country) do
	isCountry[v] = true
end

-------------------------------------------------------------------------------
-- _iscountry takes Q-id and returns true if it's a country.
-------------------------------------------------------------------------------
-- Dependencies:
-------------------------------------------------------------------------------
p._iscountry = function(qid)
	local inst = mw.wikibase.getBestStatements(qid, "P31")
	if #inst > 0 then
		for i, v in ipairs(inst) do
			local instid = v.mainsnak.datavalue and v.mainsnak.datavalue.value.id
			if isCountry[instid] then return true end
		end
	end
	return false
end

p.iscountry = function(frame)
	local args = frame.args
	if not args.qid then args = frame:getParent().args end
	return p._iscountry(args.qid) or "" -- return empty string not "false" to invoke
end


return p