Jump to content

Module:Highest archive number: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
create a recursive algorithm for finding the highest archive number of a set of archive pages
(No difference)

Revision as of 15:05, 5 October 2014

-- This module finds the highest existing archive number for a set of talk
-- archive pages.

local p = {}

local function pageExists(page)
	local success, title = pcall(mw.title.new, page)
	return success and title and title.exists
end

local function findHighestArchive(prefix, i, lower, upper)
	if pageExists(prefix .. tostring(i)) then
		lower = i
		if upper and i + 1 == upper then
			return i
		elseif upper then
			i = math.floor(lower + (upper - lower) / 2)
		else
			i = i * 2
		end
		return findHighestArchive(prefix, i, lower, upper)
	else
		upper = i
		lower = lower or math.floor(i / 2)
		i = math.floor(lower + (upper - lower) / 2)
		return findHighestArchive(prefix, i, lower, upper)
	end
end

p.find = findHighestArchive

function p._main(args)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	return p._main(args)
end

return p