btw this Wiki is not meant for a wider audience yet

Module:YYMMDDList: Difference between revisions

From HFGCS Wiki
Jump to navigation Jump to search
m Remove category from Lua module (breaks code)
Tag: Manual revert
 
(No difference)

Latest revision as of 00:00, 6 December 2025

local p = {}

-- Main function to display all YYMMDD Infobox usages from a category function p.showAll(frame)

   local output = {}
   local results = {}
   local categoryName = frame.args.category or "YYMMDD Articles"
   -- Get all pages in the category
   local category = mw.title.new(categoryName, 'Category')
   if not category then
       return "Error: Category not found"
   end
   -- Iterate through all pages in the category
   for page in category:getAllTitleObjects() do
       local pageData = p.processPage(page.fullText)
       if pageData then
           table.insert(results, pageData)
       end
   end
   -- Sort by date
   table.sort(results, function(a, b) return a.date < b.date end)
   -- Build output table
   table.insert(output, '{| class="wikitable sortable"')
   table.insert(output, '! Date !! p1 !! p2 !! p3 !! p4 !! Page')
   for _, data in ipairs(results) do
       table.insert(output, string.format('|-\n| %s || %s || %s || %s || %s || %s',
           data.date,
           mw.text.encode(data.p1),
           mw.text.encode(data.p2),
           mw.text.encode(data.p3),
           mw.text.encode(data.p4),
           data.page))
   end
   table.insert(output, '|}')
   table.insert(output, '\n\nTotal pages found: ' .. #results)
   return table.concat(output, '\n')

end

function p.processPage(pageName)

   local title = mw.title.new(pageName)
   if not title or not title.exists then
       return nil
   end
   local content = title:getContent()
   if not content then
       return nil
   end
   -- Extract template parameters
   local p1, p2, p3, p4 = p.extractParams(content)
   -- Extract date from page title (assuming YYMMDD format)
   local dateMatch = string.match(pageName, "(%d%d%d%d%d%d)")
   local displayDate = dateMatch or pageName
   return {
       date = displayDate,
       p1 = p1,
       p2 = p2,
       p3 = p3,
       p4 = p4,
       page = pageName
   }

end

function p.extractParams(content)

   local p1, p2, p3, p4 = "", "", "", ""
   -- Pattern to match YYMMDD Infobox template
   local pattern = "{{%s*YYMMDD[_ ]Infobox%s*|(.-)%s*}}"
   local templateContent = string.match(content, pattern)
   if not templateContent then
       -- Try non-greedy pattern for nested templates
       pattern = "{{%s*YYMMDD[_ ]Infobox%s*|(.-)\n}}"
       templateContent = string.match(content, pattern)
   end
   if templateContent then
       -- Extract each parameter
       p1 = string.match(templateContent, "|?%s*p1%s*=%s*([^\n|]+)") or ""
       p2 = string.match(templateContent, "|?%s*p2%s*=%s*([^\n|]+)") or ""
       p3 = string.match(templateContent, "|?%s*p3%s*=%s*([^\n|]+)") or ""
       p4 = string.match(templateContent, "|?%s*p4%s*=%s*([^\n|]+)") or ""
       -- Trim whitespace
       p1 = mw.text.trim(p1)
       p2 = mw.text.trim(p2)
       p3 = mw.text.trim(p3)
       p4 = mw.text.trim(p4)
   end
   return p1, p2, p3, p4

end

return p