Jump to content

Module:PopulationFromWikidata and Module:PopulationFromWikidata/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
Content deleted Content added
update from Module:PopulationFromWikidata/sandbox mainly to fix irritating "Lua error in Module:PopulationFromWikidata at line 150: attempt to index field 'claims' (a nil value)." at Municipality of Scone
 
Fixed comments
 
Line 1: Line 1:

---------------- Defining variables--------------------
---------------- Defining variables--------------------
local Pop_P = "P1082" -- population property
local Pop_P = "P1082" -- population property
local Applies_P = "P518" -- applies to part property
local Applies_P = "P518" -- applies to part property
local Pointin_P = "P585" -- point in time property
local Pointin_P = "P585" -- point in time property
Line 6: Line 7:
local RefURL_P = "P854" -- reference URL
local RefURL_P = "P854" -- reference URL
local RefTitle_P = "P1476" -- reference title
local RefTitle_P = "P1476" -- reference title
local RefPubIn_P = "P1433" --reference published in property
local RefPubIn_P = "P1433" --reference published in property
local DatePub_P = "P577" -- date of publication property
local DatePub_P = "P577" -- date of publication property
local Publisher_P = "P123" -- publisher property
local Publisher_P = "P123" -- publisher property
Line 14: Line 15:
local ShortN_P = "P1813" -- short name property
local ShortN_P = "P1813" -- short name property


local SAL_I = "Q33112019" -- state suburb item (includes SSC and SAL)
local SAL_I = "Q33112019" -- state suburb item (includes SSC and SAL)
--local GCCSA_I = "Q112762887" -- Greater Capital City Statistical Area item
--local GCCSA_I = "Q112762887" -- Greater Capital City Statistical Area item
local LGA_I = "Q33127844" -- Local Government Area item
local LGA_I = "Q33127844" -- Local Government Area item
Line 26: Line 27:
--------------- Function GetRefsForClaim to check, collate and format all the reference components----------------------
--------------- Function GetRefsForClaim to check, collate and format all the reference components----------------------


local function GetRefsForClaim(claim, defaulttitle)
function GetRefsForClaim(claim, defaulttitle)
local refs = ""
local refs = ""
for b,x in pairs(claim.references) do -- Loop through all references in a claim and pull out the components
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)
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
if claim.references[b].snaks[RefURL_P] ~= nil then
refurl = claim.references[b].snaks[RefURL_P][1].datavalue.value
refurl = claim.references[b].snaks[RefURL_P][1].datavalue.value
end
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
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
if claim.references[b].snaks[RefTitle_P] ~= nil then
reftitle = claim.references[b].snaks[RefTitle_P][1].datavalue.value.text
reftitle = claim.references[b].snaks[RefTitle_P][1].datavalue.value.text
end
end

local detmet = mw.wikibase.getEntity(claim.qualifiers[DetMeth_P][1].datavalue.value.id) -- Get the dertermination method item
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.
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)
if claim.references[b].snaks[RefPubIn_P] ~= nil then --Checking if the published in part of reference exists (it should for all references)
Line 46: Line 47:
pubinlabel = pubin.labels.en.value
pubinlabel = pubin.labels.en.value
end
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 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 (no date)
local pubdate = "" -- This is the fallback option if published date is missing (no date)
Line 52: Line 53:
pubdate = mw.language.getContentLanguage():formatDate('j F Y', claim.references[b].snaks[DatePub_P][1].datavalue.value.time)
pubdate = mw.language.getContentLanguage():formatDate('j F Y', claim.references[b].snaks[DatePub_P][1].datavalue.value.time)
end
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 published date sources)
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 published date sources)
refwork = detmet.labels.en.value.." "..pubinlabel -- Concatenating to get work - determination method + published in (this overwrites the refwork value for census references)
refwork = detmet.labels.en.value.." "..pubinlabel -- Concatenating to get work - determination method + published in (this overwrites the refwork value for census references)
Line 59: Line 60:
end
end
end
end

local refpublisher = "" -- The publisher is empty if missing
local refpublisher = "" -- The publisher is empty if missing
if detmet.claims[Publisher_P] ~= nil then
if detmet.claims[Publisher_P] ~= nil then
Line 65: Line 66:
refpublisher = publisheritem.labels.en.value -- Get the label of the publisher item
refpublisher = publisheritem.labels.en.value -- Get the label of the publisher item
end
end

