Jump to content

Module:DetectAbandoned

From Wikinews, the free news source you can write!
[create] Documentation
local p = {}

-- Month names
local MONTH_NAMES = {
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
}

-- Parse timestamp string to Unix timestamp
local function parseTimestamp(timestamp)
    if not timestamp or timestamp == "" then
        return nil
    end
    
    return os.time{
        year  = tonumber(timestamp:sub(1, 4)),
        month = tonumber(timestamp:sub(5, 6)),
        day   = tonumber(timestamp:sub(7, 8)),
        hour  = tonumber(timestamp:sub(9, 10)),
        min   = tonumber(timestamp:sub(11, 12)),
        sec   = tonumber(timestamp:sub(13, 14))
    }
end

-- Get last edit timestamp for a page
local function getLastEditTime(frame, pageTitle)
    if not pageTitle or not pageTitle.exists then
        return nil
    end
    
    local timestamp = frame:preprocess('{{REVISIONTIMESTAMP:' .. pageTitle.prefixedText .. '}}')
    return parseTimestamp(timestamp)
end

-- Format Unix timestamp to readable date
local function formatDate(timestamp)
    local date = os.date("*t", timestamp)
    return string.format("%s %d, %d", MONTH_NAMES[date.month], date.day, date.year)
end

function p.main(frame)
    -- Get the parameter passed to the module
    local contentIfNotAbandoned = frame.args[1] or ""
    
    -- Detect nocat parameter (any value)
    local nocat = frame.args.nocat ~= nil
    
    local title = mw.title.getCurrentTitle()
    
    -- Get article edit time
    local articleEditTime = getLastEditTime(frame, title)
    if not articleEditTime then
        return "Error: No timestamp found for article"
    end
    
    -- Determine and get talk page edit time
    local talkTitle = title.namespace % 2 == 0 
        and mw.title.new(title.text, title.namespace + 1)
        or title
    
    local talkEditTime = talkTitle and talkTitle.exists 
        and getLastEditTime(frame, talkTitle)
        or nil
    
    -- Get current time
    local now = parseTimestamp(frame:preprocess('{{CURRENTTIMESTAMP}}'))
    
    -- Determine most recent edit
    local mostRecentEditTime = (talkEditTime and talkEditTime > articleEditTime) 
        and talkEditTime 
        or articleEditTime
    
    -- Check threshold (432000 seconds = 5 days)
    local threshold = 432000
    local secondsAgo = now - mostRecentEditTime
    
    if secondsAgo >= threshold then
        -- Page is abandoned 
        local thresholdMetTime = mostRecentEditTime + threshold
        local formattedDate = formatDate(thresholdMetTime)
        
        local templateOutput = frame:expandTemplate{
            title = 'Template:Abandoned',
            args = {formattedDate}
        }
        
        if nocat then
            return templateOutput
        else
            return "[[Category:Abandoned]]" .. templateOutput
        end
    else
        -- Page is NOT abandoned
        return contentIfNotAbandoned
    end
end

return p