Jump to content

Module:Effective protection expiry/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jackmcbarn (talk | contribs) at 17:01, 14 January 2016 (save a call to mw.title.new and add a comment). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local effectiveProtectionLevel = require('Module:Effective protection level/sandbox')._main

local p = {}

-- Returns the expiry of a restriction of an action on a given title, or unknown if it cannot be known.
-- If no title is specified, the title of the page being displayed is used.
function p._main(action, pagename)
	local title
	if type(pagename) == 'table' and pagename.prefixedText then
		title = pagename
	elseif pagename then
		title = mw.title.new(pagename)
	else
		title = mw.title.getCurrentTitle()
	end
	pagename = title.prefixedText
	local _, rawExpiry = effectiveProtectionLevel(action, title)
	if rawExpiry == 'infinity' then
		return 'infinity'
	elseif rawExpiry == nil then
		-- a nil expiry gets returned for cascading-protected pages, since it's extra work to see their expiry, and not all uses of effective protection level need it. if we're called, we do need it, so find out what it is
		local frame = mw.getCurrentFrame()
		rawExpiry = 0
		for _,v in title.cascadingProtection.sources do
			local cascadingExpiry = frame:callParserFunction('PROTECTIONEXPIRY', action, v)
			if cascadingExpiry == 'infinity' then
				return 'infinity'
			end
			cascadingExpiry = tonumber(cascadingExpiry)
			if rawExpiry < cascadingExpiry then
				rawExpiry = cascadingExpiry
			end
		end
	elseif rawExpiry == '' then
		-- should never happen
		mw.log(string.format('Module:Effective_protection_level returned a blank expiry for action %s and pagename %s', action, pagename))
		return 'unknown'
	end
	local year = mw.ustring.sub( rawExpiry, 1, 4 )
	local month = mw.ustring.sub( rawExpiry, 5, 6 )
	local day = mw.ustring.sub( rawExpiry, 7, 8 )
	return year .. '-' .. month .. '-' .. day
end

setmetatable(p, { __index = function(t, k)
	return function(frame)
		return t._main(k, frame.args[1])
	end
end })

return p