local refaccessdate = "" -- The retrieved date fallback is empty
local refaccessdate = "" -- The retrieved date fallback is empty
if claim.references[b].snaks[Retrieved_P] ~= nil then
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
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
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 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 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 citewebargs = { --Putting all the cite_web arguments into a list (for a single reference)
local citewebargs = { --Putting all the cite_web arguments into a list (for a single reference)
url = refurl,
url = refurl,
Line 82: Line 83:
accessdate = refaccessdate
accessdate = refaccessdate
}
}

local wdeditpencil = mw.getCurrentFrame():expandTemplate{title = 'EditAtWikidata', args = {qid = item.id, pid = claim.id, nbsp = 1}} --Call the Edit At Wikidata template (to add the edit pencil to end of references)
local wdeditpencil = mw.getCurrentFrame():expandTemplate{title = 'EditAtWikidata', args = {qid = item.id, pid = claim.id, nbsp = 1}} --Call the Edit At Wikidata template (to add the edit pencil to end of references)

local reference = mw.getCurrentFrame():expandTemplate{ title = 'cite web', args = citewebargs } --expand template to feed arguments to cite_web
local reference = mw.getCurrentFrame():expandTemplate{ title = 'cite web', args = citewebargs } --expand template to feed arguments to cite_web


refs = refs..mw.getCurrentFrame():extensionTag{ name = 'ref', content = reference..wdeditpencil, { name = refwork.."_"..pointintime.."_"..appliespart.."_"..reftitle } } --Add the reference from this iteration to the list of references for this particular claim
refs = refs..mw.getCurrentFrame():extensionTag{ name = 'ref', content = reference..wdeditpencil, { name = refwork.."_"..pointintime.."_"..appliespart.."_"..reftitle } } --Add the reference from this iteration to the list of references for this particular claim

end
end
return refs -- List of references to be given to reflist
return refs -- List of references to be given to reflist
end
end


---------------This is a function for getting the population geography abbreviation and the Wikipedia article link for the population year ---------------
---------------This is a function for getting the population geography abbreviation and the Wikipedia article link for the population year ---------------
local function GetAbbrLabelYearLink(returnclaim)
function GetAbbrLabelYearLink(returnclaim)
local appliespartitem = mw.wikibase.getEntity(returnclaim.qualifiers[Applies_P][1].datavalue.value.id) -- This gets the item ID for the current claim Applied to Part value
local appliespartitem = mw.wikibase.getEntity(returnclaim.qualifiers[Applies_P][1].datavalue.value.id) -- This gets the item ID for the current claim Applied to Part value
local abbrelabel = appliespartitem.labels.en.value -- This is the fall back value for the geography label if no abbreviation (short name) value exists in Wikidata item
local abbrelabel = appliespartitem.labels.en.value -- This is the fall back value for the geography label if no abbreviation (short name) value exists in Wikidata item
Line 100: Line 101:
abbrelabel = mw.getCurrentFrame():expandTemplate{title = 'Abbr', args = {appliespartitem.claims[ShortN_P][1].mainsnak.datavalue.value.text , appliespartitem.labels.en.value }} -- Output the abbreviated name with tooltip showing the full label
abbrelabel = mw.getCurrentFrame():expandTemplate{title = 'Abbr', args = {appliespartitem.claims[ShortN_P][1].mainsnak.datavalue.value.text , appliespartitem.labels.en.value }} -- Output the abbreviated name with tooltip showing the full label
end
end

local year = string.sub(returnclaim.qualifiers[Pointin_P][1].datavalue.value.time, 2, 5) -- Get the population point in time as a year string
local year = string.sub(returnclaim.qualifiers[Pointin_P][1].datavalue.value.time, 2, 5) -- Get the population point in time as a year string
local yearreturn = year -- If no links to Wikipedia articles describing population determination method exist then just output year
local yearreturn = year -- If no links to Wikipedia articles describing population determination method exist then just output year

