FANDOM


--[[<nowiki>
Get Update Name for version
getVersion
    Parameters
    1. version number
        If blank or one of the options below,
        the current page name is used
        "P" = previous update
        "N" = next update
        "T" = infobox title (version number) text only
        "V" = infobox version (version + name) text only
        "R" = release number
    2. include event date after name
    3. text only (no link)
--]]
 
local p = {}
 
local versionData = mw.loadData( 'Module:UpdateNumber/data' )
 
local function prevUpdate(curVersion)
    local curDate = nil
 
    if versionData[curVersion] then
        curDate = versionData[curVersion][3]
    end
 
    local prevDate = 0
    local prevVer = ""
 
    if curDate then
        for k, v in pairs(versionData) do
            if v[3] and v[3] < curDate and v[3] > prevDate then
                prevDate = v[3]
                prevVer = k
            end
        end
    end
 
    return prevVer
end
 
local function nextUpdate(curVersion)
    local curDate = nil
 
    if versionData[curVersion] then
        curDate = versionData[curVersion][3]
    end
 
    local nextDate = 99999999
    local nextVer = ""
 
    if curDate then
        for k, v in pairs(versionData) do
            if v[3] and v[3] > curDate and v[3] < nextDate then
                nextDate = v[3]
                nextVer = k
            end
        end
    end
 
    return nextVer
end
 
local function strDate(numDate, ordinal)
    local months = {"January ", "February ", "March ",
                    "April ", "May ", "June ",
                    "July ", "August ", "September ",
                    "October ", "November ", "December "}
 
    local day = math.min(math.fmod(numDate, 100), 31)
 
    local idd = math.fmod(day, 10)
 
    local nth = " "
    if ordinal then
        nth = (idd==1 and day~=11 and "st ")
            or (idd==2 and day~=12 and "nd ")
            or (idd==3 and day~=13 and "rd ")
            or "th "
    end
 
    local year = math.floor(numDate / 100)
 
    local mon = math.min(math.fmod(year, 100), 12)
 
    year = math.floor(year/100)
 
    return (day .. nth .. months[mon] .. year)
end
 
-- return informatin for a single version/update
local function oneVersion(versionNo, inclDate, txtOnly)
    local outType = ""
 
    if versionNo:match("^[NPTVR]$") then
        outType = versionNo
        versionNo = ""
    end
 
    if versionNo == "" then
        local pageName = mw.title.getCurrentTitle().text
 
        if versionData[pageName] then
            versionNo = pageName
        else
            local updateNo = tonumber(pageName:match("^Update (%d+)"))
 
            if updateNo then
                for k, v in pairs(versionData) do
                    if v[4] and updateNo == v[4] then
                        versionNo = k
                        break
                    end
                end
            end
        end
    end
 
    if not versionData[versionNo] then
         return "[[Work In Progress|Unknown Version]]"
    end
 
    if outType == "P" then
        -- output previous version
        versionNo = prevUpdate(versionNo)
        if versionNo == "" then return "Unknown" end
    elseif outType == "N" then
        -- output next version
        versionNo = nextUpdate(versionNo)
        if versionNo == "" then return "--" end
    end
 
    local versionText = ""
 
    if outType == "T" then
        if versionData[versionNo][5] and versionData[versionNo][5] ~= "" then
            versionText = versionData[versionNo][5]
        else
            versionText = versionNo
        end
        txtOnly = true
    elseif outType == "R" then
        versionText = versionData[versionNo][1]
        txtOnly = true
    else
        local upNo = versionData[versionNo][4]
 
        if upNo and upNo == 0 then
            versionText = versionData[versionNo][2]
        else
            versionText = versionData[versionNo][2] .. " Update"
        end
 
        if outType == "V" then
            if versionData[versionNo][5] 
                    and versionData[versionNo][5] ~= "" then
                versionText = versionData[versionNo][5] .. "<br />("
                                .. versionText .. ")"
            else
                versionText = versionNo .. "<br />("
                                .. versionText .. ")"
            end
            txtOnly = true
        end
    end
 
    if not txtOnly then
        if versionData[versionNo][4] and versionData[versionNo][4] ~= 0 then
            versionText = "[[:Category:Update " 
                    .. versionData[versionNo][4] 
                    .. "|" .. versionText .. "]]"
        else            
            versionText = "[[" .. versionNo 
                    .. "|" .. versionText .. "]]"
        end
    end
 
    if inclDate then
        versionText = versionText .. " on "
                    .. strDate(versionData[versionNo][3], true)
    end
 
    return versionText
