Module:Tour stats

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

local function fetchShow(song, firstShow, fullShow) local order = 'ASC' local fullSongSql = '' if firstShow == false then order = 'DESC' end if fullShow == true then fullSongSql = ' AND tease = 0 AND abandoned = 0 AND soundcheck = 0 AND prerecorded = 0' end return cargo.query(		'live_songs, shows',		'shows._pageName=page_name, shows.artist=artist, shows.featuring_artist=featuring_artist, shows.date=date, shows.venue=venue, ' ..			'shows.location=location, live_songs.tease=tease, live_songs.abandoned=abandoned, live_songs.soundcheck=soundcheck',		{			where = 'name = "' .. song .. '"' .. fullSongSql,			join = 'live_songs._pageName = shows._pageName',			groupBy = 'page_name',			orderBy = 'date ' .. order,			limit = 1		}	) end

local function fetchLongestShow(song) return cargo.query(		'live_songs, shows',		'shows._pageName=page_name, shows.artist=artist, shows.featuring_artist=featuring_artist, shows.date=date, shows.venue=venue, ' ..			'shows.location=location, live_songs.tease=tease, live_songs.abandoned=abandoned, live_songs.soundcheck=soundcheck, live_songs.length=length',		{			where = 'name = "' .. song .. '"',			join = 'live_songs._pageName = shows._pageName',			orderBy = 'seconds DESC',			limit = 1		}	) end

local function createRowForShow(ulRoot, data, label) local showRow = ulRoot:tag('li') showRow:tag('b'):wikitext(label .. ': ')

local showStr = '' .. data['artist'] .. ' ' .. data['date'] .. ' ' if data['featuring_artist'] ~= nil and data['featuring_artist'] ~= data['artist'] then showStr = showStr .. '(featuring ' .. data['featuring_artist'] .. ') ' end if data['venue'] ~= nil then showStr = showStr .. 'at ' .. data['venue'] if data['location'] ~= '' then showStr = showStr .. ', '		else showStr = showStr .. ' '		end end if data['location'] ~= nil then showStr = showStr .. data['location'] end

showRow:tag('span'):wikitext(showStr .. ' ')

if data['length'] ~= nil then showRow:tag('span'):wikitext('[' .. data['length'] .. ']') end

if data['soundcheck'] == '1' then showRow:tag('span'):css('color', 'gray'):wikitext('(soundcheck) ') end if data['tease'] == '1' then showRow:tag('span'):css('color', 'gray'):wikitext('(tease) ') end if data['abandoned'] == '1' then showRow:tag('span'):css('color', 'gray'):wikitext('(abandoned) ') end end

function p._tour_history(song, longest) local root = mw.html.create

local counts = cargo.query(		'live_songs, shows',		"COUNT(live_songs._ID)=total, SUM(live_songs.tease)=teases, SUM(live_songs.abandoned)=abandons, SUM(live_songs.soundcheck)=soundchecks, SUM(live_songs.prerecorded)=prerecorded, COUNT(DISTINCT shows.artist)=artists",		{			where = 'live_songs.name = "' .. song .. '"',			join = 'shows._pageName = live_songs._pageName'		}	)[1]

if counts['total'] == '0' then return '' end

local statsRoot = root:tag('ul')

-- Totals local teases = tonumber(counts['teases']) local abandons = tonumber(counts['abandons']) local soundchecks = tonumber(counts['soundchecks']) local prerecorded = tonumber(counts['prerecorded']) local totalPlays = tonumber(counts['total']) - prerecorded local numArtists = tonumber(counts['artists']) local totalsRow = statsRoot:tag('li') totalsRow:tag('b'):wikitext('Total plays: ') local totalsStr = "'''[https://spcodex.wiki/wiki/Special:CargoQuery?title=Special%3ACargoQuery&tables=live_songs%2C+shows%2C+live_show_photos%2C+live_show_videos&fields=live_songs._pageName%3Dpage_name%2C+shows.date%3Ddate%2C+shows.artist%3Dartist%2C+shows.featuring_artist%3Dfeaturing_artist%2C+shows.venue%3Dvenue%2C+shows.location%3Dlocation%2C+shows.festival%3Dfestival%2C+shows.notes%3Dnotes%2C+live_songs.tease%3Dtease%2C+live_songs.abandoned%3Dabandoned%2C+live_songs.soundcheck%3Dsoundcheck%2C+IF%28live_show_photos._pageName+IS+NULL+AND+shows.poster+IS+NULL%2C+%27%27%2C+%271%27%29%3Dphotos%2C+live_show_videos._ID%3Dvideo&where=live_songs.prerecorded+%3D+0+AND+live_songs.name+%3D+%22" .. mw.uri.encode(song) .. '%22&join_on=shows._pageName+%3D+live_songs._pageName%2C+shows._pageName%3Dlive_show_photos._pageName%2C+shows._pageName%3Dlive_show_videos._pageName&group_by=shows._pageName&having=&order_by%5B0%5D=shows.date+ASC&order_by_options%5B0%5D=ASC&limit=1000&format=template&template=Live+show+row&named+args=yes ' .. totalPlays .. " plays]''' "

