Difference between revisions of "Module:Protection banner"

From Timelines
Jump to: navigation, search
(export classes for testing purposes and a few other tweaks)
(expand the Image class)
Line 47: Line 47:
  
 
-- Set expiry
 
-- Set expiry
self._expiry = args.expiry
+
self._expiry = args.expiry or 'indef'
 
end
 
end
  
Line 104: Line 104:
  
 
local Image = class('Image')
 
local Image = class('Image')
 +
 +
function Image:setFilename(filename, configObj, protectionStatusObj, namespace)
 +
if filename then
 +
self._filename = filename
 +
else
 +
local images = configObj:getConfigTable('images')
 +
local action = protectionStatusObj:getAction()
 +
local level = protectionStatusObj:getLevel()
 +
local reason = protectionStatusObj:getReason()
 +
local image
 +
if reason == 'office' or reason == 'reset' then
 +
image = images.office
 +
elseif namespace == 10 or namespace == 828 then
 +
-- We are in the template or module namespaces.
 +
if level == 'templateeditor' then
 +
image = images.template
 +
elseif level == 'sysop' then
 +
image = images.indef
 +
end
 +
elseif action == 'create' then
 +
image = images.create
 +
elseif action == 'move'
 +
and (
 +
level == 'templateeditor'
 +
or level == 'sysop'
 +
)
 +
then
 +
image = images.move
 +
elseif action == 'edit' then
 +
if level == 'sysop' then
 +
image = images.full
 +
elseif level == 'autoconfirmed' then
 +
image = images.semi
 +
end
 +
elseif action == 'autoreview' then
 +
if level == 'autoconfirmed' then
 +
image = images.pc1
 +
elseif level == 'reviewer' then
 +
image = images.pc2
 +
end
 +
else
 +
image = 'Transparent.gif'
 +
end
 +
self._filename = image
 +
end
 +
end
 +
 +
function Image:export()
 +
return mFileLink.new(self._filename)
 +
:width(self._size)
 +
:alt(self._alt)
 +
:caption(self._caption)
 +
:render()
 +
end
  
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------

Revision as of 16:47, 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.
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 or 'indef'
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 blacklist = {
		banners = true,
		defaultBanners = true
	}
	if not blacklist[key] then
		return self._cfg[key]
	else
		return nil
	end
end

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

local Image = class('Image')

function Image:setFilename(filename, configObj, protectionStatusObj, namespace)
	if filename then
		self._filename = filename
	else
		local images = configObj:getConfigTable('images')
		local action = protectionStatusObj:getAction()
		local level = protectionStatusObj:getLevel()
		local reason = protectionStatusObj:getReason()
		local image
		if reason == 'office' or reason == 'reset' then
			image = images.office
		elseif namespace == 10 or namespace == 828 then
			-- We are in the template or module namespaces.
			if level == 'templateeditor' then
				image = images.template
			elseif level == 'sysop' then
				image = images.indef
			end
		elseif action == 'create' then
			image = images.create
		elseif action == 'move'
			and (
				level == 'templateeditor'
				or level == 'sysop'
			)
		then
			image = images.move
		elseif action == 'edit' then
			if level == 'sysop' then
				image = images.full
			elseif level == 'autoconfirmed' then
				image = images.semi
			end
		elseif action == 'autoreview' then
			if level == 'autoconfirmed' then
				image = images.pc1
			elseif level == 'reviewer' then
				image = images.pc2
			end
		else
			image = 'Transparent.gif'
		end
		self._filename = image
	end
end

function Image:export()
	return mFileLink.new(self._filename)
		:width(self._size)
		:alt(self._alt)
		:caption(self._caption)
		:render()
end

--------------------------------------------------------------------------------
-- 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.exportToWiki(frame, title)
	local args = mArguments.getArgs(frame)
	return ProtectionBanner.exportToLua(args, title)
end

function ProtectionBanner.exportToLua(args, title)
	title = title or mw.title.getCurrentTitle()
	local pstatus = ProtectionStatus.new(args, title)
	local cfg = Config:new()

	-- Get the banner template object
	local banner
	do
		local bannerClass
		if yesno(args.small) then
			bannerClass = Padlock
		else
			bannerClass = Banner
		end
		banner = bannerClass:new()
	end
end

function ProtectionBanner._exportClasses()
	return {
		ProtectionStatus = ProtectionStatus,
		Config = Config,
		Image = Image,
		Blurb = Blurb,
		BannerTemplate = BannerTemplate,
		Banner = Banner,
		Padlock = Padlock,
		Category = Category,
		ProtectionCategory = ProtectionCategory,
		ErrorCategory = ErrorCategory,
		ExpiryCategory = ExpiryCategory
	}
end

return ProtectionBanner