Jump to content

Module:Editnotice load

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Awesome Aasim (talk | contribs) at 15:56, 6 May 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[
@TODO Implement category notices

]]--

local p = {}
local getArgs

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
	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 p.editnotice(frame)
	local currPage = mw.title.getCurrentTitle()
	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 protection notice for [[:" .. frame:preprocess("{{Replace|" .. currPage.text .. "|Editnotices/Protection/" .. "|}}") .. "]]."
		elseif editNoticeType == "Page" then
			return "This is the title notice for [[:" .. frame:preprocess("{{Replace|" .. currPage.text .. "|Editnotices/Page/" .. "|}}") .. "]]."
		elseif editNoticeType == "PageID" then
			return "This is the page notice for [[:" .. mw.title.new(tonumber(currPage.rootText)) .. "]]."
		elseif editNoticeType == "Group" then
			return "This is the group notice for [[:" .. frame:preprocess("{{Replace|" .. currPage.text .. "|Editnotices/Group/" .. "|}}") ..  "]]."
		else
			return '<strong class="error">This is an editnotice with an invalid name.</strong>'
		end
	end
end

function p.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame, {wrappers = 'Template:Editnotice load'})

	local noticeAction = args['notice action']
	local noticeArgs = {['notice action'] = noticeAction}
	local currentTitle = 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)
	
	builder:tag('div')
		:css('clear', 'both')
	return builder
end

return p