Module:Musician tour stats

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

local function fetchShow(artist, firstShow) local order = 'ASC' if firstShow == false then order = 'DESC' end return cargo.query(		'shows',		'shows._pageName=page_name, shows.artist=artist, shows.featuring_artist=featuring_artist, ' ..			'shows.date=date, shows.venue=venue, shows.location=location, ',		{			where = 'lineup HOLDS "' .. artist .. '"',			orderBy = 'date ' .. order,			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'] ~= '' and data['featuring_artist'] ~= data['artist'] then mw.logObject(data) showStr = showStr .. '(featuring ' .. data['featuring_artist'] .. ') ' end if data['venue'] ~= '' then showStr = showStr .. 'at ' .. data['venue'] if data['location'] ~= '' then showStr = showStr .. ', '		else showStr = showStr .. ' '		end end if data['location'] ~= '' then showStr = showStr .. data['location'] end

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

function p._tour_stats(artist) local root = mw.html.create

local counts = cargo.query(		'shows',		"COUNT(_ID)=total",		{ where = 'lineup HOLDS "' .. artist .. '"' }	)[1]

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

local statsRoot = root:tag('ul')

-- Totals local totalShows = tonumber(counts['total']) local totalsRow = statsRoot:tag('li') totalsRow:tag('b'):wikitext('Total shows: ') local totalsStr = "[https://spcodex.wiki/wiki/Special:CargoQuery?title=Special%3ACargoQuery&tables=shows&fields=shows._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&where=shows.lineup+HOLDS+%22" .. mw.uri.encode(artist) .. "%22&join_on=&group_by=shows._pageName&having=&order_by%5B0%5D=shows.date+ASC&order_by_options%5B0%5D=ASC&limit=1000&offset=&format=template&template=Live+show+row&named+args=yes&delimiter= " .. totalShows .. " shows]'''" totalsRow:tag('span') :addClass('plainlinks') :wikitext(totalsStr)

-- First show local firstShow = fetchShow(artist, true)[1] createRowForShow(statsRoot, firstShow, 'First performance')

-- Last show local lastShow = fetchShow(artist, false)[1] createRowForShow(statsRoot, lastShow, 'Last performance')

return '== Tour stats ==\n' .. tostring(root) end

function p.tour_stats(frame) local artist = frame.args[1] return p._tour_stats(artist) end

return p