Jump to content

Module:Highest archive number/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 14:41, 1 October 2019 (adapt the algorithm to accept a start value). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module finds the highest existing archive number for a set of talk
-- archive pages.

local expSearch = require('Module:Exponential search')
local p = {}

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

function p._main(prefix, start)
	if type(prefix) ~= 'string' or not prefix:find('%S') then
		error('no prefix supplied to [[Module:Highest archive number]]', 2)
	end
	start = start or 1
	local result = expSearch(function (i)
		local archiveNumber = i + start - 1
		local page = prefix .. tostring(archiveNumber)
		return pageExists(page)
	end, 10)
	if result == nil then
		return nil
	else
		return result + start - 1
	end
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		trim = false,
		removeBlanks = false,
		wrappers = 'Template:Highest archive number'
	})
	local prefix = args[1]
	return p._main(prefix)
end

return p