Module:Protection banner: Difference between revisions
start converting this to an object-oriented approach - the banner seems naturally suited to object-based code
(make outline of the main function) |
(start converting this to an object-oriented approach - the banner seems naturally suited to object-based code) |
||
Line 1: | Line 1: | ||
-- This module implements {{pp-meta}} and its daughter templates such as | -- This module implements {{pp-meta}} and its daughter templates such as | ||
-- {{pp-dispute}}, {{pp-vandalism}} and {{pp-sock}}. | -- {{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: | Line 12: | ||
local cfg = {} | local cfg = {} | ||
cfg.defaultBanners = { | |||
edit = {}, | |||
move = {}, | |||
create = {}, | |||
autoreview = {} | |||
} | |||
cfg.banners = { | cfg.banners = { | ||
Line 183: | Line 195: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Helper functions | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function toTableEnd(t, pos) | local function toTableEnd(t, pos) | ||
-- Sends the value at position pos to the end of array t, and shifts the | -- Sends the value at position pos to the end of array t, and shifts the | ||
-- other items down accordingly. | -- other items down accordingly. | ||
return | return table.insert(t, table.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 | end | ||
-------------------------------------------------------------------------------- | |||
-- Main functions | |||
-------------------------------------------------------------------------------- | |||
local p = {} | local p = {} | ||
Line 212: | Line 337: | ||
function p._main(args) | function p._main(args) | ||
-- | -- Find what action we are supposed to be protecting the page from. | ||
-- | -- Add the banner/padlock, protection category, and tracking categories. | ||
local ret = '' | local ret = '' | ||
ret = ret .. p.renderBannerOrPadlock(protectionLevel, args) | ret = ret .. p.renderBannerOrPadlock(action, protectionLevel, args) | ||
ret = ret .. p.renderProtectionCategory(action, protectionLevel, args) | |||
ret = ret .. p.renderTrackingCategories(action, protectionLevel, args) | |||
ret = ret .. p.renderTrackingCategories(protectionLevel, args) | |||
return ret | return ret | ||
end | end | ||
Line 268: | Line 375: | ||
}) | }) | ||
return protectionData | return protectionData | ||
end | end | ||
function p.getPagetype(ns) | function p.getPagetype(ns) | ||
Line 439: | Line 546: | ||
else | else | ||
local quotient = i / 2 ^ (j - 1) | local quotient = i / 2 ^ (j - 1) | ||
quotient = ceil(quotient) | quotient = math.ceil(quotient) | ||
if quotient % 2 == 1 then | if quotient % 2 == 1 then | ||
key[t.keypos] = t.val | key[t.keypos] = t.val | ||
Line 447: | Line 554: | ||
end | end | ||
end | end | ||
key = | key = table.concat(key, '-') | ||
local attempt = cats[key] | local attempt = cats[key] | ||
if attempt then | if attempt then | ||
Line 457: | Line 564: | ||
.. ' please define the category for key "all-all-all-all-all"' | .. ' please define the category for key "all-all-all-all-all"' | ||
) | ) | ||
end | end | ||