Module:Availability

local p = {}

local function pairsByKeys (t, f)	local a = {} for n in pairs(t) do table.insert(a, n) end table.sort(a, f)	local i = 0     -- iterator variable local iter = function   -- iterator function i = i + 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter end

local function dateFormat(str, precision) if precision == '2' then return string.sub(str, 0, -3) .. 'xx' elseif precision == '3' then return string.sub(str, 0, -6) .. 'xx-xx' else return str end end

local function typeFormat(str) local ret = ''

for t in string.gmatch(str, "[^,]+") do		if t == 'ep' then t = 'EP' else t = t:gsub("^%l", string.upper) end

ret = ret .. t .. ' &bull; ' end if string.sub(ret, -8) == ' &bull; ' then ret = string.sub(ret, 0, -9) end

return ret end

function p._main(song) local cargo = mw.ext.cargo local albums = cargo.query(		'track_listings, albums, releases',		"track_listings._pageName=title, releases.release_date=date, release_date__precision=precision, albums.type=type, track_listings.listing_id=listing_id, headline",		{			where = "releases.release_date != '' AND song_page_title = \"" .. song .. '"',			join = 'track_listings._pageName=albums._pageName,albums.name=releases.work',			groupBy = 'track_listings.work',			orderBy = 'releases.release_date ASC'		}	) local songs = cargo.query(		'track_listings, songs, releases',		"track_listings._pageName=title, releases.release_date=date, release_date__precision=precision, songs.type=type, track_listings.listing_id=listing_id, headline",		{			where = "releases.release_date != '' AND song_page_title = \"" .. song .. '"',			join = 'track_listings._pageName=songs._pageName,songs.name=releases.work',			groupBy = 'track_listings.work',			orderBy = 'releases.release_date ASC'		}	)

local releasesByDate = {} local has_headline = false

for r = 1, #songs do		releasesByDate[songs[r]['date']] = songs[r] if songs[r]['headline'] ~= '' then has_headline = true end end for r = 1, #albums do		releasesByDate[albums[r]['date']] = albums[r] if albums[r]['headline'] ~= '' then has_headline = true end end

local has_result = false local root = mw.html.create local tableRoot = root:tag('table') tableRoot:addClass('mw-datatable sortable jquery-tablesorter') local headerRow = tableRoot:tag('tr') headerRow:tag('th') :addClass('headerSort') :wikitext('Title') if has_headline then headerRow:tag('th') :wikitext('Release') end -- headerRow:tag('th') -- 	:addClass('headerSort') -- 	:wikitext('Release date') headerRow:tag('th') :addClass('headerSort') :wikitext('Type')

for _date, release in pairsByKeys(releasesByDate) do		has_result = true local row = tableRoot:tag('tr') row:tag('td'):wikitext( .. release['title'] .. )

if has_headline then row:tag('td'):wikitext(release['headline']) end

-- row:tag('td'):wikitext(dateFormat(release['date'], release['precision'])) row:tag('td'):wikitext(typeFormat(release['type'])) end

if has_result then return '== Availability ==\n' .. tostring(root) end

return '' end

function p.main(frame) local song = frame.args[1] return p._main(song) end

return p