Module:Infobox song: Difference between revisions

From SPCodex, The Smashing Pumpkins wiki
No edit summary
No edit summary
Line 68: Line 68:
end
end
cats = cats .. '[[Category:' .. data.category .. ']]'
cats = cats .. '[[Category:' .. data.category .. ']]'
if artist ~= nil and args['nonstandard_artist'] == nil and args['side_project'] == nil then
if artist ~= nil and args['nonstandard_artist'] == nil and args['side_project'] == nil and (is_cover and v == 'cover') == false then
cats = cats .. '[[Category:' .. artist .. ' ' .. data.category:lower() .. ']]'
cats = cats .. '[[Category:' .. artist .. ' ' .. data.category:lower() .. ']]'
end
end

Revision as of 23:00, 31 July 2023

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

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

local types = {
	['single'] = {
		color = 'khaki',
		link = '[[:Category:Singles|Single]]',
		category = 'Singles'
	},
	['song'] = {
		color = '#E6E8FA',
		link = '[[:Category:Songs|Song]]',
		category = 'Songs'
	},
	['promo'] = {
		color = '#E6E8FA',
		link = '[[:Category:Promotional singles|Promotional single]]',
		category = 'Promotional singles'
	},
	['cover'] = {
		color = '#E6E8FA',
		link = '[[:Category:Cover songs|Cover song]]',
		category = 'Cover songs'
	},
	['instrumental'] = {
		color = '#E6E8FA',
		link = '[[:Category:Instrumental songs|Instrumental song]]',
		category = 'Instrumental songs'
	}
}

function p.link( frame )
	local given = frame.args[1]
	if frame.args[1] == '' then
		given = 'song'
	end
	local given = mw.text.split(given, "%s*,%s*")
	return types[given[1]].link
end

function p.color( frame )
	local given = frame.args[1]
	if frame.args[1] == '' then
		given = 'song'
	end
	given = mw.text.split(given, "%s*,%s*")
	return types[given[1]].color
end

function p.categories( frame )
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	local given = args[1]
	if args[1] == '' then
		given = 'song'
	end
	local given = mw.text.split(given, "%s*,%s*")
	local artist = args[2]

	local cats = '[[Category:Songs]]'
	local is_cover = false

	for _, v in ipairs(given) do
		local data = types[v]
		mw.logObject(v)
		if v == 'cover' then
			is_cover = true
		end
		cats = cats .. '[[Category:' .. data.category .. ']]'
		if artist ~= nil and args['nonstandard_artist'] == nil and args['side_project'] == nil and (is_cover and v == 'cover') == false then
			cats = cats .. '[[Category:' .. artist .. ' ' .. data.category:lower() .. ']]'
		end
	end

	if args['side_project'] ~= nil then
		cats = cats .. "[[Category:" .. args['side_project'] .. " side projects]]"
	elseif artist ~= nil and args['nonstandard_artist'] == nil and is_cover == false then
		cats = cats .. "[[Category:" .. artist .. " songs]]"
	end

	return cats
end

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

	if earliestDateUS < currentDate then
		return earliestDateUS:text('ymd')
	else
		return 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]
	local normalized_release_date = p._validateOrFetchReleaseDate(artist, song, release_date)
	if normalized_release_date ~= '' and normalized_release_date ~= nil then
		frame:callParserFunction('#vardefine', 'release_date', normalized_release_date)
	end
end

return p