Module:Sandbox/Innesw/PopulationFromWikidata-upgrade
- descriptions of the proposed changes to the module, implemented in this new version
- the proposed documentation for the module once the changes are implemented.
Changes and fixes to this module have been made (in part) in association with a proposed upgrade of the Infobox template. Therefore some changes are only relevent if particular parts of that upgrade are implemented.
The existing, released version of the module is referred to here as the 'current' version, and the proposed version after these changes are implemented as the 'new' version.
Changed Behaviour - multiple population values
The new version makes a significant change to the behaviour of the module regarding how it displays population figures if they are available for a number of different geographies, ie: if they are available in the linked Wikidata item for more than one of Urban Centres and Localities (UCL), Suburbs and Localities (SAL) and Indigenous Communities (ILOC).
Current Behaviour
Having gathered a list of all Wikidata population claims that meet certain criteria, then reduced that list to the latest claim for each geography, the logic in the current version is:
- for each
|type=
value, there is a single preferred geography. If a claim for that geography is available, return only that claim. - if there are no claims for the prefered geography:
- if
|type=town
, return latest claims for SAL and / or ILOC if available - for all other
|type=
values, return latest claims for all available geographies
- if
This means that, if a claim for the preferred geography exists, no claims for any other geography are ever returned.
Discussion
The suggestion to show multiple population figures has been made in a number of forums (eg: at the module talk page and at the infobox talk page). On the other hand there has started to be use of the module outside the Infobox, for tables of suburbs in LGAs (see City_of_Rockingham, Town_of_Katherine), where showing the population for the wrong geography, or for multiple geographies, would seem to be undesirable.
There are a number of possible solutions for making available options to show populations for either (a) multiple geographies if they are available, or (b) a single specific geography:
- make the ListForInfobox function generally return a single population, but return all available geographies on specific request (
|multi=yes
) - make the ListForInfobox function generally return all available geographies, but return a single population for a specified geography on request (eg:
|geog=ucl
) - make different functions available, ie: ListForInfobox() to return all available geographies, and SinglePopulation(geog=ucl) to return a specified geography
Option 2 has been implemented in the new version. When almost all calls to the module are for the Infobox, and that context should show all available geographies, that should become the default behaviour. (Others have suggested this, and I support it.) The (currently small) number of calls wanting a single geography can be changed to specifically request it (rather than, as currently, relying on specifying |type=suburb
, assuming there will be no SAL population in Wikidata, and relying on the fallbacks to get, for example, an ILOC population).
New Behaviour
Having gathered a list of all Wikidata population claims that meet certain criteria, then reduced that list to the latest claim for each geography, the logic in the new version is:
- if a geography has been specified in
|geog=
, and a latest claim for that geography exists, return that claim- if there is no claim for the specified geography, return blank
- for each
|type=
value, a list of possible geography types is specified - latest claims that exist for any of these geography types are returned
- if there are no such claims, return blank
Other Fixes and Enhancements
Changes made in this upgrade to the module
Issue | Suggested at | Comments |
---|---|---|
integrated citation references | next steps | When a population is retrieved for article text, as well as in the infobox, the current version produces separate references to the same source. In the new version, links for the same claim in Wikidata now combine into the same citation reference.
The existing version constructed a correct name for the references, but did not pass that name correctly to the citation reference process. |
historic populations | next steps | New function, called by {{#invoke:PopulationFromWikidata|HistoricPopulations}} , returns a table of population figures for all years where census data is in wikidata. By default it has columns for all geographies. |geog= can be specified, to return only population figures for that geography.
|
no output for some |type= values
|
next steps | If |type=cadastral or |type=protected , the new version explicitly returns blank output.
It also returns blank if |
census link linkrot | For census data for 2016 and earlier, data uploaded to Wikidata has reference URLs that are no longer valid. The module now uses eg: {{Census_2016_AUS}} to produce a citation reference with the correct URL.
For claims in Wikidata that are not from census data, the existing reference URL is used as-is. | |
new |type= values
|
New |type= values have been suggested for the infobox, and implemented in the proposed upgrade. The new version of PopulationFromWikidata recognises these values as valid, and allocates one or more ABS geographies to them.
|
Unresolved Issues
Issue | Suggested at | Comments |
---|---|---|
links to explainations of ABS land units | next steps | Suggestion was to add a section to each census article, and link to it. My suggestion is a new section in Australian Bureau of Statistics, and link to sub-sections of that. |
calculate population densities | next steps | Suggestion was to automatically calculate densities from Wikidata information. What will be needed in each area claim is an 'applies to part' property that matches the population claim, and these areas claims will need to be kept up-to-date after each census. |
indigenous communty populations | next steps | The initial suggestion was to get ABS ILOC IDs into Wikidata, as the first step to uploading ILOC census data. The new version of this module will accept |geog=iloc , if that is the preferred geography for populations.
|
|type=region
|
next steps | The current version prefers lga census data for regions, suggestion is SA3s may better. There are 167 articles with |type=region in the infobox. It is not yet investigated what populations currently appear in the infobox for these articles.
|
ABS data issue | next steps | Possible differences occur between ABS datapacks (the data source for upload to Wikidata) and Quickstats (which reference URLs link to), especially for places with very low populations. This means a link to Quickstats may not supports the stated population value. |
![]() | This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
Reason for the module
The aim is to make it easier to keep population values (and associated references) up-to-date in Australian place article Infoboxes. This module looks at population claims in a linked Wikidata item and filters for the latest and most appropriate population value. It extracts this value, along with all referencing information, and gives this to the article Infobox.
Who made the module
Wikimedia Australia designed this project to coincide with the first release of the 2021 census data (in June 2022). This module was created as part of a funded project with work done by m:User:MaiaCWilliams in collaboration with (really...HUGE amounts of help from) User:Samwilson, User:99of9 and User:Canley. The project was coordinated by User:tenniscourtisland.
It is an ongoing project and we will continue to refine the module. Of course anyone is welcome to contribute!
Head to the Module_talk:PopulationFromWikidata page if you have anything to discuss.
We wrote a summary of the project for the Wikimedia Australia blog here.
What the module does
Population sources
The module is designed to be invoked from the Infobox Australian place template and gathers data from the Wikidata item linked to each article. The module may be modified and used in other places/cases in the future.
Currently, this module is invoked in such a way that it will only give the Infobox a population figure if one isn't manually given for the Infobox Australian place pop argument. This means that initially the module will not impact many articles. Over time, once we're certain it is working well, we can remove the manually added population figures in favour of the Wikidata figures brought in by the module.
See line 110 of the Infobox Australian place template for the module invoke.
How to see the module in action
Currently the module will only give a population figure to the Infobox if one has not been manually added via the Infobox Australian place template pop field. This means if you want to see the module in action for a particular place article, you should follow these steps:
- Pick a Wikipedia place article and check that the linked Wikidata item has a valid population claim (most now do, but some values will be old because not all 2021 Census data has been released yet).
- If the Wikidata item looks good, then edit the Infobox Australian place template part of the article. Remove the pop value and replace with a comment like: “<!--Leave blank to draw the latest automatically from Wikidata-->”. Remove the pop_year and pop_footnotes fields. Check if the old pop_footnotes reference had been used elsewhere in the article.
- Check the output in the article Infobox. If the output is not as expected then edit the Wikidata item or if it’s really broken, get in touch here.
Here's an example of an article with Infobox using the module, and the diff of the edit made.
The list of articles using population values from Wikidata (via this module) is here.
Assumptions
The module works with the following assumptions:
- That all Australian place Wikipedia articles are linked to relevant Wikidata items (true because Canley and 99of9 have done this work).
- Relying on the type field of the Infobox Australian place template being a required field and assuming it always has a value specified.
- We're only considering population values associated with the Australian Bureau of Statistics' defined Australian Statistical Geographic Standard areas.
- Assuming that the linked Wikidata item will likely have population statements for multiple Australian Bureau of Statistics geographic areas that encompass the item place.
- We’re ignoring any ranking of population statements.
Population selection
The high level steps of the module work flow are decribed below. There are three major steps in the process of selecting the population figure to return.
Step 1. Check which population claims have enough information to be considered
As a minimum they are required to have:
- A point in time qualifier date (this helps to choose the most recent population figures).
- An applies to part qualifier value (this states which ABS geography type the population is for and helps choose the most appropriate geographic area for the place article).
- A determination method qualifier item (this specifies if it is a census population figure or a non-census population estimate and helps define the reference components).
- Some reference information (it is a requirement to have something with which to build a reference but more than the minimum is recommended - see the Population data in Wikidata section).
After filtering for these requirements a subset of population claims is carried forward.
Step 2. Check which population claims have the most recent figures for each geography
Check within the list of claims and find the most recent population figure for each applies to part value, ie: for each applies to part value (UCL, SAL etc) we find the claim with the latest point in time value (2016, 2011 etc).
Step 3. Specify the geography types that can be returned for each place type
If a geography is specified in the |geog=
parameter, only the latest population for that geography will be returned.
If the |geog=
parameter is not specified, the possible geographies for the provided |type=
are:
City | Urban Centres and Localities (UCL) |
Suburb | Suburbs and Localities (SAL) |
Town | Urban Centres and Localities (UCL), Indigenous Locations (ILOC), Suburbs and Localities (SAL) |
Settlement | Suburbs and Localities (SAL), Indigenous Locations (ILOC) |
Locality | Suburbs and Localities (SAL), Indigenous Locations (ILOC) |
TownAndLocality | Urban Centres and Localities (UCL), Indigenous Locations (ILOC), Suburbs and Localities (SAL) |
LGA | Local Government Areas (LGA) |
Region | Local Government Areas (LGA) (for now) |
Compile the module output, using only latest claims in specified geographies
For all possible geographies, the latest claims found will be returned.
If there is only one matching claim, it is simply output:
- 100 (UCL 2021)[1]
If there is more than one claim, they are output as a bulleted list:
- 90 (ILOC 2021)[1]
- 100 (SAL 2021)[2]
References
The references for actual census data are formatted using the census template for the appropriate year, eg: Census 2021 AUS. All other references use the Cite web template.
The census population figure references take this form: Australian Bureau of Statistics (28 June 2022). "Cosmo Newberry (Indigenous Location)". 2021 Census QuickStats. Retrieved 28 June 2022.
The non-census derived population figure references take this form: Australian Bureau of Statistics (29 March 2022). "Population estimates by SA2 and above, 2001 to 2021 (Greater Capital City Statistical Areas)". Australian Regional Population. Retrieved 28 June 2022.
The references are named: name = refwork .. "_" .. year .. "_" .. appliespart .. "_" .. reftitle. This is long-winded because we are aiming for a unique reference name for each population claim.
Example outputs
There are some example outputs in the Infobox Australian place Sandbox Test Cases page here.
What it doesn't do - next steps
There are some issues that we are aware of, have considered but haven't dealt with yet. These will be tackled in time in collaboration with other place article contributors. (No doubt there are many more to add to the list - please do).
- Some tidying up the output within the infobox:
removing unnecessary bullet points when there's only one itemadd links to information about the relevant Censusadding tooltip descriptionchange geography to abbreviation- adding links to explanations of ABS geographic boundaries (add this info to the Census articles and link to sections there)
- Make a table of historic population values (from those available in Wikidata and that meet other module criteria) and test this as a new addition to place articles. As part of possible methods of preserving historic population figures in articles. Possibly a better solution than having multiple old values listed in Infoboxes (eg. Basket_Range,_South_Australia) or having to maintain individually in-text? Not the same idea, but there is a table of historic population values listed in this article).
- Done. Use
{{#invoke:PopulationFromWikidata|HistoricPopulations}}
.
- Done. Use
- Figure out the case of two Infoboxes: Jimbour East, Queensland
- Suppress the population figures for protected areas. (eg, no output for type = protected). Yes?
- Done. Populations for
|type=protected
, and|type=cadastral
are now explicitly suppressed.
- Done. Populations for
- Population density figures need to be computed and added to the Infobox using the same population (and area from corresponding geography) as this module outputs now. With area data uploaded to Wikidata?
- Test that city rank can still be displayed in the Infobox if population coming from the module.
- Done, but not in this module - done in {{Infobox Australian Place}}
- How to integrate (merge correctly) named references from the module with those used in-text. And how to retain historic population values (and references) as the Infobox population automatically updates with the most current figures. The module produces named references that are unique to the population value, but there are currently reference merging bugs associated with references from templates (and modules).
- Done.
- Should we change it so that pop2 still displays even if pop is replaced by the module population? So you can have both the automated population and a specific other population that's relevant to the article for some reason.
- Done, but not in this module - done in {{Infobox Australian Place}}
- Add some more documentation to Wikipedia:WikiProject Australian places/Population data.
- Figure out interactions with the Coord template that's used in the majority of Australian place articles. The Coord template takes a population argument and uses that to determine the display scale of the Coordinates interactive map. Should we make an equivalent module (similar to this one) to bring place coordinates (with appropriate map zoom scales) from Wikidata to the Infobox Australian place template? Then the coordinates (and map scale) can be kept up-to-date with Wikidata. This would require parallel work to determine most appropriate place coordinate definition (eg centroid? of which geographic area?) so coordinates can be bulk imported to Wikidata? Or just rely on people adding the coordinates values to Wikidata manually but cut out the need to use the Coord template to set map zoom scale? Or keep using the Coord template but give it the appropriate population value as selected by (a modified version) of this PopulationFromWikidata module.
- Connect Aboriginal and Torres Strait Islander community Wikidata items with ABS ILOC IDs so ILOC population counts can be uploaded in bulk. Will then need to revisit the ILOC vs SAL preferencing because for some towns ILOC will be more appropriate than SAL (due to geographic area covered).
- Need to revisit mapping of regions to ABS geographies. Maybe they should be mapped to SA3s? Eg: Kimberley region article Also, need to update this article and other equivalents.
- Discrepancy with places with zero population, such as Essendon Fields (Q5399482):
- In QuickStats it says: "No information can be provided because the area selected had no people or a very low population in the 2021 Census."
- But it does have data in Wikidata (population 13, for SAL20886, which is what is in the DataPack).
- This means that the reference URL ends up not backing up the displayed population figure.
- There may be a difference in how this is handled between 2016 and 2021. For example, (Q55771929) has zero population and doesn't show in either 2016 or 2021 QuickStats — but (Q7859816) had 3 people in 2016 (shown in QuickStats) and 4 people in 2021 (not shown in QuickStats). Both places have both figures in the DataPack.
What if the outputs are incorrect
All the references produced by this module are followed by an Edit at Wikidata pencil icon which links to the relevant Wikidata item (and specific population claim). This is where people should go to fix any errors in the population figure outputs or references. See next section for lists of what should ideally be included in a Wikidata population claim.
Population data in Wikidata
Wikipedia - Wikidata links
In parallel to development of this module User:99of9 and User:Canley have been working on ensuring all Australian place Wikipedia articles are linked to corresponding Wikidata items (describing that same place). This has largely been done. This enables the use of this module.
Census data
Population data has historically been manually entered to individual Wikidata items. Recently (since ~2017) User:99of9, User:Canley and others have used QuickStatements to do bulk imports of population data from Australian Bureau of Statistics datasets. Part of developing this module was to refine the list of metadata (qualifiers and reference fields) that should be imported alongside the population values.
As at July 2022 the first release of the 2021 census population data has been uploaded for the geographic areas relevant to Australian place Infoboxes. This includes data for Suburbs and Localities (SAL), Indigenous Locations (ILOC) and Local Government Areas (LGA). The Urban Centres and Localities (UCL) data is due to be released in October 2022.
The module requires these qualifiers and reference components to have values in the Wikidata population claim.
- applies to part
- point in time
- determination method
- reference: reference URL
- reference: title
- reference: published in
- reference: retrieved
- reference: Australian Statistical Geography 2021 ID (optional)
An example of a Wikidata item with a correctly filled 2021 population claim (using Census data) is:d:Q2821571#P1082.
Non-census data
Bulk uploads have been done for census data. They have not been done for between-census estimated residential population (ERP) or Data by Region figures, for example. These estimates are useful for capital cities, LGAs and regions.
The module requires that non-census population claims have these components:
- applies to part
- point in time
- determination method
- reference: reference URL
- reference: title
- reference: published in
- reference: retrieved
- reference: publication date
- reference: Australian Statistical Geography 2021 ID (optional)
An example of a Wikidata item with a correctly filled 2021 estimated resident population claim (not the other population claims) is:d:Q11568#P1082. An example of a Wikidata item with a correctly filled 2020 LGA Data by Region population claim (not the other population claims) is:d:Q704257#P1082.
Usage
The module exposes two function.
ListForInfobox( type, wikidata, geog )
{{#invoke:PopulationFromWikidata |ListForInfobox |type=t |wikidata=w |geog=g }}
Parameters:
|type=
the <syntaxhighlight lang="text" class="" style="" inline="1">type</syntaxhighlight> parameter from {{Infobox Australian place}}. Required.|wikidata=
Wikidata ID to override that of the current article. Optional.|geog=
Geography abbreviation if only those populations are to be returned. Valid values are ucl, sal, iloc, lga. Optional.
HistoricPopulations( type, wikidata, geog )
{{#invoke:PopulationFromWikidata |HistoricPopulations|type=t |wikidata=w |geog=g }}
Returns a table of census populations by year and ABS geography.
Parameters:
|type=
the <syntaxhighlight lang="text" class="" style="" inline="1">type</syntaxhighlight> parameter from {{Infobox Australian place}}. Required.|wikidata=
Wikidata ID to override that of the current article. Optional.|geog=
Geography abbreviation if only those populations are to be included in the table. Valid values are ucl, sal, iloc, lga. Optional.
---------------- Defining variables--------------------
local Pop_P = "P1082" -- population property
local Applies_P = "P518" -- applies to part property
local Pointin_P = "P585" -- point in time property
local DetMeth_P = "P459" -- determination method property
local RefURL_P = "P854" -- reference URL
local RefTitle_P = "P1476" -- reference title
local RefPubIn_P = "P1433" -- reference published in property
local DatePub_P = "P577" -- date of publication property
local Publisher_P = "P123" -- publisher property
local Retrieved_P = "P813" -- retrieved property
local Instof_P = "P31" -- instance of property
local ShortN_P = "P1813" -- short name property
local CensusAU_I = "Q5058971" -- Australian census item
local SAL_I = "Q33112019" -- state suburb item (includes SSC and SAL)
--local GCCSA_I = "Q112762887" -- Greater Capital City Statistical Area item
local LGA_I = "Q33127844" -- Local Government Area item
local UCL_I = "Q33127891" -- Urban Centre or Locality
--local SA2_I = "Q33128776" -- SA2
--local SA3_I = "Q118313924" -- SA3
local ILOC_I = "Q112729549" -- Indigenous Location
local item = nil
--------------- Function LastURLSection returns last section of a url, ie: the text after the last '/' ----------------------
local function LastURLSection(url)
local pos = 1
local f
while true do
f = string.find(url, '/', pos, true)
if (f == nil) then
break
else
pos = f + 1
end
end
return string.sub(url, pos)
end
--------------- Function IdForGeog returns the place ID for the specified geography abbreviation. Returns nil if abbreviation is blank. ----------------------
local function IdForGeog(geog)
local id = nil
if geog == "ucl" then
id = UCL_I
elseif geog == "sal" then
id = SAL_I -- includes SSC and SAL
elseif geog == "lga" then
id = UCL_I
elseif geog == "iloc" then
id = ILOC_I
end
return id
end
--------------- Function GetRefsForClaim to check, collate and format all the reference components----------------------
local function GetRefsForClaim(claim, defaulttitle)
local refs = ""
local r = 0
for b, x in pairs(claim.references) do -- loop through all references in a claim
-- each reference in the wikidata will produce a citation reference for the claim
r = r + 1
-- gather various values for potential later use
local refurl = ""
if claim.references[b].snaks[RefURL_P] ~= nil then -- if reference has a reference url, use it
refurl = claim.references[b].snaks[RefURL_P][1].datavalue.value
end
local reftitle = defaulttitle -- default title is the Wikidata item title
if claim.references[b].snaks[RefTitle_P] ~= nil then -- if reference has a title, use it
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) -- load the claim determination method item
local pubinlabel = ""
if claim.references[b].snaks[RefPubIn_P] ~= nil then -- if reference has a published in (it should for all references), use its item's label
local pubin = mw.wikibase.getEntity(claim.references[b].snaks[RefPubIn_P][1].datavalue.value.id)
pubinlabel = pubin.labels.en.value
end
local refwork = pubinlabel -- the default reference work for for non-census references, or fall-back for census references with missing parts
local pubdate = ""
if claim.references[b].snaks[DatePub_P] ~= nil then -- if reference has a date published, use it. This is the second-best option for the published date.
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
-- if determination method is an instance of an australian census
refwork = detmet.labels.en.value .. " " .. pubinlabel -- reference work is determination method label + published in
if detmet.claims[DatePub_P] ~=nil then -- if determination method has a date published, use that as the date
pubdate = mw.language.getContentLanguage():formatDate('j F Y', detmet.claims[DatePub_P][1].mainsnak.datavalue.value.time)
end
end
local refpublisher = ""
if detmet.claims[Publisher_P] ~= nil then -- if determination method has a publisher, use its item's label
local publisheritem = mw.wikibase.getEntity(detmet.claims[Publisher_P][1].mainsnak.datavalue.value.id)
refpublisher = publisheritem.labels.en.value
end
local refaccessdate = ""
if claim.references[b].snaks[Retrieved_P] ~= nil then -- if reference has an access date, use it.
refaccessdate = mw.language.getContentLanguage():formatDate('j F Y', claim.references[b].snaks[Retrieved_P][1].datavalue.value.time)
end
local appliespart = mw.wikibase.getEntity(claim.qualifiers[Applies_P][1].datavalue.value.id).labels.en.value -- the label of the item of the applies to part of the claim
local year = string.sub(claim.qualifiers[Pointin_P][1].datavalue.value.time, 2, 5) -- the population point in time as a year string
local reference
if detmet.claims[Instof_P] ~=nil and detmet.claims[Instof_P][1].mainsnak.datavalue.value.id == CensusAU_I then
-- if determination method is an instance of an australian census
refwork = detmet.labels.en.value .. " " .. pubinlabel -- reference work is determination method label + published in
-- the reference is built using the specific template for the census year, which ensures the link format is correct
local geogid = LastURLSection(refurl) -- the id for the specific ABS reference is easiest to get from the ref URL. It may be the only place it is available.
reference = mw.getCurrentFrame():expandTemplate{title = 'Census_' .. year .. '_AUS', args = {id = geogid, name = reftitle .. " (" .. appliespart .. ")", ["access-date"] = refaccessdate, quick = 'on'} }
else
-- use the provided reference url, and whatever other citation data is available
local citewebargs = {
url = refurl,
title = reftitle .. " (" .. appliespart .. ")" ,
date = pubdate,
work = refwork,
author = "[[" .. refpublisher .. "]]", -- author is used to match existing population references
accessdate = refaccessdate
}
reference = mw.getCurrentFrame():expandTemplate{ title = 'cite web', args = citewebargs }
end
local wdeditpencil = mw.getCurrentFrame():expandTemplate{title = 'EditAtWikidata', args = {qid = item.id, pid = claim.id, nbsp = 1}} -- the Edit At Wikidata icon & link
reference = reference .. wdeditpencil
-- The name of the citation reference will be the same for each wikidata claim reference. This will allow references to the same data to be combined into a single citation reference.
local refname = refwork .. "_" .. year .. "_" .. appliespart .. "_" .. reftitle
if r > 1 then -- 2nd and later references in the same wikidata claim have their number appended, to keep them unique
refname = refname .. "_" .. r
end
refs = refs .. mw.getCurrentFrame():extensionTag{ name = 'ref', content = reference, args = { name = refname} } -- accumulate the citation references
end
return refs
end
--------------- Function GetAbbrLabelYearLink gets 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) -- load the applies to part item
local abbrelabel = appliespartitem.labels.en.value -- 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, use it, with the full label as a tooltip
abbrelabel = mw.getCurrentFrame():expandTemplate{title = 'Abbr', args = {appliespartitem.claims[ShortN_P][1].mainsnak.datavalue.value.text, appliespartitem.labels.en.value } }
end
local year = string.sub(returnclaim.qualifiers[Pointin_P][1].datavalue.value.time, 2, 5) -- the population point in time as a year string
local yearreturn = year -- if no links to Wikipedia articles describing population determination method exist, default is year
local detmetitem = mw.wikibase.getEntity(returnclaim.qualifiers[DetMeth_P][1].datavalue.value.id) -- load the claim determination method item
if detmetitem.sitelinks ~=nil and detmetitem.sitelinks.enwiki ~=nil then -- if determination method item has an enwiki URL
yearreturn = "[[" .. detmetitem.sitelinks.enwiki.title .. "|" .. year .. "]]" -- use that URL as the link for the year value
elseif detmetitem.claims[Instof_P] ~=nil and detmetitem.claims[Instof_P][1].mainsnak.datavalue.value.id == CensusAU_I then -- if determination method is an instance of an australian census
yearreturn = "[[Census_in_Australia#" .. year .. "|" .. year .. "]]" -- use the section of the Census in Australia article as the link for the year value
end
return abbrelabel .. " " .. yearreturn
end
local p = {}
---------------- Function HistoricPopulations returns a wikitable of all census population values for all geography types, or a specified one ---------------
-- parameters:
-- optional: wikidata= the wikidata item to be used instead of the one in the current page
-- optional: geog= a single geography type to return pop values for. Valid are 'ucl', 'sal', 'lga', 'iloc'. If left blank, all geographies will be returned.
function p.HistoricPopulations( frame )
if frame.args.wikidata ~= nil and frame.args.wikidata ~= "" then -- if there's a Wikidata item specified, use it
item = mw.wikibase.getEntity(frame.args.wikidata)
else
item = mw.wikibase.getEntity() -- if there's a Wikidata item connected to the article it will find it here.
end
-- if there are no population claims in the item, return an empty string
if not (item and item.claims and item.claims[Pop_P]) then
return ""
end
-- Find claims with:
-- (1) point in time is not nil
-- (2) applies to part is not nil
-- (3) determination method is not nil
-- (4) References table is not empty
-- (5) The determination method for the claim is an australian census
local validpopclaims = {}
local z = 0
for j, s in pairs(item.claims[Pop_P]) do
local detmetitem = mw.wikibase.getEntity(s.qualifiers[DetMeth_P][1].datavalue.value.id) -- load the claim determination method item
local isCensus = (detmetitem.claims[Instof_P] ~=nil and detmetitem.claims[Instof_P][1].mainsnak.datavalue.value.id == CensusAU_I) -- is determination method an instance of an australian census?
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 and
isCensus then
z = z + 1
validpopclaims[z] = s -- add to valid claims table
end
end
-- if there are no valid claims, return an empty string
if #validpopclaims < 1 then
return ""
end
-- add to history table for all (or requested-geography-only) claims
local geog = nil
if frame.args.geog ~= nil and frame.args.geog ~= "" then
geog = string.lower(frame.args.geog)
end
local oneplaceid = IdForGeog(geog)
local history = {}
local years = {}
local glist= {}
for i, q in pairs(validpopclaims) do
-- get the id and name for the geography of the claim
local claimgeogid = q.qualifiers[Applies_P][1].datavalue.value.id -- the ID of the applies_to_part item in the claim
local appliespartitem = mw.wikibase.getEntity(claimgeogid) -- the applies_to_part item itself
local claimgeogname = appliespartitem.labels.en.value -- the full label for the applies part item
if appliespartitem.claims[ShortN_P] ~= nil then -- If a short name value exists then use it instead of the full item label. The short names in fact should always exist.
claimgeogname = string.upper(appliespartitem.claims[ShortN_P][1].mainsnak.datavalue.value.text)
end
local claimyear = string.sub(q.qualifiers[Pointin_P][1].datavalue.value.time, 2, 5) -- the population point in time as a year string
if (not oneplaceid) or (claimgeogid == oneplaceid) then -- if geog is not specified, or it is and the claim applies_to_part matches it
if not history[claimyear] then
history[claimyear] = {year = claimyear, claim = {}}
table.insert(years, claimyear)
end
local refs = GetRefsForClaim(q, item.labels.en.value)
history[claimyear].claim[claimgeogname] = mw.language.getContentLanguage():formatNum(tonumber(q.mainsnak.datavalue.value.amount)) .. refs
-- also build lists of years and geog names used
glist[claimgeogname] = 1
end
end
-- sort the years table
table.sort(years)
-- build the wikidata table from the history table
local wt = {}
table.insert(wt, '{| class="wikitable"') -- start of table
-- header row
table.insert(wt, '\n|-\n!') -- empty top-left cell
for g, l in pairs(glist) do
table.insert(wt, ' !! ' .. g)
end
-- data rows
for k, v in ipairs(years) do
table.insert(wt, '\n|-\n! | ' .. v) -- left column, contains years
for g, l in pairs(glist) do
if not history[v].claim[g] then
table.insert(wt, '\n| ') -- empty table cell
else
table.insert(wt, '\n| ' .. history[v].claim[g])
end
end
end
table.insert(wt, '\n|}') -- end of table
return table.concat(wt)
end
---------------- Function ListForInfobox returns the most recent population values ---------------
-- parameters:
-- required: type= the type value as for the Infobox
-- optional: wikidata= the wikidata item to be used instead of the one in the current page
-- optional: geog= a single geography type to return pop values for. Valid are 'ucl', 'sal', 'lga', 'iloc'. If left blank, all geographies will be returned.
function p.ListForInfobox( frame )
if frame.args.type == nil then
return ""
end
local luaplacetype = ""
local articleplacetype = string.lower(frame.args.type) -- for the place type supplied, change to a lower case string
if articleplacetype == "town" -- Check for valid place types
or articleplacetype == "suburb"
or articleplacetype == "city"
or articleplacetype == "settlement"
or articleplacetype == "locality"
or articleplacetype == "townlocality"
or articleplacetype == "lga"
or articleplacetype == "region"
then
-- OK to continue
elseif articleplacetype == "cadastral"
or articleplacetype == "protected" then
-- these place types don't have ABS populations
return ""
else
-- unrecognised type supplied
return ""
end
if frame.args.wikidata ~= nil and frame.args.wikidata ~= "" then -- if there's a Wikidata item specified, use it
item = mw.wikibase.getEntity(frame.args.wikidata)
else
item = mw.wikibase.getEntity() -- if there's a Wikidata item connected to the article it will find it here.
end
-- if there are no population claims in the item, return an empty string
if not (item and item.claims and item.claims[Pop_P]) then
return ""
end
------------ PART 1: Find claims that meet mimimum criteria
-- (1) point in time is not nil
-- (2) applies to part is not nil
-- (3) determination method is not nil
-- (4) References table is not empty
local validpopclaims = {}
local z = 0
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 -- add to valid claims table
end
end
-- if there are no valid claims, return an empty string
if #validpopclaims <1 then
return ""
end
--------------- PART 2: Find the latest claim for each geography found
local latestclaim = {}
for i, q in pairs(validpopclaims) do
local oclaimdate = q.qualifiers[Pointin_P][1].datavalue.value.time
local claimgeog = q.qualifiers[Applies_P][1].datavalue.value.id
if latestclaim[claimgeog] == nil
or (latestclaim[claimgeog] ~= nil and oclaimdate >= latestclaim[claimgeog].qualifiers[Pointin_P][1].datavalue.value.time) -- if the max date for a particular geography value is bigger than the last iteration, overwrite with the current claim
then
latestclaim[claimgeog] = q
end
end
--------------- PART 3: specify the geography types that can be returned for each place type
local showtypes = {}
if frame.args.geog ~= nil and frame.args.geog ~= "" then -- if geog is specified, only claims for its id are returned
table.insert(showtypes, IdForGeog(string.lower(frame.args.geog)))
elseif articleplacetype == "town" then
showtypes = {UCL_I, ILOC_I, SAL_I}
elseif articleplacetype == "suburb" then
showtypes = {SAL_I}
elseif articleplacetype == "city" then
showtypes = {UCL_I}
elseif articleplacetype == "settlement" then
showtypes = {SAL_I, ILOC_I}
elseif articleplacetype == "locality" then
showtypes = {SAL_I, ILOC_I}
elseif articleplacetype == "townandlocality" then
showtypes = {UCL_I, ILOC_I, SAL_I}
elseif articleplacetype == "lga" then
showtypes = {LGA_I}
elseif articleplacetype == "region" then -- for now saying region uses LGA_I, but unclear what is most apprpriate ABS geography type. Can revise.
showtypes = {LGA_I}
end
--------------- PART 4: Compile the module output, using only latest claims in specified geographies
local returnlist = {}
for j, t in pairs(showtypes) do
if latestclaim[t] ~= nil then
local refs = GetRefsForClaim(latestclaim[t], item.labels.en.value) -- the references for the max date claim
table.insert (returnlist, mw.language.getContentLanguage():formatNum(tonumber(latestclaim[t].mainsnak.datavalue.value.amount)) .. " (" .. GetAbbrLabelYearLink(latestclaim[t]) .. ")" .. refs)
end
end
local wikitext = ""
if #returnlist == 1 then
-- if there is only one entry in returnlist, return it without a bullet point
wikitext = returnlist[1]
else
-- if there are multiple entries in returnlist, return all the rows with new line and bullet points between them
wikitext = "\n*" .. table.concat(returnlist, "\n*")
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
end
-- ###### this function is just for testing of the upgrade during development
function p.main()
return p.HistoricPopulations( { args = {type = 'town', wikidata = 'Q649969'} } ) .. '\n\n' .. p.HistoricPopulations( { args = {type = 'town', wikidata = 'Q649969', geog='sal'} } )
-- return p.ListForInfobox( { args = {type = 'town', wikidata = 'Q649969'} } )
end
return p