Module:Infobox song: Difference between revisions

From SPCodex, The Smashing Pumpkins wiki
No edit summary
No edit summary
Line 167: Line 167:
local song = frame.args[2]
local song = frame.args[2]
local release_date = frame.args[3]
local release_date = frame.args[3]
return p._validateOrFetchReleaseDate(artist, song, release_date)
p._validateOrFetchReleaseDate(artist, song, release_date)
end
end


return p
return p

Revision as of 00:40, 30 July 2023

Documentation for this module may be created at Module:Infobox song/doc

local p = {}
local cargo = mw.ext.cargo

function p._track_listing(song, album)
	local results = cargo.query(
		'track_listings',
		'track, song, song_page_title',
		{
			where = 'work = "' .. album .. '" AND main_listing = 1',
			orderBy = 'track_listings._ID ASC'
		}
	)

	local root = mw.html.create()
	local olRoot = root:tag('ol')
	local has_match = false

	for r = 1, #results do
		local result = results[r]
		local page = mw.title.new(result['song_page_title'])

		wikitext = result['song']
		if page.exists then
			wikitext = '[[' .. result['song_page_title'] .. '|' .. result['song'] .. ']]'
		end

		olRoot:tag('li')
			:attr('value', result['track'])
			:wikitext(wikitext)

		if result['song'] == song then
			has_match = true
		end
	end
	
	if has_match then
		return tostring(root)
	end
	
	return ''

	-- local tableRoot = root:tag('table')

	-- for r = 1, #results do
	-- 	local row = tableRoot:tag('tr')
	-- 	local result = results[r]
	-- 	row:tag('td')
	-- 		:css('padding-right', '5px')
	-- 		:wikitext(result['track'] .. '.')
	-- 	row:tag('td'):wikitext('[[' .. result['song_page_title'] .. '|' .. result['song'] .. ']]')
	-- end
end

function p.track_listing(frame)
	local song = frame.args[1]
	local album = frame.args[2]
	return p._track_listing(song, album)
end

function p._studio_sessions(song)
	local cargo = mw.ext.cargo
	local results = cargo.query(
		'studio_sessions, recordings, songs',
		'studio_sessions._pageName=page',
		{
			where = 'songs._pageName = "' .. song .. '"',
			join = 'recordings.song=songs.name,studio_sessions._pageName=recordings._pageName',
			groupBy = 'studio_sessions._pageName',
			orderBy = 'studio_sessions.date_from ASC'
		}
	)

	if 0 == #results then
		return ''
	elseif 1 == #results then
		return '[[' .. results[1]['page'] .. ']]'
	end

	local root = mw.html.create()
	local ulRoot = root:tag('ul')
		:addClass('studio-sessions')

	local sessions = {}
	for r = 1, #results do
		local page = results[r]['page']

		if page ~= nil and page ~= '' then
			ulRoot:tag('li')
				:wikitext('[[' .. page .. ']]')
		end
	end

	return tostring(root)
	-- return table.concat(sessions, ' • ')
end

function p.studio_sessions(frame)
	local album = frame.args[1]
	return p._studio_sessions(album)
end

function p._validateOrFetchReleaseDate(artist, song, release_date)
	local Date = require('Module:Date')._Date
	release_date = Date(release_date)
	if release_date ~= nil then
		return release_date:text('ymd')
	end

	-- Try to find the earliest US date, or earliest date overall if there isn't a US date.
	local cargo = mw.ext.cargo
	local results = cargo.query(
		"songs, albums, releases",
		"songs._pageName, songs.name, releases.country, releases.release_date, albums.released",
		{
			where = "songs.artist = \"" .. artist .. "\" AND songs.name = \"" .. song .. '"',
			join = "songs._pageName = releases._pageName, songs.main_album = albums.name",
		}
	)

	if next(results) == nil or (#results == 1 and results[1]['releases.release_date'] == nil and results[1]['albums.released'] == nil) then
		return ''
	end

	local currentDate = Date('currentdate')
	-- earliestDate is the earliest release date, either the single or the album
	local earliestDate = currentDate
	-- earliestDateUS is only for single release dates, not albums
	local earliestDateUS = currentDate

	for r = 1, #results do
		local row = results[r]
		local release_table_date = Date(row['releases.release_date']) or currentDate
		local album_table_date = Date(row['albums.released']) or currentDate
		local isUS = row['releases.country'] == 'US' or row['releases.country'] == 'Worldwide'

		if release_table_date < album_table_date then
			-- single release date came first
			if isUS and release_table_date < earliestDateUS then
				earliestDateUS = release_table_date
			end
			if release_table_date < earliestDate then
				earliestDate = release_table_date
			end
		else
			-- album release date came first
			if isUS and release_table_date < earliestDateUS then
				earliestDateUS = release_table_date
			end
			if album_table_date < earliestDateUS then
				earliestDate = album_table_date
			end
		end
	end

	-- Store as var in current frame
	local frame = mw.getCurrentFrame()

	if earliestDateUS < currentDate then
		frame:callParserFunction('#vardefine', 'release_date', earliestDateUS:text('ymd'))
	else
		frame:callParserFunction('#vardefine', 'release_date', earliestDate:text('ymd'))
	end
end

function p.validateOrFetchReleaseDate(frame)
	local artist = frame.args[1]
	local song = frame.args[2]
	local release_date = frame.args[3]
	p._validateOrFetchReleaseDate(artist, song, release_date)
end

return p