Module:Infobox live show

From SPCodex, The Smashing Pumpkins wiki

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

local p = {}
local cargo = mw.ext.cargo
local moduleInfobox = require('Module:Infobox')
local moduleDelink = require('Module:Delink')

local members = {
	['Aronoff'] = 'Kenny Aronoff',
	['Auf der Maur'] = 'Melissa Auf der Maur',
	['Bates'] = 'Jack Bates',
	['Billy Corgan'] = 'Billy Corgan',
	['Bradley'] = 'Stephen Bradley',
	['Byrne'] = 'Mike Byrne',
	['Chamberlin'] = 'Jimmy Chamberlin',
	['Cole'] = 'Katie Cole',
	['Corgan'] = 'Billy Corgan',
	['Fiorentino'] = 'Nicole Fiorentino',
	['Flemion'] = 'Dennis Flemion',
	['Garson'] = 'Mike Garson',
	['Harriton'] = 'Lisa Harriton',
	['Hodges'] = 'Stephen Hodges',
	['Holmes'] = 'Chris Holmes',
	['Iha'] = 'James Iha',
	['McNair'] = 'Gabrial McNair',
	['Melvoin'] = 'Jonathan Melvoin',
	['Morris'] = 'Dan Morris',
	['Pooley'] = 'Kristopher Pooley',
	['Remschneider'] = 'Eric Remschneider',
	['Reyes'] = 'Ginger Pooley',
	['Schroeder'] = 'Jeff Schroeder',
	['Shankar'] = 'Gingger Shankar',
	['Stoermer'] = 'Mark Stoermer',
	['Strawberry'] = 'Linda Strawberry',
	['Swan'] = 'Sierra Swan',
	['Tulis'] = 'Mark Tulin',
	['Walker'] = 'Matt Walker',
	['Wilk'] = 'Brad Wilk',
	['Wretzky'] = "D'arcy Wretzky",
	['William Patrick Corgan'] = 'Billy Corgan'
}

function p._delinkList(list)
	local t = {}
	for m in string.gmatch(list, "([^,]+)") do
		table.insert(t, moduleDelink._delink({m}))
	end
	return table.concat(t, ',')
end

function p._normalizeMembers(names, linkIfExists)
	local t = {}
	for m in string.gmatch(names, "([^,]+)") do
		m = mw.text.trim(m)
		local member = m
		if members[m] ~= nil then
			member = members[m]
		end

		if linkIfExists then
			local mw_title = mw.title.makeTitle(0, member)
			if mw_title.exists then
				member = '[[' .. member .. ']]'
			end
		end
		table.insert(t, member)
	end
	return table.concat(t, ', ')
end

function p.normalizeMembers(frame)
	local names = frame.args[1]
	local linkIfExists = frame.args[2]
	return p._normalizeMembers(names, linkIfExists)
end

function p._prevShowLink(target)
	local span = mw.html.create('span')
	span:wikitext('<div>[[File:Font Awesome 5 regular arrow-alt-circle-left.svg|50px|link=' .. target .. '|alt=Previous show]]</div>[[' .. target .. '|Prev. show]]')
	return tostring(span)
end

function p._prevShow(showDate, pageName)
	local prevShow = cargo.query('shows', '_pageName',
		{
			where = 'date < "' .. showDate .. '" OR (date = "' .. showDate .. '" AND _pageName < "' .. pageName .. '")',
			limit = 1,
			orderBy = 'date DESC, _pageName DESC'
		}
	)[1]

	if prevShow == nil then
		return ''
	end

	return p._prevShowLink(prevShow['_pageName'])
end

function p.prevShowLink(frame)
	return p.prevShowLink(frame.args[1])
end

function p.prevShow(frame)
	local showDate = frame.args[1]
	local pageName = frame.args[2]
	return p._prevShow(showDate, pageName)
end

function p._nextShowLink(target)
	local span = mw.html.create('span')
	span:wikitext('<div>[[File:Font Awesome 5 regular arrow-alt-circle-right.svg|50px|link=' .. target .. '|alt=Next show]]</div>[[' .. target .. '|Next show]]')
	return tostring(span)
end

function p.nextShowLink(frame)
	return p._nextShowLink(frame.args[1])
end

