Jump to content

Module:PopulationFromWikidata

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MaiaCWilliams (talk | contribs) at 07:41, 26 June 2022 (Created page with ' -- defining variables for p.claimlist local Pop_P = "P166971" -- population property local Applies_P = "P252386" -- applies to part property local Pointin_P = "P253134" -- point in time property local DetMeth_P = "P759" -- determination method property local RefURL_P = "P709" -- reference URL local RefTitle_P = "P711" -- reference title local RefPubIn_P = "P253136" --reference published in property local DatePub_P = "P766" -- date of publication propert...'). 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)

-- defining variables for p.claimlist
local Pop_P = "P166971" 	-- population property
local Applies_P = "P252386"	-- applies to part property
local Pointin_P = "P253134" -- point in time property
local DetMeth_P = "P759"	-- determination method property
local RefURL_P = "P709"		-- reference URL
local RefTitle_P = "P711"	-- reference title
local RefPubIn_P = "P253136"	--reference published in property	
local DatePub_P = "P766"	-- date of publication property
local Publisher_P = "P760"	-- publisher property
local Retrieved_P = "P253135"	-- retrieved property
local CensusAU_I = "Q610589"	-- Australian census item
local Instof_P = "P694"			-- instance of property

local SAL_I = "Q608095" 	-- state suburb item (includes SSC and SAL)
--local SUA_I = "Q608096"		-- Significant Urban area item
--local GCCSA_I = ""			-- Greater Capital City Statistical Area item
local LGA_I = ""			-- Local Government Area item
local UCL_I = "Q610039"		-- Urban Centre or Locality
local SA2_I = "Q610042"		-- SA2
local ILOC_I = ""			-- Indigenous Location


local Place_I = "Q608097"   -- Ulladulla Wikidata item

-------------------------
function GetRefsForClaim(claim, defaulttitle)								-- Function to check and gather and format all the reference components from a single claim
	local refs = ""
	for b,x in pairs(claim.references) do																	-- Loop through all references in a claim and pull out the components
		local refurl = ""																					-- initialise an empty URL, check if there is one then populate with the actual value (if not it stays as an empty string)
		if claim.references[b].snaks[RefURL_P] ~= nil then
			refurl = claim.references[b].snaks[RefURL_P][1].datavalue.value	
		end
		
		local reftitle = defaulttitle																		-- Initialise the default title as the Wikidata item title. This is the fallback title if one isn't provided in the references
		if claim.references[b].snaks[RefTitle_P] ~= nil then
			reftitle = claim.references[b].snaks[RefTitle_P][1].datavalue.value
		end
		
		local detmet = mw.wikibase.getEntity(claim.qualifiers[DetMeth_P][1].datavalue.value.id)					-- Get the dertermination method item
		
		local pubinlabel = ""																					--Initalising an empty published in label. This is the last option for this value.
		if claim.references[b].snaks[RefPubIn_P] ~= nil then													--Checking if the published in part of reference exists (it should for all references)
			local pubin = mw.wikibase.getEntity(claim.references[b].snaks[RefPubIn_P][1].datavalue.value.id)	--If it does then grab the published item and the label of that item
			pubinlabel = pubin.labels.en.value
		end
								
		local refwork = pubinlabel																				-- This value is used for non-census references, or as a fall-back value for census references with missing parts
		local pubdate = ""																						-- This is the fallback option if published date is missing
		if claim.references[b].snaks[DatePub_P] ~= nil then														-- This is the second option for  the published date (this is used for non-census references)
			pubdate = mw.language.getContentLanguage():formatDate('j F Y', claim.references[b].snaks[DatePub_P][1].datavalue.value.time)
		end
		
		if detmet.claims[Instof_P] ~=nil and detmet.claims[Instof_P][1].mainsnak.datavalue.value.id == CensusAU_I then		-- Checking for census references (which will have different refwork and pulished date sources)
			refwork = detmet.labels.en.value.." "..pubinlabel																-- Concatenating to get work - determination method + published in (this overwrites the twork value for the non-census references)
			if detmet.claims[DatePub_P] ~=nil then
				pubdate = mw.language.getContentLanguage():formatDate('j F Y', detmet.claims[DatePub_P][1].mainsnak.datavalue.value.time) -- Overwrite the published date value if the determination method item has a date published (for census references)
			end
		end
		
		local refpublisher = ""																								-- The publisher is empty if missing
		if detmet.claims[Publisher_P] ~= nil then
			local publisheritem = mw.wikibase.getEntity(detmet.claims[Publisher_P][1].mainsnak.datavalue.value.id)			-- Get the determination method item and the publisher item ID
			refpublisher = publisheritem.labels.en.value																	-- Get the label of the publisher item
		end
		
		local refaccessdate = ""																							-- The retrieved date fallback is empty
		if claim.references[b].snaks[Retrieved_P] ~= nil then
			refaccessdate = mw.language.getContentLanguage():formatDate('j F Y', claim.references[b].snaks[Retrieved_P][1].datavalue.value.time) -- Populate the retrieved date if it's there
		end
		
		local appliespart = mw.wikibase.getEntity(claim.qualifiers[Applies_P][1].datavalue.value.id).labels.en.value					-- The ABS geography type for the particular claim (to use in reference name)
		local pointintime = mw.language.getContentLanguage():formatDate('Y', claim.qualifiers[Pointin_P][1].datavalue.value.time)		-- Getting the point in time as a YYYY (to use in the reference name)
		
		local webciteargs = {																								--Putting all the web_cite arguments into a list (for a single reference)
			url = refurl,
			title = reftitle,
			date = pubdate,
			work = refwork,
			publisher = refpublisher,
			accessdate = refaccessdate
		}
		
		local reference = mw.getCurrentFrame():expandTemplate{ title = 'cite web', args = webciteargs }						--expand template to feed arguments to web_cite

		refs = refs..mw.getCurrentFrame():extensionTag{ name = 'ref', content = reference, { name = refwork.."_"..pointintime.."_"..appliespart.."_"..reftitle } }			--Add the reference from this iteration to the list of references
