پرش به محتوا

پودمان:Highest archive number

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
-- This module finds the highest existing archive number for a set of talk
-- archive pages.

local expSearch = require('Module:Exponential search')
local num_con = require('Module:Numeral converter').convert
local p = {}

local function raiseStartNumberError(start)
	error(mw.ustring.format(
		'شماره آغازین «%s» که به [[پودمان:Highest archive number]] فرستاده شده، نامعتبر است (باید رقم باشد)',
		num_con('fa', tostring(start))
	), 3)
end

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)
	-- Check our inputs
	if type(prefix) ~= 'string' or not mw.ustring.find(prefix, '%S') then
		error('هیچ پیشوندی به [[پودمان:Highest archive number]] وارد نشده‌است', 2)
	end
	if start ~= nil and (type(start) ~= "number" or math.floor(start) ~= start) then
		raiseStartNumberError(start)
	end
	start = start or 1
	
	-- Do an exponential search for the highest archive number
	local result = expSearch(function (i)
		local archiveNumber = i + start - 1
		local page = prefix .. num_con('fa', tostring(archiveNumber))
		return pageExists(page)
	end, 10)
	
	if result == nil then
		-- We didn't find any archives for our prefix + start number
		return nil
	else
		-- We found the highest archive, but the number is always 1-based, so
		-- adjust it for our start number
		return result + start - 1
	end
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		trim = false,
		removeBlanks = false,
		wrappers = 'الگو:Highest archive number'
	})
	local prefix = args[1]
	
	-- Get the start archive number, if specified.
	local start = args.start or args['آغاز']
	if start == "" then
		start = nil
	elseif start then
		start = num_con('fa', tonumber(start))
		if not start then
			raiseStartNumberError(args.start or args['آغاز'])
		end
	end

	return p._main(prefix, start)
end

return p