function p._nextShow(showDate, pageName)
	local nextShow = cargo.query('shows', '_pageName',
		{
			where = 'date > "' .. showDate .. '" OR (date = "' .. showDate .. '" AND _pageName > "' .. pageName .. '")',
			limit = 1,
			orderBy = 'date ASC, _pageName ASC'
		}
	)[1]

	if nextShow == nil then
		return ''
	end
	
	return p._nextShowLink(nextShow['_pageName'])
end

function p.nextShow(frame)
	local showDate = frame.args[1]
	local pageName = frame.args[2]
	return p._nextShow(showDate, pageName)
end

function p._venueInfo(pageTitle)
	local info = cargo.query('venues', 'name, location, coords, type, capacity',
		{
			where = '_pageName = "' .. pageTitle .. '"',
			limit = 1
		}
	)[1]

	if info == nil then
		return ''
	end

	return info
end

function p.venueInfo(frame)
	local pageName = frame.args[1]
	return p._venueInfo(pageName)
end

function p.infobox(frame)
	local is_mainspace = mw.title.getCurrentTitle().namespace == 0
	local dateStr = frame:getParent().args.date or ''
	local venue = frame:getParent().args.venue or ''
	local venue_name = moduleDelink._delinkTarget({venue})
	local info = {}
	if venue:find('^%[%[') then
		info = p._venueInfo(venue_name)
	end
	local lat = frame:getParent().args.lat or ''
	local lng = frame:getParent().args.lng or ''
	local location = frame:getParent().args.location or info['location'] or ''
	local venue_type = frame:getParent().args.venue_type or info['type'] or ''
	local capacity = frame:getParent().args.capacity or info['capacity'] or ''
	local categories = '[[Category:Live performances]]' ..
		'[[Category:' .. moduleDelink._delink({frame:getParent().args.featuring_artist or frame:getParent().args.artist}) .. ' live performances]]' ..
		'[[Category:' .. mw.language.new('en'):formatDate('Y', dateStr) .. ' live performances]]'

	frame.args.above = frame:getParent().args.name or (dateStr .. ' – ' .. location)

	if info ~= '' and info['coords'] ~= nil then
		local coords = mw.text.split( info['coords'], ',' )
		lat = coords[1]
		lng = coords[2]
	end

	if lat ~= '' and lng ~= '' then
		frame.args.data7 = frame:expandTemplate{
			title = 'maplink single',
			args = { lat, lng }
		}
	else
		categories = categories .. '[[Category:Live performances with missing coordinates]]'
	end
	frame.args.data8 = location
	frame.args.data9 = frame:expandTemplate{
		title = 'venue type',
		args = { venue_type }
	}

	local venuesWithoutCapacity = {
		['Studio'] = true,
		['Radio studio'] = true,
		['Recording studio'] = true,
		['TV studio'] = true,
		['Hotel room'] = true,
		['Cemetery'] = true,
		['House'] = true,
		['Private home'] = true,
	}
	if capacity ~= '' then
		frame.args.data10 = frame:callParserFunction('FORMATNUM', { capacity })
	elseif venuesWithoutCapacity[frame.args.data9] == nil then
		-- frame.args.data10 =  'Unknown'
		categories = categories .. '[[Category:Live performances with unknown venue capacity]]'
	end

	if is_mainspace then
		local coords = ''
		local country = ''
		if lat ~= '' and lng ~= '' then
			coords = lat .. ',' .. lng
		end
		if location ~= '' then
			country = string.sub(location, -2)
		end
		local cargo_data = {
			'_table=shows',
			artist = moduleDelink._delink({frame:getParent().args.artist}),
			featuring_artist = moduleDelink._delink({frame:getParent().args.featuring_artist}),
			tour = frame:getParent().args.tour or '',
			date = dateStr,
			venue = venue_name,
			venue_wikitext = frame:getParent().args.venue,
			location = location,
			country = country,
			coords = coords,
			venue_type = venue_type,
			capacity = capacity,
			lineup = p._normalizeMembers(frame:getParent().args.personnel or frame:getParent().args.lineup),
			bands = p._delinkList(frame:getParent().args.bands or ''),
			poster = frame:getParent().args.poster or '',
			festival = frame:getParent().args.festival or '',
			notes = frame:getParent().args.notes or ''
		}

		frame:callParserFunction('#cargo_store', cargo_data)
	end

	return moduleInfobox.infobox(frame.args) .. (is_mainspace and categories or '')
end

return p