--			 
	end	
	return refs																												-- A concatentated string of references for web_cite for a single claim
end	
-------------------------
local p = {} --p stands for package (a function that external things can use. You invoke p things.)
--------------------------


-- This function is being used to grab the correct population value and pull out the associated reference(s)

function p.claimlist( frame )					  
	local luaplacetype = ""															--initialise the local place type
	local articleplacetype = frame.args.type										-- bring in the place type entered into Infobox Australian place template
	if articleplacetype == "town" or articleplacetype == "Town" then				-- Check which place type and match to the equivalent ABS geographic area "city", "suburb", "town", "lga", "region", "cadastral", "protected" or "other" 
		luaplacetype = ILOC_I
		elseif articleplacetype == "suburb" or articleplacetype == "suburb" then
			luaplacetype = SAL_I
		elseif articleplacetype == "city" or articleplacetype == "City" then
			luaplacetype = UCL_I
		elseif articleplacetype == "lga" or articleplacetype == "LGA" then
			luaplacetype = LGA_I
		elseif articleplacetype == "region" or articleplacetype == "Region" then	-- for now saying region == LGA_I... but need to revise this.
			luaplacetype = LGA_I
	end	
	local item = mw.wikibase.getEntity() 							-- hard wired to Ulladulla for now but should just be ()
--	mw.logObject(item)
	
 --------------- CHECK: If item.claims[Pop_P] is nil. If it is return a html message
	if item.claims[Pop_P] == nil then
		return "<!-- No population claim-->"
	end
	
	------------ PART 1: Find claims with (1) point in time is not nil, (2) applies to part is not nil, (3) determination method is not nil and (4) References table is not empty
	local validpopclaims = {}							--initialise the blank claim table for storing all population claims that satisfy the four conditions 
	z=0													-- initialise the table row count
	for j, s in pairs(item.claims[Pop_P]) do
