Module:Live song counter

From Weezerpedia

Documentation for this module may be created at Module:Live song counter/doc

local getArgs = require('Module:Arguments').getArgs
local p = {}
local tableTools = require('Module:TableTools')

-- 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}}')

    -- Check if times performed is 0
    if timesPerformed == "0" then
        timesPerformed = "0"  -- Default value for zero times performed
    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 = ""
    if timesPerformed > "0" then
        earliest = earliestDate .. ' in ' .. earliestCity
    else
        earliest = "No known performances"
    end

    -- 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 = ""
    if timesPerformed > "0" then
        latest = latestDate .. ' in ' .. latestCity
    else
        latest = "No known performances"
    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
            -- Keep the original songLink format as requested
            local songLink = "\"[[" .. song .. "]]\""
            
            -- Get performance data
            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