Module:Sandbox/LVL: Difference between revisionsGive feedback
Jump to navigation
Jump to search
Blanked the page Tags: Blanking Manual revert |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | |||
-- Load language codes | |||
local lang_codes = mw.loadJsonData("Data:LangCodes.json") | |||
-- cache page contents | |||
local cache = {} | |||
-- Find the end by counting braces (unchanged) | |||
local function findBlockEnd(content, start_pos) | |||
local open_braces = 0 | |||
for i = start_pos, #content do | |||
local char = content:sub(i, i) | |||
if char == "{" then | |||
open_braces = open_braces + 1 | |||
elseif char == "}" then | |||
open_braces = open_braces - 1 | |||
if open_braces == 0 then | |||
return i | |||
end | |||
end | |||
end | |||
return nil | |||
end | |||
-- Detect language and base page name (unchanged) | |||
local function getLangAndBase() | |||
local fullTitle = mw.title.getCurrentTitle().fullText | |||
local parts = mw.text.split(fullTitle, "/") | |||
local last = parts[#parts] | |||
if lang_codes[last] then | |||
table.remove(parts) | |||
return last, table.concat(parts, "/") | |||
else | |||
return "en", fullTitle | |||
end | |||
end | |||
-- Helper to get page content (cached) | |||
local function getPageContent(title) | |||
if cache[title] then return cache[title] end | |||
local page = mw.title.new(title) | |||
local raw = page and page:getContent() or '' | |||
cache[title] = raw | |||
return raw | |||
end | |||
-- Main function to extract recent updates | |||
function p.getRecentUpdates(frame) | |||
local args = frame.args | |||
local forceDefault = args.default == "true" | |||
local limit = math.min(tonumber(args.limit) or 3, 50) | |||
local lang, basePage = getLangAndBase() | |||
if forceDefault then lang = "en" end | |||
-- Try localized version first (cached) | |||
local localizedTitle = basePage .. "/Update history" .. (lang ~= "en" and ("/" .. lang) or "") | |||
local content = getPageContent(localizedTitle) | |||
-- Fallback to english (cached) | |||
if not content or content == "" then | |||
content = getPageContent(basePage .. "/Update history") or "" | |||
end | |||
local blocks = {} | |||
local pattern = "{{Update history table/row" | |||
local start = content:find(pattern) | |||
local count, limit = 0, limit | |||
while start and count < limit do | |||
local finish = findBlockEnd(content, start) | |||
if not finish then break end | |||
table.insert(blocks, frame:preprocess(content:sub(start, finish))) | |||
count = count + 1 | |||
start = content:find(pattern, finish + 1) | |||
end | |||
if #blocks == 0 then | |||
return '<span style="color:#777;">No recent updates.</span>' | |||
end | |||
return table.concat(blocks, "\n") | |||
end | |||
return p | |||