Jump to content

Module:Sandbox/Mr. Stradivarius/sandbox4

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 16:20, 10 November 2013 (begin replacement for Template:Pp-meta; add protection detection). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module implements {{pp-meta}}, a meta-template for protection templates.

----------------------------------------------------------------------
-- title object
----------------------------------------------------------------------

-- Get the title object for the current page and add data about protection level to it.
-- We are only doing this for the current page, as this is all the {{PENDINGCHANGELEVEL}}
-- magic word supports, as of November 2013.

local title = mw.title.getCurrentTitle()

local function getProtectionLevel(protectionType)
	-- Gets the protection level of a title object. Current possible values on enwiki (as of October 2013) are "autoconfirmed", "templateeditor" and "sysop".
	local frame = mw.getCurrentFrame()
	local level
	if protectionType == 'pc' then
		level = frame:preprocess('{{PENDINGCHANGELEVEL}}')
	else
		level = frame:callParserFunction('PROTECTIONLEVEL', protectionType)
	end
	if level == '' then -- Make unprotected pages return "unprotected" rather than the blank string.
		level = 'unprotected'
	end
	return level
end

title.createProtection = getProtectionLevel('create')
title.editProtection = getProtectionLevel('edit')
title.moveProtection = getProtectionLevel('move')
title.pcProtection = getProtectionLevel('pc')

----------------------------------------------------------------------
-- icon class
----------------------------------------------------------------------

----------------------------------------------------------------------
-- mbox class
----------------------------------------------------------------------

----------------------------------------------------------------------
-- Process arguments and initialise objects
----------------------------------------------------------------------

local p = {}

function p._main(args)
	return title[args[1]]
end

function p.main(frame)
	-- If called via #invoke, use the args passed into the invoking template, or the args passed to #invoke if any exist.
	-- Otherwise assume args are being passed directly in from the debug console or from another Lua module.
	local origArgs
	if frame == mw.getCurrentFrame() then
		origArgs = frame:getParent().args
		for k, v in pairs(frame.args) do
			origArgs = frame.args
			break
		end
	else
		origArgs = frame
	end
	-- Trim whitespace and remove blank arguments.
	local args = {}
	for k, v in pairs(origArgs) do
		if type(v) == 'string' then
			v = mw.text.trim(v)
		end
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end

return p