local detmetitem = mw.wikibase.getEntity(returnclaim.qualifiers[DetMeth_P][1].datavalue.value.id) -- Get the current claim determination method item
local detmetitem = mw.wikibase.getEntity(returnclaim.qualifiers[DetMeth_P][1].datavalue.value.id) -- Get the current claim determination method item
if detmetitem.sitelinks ~=nil and detmetitem.sitelinks.enwiki ~=nil then -- Check if the determination method item has an enwiki URL
if detmetitem.sitelinks ~=nil and detmetitem.sitelinks.enwiki ~=nil then -- Check if the determination method item has an enwiki URL
Line 109: Line 110:
elseif detmetitem.claims[Instof_P] ~=nil and detmetitem.claims[Instof_P][1].mainsnak.datavalue.value.id == CensusAU_I then -- If there isn't a populated enwiki URL for determination method item AND it's a census determination method then...
elseif detmetitem.claims[Instof_P] ~=nil and detmetitem.claims[Instof_P][1].mainsnak.datavalue.value.id == CensusAU_I then -- If there isn't a populated enwiki URL for determination method item AND it's a census determination method then...
yearreturn = "[[Census_in_Australia#"..year.."|".. year.."]]" -- ... return the population year linked to the corresponding section of the Census in Australia article
yearreturn = "[[Census_in_Australia#"..year.."|".. year.."]]" -- ... return the population year linked to the corresponding section of the Census in Australia article
end
end
return abbrelabel.." "..yearreturn -- Join the geography abbreviation to the year (with link) ready for the final output
return abbrelabel.." "..yearreturn -- Join the geography abbreviation to the year (with link) ready for the final output
end
end





---------------- This ListForInfobox function is being used to grab the correct population value ---------------
---------------- This ListForInfobox function is being used to grab the correct population value ---------------


local p = {}
local p = {}


function p.ListForInfobox( frame )
function p.ListForInfobox( frame )
local luaplacetype = "" --Initialise the local place type
local luaplacetype = "" --Initialise the local place type
if frame.args.type == nil then
if frame.args.type == nil then
Line 135: Line 136:
elseif articleplacetype == "region" then -- for now saying region == LGA_I... but unclear what is most apprpriate ABS geography type.... can revise
elseif articleplacetype == "region" then -- for now saying region == LGA_I... but unclear what is most apprpriate ABS geography type.... can revise
luaplacetype = LGA_I
luaplacetype = LGA_I
end
end

if frame.args.wikidata ~= nil and frame.args.wikidata ~= "" then -- checking if there's a linked Wikidata item for the article
if frame.args.wikidata ~= nil and frame.args.wikidata ~= "" then -- checking if there's a linked Wikidata item for the article
item = mw.wikibase.getEntity(frame.args.wikidata) -- this is the default item specified by the wikidata parameter in the template
item = mw.wikibase.getEntity(frame.args.wikidata) -- this is the default item specified by the wikidata parameter in the template
else
else
item = mw.wikibase.getEntity() -- If there's a Wikidata item connected to the article it will find it here.
item = mw.wikibase.getEntity() -- If there's a Wikidata item connected to the article it will find it here.
end
end



-- mw.logObject(item)
-- mw.logObject(item)

--------------- CHECK: If item.claims[Pop_P] is nil. If it is return an empty string ----------------------
--------------- CHECK: If item.claims[Pop_P] is nil. If it is return an empty string ----------------------
if not (item and item.claims and item.claims[Pop_P]) then
if item == nil or item.claims[Pop_P] == nil then
return ""
return ""
end
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-------------
------------ 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
local validpopclaims = {} --initialise the blank claim table for storing all population claims that satisfy the four conditions
local z = 0 -- initialise the table row count
z=0 -- initialise the table row count
for j, s in pairs(item.claims[Pop_P]) do
for j, s in pairs(item.claims[Pop_P]) do
if s.qualifiers ~= nil and
if s.qualifiers ~= nil and
s.qualifiers[Pointin_P]~= nil and
s.qualifiers[Pointin_P]~= nil and
s.qualifiers[Applies_P] ~= nil and
s.qualifiers[Applies_P] ~= nil and
s.qualifiers[DetMeth_P] ~= nil and
s.qualifiers[DetMeth_P] ~= nil and
s.references ~= nil then
s.references ~= nil then
z = z + 1
z=z+1
validpopclaims[z]=s -- give the claim a new key in the table
validpopclaims[z]=s -- give the claim a new key in the table
end
end
end
end


--------------- 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.--------------
--------------- 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.--------------
Line 171: Line 173:
return ""
return ""
end
end

