<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FFrietjes%2Fcountry_population_table</id>
	<title>Module:Sandbox/Frietjes/country population table - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://stockhub.co/index.php?action=history&amp;feed=atom&amp;title=Module%3ASandbox%2FFrietjes%2Fcountry_population_table"/>
	<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Frietjes/country_population_table&amp;action=history"/>
	<updated>2026-05-27T01:21:48Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://stockhub.co/index.php?title=Module:Sandbox/Frietjes/country_population_table&amp;diff=145433&amp;oldid=prev</id>
		<title>imported&gt;Frietjes: Frietjes moved page Module:Country population table to Module:Sandbox/Frietjes/country population table without leaving a redirect</title>
		<link rel="alternate" type="text/html" href="https://stockhub.co/index.php?title=Module:Sandbox/Frietjes/country_population_table&amp;diff=145433&amp;oldid=prev"/>
		<updated>2020-09-15T13:34:23Z</updated>

		<summary type="html">&lt;p&gt;Frietjes moved page &lt;a href=&quot;/index.php?title=Module:Country_population_table&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Module:Country population table (page does not exist)&quot;&gt;Module:Country population table&lt;/a&gt; to &lt;a href=&quot;/research/Module:Sandbox/Frietjes/country_population_table&quot; title=&quot;Module:Sandbox/Frietjes/country population table&quot;&gt;Module:Sandbox/Frietjes/country population table&lt;/a&gt; without leaving a redirect&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
local lang = mw.language.getContentLanguage()&lt;br /&gt;
&lt;br /&gt;
local function getPopulations(qid)&lt;br /&gt;
	-- Return table of all population or nil if none found.&lt;br /&gt;
	-- Each entry is a table { POP, TIMESTAMP } where POP is a number.&lt;br /&gt;
	local populations = {}&lt;br /&gt;
	local props = mw.wikibase.getAllStatements(qid, &amp;#039;P1082&amp;#039;)&lt;br /&gt;
	if not props then return nil end&lt;br /&gt;
	for _, v in pairs(props) do&lt;br /&gt;
		if v.qualifiers and v.qualifiers[&amp;quot;P585&amp;quot;] then&lt;br /&gt;
			local timestamp = v.qualifiers[&amp;quot;P585&amp;quot;][1].datavalue.value.time&lt;br /&gt;
			timestamp = timestamp:gsub(&amp;quot;%-00%-00T&amp;quot;, &amp;quot;-01-01T&amp;quot;)&lt;br /&gt;
			if string.sub(timestamp, 1, 1) ~= &amp;#039;-&amp;#039; then&lt;br /&gt;
				populations[#populations + 1] = {&lt;br /&gt;
					tonumber(v.mainsnak.datavalue.value.amount),  -- number or nil&lt;br /&gt;
					tonumber(lang:formatDate(&amp;#039;U&amp;#039;,timestamp))  -- timestamp&lt;br /&gt;
				}&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if next(populations) then  -- if table is not empty&lt;br /&gt;
		return populations&lt;br /&gt;
	end&lt;br /&gt;
	return nil&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function getPopEst(qid)&lt;br /&gt;
	local populations = getPopulations(qid)&lt;br /&gt;
	if not populations then return -1 end&lt;br /&gt;
	-- get the two most recent population counts&lt;br /&gt;
	local popCurrent, popPrevious, timeCurrent, timePrevious = -1, -1, -1, -1&lt;br /&gt;
	for i, v in ipairs(populations) do&lt;br /&gt;
		if v[2] &amp;gt; timeCurrent then&lt;br /&gt;
			popPrevious = popCurrent&lt;br /&gt;
			timePrevious = timeCurrent&lt;br /&gt;
			popCurrent = v[1]&lt;br /&gt;
			timeCurrent = v[2]&lt;br /&gt;
		elseif v[2] &amp;gt; timePrevious then&lt;br /&gt;
			popPrevious = v[1]&lt;br /&gt;
			timePrevious = v[2]&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if (timeCurrent &amp;gt; 0) and (timePrevious &amp;gt; 0) then&lt;br /&gt;
		local timeNow = lang:formatDate( &amp;#039;U&amp;#039;, nil, true )&lt;br /&gt;
		return math.floor(popCurrent + (popCurrent - popPrevious)/(timeCurrent - timePrevious)*(timeNow - timeCurrent) + 0.5)&lt;br /&gt;
	else&lt;br /&gt;
		return -1&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.row(frame)&lt;br /&gt;
    local function formatnum(num)&lt;br /&gt;
        return frame:callParserFunction{ name = &amp;#039;formatnum&amp;#039;, args = num }&lt;br /&gt;
    end&lt;br /&gt;
    local function flag(str)&lt;br /&gt;
    	return frame:expandTemplate{ title = &amp;#039;flag&amp;#039;, args = { str } }&lt;br /&gt;
    end&lt;br /&gt;
   	local args = frame.args&lt;br /&gt;
   	local res = &amp;#039;&amp;#039;&lt;br /&gt;
   	&lt;br /&gt;
    local argnums = {}&lt;br /&gt;
    for k, v in pairs( args ) do&lt;br /&gt;
		if type( k ) == &amp;#039;number&amp;#039; and v ~= &amp;#039;&amp;#039; then&lt;br /&gt;
			argnums[#argnums + 1] = k&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	table.sort(argnums)&lt;br /&gt;
	&lt;br /&gt;
	local wpop = getPopEst(&amp;#039;Q2&amp;#039;)&lt;br /&gt;
	&lt;br /&gt;
	for j = 1,#argnums do&lt;br /&gt;
		local country = args[argnums[j]]&lt;br /&gt;
&lt;br /&gt;
		local qid = nil&lt;br /&gt;
		if country == nil or country == &amp;quot;&amp;quot; then &lt;br /&gt;
			qid = mw.wikibase.getEntityIdForCurrentPage()&lt;br /&gt;
		else&lt;br /&gt;
			qid = mw.wikibase.getEntityIdForTitle(country)&lt;br /&gt;
		end&lt;br /&gt;
	&lt;br /&gt;
		local cpop = getPopEst(qid)&lt;br /&gt;
&lt;br /&gt;
		if (cpop &amp;gt; 0) and (wpop &amp;gt; 0) then&lt;br /&gt;
			local pct = math.floor(1000*cpop / wpop + 0.5) / 10&lt;br /&gt;
			cpop = formatnum(cpop)&lt;br /&gt;
			res = res .. &amp;#039;|-\n|&amp;#039; .. flag(country) .. &amp;#039;||&amp;#039; .. cpop .. &amp;#039; || &amp;#039; .. lang:formatDate( &amp;#039;d F Y&amp;#039;, nil, true ) .. &amp;#039; || &amp;#039; .. pct .. &amp;#039;%\n&amp;#039;&lt;br /&gt;
		else&lt;br /&gt;
			res = res .. &amp;#039;Error: Country = &amp;#039; .. country .. &amp;#039; Country population estimate = &amp;#039; .. cpop .. &amp;#039; World population estimate = &amp;#039; .. wpop&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return res&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>imported&gt;Frietjes</name></author>
	</entry>
</feed>