Module:Live song

local p = {}

local function link(name, title) local out = '[['

if title == '' then out = out .. name else out = out .. name .. '|' .. title end

return out .. ']]' end

local function live_debut(name) local page_title = mw.title.getCurrentTitle.text local show_date = p._parse_date(page_title) local cargo = mw.ext.cargo local debut = cargo.query(		'live_songs, ',		'MIN(date)=min_date, MAX(date)=max_date, DATE_FORMAT(NOW, "%Y-%m-%d")=today, DATE_FORMAT(DATE_SUB(NOW, INTERVAL 1 DAY), "%Y-%m-%d")=yesterday',		{			where = 'name = "' .. name .. '" AND abandoned = 0 AND soundcheck = 0 AND tease = 0'		}	)

if next(debut) == nil then return false end mw.logObject(debut[1]['today']) mw.logObject(debut[1]['yesterday'])

-- 1 = live debut, 2 = final performance, 3 = only performance if debut[1]['min_date'] == show_date then if debut[1]['max_date'] == show_date then return 3 end return 1 elseif debut[1]['max_date'] == show_date and debut[1]['today'] ~= show_date and debut[1]['yesterday'] ~= show_date then return 2 end end

function p._entry(name, title, note, cover, tease, abandoned, length, acoustic, piano, soundcheck, prerecorded, vip) local out = '"'

if cover ~=  or tease ~=  or abandoned ~= '' then local mw_title = mw.title.makeTitle(0, name)

if mw_title.exists then out = out .. link(name, title) elseif title == '' then out = out .. name else out = out .. title end else out = out .. link(name, title) end

out = out .. '" '

if cover ~= '' then out = out .. '&#91;' .. cover .. '&#93; '	end

if vip ~= '' then out = out .. '(VIP) ' elseif prerecorded ~= '' then out = out .. '(prerecorded) ' elseif tease ~=  and abandoned ~=  then out = out .. ' (tease / abandoned) ' elseif tease ~= '' then out = out .. ' (tease) ' elseif abandoned ~= '' then out = out .. ' (abandoned) ' end

if piano ~= '' then out = out .. ' (piano) ' elseif acoustic ~= '' then out = out .. ' (acoustic) ' end

if note ~= '' then out = out .. '(' .. note .. ') ' end

if length ~= '' then out = out .. '[' .. length .. '] '	end

if not(soundcheck ~= '') then local debut_status = live_debut(name) if debut_status == 1 then out = out .. " (live debut) " elseif debut_status == 2 then out = out .. " (final performance) " elseif debut_status == 3 then out = out .. " (only performance) " end end

return out end

function p.entry(frame) local name = frame.args[1] local title = frame.args[2] local note = frame.args[3] local cover = frame.args[4] local tease = frame.args[5] local abandoned = frame.args[6] local length = frame.args[7] local acoustic = frame.args[8] local piano = frame.args[9] local soundcheck = frame.args[10] local prerecorded = frame.args[11] local vip = frame.args[12] return p._entry(name, title, note, cover, tease, abandoned, length, acoustic, piano, soundcheck, prerecorded, vip) end

function p._parse_date(title) return string.match(title, "%d%d%d%d%-%d%d%-%d%d") end

function p.parse_date(frame) local name = frame.args[1] return p._parse_date(name) end

function p.time_to_seconds(frame) local str = frame.args[1] local hours, minutes, seconds = string.match(str, '(%d+):(%d%d):(%d%d)') if hours == nil then hours = 0 minutes, seconds = string.match(str, '(%d+):(%d%d)') end

return (60 * 60 * hours) + (60 * minutes) + seconds end

function p.seconds_to_time(frame) local given = frame.args[1] local hours = math.floor(math.fmod(given, 86400) / 3600) local minutes = math.floor(math.fmod(given, 3600) / 60) local seconds = math.floor(math.fmod(given, 60))

if hours > 0 then return string.format("%d:%02d:%02d", hours, minutes, seconds) elseif minutes > 0 then return string.format("%d:%02d", minutes, seconds) elseif seconds > 0 then return string.format("0:%02d", seconds) else return '0:00' end end

return p