Module:Availability: Difference between revisions

From SPCodex, The Smashing Pumpkins wiki
No edit summary
(fix issue with grouping by date)
Line 72: Line 72:


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

Revision as of 01:46, 2 July 2020

This module implements the {{availability}} template, which is placed on every song page. This generates the "Availability" and "Tour stats" sections, as applicable.


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 .. ' • '
	end
	
	if string.sub(ret, -8) == ' • ' 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._pageName',
			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'] .. r] = songs[r]
		if songs[r]['headline'] ~= '' then
			has_headline = true
		end
	end
	for r = 1, #albums do
		releasesByDate[albums[r]['date'] .. r] = 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('Notes')
	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'] .. '#track-listing-' .. release['listing_id'] .. '|' .. 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 '[[Category:Songs with no availability]]'
end

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

return p