if teases + abandons + soundchecks + prerecorded > 0 then local subcounts = {} if teases > 0 then table.insert(subcounts, teases .. ' tease') end if abandons > 0 then table.insert(subcounts, abandons .. ' abandoned') end if soundchecks > 0 then table.insert(subcounts, soundchecks .. ' soundcheck') end if prerecorded > 0 then local prerecordedStr = "[https://spcodex.wiki/wiki/Special:CargoQuery?title=Special%3ACargoQuery&tables=live_songs%2C+shows%2C+live_show_photos%2C+live_show_videos&fields=live_songs._pageName%3Dpage_name%2C+shows.date%3Ddate%2C+shows.artist%3Dartist%2C+shows.featuring_artist%3Dfeaturing_artist%2C+shows.venue%3Dvenue%2C+shows.location%3Dlocation%2C+shows.festival%3Dfestival%2C+shows.notes%3Dnotes%2C+live_songs.tease%3Dtease%2C+live_songs.abandoned%3Dabandoned%2C+live_songs.soundcheck%3Dsoundcheck%2C+IF%28live_show_photos._pageName+IS+NULL+AND+shows.poster+IS+NULL%2C+%27%27%2C+%271%27%29%3Dphotos%2C+live_show_videos._ID%3Dvideo&where=live_songs.prerecorded+%3D+1+AND+live_songs.name+%3D+%22" .. mw.uri.encode(song) .. '%22&join_on=shows._pageName+%3D+live_songs._pageName%2C+shows._pageName%3Dlive_show_photos._pageName%2C+shows._pageName%3Dlive_show_videos._pageName&group_by=shows._pageName&having=&order_by%5B0%5D=shows.date+ASC&order_by_options%5B0%5D=ASC&limit=1000&format=template&template=Live+show+row&named+args=yes ' .. prerecorded .. " prerecorded]" table.insert(subcounts, prerecordedStr) end

local fullPlaysLink = "[https://spcodex.wiki/wiki/Special:CargoQuery?title=Special%3ACargoQuery&tables=live_songs%2C+shows%2C+live_show_photos%2C+live_show_videos&fields=live_songs._pageName%3Dpage_name%2C+shows.date%3Ddate%2C+shows.artist%3Dartist%2C+shows.featuring_artist%3Dfeaturing_artist%2C+shows.venue%3Dvenue%2C+shows.location%3Dlocation%2C+shows.festival%3Dfestival%2C+shows.notes%3Dnotes%2C+live_songs.tease%3Dtease%2C+live_songs.abandoned%3Dabandoned%2C+live_songs.soundcheck%3Dsoundcheck%2C+IF%28live_show_photos._pageName+IS+NULL+AND+shows.poster+IS+NULL%2C+%27%27%2C+%271%27%29%3Dphotos%2C+live_show_videos._ID%3Dvideo&where=live_songs.tease+%3D+0+AND+live_songs.abandoned+%3D+0+AND+live_songs.prerecorded+%3D+0+AND+live_songs.name+%3D+%22" .. mw.uri.encode(song) .. '%22&join_on=shows._pageName+%3D+live_songs._pageName%2C+shows._pageName%3Dlive_show_photos._pageName%2C+shows._pageName%3Dlive_show_videos._pageName&group_by=shows._pageName&having=&order_by%5B0%5D=shows.date+ASC&order_by_options%5B0%5D=ASC&limit=1000&format=template&template=Live+show+row&named+args=yes ' .. (totalPlays - teases - abandons) .. ' full]' totalsStr = totalsStr .. '(' .. fullPlaysLink .. ', ' .. table.concat(subcounts, ', ') .. ')' end if numArtists > 1 then totalsStr = totalsStr .. ', ' .. numArtists .. ' artists ' end totalsRow:tag('span') :addClass('plainlinks') :wikitext(totalsStr)

-- First show local firstShow = fetchShow(song, true)[1] if totalPlays == 1 then createRowForShow(statsRoot, firstShow, 'Only performance') else createRowForShow(statsRoot, firstShow, 'First performance') if (firstShow['tease'] == '1' or firstShow['abandoned'] == '1' or firstShow['soundcheck'] == '1') then local firstFullShow = fetchShow(song, true, true) if next(firstFullShow) ~= nil then createRowForShow(statsRoot, firstFullShow[1], 'First full performance') end end

-- Last show local lastShow = fetchShow(song, false)[1] createRowForShow(statsRoot, lastShow, 'Last performance') if lastShow['tease'] == '1' or lastShow['abandoned'] == '1' then local lastFullShow = fetchShow(song, false, true) if next(lastFullShow) ~= nil then createRowForShow(statsRoot, lastFullShow[1], 'Last full performance') end end

if longest ~= '' then local longestShow = fetchLongestShow(song)[1] createRowForShow(statsRoot, longestShow, 'Longest performance') end end

return tostring(root) end

function p.tour_history(frame) local song = frame.args[1] local longest = frame.args[2] return p._tour_history(song, longest) end

return p