User:Coffi/Module:ToolbotArchive

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

-- ToolbotArchive -- MediaWiki Lua module to auto‑close dormant threads after 24 h -- and archive talk pages exceeding 3000 words (WordCounter.net logic) -- -- Usage (in wikitext): -- Script error: No such module "User:Coffi/Module:ToolbotArchive". -- -- No parameters needed – user details are hard‑coded for User:Coffi.


local p = {} local mw = mw


-- CONFIGURATION ---------------------------------------------------


local USER_PAGE = "User talk:Coffi" local ARCHIVE_PREFIX = "User talk:Coffi/Archive/" local WORD_LIMIT = 1 -- words local INACTIVITY_HOURS = 24 -- hours


-- Helper: WordCounter.net‑style word count ------------------------ local function wordCountWCNet(text)

   -- WordCounter counts a sequence of alphabetic chars or numbers
   -- apostrophes inside a word count as part of the word (e.g. "don't" = 1)
   -- Hyphens split words ("well‑known" = 2)
   -- Anything else is a delimiter.
   text = mw.ustring.gsub(text, "[%z\1-\31]", " ")
   -- replace hyphens with space
   text = mw.ustring.gsub(text, "‑", " ")
   local count = 0
   for word in mw.ustring.gmatch(text, "[%w']+") do
       count = count + 1
   end
   return count

end

-- Helper: collapse a thread -------------------------------------- local function collapseThread(threadTitle)

return string.format("

Discussion finished
\n== %s ==\n|}", threadTitle)

end

-- Main algorithm -------------------------------------------------- function p.run()

   local page = mw.title.new(USER_PAGE)
   if not page then return "" end
   local content = page:getContent() or ""
   local now = os.time()
   local modified = false
   -- Detect section headers and timestamps
   local newContent = {}
   local currentSection = {}
   local sectionTitle = nil
   local lastTimestamp = nil
   local function flushSection()
       if sectionTitle then
           -- check inactivity
           if lastTimestamp and (now - lastTimestamp) > INACTIVITY_HOURS*3600 then
table.insert(currentSection, 1, "
Discussion finished
") table.insert(currentSection, "|}")
               modified = true
           end
           table.insert(newContent, "== "..sectionTitle.." ==")
           for _,l in ipairs(currentSection) do table.insert(newContent, l) end
       end
       currentSection = {}
       sectionTitle = nil
       lastTimestamp = nil
   end
   for line in mw.text.gsplit(content, "\n", true) do
       local header = line:match("^==+%s*(.-)%s*==+")
       if header then
           flushSection()
           sectionTitle = header
       else
           -- capture signature timestamps like "18:05, 25 April 2025 (UTC)"
           local ts = line:match("(%d%d:%d%d, %d%d? %u%l+ %d%d%d%d) %(UTC%)")
           if ts then
               lastTimestamp = mw.language.getContentLanguage():formatDate("U", ts)
           end
       end
       table.insert(currentSection, line)
   end
   flushSection()
   local finalText = table.concat(newContent, "\n")
   -- word count & archive
   if wordCountWCNet(finalText) > WORD_LIMIT then
       local archiveTitle = ARCHIVE_PREFIX..os.date("Archive %Y‑%m‑%d", now)
       local at = mw.title.new(archiveTitle)
       if at and not at.exists then
           at:create(finalText, "Toolbot: new automatic archive")
           finalText = "CoffeeAutomatically archived to "..archiveTitle..""
       end
       modified = true
   end
   if modified then
       page:save(finalText, "Toolbot: auto‑archive/close inactive threads")
   end
   return "" -- no visible output

end

return p