Module:Sandbox/Awesome Aasim/Editnotice Load

local p = {} local getArgs = require('Module:Arguments').getArgs

local function getCategoriesFromWikitext(wikitext) local categories = {} local inserted = {} for category in mw.ustring.gmatch(wikitext, "%[%[Category:([%w%s%-_]+)%]%]") do   	if inserted[category] == nil then inserted[category] = true table.insert(categories, category) end 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 ', 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("" .. currPage.text .. "") 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 = ' This is an editnotice with an invalid name. '	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 local nsNoticeName = 'Template:Editnotices/Namespace/' .. currentTitle.rootPageTitle.prefixedText local nsNoticeContent = getNoticeContent(frame, nsNoticeName, noticeArgs) makeLink(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'), nsNoticeName, 'Namespace notice', nsNoticeContent) displayEditnotice(builder, 'editnotice-ns', nsNoticeContent) end

if mw.site.namespaces[currentTitle.namespace].hasSubpages then local groupNoticeName = 'Template:Editnotices/Group/' .. currentTitle.rootPageTitle.prefixedText local groupNoticeContent = getNoticeContent(frame, groupNoticeName, noticeArgs) makeLink(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'), 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(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'), 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(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'), titleNoticeName, 'Title notice', titleNoticeContent) displayEditnotice(builder, 'editnotice-title', titleNoticeContent) local pageNoticeName = 'Template:Editnotices/PageID/' .. currentTitle.id	local pageNoticeContent = getNoticeContent(frame, pageNoticeName, noticeArgs) makeLink(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'), pageNoticeName, 'Page notice', pageNoticeContent) displayEditnotice(builder, 'editnotice-page', pageNoticeContent) local categories = getCategoriesFromWikitext(frame:preprocess(mw.title.getCurrentTitle:getContent)) mw.logObject(categories) for k,v in ipairs(categories) do local categoryNoticeName = 'Template:Editnotices/Category/' .. v		local categoryNoticeContent = getNoticeContent(frame, categoryNoticeName, noticeArgs) makeLink(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'), categoryNoticeName, 'Category notice (' .. v .. ')', categoryNoticeContent) displayEditnotice(builder, 'editnotice-category', categoryNoticeContent) end builder:tag('div') :css('clear', 'both') return builder end

return p