--			mw.logObject(s.qualifiers[Applies_P][1].datavalue.value.id)
		
		if	s.qualifiers[Pointin_P]~= nil and
			s.qualifiers[Applies_P] ~= nil and 
			s.qualifiers[DetMeth_P] ~= nil and 
			s.references ~= nil then
	
			z=z+1																-- give the claim a new key in the table
			validpopclaims[z]=s
		end
	end	
--			mw.logObject(validpopclaims)

--------------- CHECK: If count of validpopclaims is less than one (eg 0) then return a html message. This checks we have something from which to get a population value and basic reference.
	if #validpopclaims <1 then
		return "<!-- No claims with minimum requirements-->"
	end
	
	
	
	------------ PART 2: Compare claim 'applies to part' values against template place type
 		local templategeog = {}							--initialise the blank claim table for if the template type matches to Wikidata claims
		local othergeog = {}						--initialise the blank claim table for when the tempalte type doesn't match Wikidata claims and want to show all the other place types with max date.
		c=0
		d=0
		for i, q in pairs(validpopclaims) do
--			mw.logObject(q.qualifiers[Partof_P][1].datavalue.value.id)

			if q.qualifiers[Applies_P][1].datavalue.value.id == luaplacetype then	--filter for claims where applies to part = template type
				c=c+1																	-- give the claim a new key in the table											
				templategeog[c]=q													-- claims with template type geography
			else
				d=d+1
				othergeog[d]=q														-- claims with geography other than specified in the template.
			end
		end	
--			mw.logObject(templategeog)
--			mw.logObject(item.claims[Pop_P])

	----------PART 3: Get claims with the maximum 'Point in time' values ------------------
	----------PART 3A: Find the max date of claims with template geography-----------------

	local maxclaimspertemplategeog = nil												--initialise the blank claim corresponding to the max date
	for k, v in pairs(templategeog) do													--loop through all the claims with geography type = template place type
		local tclaimdate = v.qualifiers[Pointin_P][1].datavalue.value.time
		if  maxclaimspertemplategeog == nil
			or (maxclaimspertemplategeog ~= nil
				and tclaimdate >= maxclaimspertemplategeog.qualifiers[Pointin_P][1].datavalue.value.time)
			then								 
				maxclaimspertemplategeog=v
		end
	end
	--	mw.logObject(maxdatetemplategeog)
	--	mw.logObject(maxclaimspertemplategeog)
	
	----------Reference components for 3A claim (max date of population with Infobox template geography type)-----------------
	local templategeogrefs = ""																										--initialise an empty table to store the reference details per reference

	if maxclaimspertemplategeog ~=nil then																				
		templategeogrefs = GetRefsForClaim(maxclaimspertemplategeog, item.labels.en.value)
	end
	
	
	----------PART 3B: Find the max date of claims with non-template geography-----------------
--	local maxdateothergeog = "+0000-00-00T00:00:00Z"									--initialise the maxdate
	local maxclaimsperothergeog = {}
--	local maxdateclaimsothergeog = {}													--initialise the blank claim table corresponding to the max date
	for l, m in pairs(othergeog) do													--loop through all the claims with geography type = non template place type
		local oclaimdate = m.qualifiers[Pointin_P][1].datavalue.value.time
		local claimgeog = m.qualifiers[Applies_P][1].datavalue.value.id
		if  maxclaimsperothergeog[claimgeog] == nil									-- using the applies to part (geography) value as the table key
			or (maxclaimsperothergeog[claimgeog] ~= nil
				and oclaimdate >= maxclaimsperothergeog[claimgeog].qualifiers[Pointin_P][1].datavalue.value.time)  -- checking the max date for a particular geography value
			then								 
				maxclaimsperothergeog[claimgeog]=m									-- overwrites with a geography - max claim pair whenever the point in time is bigger than the last iteration.
		end
	end
		
	----------Reference components for 3B claim (max dates of all population claims per non-template geography type)-----------------

	if maxclaimspertemplategeog ==nil and maxclaimsperothergeog ~=nil then																				
		for f, w in pairs(maxclaimsperothergeog) do
			local othergeogrefs = ""
			
			othergeogrefs = GetRefsForClaim(w, item.labels.en.value)

			w.oreflist = othergeogrefs																				--put the list of references from each claim into the maxclaimsperothergeog table per claim
		end
	end	
	

	
