Jump to content

Module:Sandbox/DVRTed

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by DVRTed (talk | contribs) at 08:19, 27 June 2025 (copying from testwiki:Module:Sandbox/DVRTed/test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
local functs = {}

local function parseTimestamp(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 section_body_pattern = "==%s*(.-)%s*==%s*([^=]+)"
local subsection_pattern = "===(=*)%s*(.-)%s*===(=*)%s*"
local timestamp = "%d%d:%d%d, %d+ [^ ]+ %d%d%d%d %(UTC%)"

functs.talkstats = function(frame)
	local prefixed_title = frame.args[1]
    local title = mw.title.new(prefixed_title)
    local rawContent = title and title.getContent and title:getContent()
    if not rawContent then return "Page not found or empty" end

    -- replace ===(...=) subsection ===(...=) with [subsec]subsection[subsec]
    -- so it's easier to match sections w/ regex
    local content = rawContent:gsub(subsection_pattern, "[subsec]%2[subsec]")
    content = content .. "\n==END=="

    local sections = {}

    for header, body in content:gmatch(section_body_pattern) do

        local latest_time = 0
        local latest_raw = nil

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

        if latest_time > 0 then
            table.insert(sections, {
                title = header,
                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(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

    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