Module:Protection banner: Difference between revisions

start converting this to an object-oriented approach - the banner seems naturally suited to object-based code
Enwikipedia>Mr. Stradivarius
(make outline of the main function)
Enwikipedia>Mr. Stradivarius
(start converting this to an object-oriented approach - the banner seems naturally suited to object-based code)
Line 1:
-- This module implements {{pp-meta}} and its daughter templates such as
-- {{pp-dispute}}, {{pp-vandalism}} and {{pp-sock}}.
 
-- Initialise necessary modules.
local mArguments = require('Module:Arguments')
local mProtectionLevel = require('Module:Effective protection level')._main
local mMessageBox -- only needs to be loaded if we are outputting a banner, so lazily initialise
 
--------------------------------------------------------------------------------
Line 7 ⟶ 12:
 
local cfg = {}
 
cfg.defaultBanners = {
edit = {},
move = {},
create = {},
autoreview = {}
}
 
cfg.banners = {
Line 183 ⟶ 195:
 
--------------------------------------------------------------------------------
-- MainHelper functions
--------------------------------------------------------------------------------
 
-- Initialise necessary modules.
local mArguments = require('Module:Arguments')
local mProtectionLevel = require('Module:Effective protection level')._main
local mMessageBox -- only needs to be loaded if we are outputting a banner, so lazily initialise
 
-- Define often-used functions as local variables.
local tconcat = table.concat
local tinsert = table.insert
local tremove = table.remove
local ceil = math.ceil
local format = string.format
 
local function toTableEnd(t, pos)
-- Sends the value at position pos to the end of array t, and shifts the
-- other items down accordingly.
return tinserttable.insert(t, tremovetable.remove(t, pos))
end
 
--------------------------------------------------------------------------------
-- Banner object
--------------------------------------------------------------------------------
 
local banner = {}
 
function banner.new(args)
local obj = {}
setmetatable(obj, {
__index = banner
})
-- Set the protection action.
-- This is the action we are supposed to be protecting the page against,
-- and does not necessarily correspond to the actual protection status.
obj.action = args.action or 'edit'
-- Get the title object of the page we are working on.
if args.page then
obj.title = mw.title.new(args.page)
else
obj.title = mw.title.getCurrentTitle()
end
-- Get the protection level of the title object for the given protection
-- action. This is always a string, even for invalid actions.
do
local protectionData = p.getProtectionData(obj.title)
local protectionLevel = protectionData[obj.action]
obj.protectionLevel = protectionLevel or '*'
end
-- Fetch the banner data.
obj.reason = args.reason
if obj.reason
and cfg.banners[obj.action]
and cfg.banners[obj.action][obj.reason]
then
obj.data = cfg.banners[obj.action][obj.reason]
elseif cfg.defaultBanners and cfg.defaultBanners[obj.action] then
obj.data = cfg.defaultBanners[obj.action]
elseif cfg.defaultBanners and cfg.defaultBanners.edit then
obj.data = cfg.defaultBanners.edit
else
error('no banner data found; please define data for cfg.defaultBanners.edit')
end
return obj
end
 
function banner:renderImageLink(image, size, link, text, alt)
--[[
-- Renders the image link wikitext All parameters are optional
-- apart from the display text.
--
-- @parameters:
-- image - the image name
-- size - the image size, as a number
-- link - page linked to by the image
-- text - the tooltip text
-- alt - the alt text
--
-- All parameters are optional apart from the text parameter.
--]]
image = image or 'Transparent.gif'
size = size or 20
if link then
link = '|link=' .. link
else
link = ''
end
text = text or error('No text parameter supplied to p.renderImageLink')
if alt then
alt = '|alt=' .. alt
else
alt = ''
end
return string.format('[[Image:%s|%dpx%s|%s%s]]', image, size, link, text, alt)
end
 
function banner:renderPadlock(image, right)
--[[
-- Renders the html of the padlock seen in the top-right-hand corner
-- of protected pages.
--
-- @parameters:
-- image - the image wikitext (required)
-- right - the "right" css property value, as a string (optional)
--]]
image = image or error('No image parameter specified in p.renderPadlock')
local root = mw.html.create('div')
root
:addClass('metadata topicon nopopups')
:attr('id', 'protected-icon')
:css{display = 'none', right = right or '55px'}
:wikitext(image)
return tostring(root)
end
 
function banner:renderBanner(page, image, text)
--[[
-- Renders the large protection banner placed at the top of articles,
-- using the data provided in the data table.
--
-- @parameters:
-- page - demo page parameter to pass to {{mbox}}
-- image - the image wikitext
-- text - the text to display
--
-- All parameters are optional.
--]]
mMessageBox = require('Module:Message box')
local mbargs = { -- arguments for the message box module
page = page,
type = 'protection',
image = image,
text = text
}
return mMessageBox.main('mbox', mbargs)
end
 
--------------------------------------------------------------------------------
-- Main functions
--------------------------------------------------------------------------------
 
local p = {}
Line 212 ⟶ 337:
 
function p._main(args)
-- Find what action we are supposed to be protecting the page from.
-- Get the protection level of the title we are working on.
 
local protectionLevel
do
local title
if args.page then
title = mw.title.new(args.page)
else
title = mw.title.getCurrentTitle()
end
local protectionData = p.getProtectionData(title)
protectionLevel = protectionData[args.action or 'edit']
protectionLevel = protectionLevel or '*'
end
-- GenerateAdd the banner/padlock, orprotection padlockcategory, and tracking outputcategories.
local ret = ''
ret = ret .. p.renderBannerOrPadlock(action, protectionLevel, args)
ret = ret .. p.renderProtectionCategory(action, protectionLevel, args)
ret = ret .. p.renderTrackingCategories(action, protectionLevel, args)
-- Add protection category
if protectionLevel ~= '*' then
ret = ret .. p.renderProtectionCategory(protectionLevel, args)
end
-- Add tracking categories
ret = ret .. p.renderTrackingCategories(protectionLevel, args)
return ret
end
Line 268 ⟶ 375:
})
return protectionData
end
 
