Module:Live song counter
Documentation for this module may be created at Module:Live song counter/doc
local getArgs = require('Module:Arguments').getArgs
local tableTools = require('Module:TableTools')
local mwText = mw.text
local p = {}
-- Helper function to get performance data for a song
local function getPerformanceData(song, frame)
-- Times performed
local timesPerformed = frame:preprocess('{{#ask:[[Live song::' .. song .. ']]|format=count}}')
local timesNum = tonumber(timesPerformed) or 0
-- Handle case of no known performances early
if timesNum == 0 then
return "0", "No known performances", "–"
end
-- Earliest known performance
local earliestDate = frame:preprocess('{{#ask:[[Live song::' .. song .. ']]|?Concert date=|sort=Concert date|order=asc|limit=1|link=none|searchlabel=|mainlabel=-}}')
local earliestCity = frame:preprocess('{{#ask:[[Live song::' .. song .. ']]|?Concert city=|sort=Concert date|order=asc|mainlabel=-|limit=1|searchlabel=}}')
local earliest = earliestDate .. ' – ' .. earliestCity
-- Latest known performance
local latestDate = frame:preprocess('{{#ask:[[Live song::' .. song .. ']]|?Concert date=|sort=Concert date|order=desc|limit=1|link=none|searchlabel=|mainlabel=-}}')
local latestCity = frame:preprocess('{{#ask:[[Live song::' .. song .. ']]|?Concert city=|sort=Concert date|order=desc|mainlabel=-|limit=1|searchlabel=}}')
local latest = latestDate .. ' – ' .. latestCity
return tostring(timesNum), earliest, latest
end
function p.main(frame)
local args = getArgs(frame)
args = tableTools.removeDuplicates(args)
return p._main(args, frame)
end
function p._main(args, frame)
local rows = {
'{| class="wikitable sortable"',
'! Song !! Times<br />performed !! First known<br />performance !! Last known<br />performance'
}
for i, song in ipairs(args) do
if song and song ~= "" then
-- Sanitize song name where necessary
local safeSong = mwText.nowiki(song) -- Optional for debug/logging
-- Use MediaWiki link syntax directly
local songLink = "\"[[" .. song .. "]]\""
-- Get performance data
local timesPerformed, earliest, latest = getPerformanceData(song, frame)
table.insert(rows, '|-')
table.insert(rows, '| ' .. songLink)
table.insert(rows, '| style="text-align:center;" | ' .. timesPerformed)
table.insert(rows, '|| ' .. earliest)
table.insert(rows, '|| ' .. latest)
end
end
table.insert(rows, '|}')
return table.concat(rows, '\n')
end
return p