Module:Protection banner

Revision as of 15:40, 8 June 2014 by imported>Jackmcbarn (add no globals now, to catch any problems sooner rather than later)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Protection banner/doc

-- This module implements {{pp-meta}} and its daughter templates such as
-- {{pp-dispute}}, {{pp-vandalism}} and {{pp-sock}}.

-- Initialise necessary modules.
require('Module:No globals')
local class = require('Module:Middleclass').class
local mArguments = require('Module:Arguments')
local mFileLink = require('Module:File link')
local mProtectionLevel = require('Module:Effective protection level')
local yesno = require('Module:Yesno')

--------------------------------------------------------------------------------
-- ProtectionStatus class
--------------------------------------------------------------------------------

local ProtectionStatus = class('ProtectionStatus')

function ProtectionStatus:initialize(args, titleObj)
	-- Set action
	do
		local actions = {
			create = true,
			edit = true,
			move = true,
			autoreview = true
		}
		if args.action and actions[args.action] then
			self._action = args.action
		else
			self._action = 'edit'
		end
	end

	-- Set level
	do
		local level = mProtectionLevel._main(self._action, titleObj)
		if level == 'accountcreator' then
			-- Lump titleblacklisted pages in with template-protected pages,
			-- since templateeditors can do both.
			level = 'templateeditor'
		end
		self._level = level or '*'
	end

	-- Set reason
	self._reason = args.reason

	-- Set expiry
	self._expiry = args.expiry
end

function ProtectionStatus:getAction()
	return self._action
end

function ProtectionStatus:getLevel()
	return self._level
end

function ProtectionStatus:getReason()
	return self._reason
end

function ProtectionStatus:getExpiry()
	return self._expiry
end

--------------------------------------------------------------------------------
-- Config class
--------------------------------------------------------------------------------

local Config = class('Config')

function Config:initialize()
	self._cfg = mw.loadData('Module:Protection banner/config')
end

function Config:getBannerConfig(protectionStatusObj)
	local cfg = self._cfg
	local action = protectionStatusObj:getAction()
	local reason = protectionStatusObj:getReason()
	if cfg.banners[action][reason] then
		return cfg.banners[action][reason]
	else
		return cfg.defaultBanners[action]
	end
end

function Config:getConfigTable(key)
	local whitelisted = {
		images = true,
		categories = true,
		categoryNamespaces = true,
		pagetypeNamespaces = true,
		errorCategories = true
	}
	if whitelisted[key] then
		return self._cfg[key]
	else
		return nil
	end
end

--------------------------------------------------------------------------------
-- Image class
--------------------------------------------------------------------------------

local Image = class('Image')

--------------------------------------------------------------------------------
-- Blurb class
--------------------------------------------------------------------------------

local Blurb = class('Blurb')

function Blurb:initialize(bannerConfig)
	self._config = bannerConfig
end

--------------------------------------------------------------------------------
-- BannerTemplate class
--------------------------------------------------------------------------------

local BannerTemplate = class('BannerTemplate')

function BannerTemplate:initialize()
end

function BannerTemplate:render()
end

--------------------------------------------------------------------------------
-- Banner class
--------------------------------------------------------------------------------

local Banner = BannerTemplate:subclass('Banner')

--------------------------------------------------------------------------------
-- Padlock class
--------------------------------------------------------------------------------

local Padlock = BannerTemplate:subclass('Padlock')

--------------------------------------------------------------------------------
-- Category class
--------------------------------------------------------------------------------

local Category = class('Category')

function Category:initialize()
end

function Category:export()
	if self._categoryName then
		return string.format(
			'[[%s:%s]]',
			mw.site.namespaces[14].name,
			self._categoryName
		)
	else
		return ''
	end
end

--------------------------------------------------------------------------------
-- ProtectionCategory class
--------------------------------------------------------------------------------

local ProtectionCategory = Category:subclass('ProtectionCategory')

--------------------------------------------------------------------------------
-- ErrorCategory class
--------------------------------------------------------------------------------

local ErrorCategory = Category:subclass('ErrorCategory')

--------------------------------------------------------------------------------
-- ExpiryCategory class
--------------------------------------------------------------------------------

local ExpiryCategory = Category:subclass('ExpiryCategory')

--------------------------------------------------------------------------------
-- ProtectionBanner class
--------------------------------------------------------------------------------

local ProtectionBanner = {}

function ProtectionBanner._newBannerTemplate(args)
	-- Makes a new banner template object. This will be a new instance of
	-- the most suitable subclass of BannerTemplate, e.g. Banner or Padlock.
	if yesno(args.small) then
		return Padlock:new(args)
	else
		return Banner:new(args)
	end
end

function ProtectionBanner.exportToWiki(frame, titleObj)
	local args = mArguments.getArgs(frame)
	return ProtectionBanner.exportToLua(args, titleObj)
end

function ProtectionBanner.exportToLua(args, title)
	title = title or mw.title.getCurrentTitle()
end

return ProtectionBanner