-----------------------------outputs------------		
--		mw.logObject(maxdate)												-- return the maxdate ended on
--	    mw.logObject(maxdatetable)											-- return the corresponding claims
--	    mw.logObject(maxdateplaces)											-- return the claim where 'applies to part' = template type
--	    mw.logObject(maxdateotherplaces)									-- return the claims where 'applies to part' not = template type
	    
-------------------Return to template-----------------	    
    local div = mw.html.create( 'div' )									-- formulate giving the value back to the Infobox Australian place template
    if maxclaimspertemplategeog ~=nil then
    	local appliespartitem = mw.wikibase.getEntity(maxclaimspertemplategeog.qualifiers[Applies_P][1].datavalue.value.id)
--				mw.logObject(instanceof.labels.en.value)
		div:wikitext(mw.language.getContentLanguage():formatNum(tonumber(maxclaimspertemplategeog.mainsnak.datavalue.value.amount)).." ("..appliespartitem.labels.en.value.." "..mw.language.getContentLanguage():formatDate('Y', maxclaimspertemplategeog.qualifiers[Pointin_P][1].datavalue.value.time)..")"..templategeogrefs)  --Just return the population value if 'applies to part' = template type
   
    elseif (articleplacetype == "town" or articleplacetype == "Town") and maxclaimspertemplategeog ==nil then
    	if maxclaimsperothergeog[UCL_I]~=nil then
    		local appliespartUCL = mw.wikibase.getEntity(maxclaimsperothergeog[UCL_I].qualifiers[Applies_P][1].datavalue.value.id)
			div:wikitext("\n* "..mw.language.getContentLanguage():formatNum(tonumber(maxclaimsperothergeog[UCL_I].mainsnak.datavalue.value.amount)).." ("..appliespartUCL.labels.en.value.." "..mw.language.getContentLanguage():formatDate('Y', maxclaimsperothergeog[UCL_I].qualifiers[Pointin_P][1].datavalue.value.time)..")"..maxclaimsperothergeog[UCL_I].oreflist)
    	end
		if maxclaimsperothergeog[SAL_I]~=nil then
    		local appliespartSAL = mw.wikibase.getEntity(maxclaimsperothergeog[SAL_I].qualifiers[Applies_P][1].datavalue.value.id)
			div:wikitext("\n* "..mw.language.getContentLanguage():formatNum(tonumber(maxclaimsperothergeog[SAL_I].mainsnak.datavalue.value.amount)).." ("..appliespartSAL.labels.en.value.." "..mw.language.getContentLanguage():formatDate('Y', maxclaimsperothergeog[SAL_I].qualifiers[Pointin_P][1].datavalue.value.time)..")"..maxclaimsperothergeog[SAL_I].oreflist)
		end
	else
		for a, w in pairs(maxclaimsperothergeog) do
			local appliespartitem = mw.wikibase.getEntity(maxclaimsperothergeog[a].qualifiers[Applies_P][1].datavalue.value.id)
--				mw.logObject(instanceof.labels.en.value)
			div:wikitext("\n* "..mw.language.getContentLanguage():formatNum(tonumber(maxclaimsperothergeog[a].mainsnak.datavalue.value.amount)).." ("..appliespartitem.labels.en.value.." "..mw.language.getContentLanguage():formatDate('Y', maxclaimsperothergeog[a].qualifiers[Pointin_P][1].datavalue.value.time)..")"..w.oreflist)    --return a list of all the maxdate -Just return the population value if 'applies to part' population values and place types
		end
		
    end
	return div
end

return p