Changes
start converting this to an object-oriented approach - the banner seems naturally suited to object-based code
-- 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
--------------------------------------------------------------------------------
local cfg = {}
cfg.defaultBanners = {
edit = {},
move = {},
create = {},
autoreview = {}
}
cfg.banners = {
--------------------------------------------------------------------------------
-- Main Helper functions
--------------------------------------------------------------------------------
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 objend 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 = {}
function p._main(args)
-- Get the protection level of the title Find what action we are working on. local protectionLevel do local title if args.supposed to be protecting the page then title = mwfrom.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
-- Generate Add the banner or /padlock output, protection category, and tracking categories.
local ret = ''
ret = ret .. p.renderBannerOrPadlock(action, protectionLevel, args) -- Add protection category if protectionLevel ~= '*' then ret = ret .. p.renderProtectionCategory(action, protectionLevel, args) end -- Add tracking categories ret = ret .. p.renderTrackingCategories(action, protectionLevel, args)
return ret
end
})
return protectionData
end
function p.getPagetype(ns)
else
local quotient = i / 2 ^ (j - 1)
quotient = math.ceil(quotient)
if quotient % 2 == 1 then
key[t.keypos] = t.val
end
end
key = tconcattable.concat(key, '-')
local attempt = cats[key]
if attempt then
.. ' please define the category for key "all-all-all-all-all"'
)
end