Module:Infobox song: Difference between revisions

From SPCodex, The Smashing Pumpkins wiki
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local cargo = mw.ext.cargo
local cargo = mw.ext.cargo
local types = {
['single'] = {
color = 'khaki',
link = '[[:Category:Singles|Singles]]',
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 = mw.text.split(frame.args[1], "%s*,%s*")
return types[given[1]].link
end
function p.color( frame )
local given = mw.text.split(frame.args[1], "%s*,%s*")
return types[given[1]].color
end
function p.categories( frame )
local given = mw.text.split(frame.args[1], "%s*,%s*")
local artist = frame.args[2]
local cats = '[[Category:Songs]]'
local is_cover = false
for _, v in ipairs(given) do
local data = types[v]
cats = cats .. '[[Category:' .. data.category .. ']]'
if v == 'cover' then
is_cover = true
end
end
if is_cover == false then
cats = cats .. '[[Category:' .. artist .. ' ' .. data.category .. ']]'
end
return cats
end


function p._track_listing(song, album)
function p._track_listing(song, album)

Revision as of 21:30, 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|Singles]]',
		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 = mw.text.split(frame.args[1], "%s*,%s*")
	return types[given[1]].link
end

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

function p.categories( frame )
	local given = mw.text.split(frame.args[1], "%s*,%s*")
	local artist = frame.args[2]

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

	for _, v in ipairs(given) do
		local data = types[v]
		cats = cats .. '[[Category:' .. data.category .. ']]'

		if v == 'cover' then
			is_cover = true
		end
	end

	if is_cover == false then
		cats = cats .. '[[Category:' .. artist .. ' ' .. data.category .. ']]'
	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