Module:Today in history

local p = {} local cargo = mw.ext.cargo local ageModule = require('Module:Age')

local function get_date_sql(month, day, date_field) if date_field == nil then date_field = 'release_date' end local sql = date_field .. '__precision = 1 AND ' if month == nil and day == nil then return sql .. 'MONTH(' .. date_field .. ') = MONTH(NOW) AND DAYOFMONTH(' .. date_field .. ') = DAYOFMONTH(NOW)' else return sql .. 'MONTH(' .. date_field .. ') = "' .. month .. '" AND DAYOFMONTH(' .. date_field .. ') = "' .. day .. '"' end end

local function get_age(old_year, new_year) local age = tonumber(new_year) - tonumber(old_year)

if age == 11 or age == 12 or age == 13 then return age .. 'th' end local last = string.sub(age, -1) if last == 1 then return age .. 'st' elseif last == 2 then return age .. 'nd' elseif last == 3 then return age .. 'rd' end

return age .. 'th' end

local function honorific(subject) if subject == 'Billy Corgan' then return 'the almighty' elseif subject == 'Jimmy Chamberlin' then return 'drummer god' else return 'the one and only' end end

function p._birthdays(month, day) local results = cargo.query(		'personnel',		'_pageName=subject, YEAR(birth_date)=year, YEAR(NOW)=this_year, (YEAR(NOW) - YEAR(birth_date))=age',		{			where = get_date_sql(month, day, 'birth_date') .. ' AND death_date IS NULL',			orderBy = 'birth_date ASC'		}	)

if next(results) == nil then return '' end

local messages = {} for r = 1, #results do		local personnel = results[r] local subject = personnel['subject']

messages[#messages + 1] = 'Happy ' .. get_age(personnel['year'], personnel['this_year']) .. ' birthday to ' .. honorific(subject) .. ' ' .. subject .. '!' end

messages = table.concat(messages, "\n* ") return '* ' .. messages end

function p.birthdays(frame) local month = frame.args[1] local day = frame.args[2] return p._birthdays(month, day) end

local function country_msg(country) if string.sub(country, 0, 2) == 'UK' or string.sub(country, 0, 2) == 'US' then return 'the ' .. country else return country end end

function p._releases(month, day) local results = cargo.query(		'releases',		'_pageName=subject, YEAR(release_date)=year, country, format',		{			where = get_date_sql(month, day),			orderBy = 'release_date ASC'		}	)

if next(results) == nil then return '' end

local releases_by_subject = {} local subject_years = {} for r = 1, #results do		local subject = results[r]['subject'] local country = results[r]['country'] or '*' local format_ = results[r]['format']

subject_years[subject] = results[r]['year']

if releases_by_subject[subject] == nil then releases_by_subject[subject] = { [country] = { format_ } }		else if releases_by_subject[subject][country] == nil then releases_by_subject[subject][country] = { format_ } else table.insert(releases_by_subject[subject][country], format_) end end end

local messages = {} for subject, data in pairs(releases_by_subject) do local msg = "'''" .. subject_years[subject] .. "''': " .. subject .. ' was released' local countries = {}

for country, formats in pairs(data) do			if country ~= '*' then countries[#countries + 1] = country_msg(country) .. ' (' .. table.concat(formats, ', ') .. ')' end end

if next(countries) ~= nil then msg = msg .. ' in ' .. mw.text.listToText(countries) end

messages[#messages + 1] = msg end

table.sort(messages) -- sort by year DESC return messages end

function p.releases(frame) local month = frame.args[1] local day = frame.args[2]

ret = p._releases(month, day)

if ret == '' then return '' end

ret = table.concat(ret, "\n* ") ret = '* ' .. ret return "\n==== Releases ====\n" .. tostring(ret) end

return p