Module:CIA World Factbook
From Phuketer
Documentation for this module may be created at Module:CIA World Factbook/doc
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