Module:CIA World Factbook
Appearance
| This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
| This module depends on the following other modules: |
Used by {{CIA World Factbook}} and {{Cite CIA World Factbook}}
Usage
Country
{{#invoke:CIA World Factbook|country|country=|section=}}
- Generates the URL for a country entry in the CIA World Factbook.
|country=is the topic country (optional)|section=is the section anchor to link to (e.g., "People and Society") (optional)|year=the year of the articlee|date=the date of the article in the Factbook (either on the article itself, or the access date of the article)
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