Module:Live song counter: Difference between revisions
No edit summary Tag: Manual revert |
No edit summary Tag: Reverted |
||
Line 1: | Line 1: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local tableTools = require('Module:TableTools') | |||
local Date = require('Module:Date')._Date | |||
local p = {} | local p = {} | ||
local | |||
-- Helper function to safely format a date using Module:Date | |||
local function formatDate(d) | |||
if d and d ~= '' then | |||
local ok, parsed = pcall(Date, d) | |||
if ok and parsed then | |||
return parsed:tostring() | |||
end | |||
end | |||
return d | |||
end | |||
-- Helper function to get performance data for a song | -- Helper function to get performance data for a song | ||
Line 8: | Line 21: | ||
local timesPerformed = frame:preprocess('{{#ask:[[Live song::' .. song .. ']]|format=count}}') | local timesPerformed = frame:preprocess('{{#ask:[[Live song::' .. song .. ']]|format=count}}') | ||
if timesPerformed == "0" then | if timesPerformed == "0" then | ||
timesPerformed = "0" | timesPerformed = "0" | ||
end | end | ||
-- Earliest known performance | -- Earliest known performance | ||
local | local earliestDateRaw = 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 earliestCity = frame:preprocess('{{#ask:[[Live song::' .. song .. ']]|?Concert city=|sort=Concert date|order=asc|mainlabel=-|limit=1|searchlabel=}}') | ||
local earliest = "" | local earliest = "No known performances" | ||
if timesPerformed > "0" then | if timesPerformed > "0" then | ||
earliest = | local formattedDate = formatDate(earliestDateRaw) | ||
earliest = formattedDate .. ' in ' .. earliestCity | |||
end | end | ||
-- Latest known performance | -- Latest known performance | ||
local | local latestDateRaw = 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 latestCity = frame:preprocess('{{#ask:[[Live song::' .. song .. ']]|?Concert city=|sort=Concert date|order=desc|mainlabel=-|limit=1|searchlabel=}}') | ||
local latest = "" | local latest = "No known performances" | ||
if timesPerformed > "0" then | if timesPerformed > "0" then | ||
latest = | local formattedDate = formatDate(latestDateRaw) | ||
latest = formattedDate .. ' in ' .. latestCity | |||
end | end | ||
Line 51: | Line 61: | ||
for i, song in ipairs(args) do | for i, song in ipairs(args) do | ||
if song and song ~= "" then | if song and song ~= "" then | ||
local songLink = "\"[[" .. song .. "]]\"" | local songLink = "\"[[" .. song .. "]]\"" | ||
local timesPerformed, earliest, latest = getPerformanceData(song, frame) | local timesPerformed, earliest, latest = getPerformanceData(song, frame) | ||
Revision as of 18:45, 7 April 2025
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 Date = require('Module:Date')._Date
local p = {}
-- Helper function to safely format a date using Module:Date
local function formatDate(d)
if d and d ~= '' then
local ok, parsed = pcall(Date, d)
if ok and parsed then
return parsed:tostring()
end
end
return d
end
-- 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}}')
if timesPerformed == "0" then
timesPerformed = "0"
end
-- Earliest known performance
local earliestDateRaw = 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 = "No known performances"
if timesPerformed > "0" then
local formattedDate = formatDate(earliestDateRaw)
earliest = formattedDate .. ' in ' .. earliestCity
end
-- Latest known performance
local latestDateRaw = 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 = "No known performances"
if timesPerformed > "0" then
local formattedDate = formatDate(latestDateRaw)
latest = formattedDate .. ' in ' .. latestCity
end
return timesPerformed, earliest, latest
end
function p.main(frame)
local args = getArgs(frame)
-- Remove duplicates
args = tableTools.removeDuplicates(args)
return p._main(args, frame)
end
function p._main(args, frame)
local output = '{| class="wikitable sortable"'
output = output .. '\n! Song !! Times performed !! Earliest known performance !! Latest known performance'
for i, song in ipairs(args) do
if song and song ~= "" then
local songLink = "\"[[" .. song .. "]]\""
local timesPerformed, earliest, latest = getPerformanceData(song, frame)
output = output .. '\n|-\n| ' .. songLink
output = output .. ' || style="text-align:center;" | ' .. timesPerformed
output = output .. ' || ' .. earliest
output = output .. ' || ' .. latest
end
end
output = output .. '\n|}'
return output
end
return p