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

function p._music_videos(month, day) local results = cargo.query(		'music_videos',		'_pageName, song, YEAR(release_date)=year',		{			where = get_date_sql(month, day),			orderBy = 'release_date ASC'		}	)

if next(results) == nil then return {} end

local messages = {} for r = 1, #results do		local song = results[r] messages[#messages + 1] = "'''" .. song['year'] .. "''': The music video for \"" .. song['song'] .. "\" was released" end

return messages end

function p.music_videos(frame) local month = frame.args[1] local day = frame.args[2] return '* ' .. table.concat(p._music_videos(month, day), "\n* ") 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, songs, albums',		'IF(songs.artist IS NULL, albums.artist, songs.artist)=artist, releases._pageName=subject, IF(songs._ID IS NULL, "album", "song")=type, YEAR(releases.release_date)=year, releases.country=country, releases.format=format',		{			where = get_date_sql(month, day),			orderBy = 'release_date ASC',			join = 'releases._pageName = songs._pageName, releases._pageName = albums._pageName',			groupBy = 'releases._ID'		}	) local music_videos = p._music_videos(month, day)

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

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

subject_data[subject] = { artist = results[r]['artist'], type = results[r]['type'], year = 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 formatting = '"'		if subject_data[subject]['type'] == 'album' then			formatting = "''"		end

local msg = "'''" .. subject_data[subject]['year'] .. "''': " .. formatting .. "" .. subject .. '' .. formatting if subject_data[subject]['artist'] ~= 'The Smashing Pumpkins' then msg = msg .. ' by ' .. subject_data[subject]['artist'] end msg = msg .. ' 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

-- merge in music videos for k,v in pairs(music_videos) do		messages[#messages + 1] = v	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