function p.getPagetype(ns)
Line 439 ⟶ 546:
else
local quotient = i / 2 ^ (j - 1)
quotient = math.ceil(quotient)
if quotient % 2 == 1 then
key[t.keypos] = t.val
Line 447 ⟶ 554:
end
end
key = tconcattable.concat(key, '-')
local attempt = cats[key]
if attempt then
Line 457 ⟶ 564:
.. ' please define the category for key "all-all-all-all-all"'
)
end
 
function p.renderImageLink(image, size, link, text, alt)
--[[
-- Renders the image link wikitext All parameters are optional
-- apart from the display text.
--
-- @parameters:
-- image - the image name
-- size - the image size, as a number
-- link - page linked to by the image
-- text - the tooltip text
-- alt - the alt text
--
-- All parameters are optional apart from the text parameter.
--]]
image = image or 'Transparent.gif'
size = size or 20
if link then
link = '|link=' .. link
else
link = ''
end
text = text or error('No text parameter supplied to p.renderImageLink')
if alt then
alt = '|alt=' .. alt
else
alt = ''
end
return string.format('[[Image:%s|%dpx%s|%s%s]]', image, size, link, text, alt)
end
 
function p.renderPadlock(image, right)
--[[
-- Renders the html of the padlock seen in the top-right-hand corner
-- of protected pages.
--
-- @parameters:
-- image - the image wikitext (required)
-- right - the "right" css property value, as a string (optional)
--]]
image = image or error('No image parameter specified in p.renderPadlock')
local root = mw.html.create('div')
root
:addClass('metadata topicon nopopups')
:attr('id', 'protected-icon')
:css{display = 'none', right = right or '55px'}
:wikitext(image)
return tostring(root)
end
 
function p.renderBanner(page, image, text)
--[[
-- Renders the large protection banner placed at the top of articles,
-- using the data provided in the data table.
--
-- @parameters:
-- page - demo page parameter to pass to {{mbox}}
-- image - the image wikitext
-- text - the text to display
--
-- All parameters are optional.
--]]
mMessageBox = require('Module:Message box')
local mbargs = { -- arguments for the message box module
page = page,
type = 'protection',
image = image,
text = text
}
return mMessageBox.main('mbox', mbargs)
end