Module:Availability: Difference between revisions
MusikAnimal (talk | contribs) No edit summary |
MusikAnimal (talk | contribs) No edit summary |
||
Line 131: | Line 131: | ||
local function fetchShow(song, firstShow, fullShow) | local function fetchShow(song, firstShow, fullShow) | ||
local order = 'ASC' | local order = 'ASC' | ||
local | local fullSongSql = '' | ||
if firstShow == false then | if firstShow == false then | ||
order = 'DESC' | order = 'DESC' | ||
end | end | ||
if fullShow == true then | if fullShow == true then | ||
fullSongSql = ' AND tease = 0 AND abandoned = 0' | |||
end | end | ||
return cargo.query( | return cargo.query( | ||
'live_songs, shows', | 'live_songs, shows', | ||
'shows._pageName=page_name, shows.artist=artist, shows.featuring_artist=featuring_artist, shows.date=date, ' .. | '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 .. '" | where = 'name = "' .. song .. '"' .. fullSongSql, | ||
join = 'live_songs._pageName = shows._pageName', | join = 'live_songs._pageName = shows._pageName', | ||
groupBy = 'page_name', | groupBy = 'page_name', | ||
Line 153: | Line 153: | ||
local function createRowForShow(tableRoot, data, label) | local function createRowForShow(tableRoot, data, label) | ||
local | local showRow = tableRoot:tag('tr') | ||
showRow:tag('th'):wikitext(label) | |||
local | local showStr = '[[' .. data['page_name'] .. ']] ' | ||
if data['featuring_artist'] ~= '' then | if data['featuring_artist'] ~= '' then | ||
showStr = showStr .. '(featuring [[' .. data['featuring_artist'] .. ']]) ' | |||
end | |||
if data['soundcheck'] == '1' then | |||
showStr = showStr .. '(soundcheck) ' | |||
end | end | ||
if data['tease'] == '1' then | if data['tease'] == '1' then | ||
showStr = showStr .. '(tease) ' | |||
end | end | ||
if data['abandoned'] == '1' then | if data['abandoned'] == '1' then | ||
showStr = showStr .. '(abandoned) ' | |||
end | end | ||
showRow:tag('td'):wikitext(showStr) | |||
end | end | ||
Line 172: | Line 175: | ||
local counts = cargo.query( | local counts = cargo.query( | ||
'live_songs | 'live_songs', | ||
"COUNT( | "COUNT(_ID)=total, SUM(tease)=teases, SUM(abandoned)=abandons, SUM(soundcheck)=soundchecks", | ||
{ | { | ||
where = 'live_songs.name = "' .. song .. '" | where = 'live_songs.name = "' .. song .. '"' | ||
} | } | ||
) | )[1] | ||
if | if counts['total'] == '0' then | ||
return '' | return '' | ||
end | end | ||
Line 188: | Line 188: | ||
local statsRoot = root:tag('table') | local statsRoot = root:tag('table') | ||
statsRoot:addClass('wikitable') | statsRoot:addClass('wikitable') | ||
-- Totals | |||
local totalPlays = tonumber(counts['total']) | |||
local teases = tonumber(counts['teases']) | |||
local abandons = tonumber(counts['abandons']) | |||
local soundchecks = tonumber(counts['soundchecks']) | |||
local totalsRow = statsRoot:tag('tr') | local totalsRow = statsRoot:tag('tr') | ||
totalsRow:tag('th'):wikitext('Total plays') | totalsRow:tag('th'):wikitext('Total plays') | ||
local totalsStr = totalPlays .. ' plays ' | |||
local | if teases + abandons + soundchecks > 0 then | ||
local subcounts = {} | |||
if teases > 0 then | |||
table.insert(subcounts, teases .. ' tease') | |||
end | |||
if abandons > 0 then | |||
table.insert(subcounts, abandons .. ' abandoned') | |||
table.insert( | end | ||
if soundchecks > 0 then | |||
table.insert(subcounts, soundchecks .. ' soundcheck') | |||
end | end | ||
totalsStr = totalsStr .. '(' .. table.concat(subcounts, ', ') .. ')' | |||
end | end | ||
totalsRow:tag('td'):wikitext(totalsStr) | |||
local firstShow = fetchShow(song, true)[1] | local firstShow = fetchShow(song, true)[1] | ||
createRowForShow(statsRoot, firstShow, 'First performance') | createRowForShow(statsRoot, firstShow, 'First performance') | ||
if firstShow['tease'] == '1' or firstShow['abandoned'] == '1' then | if firstShow['tease'] == '1' or firstShow['abandoned'] == '1' then | ||
local firstFullShow = fetchShow(song, true, true) | local firstFullShow = fetchShow(song, true, true) | ||
Line 220: | Line 222: | ||
local lastShow = fetchShow(song, false)[1] | local lastShow = fetchShow(song, false)[1] | ||
createRowForShow(statsRoot, lastShow, 'Last performance') | 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 | |||
return '== Tour stats ==\n' .. tostring(root) | return '== Tour stats ==\n' .. tostring(root) |
Revision as of 20:19, 13 October 2020
This module implements the {{availability}} template, which is placed on every song page. This generates the "Availability" and "Tour stats" sections, as applicable.
local p = {}
local cargo = mw.ext.cargo
local function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
local function dateFormat(str, precision)
if precision == '2' then
return string.sub(str, 0, -3) .. 'XX'
elseif precision == '3' then
return string.sub(str, 0, -6) .. 'XX-XX'
else
return str
end
end
local function typeFormat(str)
local ret = ''
for t in string.gmatch(str, "[^,]+") do
if t == 'ep' then
t = 'EP'
else
t = t:gsub("^%l", string.upper)
end
ret = ret .. t .. ' • '
end
if string.sub(ret, -8) == ' • ' then
ret = string.sub(ret, 0, -9)
end
return ret
end
function p._main(song)
local albums = cargo.query(
'track_listings, albums, releases',
"track_listings._pageName=title, releases.release_date=date, release_date__precision=precision, albums.type=type, track_listings.listing_id=listing_id, headline",
{
where = "releases.release_date != '' AND song_page_title = \"" .. song .. '"',
join = 'track_listings._pageName=albums._pageName,albums.name=releases._pageName',
groupBy = 'track_listings.work',
orderBy = 'releases.release_date ASC'
}
)
local songs = cargo.query(
'track_listings, songs, releases',
"track_listings._pageName=title, releases.release_date=date, release_date__precision=precision, songs.type=type, track_listings.listing_id=listing_id, headline",
{
where = "releases.release_date != '' AND song_page_title = \"" .. song .. '"',
join = 'track_listings._pageName=songs._pageName,songs.name=releases.work',
groupBy = 'track_listings.work',
orderBy = 'releases.release_date ASC'
}
)
local releasesByDate = {}
local has_headline = false
for r = 1, #songs do
releasesByDate[songs[r]['date'] .. r] = songs[r]
if songs[r]['headline'] ~= '' then
has_headline = true
end
end
for r = 1, #albums do
releasesByDate[albums[r]['date'] .. r] = albums[r]
if albums[r]['headline'] ~= '' then
has_headline = true
end
end
local has_result = false
local root = mw.html.create()
local tableRoot = root:tag('table')
tableRoot:addClass('wikitable sortable')
local headerRow = tableRoot:tag('tr')
headerRow:tag('th')
:addClass('headerSort')
:wikitext('Title')
if has_headline then
headerRow:tag('th')
:wikitext('Notes')
end
-- headerRow:tag('th')
-- :addClass('headerSort')
-- :wikitext('Release date')
headerRow:tag('th')
:addClass('headerSort')
:wikitext('Type')
for _date, release in pairsByKeys(releasesByDate) do
has_result = true
local row = tableRoot:tag('tr')
row:tag('td'):wikitext('[[' .. release['title'] .. '#track-listing-' .. release['listing_id'] .. '|' .. release['title'] .. ']]')
if has_headline then
row:tag('td'):wikitext(release['headline'])
end
-- row:tag('td'):wikitext(dateFormat(release['date'], release['precision']))
row:tag('td'):wikitext(typeFormat(release['type']))
end
if has_result then
return '== Availability ==\n' .. tostring(root)
end
return '[[Category:Songs with no availability]]'
end
function p.main(frame)
local song = frame.args[1]
return p._main(song)
end
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'
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 createRowForShow(tableRoot, data, label)
local showRow = tableRoot:tag('tr')
showRow:tag('th'):wikitext(label)
local showStr = '[[' .. data['page_name'] .. ']] '
if data['featuring_artist'] ~= '' then
showStr = showStr .. '(featuring [[' .. data['featuring_artist'] .. ']]) '
end
if data['soundcheck'] == '1' then
showStr = showStr .. '(soundcheck) '
end
if data['tease'] == '1' then
showStr = showStr .. '(tease) '
end
if data['abandoned'] == '1' then
showStr = showStr .. '(abandoned) '
end
showRow:tag('td'):wikitext(showStr)
end
function p._tour_history(song)
local root = mw.html.create()
local counts = cargo.query(
'live_songs',
"COUNT(_ID)=total, SUM(tease)=teases, SUM(abandoned)=abandons, SUM(soundcheck)=soundchecks",
{
where = 'live_songs.name = "' .. song .. '"'
}
)[1]
if counts['total'] == '0' then
return ''
end
local statsRoot = root:tag('table')
statsRoot:addClass('wikitable')
-- Totals
local totalPlays = tonumber(counts['total'])
local teases = tonumber(counts['teases'])
local abandons = tonumber(counts['abandons'])
local soundchecks = tonumber(counts['soundchecks'])
local totalsRow = statsRoot:tag('tr')
totalsRow:tag('th'):wikitext('Total plays')
local totalsStr = totalPlays .. ' plays '
if teases + abandons + soundchecks > 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
totalsStr = totalsStr .. '(' .. table.concat(subcounts, ', ') .. ')'
end
totalsRow:tag('td'):wikitext(totalsStr)
local firstShow = fetchShow(song, true)[1]
createRowForShow(statsRoot, firstShow, 'First performance')
if firstShow['tease'] == '1' or firstShow['abandoned'] == '1' then
local firstFullShow = fetchShow(song, true, true)
if next(firstFullShow) ~= nil then
createRowForShow(statsRoot, firstFullShow[1], 'First full performance')
end
end
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
return '== Tour stats ==\n' .. tostring(root)
end
return p