--------------- PART 2: Compare claim 'applies to part' values against template place type-------------------------
--------------- 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 claim applies to part
local templategeog = {} --initialise the blank claim table for if the template type matches to Wikidata claim applies to part
local othergeog = {} --initialise the blank claim table for when the template type doesn't match Wikidata claims applies to part
local othergeog = {} --initialise the blank claim table for when the template type doesn't match Wikidata claims applies to part
local c=0
local c=0
Line 179: Line 181:
for i, q in pairs(validpopclaims) do
for i, q in pairs(validpopclaims) do
if q.qualifiers[Applies_P][1].datavalue.value.id == luaplacetype then --filter for claims where applies to part = Infobox template type
if q.qualifiers[Applies_P][1].datavalue.value.id == luaplacetype then --filter for claims where applies to part = Infobox template type
c=c+1 -- give the claim a new key in the table
c=c+1 -- give the claim a new key in the table
templategeog[c]=q -- claims with Infobox template type geography
templategeog[c]=q -- claims with Infobox template type geography
else
else
Line 185: Line 187:
othergeog[d]=q -- claims with geography other than specified in the Infobox template type.
othergeog[d]=q -- claims with geography other than specified in the Infobox template type.
end
end
end
end


----------PART 3: Get claims with the maximum 'Point in time' values ------------------
----------PART 3: Get claims with the maximum 'Point in time' values ------------------
Line 196: Line 198:
or (maxclaimspertemplategeog ~= nil
or (maxclaimspertemplategeog ~= nil
and tclaimdate >= maxclaimspertemplategeog.qualifiers[Pointin_P][1].datavalue.value.time)
and tclaimdate >= maxclaimspertemplategeog.qualifiers[Pointin_P][1].datavalue.value.time)
then
then
maxclaimspertemplategeog=v
maxclaimspertemplategeog=v
end
end
Line 212: Line 214:
or (maxclaimsperothergeog[claimgeog] ~= nil
or (maxclaimsperothergeog[claimgeog] ~= nil
and oclaimdate >= maxclaimsperothergeog[claimgeog].qualifiers[Pointin_P][1].datavalue.value.time) -- checking the max date for a particular geography value
and oclaimdate >= maxclaimsperothergeog[claimgeog].qualifiers[Pointin_P][1].datavalue.value.time) -- checking the max date for a particular geography value
then
then
maxclaimsperothergeog[claimgeog]=m -- overwrites with a geography-max date claim pair whenever the point in time is bigger than the last iteration.
maxclaimsperothergeog[claimgeog]=m -- overwrites with a geography-max date claim pair whenever the point in time is bigger than the last iteration.
end
end
end
end




------------------------------Compiling the module output--------------------------------

------------------------------Compiling the module output--------------------------------


local returnlist = {} -- Initiate an empty table to store the output claims
local returnlist = {} -- Initiate an empty table to store the output claims
if maxclaimspertemplategeog ~=nil then -- Situation 1: Getting population for max date claim where applies to part matches the Infobox place type
if maxclaimspertemplategeog ~=nil then -- Situation 1: Getting population for max date claim where applies to part matches the Infobox place type
local templategeogrefs = GetRefsForClaim(maxclaimspertemplategeog, item.labels.en.value) -- Getting the references for max date claim where Wikidata applies to part matches the Infobox place type
local templategeogrefs = GetRefsForClaim(maxclaimspertemplategeog, item.labels.en.value) -- Getting the references for max date claim where Wikidata applies to part matches the Infobox place type
table.insert (returnlist, mw.language.getContentLanguage():formatNum(tonumber(maxclaimspertemplategeog.mainsnak.datavalue.value.amount)).." ("..GetAbbrLabelYearLink(maxclaimspertemplategeog)..")"..templategeogrefs) --Insert the return string to returnlist. With population value, applies to part, point in time, reference
table.insert (returnlist, mw.language.getContentLanguage():formatNum(tonumber(maxclaimspertemplategeog.mainsnak.datavalue.value.amount)).." ("..GetAbbrLabelYearLink(maxclaimspertemplategeog)..")"..templategeogrefs) --Insert the return string to returnlist. With population value, applies to part, point in time, reference

