Difference between revisions of "Module:Highest archive number"

From blackwiki
Jump to navigation Jump to search
blackwiki>Mr. Stradivarius
m (Protected Module:Highest archive number: High-risk Lua module ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only template editors and admins] (indefinite)))
blackwiki>Mr. Stradivarius
(alter pageExists so that title.exists is caught by pcall)
Line 5: Line 5:
  
 
local function pageExists(page)
 
local function pageExists(page)
local success, title = pcall(mw.title.new, page)
+
local success, exists = pcall(function()
return success and title and title.exists
+
return mw.title.new(page).exists
 +
end)
 +
return success and exists
 
end
 
end
  

Revision as of 13:56, 10 February 2015

This module finds the highest archive number for a given set of numbered archive pages. The only input required is the prefix of the archive set. For example, for Talk:France/Archive 1, Talk:France/Archive 2, etc., the prefix would be "Talk:France/Archive ".

The module uses an exponential search algorithm, so it will make relatively few expensive function calls even for large numbers of archives. (The expensive function call is necessary to determine whether an individual archive exists.) For 1-10 archive pages, the module typically uses 4-6 expensive function calls; for 20-100 archives, it uses 6-12 calls; and for hundreds of archives, it uses 12-20 calls.

Because the module uses an exponential search, no gaps are allowed in the archive numbers; if there are any gaps, then the module may return the number of any archive page that exists where the following archive page does not exist. However, archive numbers do not have to start at 1; if they start at a higher number, you can specify this number in the start parameter.

Usage

From wikitext

From wikitext this module is usually used via the {{highest archive number}} template. However, it is also possible to use it directly from invoke with the syntax {{#invoke:highest archive number|main|prefix|start=start}}.

From Lua

Load the module with the following code:

local mHAN = require('Module:Highest archive number')

You can then use it like this:

mHAN._main(prefix, start)

The prefix variable is the prefix of the archive set.

Examples

#invoke code Lua code Result
{{#invoke:highest archive number|main|Wikipedia:Administrators' noticeboard/IncidentArchive}} mHAN._main("Wikipedia:Administrators' noticeboard/IncidentArchive")
{{#invoke:highest archive number|main|Talk:Byzantine Empire/Archive }} mHAN._main("Talk:Byzantine Empire/Archive ")
{{#invoke:highest archive number|main|Wikipedia talk:WikiProject Chemicals/Archive |start=2005}} mHAN._main("Wikipedia talk:WikiProject Chemicals/Archive ", 2005)



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

local p = {}

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

local function midPoint(lower, upper)
	return math.floor(lower + (upper - lower) / 2)
end

local function findHighestArchive(prefix, i, lower, upper)
	if i < 1 then
		return nil
	elseif pageExists(prefix .. tostring(i)) then
		lower = i
		if upper and i + 1 == upper then
			return i
		elseif upper then
			i = midPoint(lower, upper)
		else
			i = i * 2
		end
		return findHighestArchive(prefix, i, lower, upper)
	else
		upper = i
		lower = lower or 0
		i = midPoint(lower, upper)
		return findHighestArchive(prefix, i, lower, upper)
	end
end

function p._main(prefix)
	if type(prefix) ~= 'string' or not prefix:find('%S') then
		error('no prefix supplied to [[Module:Highest archive number]]', 2)
	end
	return findHighestArchive(prefix, 10)
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