Module:CIA World Factbook

Used by {{CIA World Factbook}} and {{Cite CIA World Factbook}}

Module:CIA World Factbook

local p = {}
local getArgs = require('Module:Arguments').getArgs

-- prefix of all World Factbook pages
local factbookPrefix = 'https://web.archive.org/web/%04d%02d%02d000000/https://www.cia.gov/the-world-factbook/'
local archiveURL = 'https://web.archive.org/web/20211205000000/https://www.cia.gov/the-world-factbook/about/archives/download/factbook-%d.zip'

-- Function to turn a string into a URL fragment appropriate for CIA website
local function parseFragment(s)
	if not s then
		return ''
	end
	s = mw.ustring.lower(s)
	s = mw.ustring.gsub(s,' ','-')
	s = mw.ustring.gsub(s,',','')
	return s
end

local monthMap = {january=1,february=2,march=3,april=4,may=5,june=6,
	              july=7,august=8,september=9,october=10,november=11,december=12,
	              jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12}

local function parseDate(date)
	local year, month, day
	if not date then return nil, nil, nil end
	_, _, year, month, day = mw.ustring.find(date,"(20%d%d)-(%d+)-(%d+)")
	if year then return year, month, day end
	_, _, day, month, year = mw.ustring.find(date,"(%d+)%s+(%a+)%s+(20%d%d)")
	month = month and monthMap[mw.ustring.lower(month)]
	if year then return year, month, day end
	_, _, month, day, year = mw.ustring.find(date,"(%a+)%s(%d+),%s+(20%d%d)")
	month = month and monthMap[mw.ustring.lower(month)]
	if year then return year, month, day end
	_, _, year = mw.ustring.find(date,"(20%d%d)")
	if year then return year, nil, nil end
	return nil, nil, nil
end

local function archiveDate(args)
	local year, month, day = parseDate(args.date)
	if not args.year or args.year == year then return tonumber(year), month, day end
	return tonumber(args.year), nil, nil
end

-- Function to fill in factbook link:
-- Arguments:
--    args.country: topic of page (optional)
--    args.section: section of page (optional)
-- Returns:
--    link to World Factbook page about country, with section anchor
function p._country(args)
	local year, month, day = archiveDate(args)
	if not year or year > 2025 then
		year = 2025
		month = 12
		day = 5
	end
	if year < 2021 then
		return mw.ustring.format(archiveURL,year)
	end
	if not month then month = 12 end
	if not day then day = 5 end
	local prefix = mw.ustring.format(factbookPrefix,year,month,day)
	if not args.country then
		return prefix
	end
	local result = prefix..'countries/'..parseFragment(args.country)
	if args.section then
		return result..'/#'..parseFragment(args.section)
	end
	return result
end


function p.country(frame)
	local args = getArgs(frame)
	return p._country(args)
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.