Difference between revisions of "Module:User:Mr. Stradivarius/sandbox4"
Jump to navigation
Jump to search
blackwiki>Mr. Stradivarius (better comment for getProtectionLevel) |
blackwiki>Mr. Stradivarius (try putting all the title code in a title.getCurrentTitle method) |
||
| Line 9: | Line 9: | ||
-- magic word supports, as of November 2013. | -- magic word supports, as of November 2013. | ||
| − | local title = | + | local title = {} |
| − | local function getProtectionLevel(protectionType) | + | function title.getCurrentTitle() |
| − | + | local obj = mw.title.getCurrentTitle() | |
| − | + | ||
| − | + | local function getProtectionLevel(protectionType) | |
| − | + | -- Gets the protection level or pending change level of the current title. | |
| − | + | local frame = mw.getCurrentFrame() | |
| − | + | local level | |
| − | + | if protectionType == 'pc' then | |
| + | level = frame:preprocess('{{PENDINGCHANGELEVEL}}') | ||
| + | else | ||
| + | level = frame:callParserFunction('PROTECTIONLEVEL', protectionType) | ||
| + | end | ||
| + | if level == '' then -- Make unprotected pages return "unprotected" rather than the blank string. | ||
| + | level = 'unprotected' | ||
| + | end | ||
| + | return level | ||
end | end | ||
| − | + | ||
| − | + | obj.createProtection = getProtectionLevel('create') | |
| − | + | obj.editProtection = getProtectionLevel('edit') | |
| − | return | + | obj.moveProtection = getProtectionLevel('move') |
| + | obj.pcProtection = getProtectionLevel('pc') | ||
| + | return obj | ||
end | end | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ||
| Line 46: | Line 51: | ||
function p._main(args) | function p._main(args) | ||
| − | return | + | local currentTitle = title.getCurrentTitle() |
| + | return currentTitle[args[1]] | ||
end | end | ||
Revision as of 01:09, 11 November 2013
| This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
Usage
Template:Missing documentation
{{#invoke:User:Mr. Stradivarius|function_name}}
-- This module implements {{pp-meta}}, a meta-template for protection templates.
----------------------------------------------------------------------
-- title object
----------------------------------------------------------------------
-- Get the title object for the current page and add data about protection level to it.
-- We are only doing this for the current page, as this is all the {{PENDINGCHANGELEVEL}}
-- magic word supports, as of November 2013.
local title = {}
function title.getCurrentTitle()
local obj = mw.title.getCurrentTitle()
local function getProtectionLevel(protectionType)
-- Gets the protection level or pending change level of the current title.
local frame = mw.getCurrentFrame()
local level
if protectionType == 'pc' then
level = frame:preprocess('{{PENDINGCHANGELEVEL}}')
else
level = frame:callParserFunction('PROTECTIONLEVEL', protectionType)
end
if level == '' then -- Make unprotected pages return "unprotected" rather than the blank string.
level = 'unprotected'
end
return level
end
obj.createProtection = getProtectionLevel('create')
obj.editProtection = getProtectionLevel('edit')
obj.moveProtection = getProtectionLevel('move')
obj.pcProtection = getProtectionLevel('pc')
return obj
end
----------------------------------------------------------------------
-- icon class
----------------------------------------------------------------------
----------------------------------------------------------------------
-- mbox class
----------------------------------------------------------------------
----------------------------------------------------------------------
-- Process arguments and initialise objects
----------------------------------------------------------------------
local p = {}
function p._main(args)
local currentTitle = title.getCurrentTitle()
return currentTitle[args[1]]
end
function p.main(frame)
-- If called via #invoke, use the args passed into the invoking template, or the args passed to #invoke if any exist.
-- Otherwise assume args are being passed directly in from the debug console or from another Lua module.
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs(frame.args) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Trim whitespace and remove blank arguments.
local args = {}
for k, v in pairs(origArgs) do
if type(v) == 'string' then
v = mw.text.trim(v)
end
if v ~= '' then
args[k] = v
end
end
return p._main(args)
end
return p