Module:Protection banner: Difference between revisions
From Kaboom Wiki
				>Mr. Stradivarius  give up on the OOP approach, lay out the main module structure in more detail, and move the config to Module:Protection banner/config  | 
				>Mr. Stradivarius  Yet Another Rewrite - this time using Module:Middleclass  | 
				||
| Line 3: | Line 3: | ||
-- Initialise necessary modules.  | -- Initialise necessary modules.  | ||
local class = require('Module:Middleclass').class  | |||
local mArguments = require('Module:Arguments')  | local mArguments = require('Module:Arguments')  | ||
local mFileLink = require('Module:File link')  | local mFileLink = require('Module:File link')  | ||
local mProtectionLevel = require('Module:Effective protection level')  | |||
local yesno = require('Module:Yesno')  | local yesno = require('Module:Yesno')  | ||
--   | --------------------------------------------------------------------------------  | ||
-- ProtectionStatus class  | |||
--------------------------------------------------------------------------------  | |||
local   | 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  | 	end  | ||
	-- Set reason  | |||
	self._reason = args.reason  | |||
	-- Set expiry  | |||
	self._expiry = args.expiry  | |||
end  | end  | ||
function ProtectionStatus:getAction()  | |||
	return self._action  | |||
end  | |||
	return   | |||
function ProtectionStatus:getLevel()  | |||
	return self._level  | |||
end  | |||
function ProtectionStatus:getReason()  | |||
	return self._reason  | |||
end  | |||
function ProtectionStatus:getExpiry()  | |||
	return self._expiry  | |||
end  | end  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
--   | -- Config class  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
local Config = class('Config')  | |||
function Config:initialize()  | |||
	self._cfg = mw.loadData('Module:Protection banner/config')  | |||
end  | end  | ||
function   | function Config:getBannerConfig(protectionStatusObj)  | ||
	local   | 	local cfg = self._cfg  | ||
	local   | 	local action = protectionStatusObj:getAction()  | ||
	local   | 	local reason = protectionStatusObj:getReason()  | ||
	if cfg.banners[action][reason] then  | |||
		return cfg.banners[action][reason]  | |||
	else  | |||
		return cfg.defaultBanners[action]  | |||
	end  | |||
end  | |||
	local   | function Config:getConfigTable(key)  | ||
	if   | 	local whitelisted = {  | ||
		images = true,  | |||
		categories = true,  | |||
		categoryNamespaces = true,  | |||
		pagetypeNamespaces = true,  | |||
		errorCategories = true  | |||
	}  | |||
	if whitelisted[key] then  | |||
		return self._cfg[key]  | |||
	else  | 	else  | ||
		return nil  | |||
	end  | 	end  | ||
end  | end  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
--   | -- Image class  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
local Image = class('Image')  | |||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
--   | -- Blurb class  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
function   | local Blurb = class('Blurb')  | ||
function Blurb:initialize(bannerConfig)  | |||
	self._config = bannerConfig  | |||
end  | end  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
--   | -- BannerTemplate class  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
local BannerTemplate = class('BannerTemplate')  | |||
function   | function BannerTemplate:initialize()  | ||
end  | end  | ||
function   | function BannerTemplate:render()  | ||
end  | end  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
-- Banner   | -- Banner class  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
local Banner = BannerTemplate:subclass('Banner')  | |||
--------------------------------------------------------------------------------  | |||
-- Padlock class  | |||
--------------------------------------------------------------------------------  | |||
local Padlock = BannerTemplate:subclass('Padlock')  | |||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
--   | -- Category class  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
function   | local Category = class('Category')  | ||
function Category:initialize()  | |||
end  | end  | ||
function   | function Category:export()  | ||
	if self._categoryName then  | |||
		return string.format(  | |||
			'[[%s:%s]]',  | |||
			mw.site.namespaces[14].name,  | |||
			self._categoryName  | |||
		)  | |||
	else  | |||
		return ''  | |||
	end  | 	end  | ||
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  | ||
--------------------------------------------------------------------------------  | --------------------------------------------------------------------------------  | ||
function   | 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  | 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   | return ProtectionBanner  | ||
Revision as of 15:26, 8 June 2014
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.
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