---------------- Defining variables--------------------local Pop_P = "P1082" -- population propertylocal Applies_P = "P518" -- applies to part propertylocal Pointin_P = "P585" -- point in time propertylocal DetMeth_P = "P459" -- determination method propertylocal RefURL_P = "P854" -- reference URLlocal RefTitle_P = "P1476" -- reference titlelocal RefPubIn_P = "P1433" --reference published in propertylocal DatePub_P = "P577" -- date of publication propertylocal Publisher_P = "P123" -- publisher propertylocal Retrieved_P = "P813" -- retrieved propertylocal CensusAU_I = "Q5058971" -- Australian census itemlocal Instof_P = "P31" -- instance of propertylocal ShortN_P = "P1813" -- short name property
local SAL_I = "Q33112019" -- state suburb item (includes SSC and SAL)--local GCCSA_I = "Q112762887" -- Greater Capital City Statistical Area itemlocal LGA_I = "Q33127844" -- Local Government Area itemlocal UCL_I = "Q33127891" -- Urban Centre or Locality--local SA2_I = "Q33128776" -- SA2local ILOC_I = "Q112729549" -- Indigenous Location
local item = nil
--------------- Function GetRefsForClaim to check, collate and format all the reference components----------------------
local function GetRefsForClaim(claim, defaulttitle) 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.text 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 (no date) if claim.references[b].snaks[DatePub_P] ~= nil then -- This is the second option for the published date (given with references - 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
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 citewebargs =
local wdeditpencil = mw.getCurrentFrame:expandTemplate --Call the Edit At Wikidata template (to add the edit pencil to end of references)
local reference = mw.getCurrentFrame:expandTemplate --expand template to feed arguments to cite_web
refs = refs..mw.getCurrentFrame:extensionTag --Add the reference from this iteration to the list of references for this particular claim
end return refs -- List of references to be given to reflistend
---------------This is a function for getting the population geography abbreviation and the Wikipedia article link for the population year ---------------local 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 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 if appliespartitem.claims[ShortN_P] ~= nil then -- If a short name value exists then use thi value instead of the full item label. abbrelabel = mw.getCurrentFrame:expandTemplate -- Output the abbreviated name with tooltip showing the full label 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 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 if detmetitem.sitelinks ~=nil and detmetitem.sitelinks.enwiki ~=nil then -- Check if the determination method item has an enwiki URL yearreturn = "".. year.."" -- If it does, use this URL as the link with the year value elseif detmetitem.claims[Instof_P] ~=nil and detmetitem.claims[Instof_P][1].mainsnak.datavalue.value.id
---------------- This ListForInfobox function is being used to grab the correct population value ---------------
local p =
function p.ListForInfobox(frame) local luaplacetype = "" --Initialise the local place type if frame.args.type
"town" then -- Check which place type and match to the equivalent ABS geographic area "city", "suburb", "town", "lga", "region" luaplacetype = UCL_I elseif articleplacetype
"city" then luaplacetype = UCL_I elseif articleplacetype
"region" then -- for now saying region
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 else item = mw.wikibase.getEntity -- If there's a Wikidata item connected to the article it will find it here. end
-- mw.logObject(item)
--------------- 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 return "" 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 local z = 0 -- initialise the table row count for j, s in pairs(item.claims[Pop_P]) do if s.qualifiers ~= nil and 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 validpopclaims[z]=s -- give the claim a new key in the table 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.-------------- if #validpopclaims <1 then return "" 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 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 c=0 local d=0 for i, q in pairs(validpopclaims) do if q.qualifiers[Applies_P][1].datavalue.value.id
----------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 = Infobox template type local tclaimdate = v.qualifiers[Pointin_P][1].datavalue.value.time if maxclaimspertemplategeog
----------PART 3B: Find the max date of claims with non-Infobox template geography-----------------
local maxclaimsperothergeog =
for l, m in pairs(othergeog) do --loop through all the claims with geography type = non Infobox 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]
------------------------------Compiling the module output--------------------------------
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 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
elseif articleplacetype
local wikitext = "" -- Initialise an empty string output (this is the value that goes back to the Infobox Australian place) if #returnlist
return p