Jump to content

Module:Update history

From The Deadlock Wiki

Main module for rendering the last 3 update entries on a page. Note: This module is not intended for direct use.

Usage[edit source]


local p = {}

-- Load language codes
local lang_codes = mw.loadJsonData("Data:LangCodes.json")

-- Find the end by counting braces
local function findBlockEnd(content, start_pos)
    local open_braces = 0
    for i = start_pos, #content do
        local char = content:sub(i, i)
        if char == "{" then
            open_braces = open_braces + 1
        elseif char == "}" then
            open_braces = open_braces - 1
            if open_braces == 0 then
                return i
            end
        end
    end
    return nil
end

-- Detect language and base page name
local function getLangAndBase()
    local fullTitle = mw.title.getCurrentTitle().fullText
    local parts = mw.text.split(fullTitle, "/")
    local last = parts[#parts]

    if lang_codes[last] then
        table.remove(parts)
        return last, table.concat(parts, "/")
    else
        return "en", fullTitle
    end
end

-- Helper to get page content
local function getPageContent(title)
    local page = mw.title.new(title)
    return page and page:getContent() or nil
end

-- Main function to extract recent updates
function p.getRecentUpdates(frame)
    local forceDefault = frame.args.default == "true"
    local lang, basePage = getLangAndBase()
    if forceDefault then lang = "en" end

    -- Try localized version first
    local localizedTitle = basePage .. "/Update history" .. (lang ~= "en" and ("/" .. lang) or "")
    local content = getPageContent(localizedTitle)

    -- Fallback to english
    if not content or content == "" then
        content = getPageContent(basePage .. "/Update history") or ""
    end

    local blocks = {}
    local pattern = "{{Update history table/row"
    local start = content:find(pattern)
    local count, limit = 0, 3

    while start and count < limit do
        local finish = findBlockEnd(content, start)
        if not finish then break end
        table.insert(blocks, frame:preprocess(content:sub(start, finish)))
        count = count + 1
        start = content:find(pattern, finish + 1)
    end

    return table.concat(blocks, "\n")
end

return p