end
 
-- return a list of versions for year
local function multiVersion(year, vertical)
    local tIndex  = {}
 
    if year and year ~= 0  then
        for k, v in pairs(versionData) do
            if year == math.floor(v[3] / 10000) then
                table.insert(tIndex, k)
            end
        end
    else
        for k, v in pairs(versionData) do
            table.insert(tIndex, k)
        end
    end
 
    if vertical then
        table.sort(tIndex, function(a,b) return a > b end)
    else
        table.sort(tIndex)
    end
 
    local outText = ""
    local sDot    = vertical and "* " or ""
 
    for _, ver in ipairs(tIndex) do
        local verText = ""
 
        if versionData[ver][5] and versionData[ver][5] ~= "" then
            verText = versionData[ver][5]
        end
 
        if vertical or not versionData[ver][5] or versionData[ver][5] ~= "" then
            if versionData[ver][4] and versionData[ver][4] ~= 0 then
                outText = outText .. sDot
                        .. "[[:Category:Update "
                        .. versionData[ver][4]
 
                if verText ~= "" then
                    outText = outText .. "|" .. verText .. "]]"
                else
                    outText = outText .. "|" .. ver .. "]]"
                end
            else
                outText = outText .. sDot
                        .. "[[" .. ver
                if verText ~= "" then
                    outText = outText .. "|" .. verText
                end
                outText = outText .. "]]"
            end
 
            sDot = vertical and "\n* " or " '''ยท''' "
        end
 
        if vertical then
            outText = outText .. " - "
                    ..  string.gsub(tostring(versionData[ver][3]),
                            "(%d%d%d%d)(%d%d)(%d%d)", "%3/%2/%1")
                    .. " - "
                    .. versionData[ver][2]
 
                if not versionData[ver][4] or versionData[ver][4] ~= 0 then
                    outText = outText .. " Update"
                end
        end
    end
 
    return outText
end
 
-- return last X versions formatted for Main Page
local function lastVersions(count)
    local tIndex  = {}
    local today = tonumber(os.date("%Y%m%d"))
 
    for k, v in pairs(versionData) do
        if today >= v[3] and v[4] and v[4] ~= 0 then
            table.insert(tIndex, k)
        end
    end
 
    table.sort(tIndex, function(a,b) return a > b end)
 
    local outText = ""
 
    for i, ver in ipairs(tIndex) do
        local dateText = strDate(versionData[ver][3], false)
        local verText = mw.text.truncate(versionData[ver][2] .. " Update",
                        43 - mw.ustring.len(dateText), nil, true)
 
        outText = outText 
                .. "* [[:Category:Update " 
                .. versionData[ver][4] 
                .. "|"
                .. verText
                .. "]] <small>"
                .. dateText
                .. "</small>\n"
 
        if i >= count then break end
    end
 
    return outText
end
 
-- function get Version Name
function p.getVersion(frame)
    local tArgs
 
    if frame == mw.getCurrentFrame() then
        fParent = frame:getParent()
        tArgs = fParent.args
 
        if (fParent.args[1]) then
            tArgs = fParent.args
        else
            tArgs = frame.args
        end
    else
        tArgs = frame
    end
 
    local versionNo = tArgs[1] or ""
    local inclDate  = (tArgs[2] and tArgs[2] ~= "")
    local txtOnly   = (tArgs[3] and tArgs[3] ~= "")
 
    local multi, year = versionNo:match("^(multi)(%d*)$")
 
    if multi then
        return multiVersion(tonumber(year), inclDate)
    else
        return oneVersion(versionNo, inclDate, txtOnly)
    end
end
 
-- function get Last X Versions formatted for Main Page
function p.getLastVer(frame)
    local tArgs
 
    if frame == mw.getCurrentFrame() then
        fParent = frame:getParent()
        tArgs = fParent.args
 
        if (fParent.args[1]) then
            tArgs = fParent.args
        else
            tArgs = frame.args
        end
    else
        tArgs = frame
    end
 
    local count = tArgs[1] or 5
    count = tonumber(count) or 5
 
    return lastVersions(count)
end
 
return p
 
-- </nowiki>
-- [[Category:Lua Modules]]
Community content is available under CC-BY-SA unless otherwise noted.