Module:Editnotice load
Appearance
![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
An enhanced editnotice loader.
Features:
- Category editnotices
- Editnotices based on page ID
- Group notices by prefixes
Relevant pages:
- Template:Editnotice/notice
- MediaWiki:Noarticletext-nopermission
- MediaWiki:Protectedpagetext
- MediaWiki:Cascadeprotected
- Template:Editnotices/Group/Template:Editnotices
- MediaWiki:Titleblacklist-custom-editnotice
Usage
{{#invoke:Editnotice load|function_name}}
--[[
@TODO Implement category notices
]]--
local p = {}
local getArgs = require('Module:Arguments').getArgs
local function getCategoriesFromWikitext(wikitext)
local categories = {}
for category in mw.ustring.gmatch(wikitext, "%[%[Category:([%w%s%-_]+)%]%]") do
table.insert(categories, category)
end
return categories
end
local function getNoticeContent(frame, title, args)
local success, result = pcall(frame.expandTemplate, frame, { title = title, args = args })
if success then
result = mw.text.trim(result)
if result ~= '' and result ~= '-' then
return result
end
return ''
end
end
local function makeLink(builder, target, text, exists)
if not exists and not mw.title.getCurrentTitle():hasSubjectNamespace(2) then
builder = builder:tag('span')
:addClass('editnotice-redlink sysop-show templateeditor-show extendedmover-show')
end
builder:wikitext(string.format('[[%s|%s]] ', target, text))
end
local function displayEditnotice(builder, class, content)
if content then
return builder:tag('div')
:addClass(class)
:css('clear', 'both')
:css('width', '100%')
:wikitext(content)
end
end
function getEditnoticeType(title)
if title.baseText == title.rootText then
return title.subpageText
else
return getEditnoticeType(title.basePageTitle)
end
end
function getEditnoticeMainPage(currPage, editNoticeType)
return mw.getCurrentFrame():preprocess("{{Replace|" .. currPage.text .. "|Editnotices/" .. editNoticeType .. "/" .. "|}}")
end
function p.editnotice(frame)
local args = getArgs(frame)
local currPage = args['title'] and mw.title.new(args['title']) or mw.title.getCurrentTitle()
local invalidEditnotice = '<strong class="error">This is an editnotice with an invalid name.</strong>'
if currPage.rootText == "Editnotices" and currPage:hasSubjectNamespace(10) then
local editNoticeType = getEditnoticeType(currPage)
if editNoticeType == "Editnotices" then
return "Subpages of this page consist of editnotices, which are generally only editable by template editors and administrators."
elseif editNoticeType == "Protection" then
return "This is the title protection notice for [[:" .. getEditnoticeMainPage(currPage, editNoticeType) .. "]]."
elseif editNoticeType == "ProtectionID" then
local title = pcall(function()
return mw.title.new(tonumber(getEditnoticeMainPage(currPage, editNoticeType))).text
end) or nil
return title ~= nil and "This is the protection notice for [[:" .. title .. "]]." or invalidEditnotice
elseif editNoticeType == "Page" then
return "This is the title notice for [[:" .. getEditnoticeMainPage(currPage, editNoticeType) .. "]]."
elseif editNoticeType == "PageID" then
local title = pcall(function()
return mw.title.new(tonumber(getEditnoticeMainPage(currPage, editNoticeType))).text
end) or nil
return title ~= nil and "This is the page notice for [[:" .. title .. "]]." or invalidEditnotice
elseif editNoticeType == "Group" then
return "This is the group notice for [[:" .. getEditnoticeMainPage(currPage, editNoticeType) .. "]]."
elseif editNoticeType == "GroupProtection" then
return "This is the group protection notice for [[:" .. getEditnoticeMainPage(currPage, editNoticeType) .. "]]."
elseif editNoticeType == "Category" then
return "This is the category notice for pages in the category [[:Category:" .. getEditnoticeMainPage(currPage, editNoticeType) .. "]]."
else
return invalidEditnotice
end
end
end
function p.main(frame)
local args = getArgs(frame, {wrappers = 'Template:Editnotice load'})
local noticeAction = args['notice action']
local noticeArgs = {['notice action'] = noticeAction}
local currentTitle = args['title'] and mw.title.new(args['title']) or mw.title.getCurrentTitle()
local builder = mw.html.create('div')
:attr('id', 'editnotice-area')
:addClass('editnotice-area')
:css('clear', 'both')
:css('width', '100%')
if noticeAction ~= 'view' then
local namespace = currentTitle.nsText
if namespace == '' then namespace = 'Main' end
displayEditnotice(builder, 'editnotice-namespace', getNoticeContent(frame, 'Template:Editnotices/Namespace/' .. namespace))
end
local linkContainer = builder:tag('div')
:addClass('editnotice-link')
:css('clear', 'both')
:css('float', 'right')
:css('margin', '0px 0.8em')
:css('padding', 0)
:css('line-height', '1em')
:tag('small')
if mw.site.namespaces[currentTitle.namespace].hasSubpages then
local groupNoticeName = 'Template:Editnotices/Group/' .. currentTitle.rootPageTitle.prefixedText
local groupNoticeContent = getNoticeContent(frame, groupNoticeName, noticeArgs)
makeLink(linkContainer, groupNoticeName, 'Group notice', groupNoticeContent)
displayEditnotice(builder, 'editnotice-group', groupNoticeContent)
end
if (currentTitle:hasSubjectNamespace(2)) and not currentTitle.isSubpage then
-- display user page notice
local userPageNoticeName = currentTitle.prefixedText .. '/Editnotice'
local userPageNoticeContent = getNoticeContent(frame, userPageNoticeName, noticeArgs)
makeLink(linkContainer, userPageNoticeName, 'User page notice', userPageNoticeContent)
displayEditnotice(builder, 'usernotice-page', userPageNoticeContent)
end
local titleNoticeName = 'Template:Editnotices/Page/' .. currentTitle.prefixedText
local titleNoticeContent = getNoticeContent(frame, titleNoticeName, noticeArgs)
makeLink(linkContainer, titleNoticeName, 'Title notice', titleNoticeContent)
displayEditnotice(builder, 'editnotice-title', titleNoticeContent)
local pageNoticeName = 'Template:Editnotices/PageID/' .. currentTitle.id
local pageNoticeContent = getNoticeContent(frame, pageNoticeName, noticeArgs)
makeLink(linkContainer, pageNoticeName, 'Page notice', pageNoticeContent)
displayEditnotice(builder, 'editnotice-page', pageNoticeContent)
local categories = getCategoriesFromWikitext(frame:preprocess(mw.title.getCurrentTitle():getContent()))
for k,v in ipairs(categories) do
local categoryNoticeName = 'Template:Editnotices/Category/' .. v
local categoryNoticeContent = getNoticeContent(frame, pageNoticeName, noticeArgs)
makeLink(linkContainer, categoryNoticeName, 'Category notice', categoryNoticeContent)
displayEditnotice(builder, 'editnotice-category', pageNoticeContent)
end
builder:tag('div')
:css('clear', 'both')
return builder
end
return p