Jump to content

Module:Sandbox/DVRTed

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by DVRTed (talk | contribs) at 09:15, 27 June 2025 (reflect changes). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
local functs = {}

local function parse_timestamp(ts)
    local hour, min, day, monthname, year = ts:match(
                                                "(%d%d):(%d%d), (%d+) ([^ ]+) (%d+) %(UTC%)")
    if not (hour and min and day and monthname and year) then return nil end

    local months = {
        January = 1,
        February = 2,
        March = 3,
        April = 4,
        May = 5,
        June = 6,
        July = 7,
        August = 8,
        September = 9,
        October = 10,
        November = 11,
        December = 12
    }

    return os.time {
        year = tonumber(year),
        month = months[monthname],
        day = tonumber(day),
        hour = tonumber(hour),
        min = tonumber(min),
        sec = 0
    }
end

function trim(s) return s:match("^%s*(.-)%s*$") end

local function populate_sections(raw_content)
    local sections = {}
    local current_title = nil
    local current_body = ""

    for line in raw_content:gmatch("[^\r\n]+") do
        local section_title = line:match("^==%s*(.-)%s*==$")
        if section_title then
            if current_title then
                table.insert(sections, {
                    title = current_title,
                    body = current_body:gsub("^%s*", ""):gsub("%s*$", "")
                })
            end
            current_title = section_title
            current_body = ""
        else
            if current_title then
                current_body = current_body .. line .. "\n"
            end
        end
    end

    if current_title then
        table.insert(sections, {
            title = current_title,
            body = current_body:gsub("^%s*", ""):gsub("%s*$", "")
        })
    end
    return sections

end

function getlatest_section(sections)
    local timestamp = "%d%d:%d%d, %d+ [^ ]+ %d%d%d%d %(UTC%)"
    local timed_sections = {}
    for _, section in ipairs(sections) do
        local body = section.body
        local title = section.title
        local latest_time = 0
        local latest_raw = nil

        for ts in body:gmatch(timestamp) do
            local parsed = parse_timestamp(ts)
            if parsed and parsed > latest_time then
                latest_time = parsed
                latest_raw = ts
            end
        end

        if latest_time > 0 then
            table.insert(timed_sections, {
                title = title,
                body = body,
                latest_time = latest_time,
                latest_raw = latest_raw
            })
        end
    end

    local latest_section = {raw = nil, time = 0, title = nil}

    for _, section in ipairs(timed_sections) do
        if (section.latest_time > latest_section.time) then
            latest_section.raw = section.latest_raw
            latest_section.time = section.latest_time
            latest_section.title = section.title
        end
    end
    return latest_section

end

functs.talkstats = function(frame)
    local prefixed_title = frame.args[1]
    local title = mw.title.new(prefixed_title)
    local raw_content = title and title.getContent and title:getContent()

    local sections = populate_sections(raw_content)
    local latest_section = getlatest_section(sections)
    if latest_section.title then

        local time_ago = frame:preprocess(
                             "{{time ago|" .. latest_section.raw .. "}}")
        return string.format(
                   "Number of threads: '''%d'''<br>Thread with the most recent comment: [[%s#%s|%s]] (%s)",
                   #sections, prefixed_title, latest_section.title,
                   latest_section.title, time_ago)
    end
end

return functs