Module:Sandbox/Martijn Hoekstra/archivecounter explained

local p = ;local helper = ;helper.rec = function(min, max, prefix, namespace) if (min + 1)

max then return min elseif max

nil then local doublemin = min * 2 local title = mw.title.new(prefix .. " " .. doublemin, namespace) if title.exists then return helper.rec(doublemin, max, prefix, namespace) else return helper.rec(min, doublemin, prefix, namespace) end else local dif = max - min local test = min + math.ceil(dif/2) local title = mw.title.new(prefix .. " " .. test, namespace) if title.exists then return helper.rec(test, max, prefix, namespace) else return helper.rec(min, test, prefix, namespace) end endend

p.archivenumber = function (namespace, prefix) return helper.rec(1, nil, prefix, namespace)end

helper.buildtable = function(agg, cur, last, namespace, page, archiveprefix) if cur > last then return table.concat(agg, "") else local next = agg if ((cur - 1) % 3)

0 then table.insert(next, "\n|-") end table.insert(next, "\n|") table.insert(next, archiveprefix) table.insert(next, " ") table.insert(next, cur .. "") return helper.buildtable(next, cur + 1, last, namespace, page, archiveprefix) endend

p.buildtable = function(namespace, page, archiveprefix) local init = table.insert(init, "

" return wikitextend

p.run = function(frame) local namespace local page local prefix local archive if (frame.args.root and frame.args.root ~= "") then local pattern = "([^:]+):([^/]+)/(.*)" _, _, namespace, page, prefix = mw.ustring.find(frame.args.root, pattern) namespace = mw.ustring.gsub(namespace, " ", "_") else local current = mw.title.getCurrentTitle namespace = mw.ustring.gsub(current.nsText, " ", "_") page = current.text archive = frame.args.archivename or "Archive" end return p.buildtable(namespace, page, archive)end

return p