elseif articleplacetype == "town" then -- Situation 2: Getting population for max date claims where Infobox place type = town. If no UCL populations (earlier default) then get ILOC and SAL populations.
elseif articleplacetype == "town" then -- Situation 2: Getting population for max date claims where Infobox place type = town. If no UCL populations (earlier default) then get ILOC and SAL populations.
if maxclaimsperothergeog[ILOC_I]~=nil then
if maxclaimsperothergeog[ILOC_I]~=nil then
local ILOCrefs = GetRefsForClaim(maxclaimsperothergeog[ILOC_I], item.labels.en.value) -- Getting the references for max date claim where applies to part = ILOC
local ILOCrefs = GetRefsForClaim(maxclaimsperothergeog[ILOC_I], item.labels.en.value) -- Getting the references for max date claim where applies to part = ILOC
table.insert (returnlist, mw.language.getContentLanguage():formatNum(tonumber(maxclaimsperothergeog[ILOC_I].mainsnak.datavalue.value.amount)).." ("..GetAbbrLabelYearLink(maxclaimsperothergeog[ILOC_I])..")"..ILOCrefs) --Insert the return string to returnlist. With population value, applies to part, point in time, reference
table.insert (returnlist, mw.language.getContentLanguage():formatNum(tonumber(maxclaimsperothergeog[ILOC_I].mainsnak.datavalue.value.amount)).." ("..GetAbbrLabelYearLink(maxclaimsperothergeog[ILOC_I])..")"..ILOCrefs) --Insert the return string to returnlist. With population value, applies to part, point in time, reference
end
end
if maxclaimsperothergeog[SAL_I]~=nil then
if maxclaimsperothergeog[SAL_I]~=nil then
local SALrefs = GetRefsForClaim(maxclaimsperothergeog[SAL_I], item.labels.en.value) -- Getting the references for max date claim where applies to part = SAL
local SALrefs = GetRefsForClaim(maxclaimsperothergeog[SAL_I], item.labels.en.value) -- Getting the references for max date claim where applies to part = SAL
Line 238: Line 240:
for a, w in pairs(maxclaimsperothergeog) do -- Situation 3: Getting population for max date claims where applies to part doesn't = Infobox place type, and Infobox place type doesn't = town.
for a, w in pairs(maxclaimsperothergeog) do -- Situation 3: Getting population for max date claims where applies to part doesn't = Infobox place type, and Infobox place type doesn't = town.
local othergeogrefs = GetRefsForClaim(w, item.labels.en.value) -- Loop through the claims in maxclaimsperothergeog and output all of them
local othergeogrefs = GetRefsForClaim(w, item.labels.en.value) -- Loop through the claims in maxclaimsperothergeog and output all of them
table.insert (returnlist, mw.language.getContentLanguage():formatNum(tonumber(maxclaimsperothergeog[a].mainsnak.datavalue.value.amount)).." ("..GetAbbrLabelYearLink(maxclaimsperothergeog[a])..")"..othergeogrefs) --Insert the return string to returnlist. With population value, applies to part, point in time, reference
table.insert (returnlist, mw.language.getContentLanguage():formatNum(tonumber(maxclaimsperothergeog[a].mainsnak.datavalue.value.amount)).." ("..GetAbbrLabelYearLink(maxclaimsperothergeog[a])..")"..othergeogrefs) --Insert the return string to returnlist. With population value, applies to part, point in time, reference
end
end
end
end

local wikitext = "" -- Initialise an empty string output (this is the value that goes back to the Infobox Australian place)
local wikitext = "" -- Initialise an empty string output (this is the value that goes back to the Infobox Australian place)
if #returnlist == 1 then -- If there is only one formatted "population (geography year)" string (one row) in returnlist then return it without a bullet point
if #returnlist == 1 then -- If there is only one formatted "population (geography year)" string (one row) in returnlist then return it without a bullet point
wikitext = returnlist[1]
wikitext = returnlist[1]
else
else
wikitext = "\n*"..table.concat(returnlist,"\n*") -- If there are multiple formatted "population (geography year)" strings (multiple rows) in returnlist then return all the rows with new line and bullet points between them
wikitext = "\n*"..table.concat(returnlist,"\n*") -- If there are multiple formatted "population (geography year)" strings (multiple rows) in returnlist then return all the rows with new line and bullet points between them
end
end
return wikitext..'[[Category:Australian place articles using Wikidata population values]]' -- Append the category to the output so we can keep track of which articles are using this module to output a population value
return wikitext..'[[Category:Australian place articles using Wikidata population values]]' -- Append the category to the output so we can keep track of which articles are using this module to output a population value
end
end