Pergi ke kandungan

Modul:GPBawah

Daripada Wikipedia, ensiklopedia bebas.
local p = {}

-- Helper function to format the template name.
function p.formatTemplate(minggu, tahun)
    return 'Templat:GP/Minggu ' .. minggu .. ' ' .. tahun
end

-- Main function to find an existing page link.
function p.getLink(frame)
    local currentWeek = tonumber(frame.args.minggu) or tonumber(os.date("%V"))
    local currentYear = tonumber(frame.args.tahun) or tonumber(os.date("%Y"))
    local retries = tonumber(frame.args.retries) or 3 -- Number of weeks to go back.

    for i = 0, retries do
        local minggu = currentWeek - i
        local tahun = currentYear

        -- Handle week roll-over to previous year.
        if minggu < 1 then
            minggu = minggu + 53
            tahun = tahun - 1
        end

        -- Check if the template exists.
        local templat = p.formatTemplate(minggu, tahun)
        local exists = frame:callParserFunction("#ifexist", { templat, "1", "0" })

        if exists == "1" then
            -- Return a link to the existing template.
            return minggu .. ' ' .. tahun
        end
    end

    -- If no template is found, return a fallback message.
    return "'''Tiada gambar pilihan tersedia'''"
end

return p