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 03:46, 10 September 2024 (make templatestyles work). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}
local getArgs = require('Module:Arguments').getArgs
local cfg = mw.loadData("Module:Editnotice load/config")
local pseudoNs = mw.title.new(cfg.pseudo_ns_name)
local enTypes = cfg.editnotice_types
local enNames = cfg.editnotice_names
local protLevel = require("Module:Effective protection level")._main
local yn = require("Module:Yesno")

local function getNoticeContent(frame, title, args, noText)
	if noText then return '' end
	local t = mw.title.new(title)
	if t == nil or t.id == 0 or not t.exists then
		return ''
	end
	local result = frame:expandTemplate{ title = title, args = args }
	result = mw.text.trim(result)
	if result ~= '' and result ~= '-' then
		return result
	end
	return ''
end

local function makeLink(builder, links, target, text, contents)
	local builderLinksNodeSame = builder == links
	if (contents == '' or contents == nil) and not builderLinksNodeSame then
		builder = links
		builder = builder
			:tag('li')
	else
		builder = builder:tag('span')
	end
	builder = builder
		:addClass('editnotice-link')
		:css('clear', 'both')
		:css('margin', '0px 0.8em')
		:css('padding', 0)
		:css('line-height', '1em')
		:css('font-size', '90%')
	if not ((contents == '' or contents == nil) and not builderLinksNodeSame) then
		builder = builder:css('float', 'right')
	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.ustring.sub(currPage.fullText, mw.ustring.len(pseudoNs.fullText .. "/" .. editnoticeType .. "/") + 1) .. ""
end

function p.page(frame)
	local args = getArgs(frame)
	local currPage = args['title'] and mw.title.new(args['title']) or mw.title.getCurrentTitle()
	local editnoticeType = p.editnotice(frame)
	local titleName = mw.ustring.sub(currPage.fullText, mw.ustring.len(pseudoNs.fullText .. "/" .. enTypes[editnoticeType] .. "/") + 1) .. ""
	if editnoticeType == "page" or editnoticeType == "protection_id" then
		local page = mw.title.new(tonumber(titleName))
		return page and page.fullText or ""
	else
		return enTypes[editnoticeType] ~= nil and titleName .. "" or ''
	end
end

function key(title, var, name)
	return title ~= nil and title ~= 0 and (var[name] or name) or nil
end

function p.protectionEditnotice(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 mw-parser-output')
		:css('clear', 'both')
		:css('width', '100%')
		
	local protectionTitleNoticeName = pseudoNs.prefixedText .. "/" .. enTypes["protection"] .. "/" ..  currentTitle.rootPageTitle.prefixedText
	local protectionTitleNoticeContent = getNoticeContent(frame, protectionTitleNoticeName, noticeArgs)
	if protectionTitleNoticeContent ~= "" then
		makeLink(builder, builder, protectionTitleNoticeName, enNames["protection"], protectionTitleNoticeContent)
		displayEditnotice(builder, 'editnotice-protection-title', protectionTitleNoticeContent)
	end
	
	local protectionNoticeName = pseudoNs.prefixedText .. "/" .. enTypes["protection_id"] .. "/" .. currentTitle.id
	local protectionNoticeContent = getNoticeContent(frame, protectionNoticeName, noticeArgs)
	if protectionNoticeContent ~= "" then
		makeLink(builder, builder, protectionNoticeName, enNames["protection_id"], protectionNoticeContent)
		displayEditnotice(builder, 'editnotice-protection', protectionNoticeContent)
	end
	
	builder:tag('div')
		:css('clear', 'both')
	if protectionNoticeContent == "" and protectionTitleNoticeContent == "" then
		return ""
	end
	return builder
end

function p.editnotice(frame)
	local args = getArgs(frame)
	local currPage = args['title'] and mw.title.new(args['title']) or mw.title.getCurrentTitle()
	if currPage.rootText == pseudoNs.text and currPage.nsText == pseudoNs.nsText then
		local editNoticeType = getEditnoticeType(currPage)
		local title = mw.title.new(getEditnoticeMainPage(currPage, editNoticeType))
		if editNoticeType == enTypes["protection"] then
			return key(title, args, "protection") or args["#default"] or ''
		elseif editNoticeType == enTypes["protection_id"] then
			title = mw.title.new(tonumber(getEditnoticeMainPage(currPage, editNoticeType)))
			return key(title, args, "protection_id") or args["#default"] or ''
		elseif editNoticeType == enTypes["title"] then
			return key(title, args, "title") or args["#default"] or ''
		elseif editNoticeType == enTypes["page"] then
			title = mw.title.new(tonumber(getEditnoticeMainPage(currPage, editNoticeType)))
			return key(title, args, "page") or args["#default"] or ''
		elseif editNoticeType == enTypes["group"] then
			return key(title, args, "group") or args["#default"] or ''
		elseif editNoticeType == enTypes["category"] then
			return key(title, args, "category") or args["#default"] or ''
		elseif editNoticeType == enTypes["namespace"] then
			return key(title, args, "namespace") or args["#default"] or ''
		else
			return args["#default"] or ''
		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 noText = yn(args['notext']) or false
	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 mw-parser-output')
		:css('clear', 'both')
		:css('width', '100%')
	
	if noText then
		local editNoticePage = mw.title.new("MediaWiki:Editnotice-" .. tostring(currentTitle.namespace))
		if not editNoticePage.exists then
			builder
				:tag("span")
				:addClass("sysop-show")
				:tag("strong")
				:addClass("error")
				:wikitext(mw.ustring.format(cfg.noEditnoticePage, '[[' .. editNoticePage.fullText .. ']]'))
		end
	end
	
	local links = builder:tag("table")
		:addClass('wikitable editnotice-links mw-collapsible mw-collapsed')
		:addClass(cfg.editnotice_classes)
		:css("width", "100%")
		:tag("tr")
		:tag("th")
		:wikitext(cfg.links):done():done()
		:tag("td")
		:tag("ul")
		:attr("id", "editnotice-links")
		:css("display", "block")
		:addClass('hlist')

	local editnotices = ''

	if noticeAction ~= 'view' and noticeAction ~= 'protect' then
		local namespace = currentTitle.nsText
		if namespace == '' then namespace = 'Main' end
		local nsNoticeName = pseudoNs.prefixedText .. "/" .. enTypes["namespace"] .. "/" .. namespace
		local nsNoticeContent = getNoticeContent(frame, nsNoticeName, noticeArgs, noText)
		editnotices = editnotices .. nsNoticeContent
		makeLink(builder, links, nsNoticeName, enNames["namespace"], nsNoticeContent)
		displayEditnotice(builder, 'editnotice-ns', nsNoticeContent)
	end
	if protLevel("edit", currentTitle.fullText) or 
		protLevel("move", currentTitle.fullText) or 
		protLevel("create", currentTitle.fullText) or 
		protLevel("upload", currentTitle.fullText) then
		local protectionTitleNoticeName = pseudoNs.prefixedText .. "/" .. enTypes["protection"] .. "/" ..  currentTitle.rootPageTitle.prefixedText
		local protectionTitleNoticeContent = noticeAction == "protect" and getNoticeContent(frame, protectionTitleNoticeName, noticeArgs, noText) or ""
		editnotices = editnotices .. protectionTitleNoticeContent
		makeLink(builder, links, protectionTitleNoticeName, enNames["protection"], protectionTitleNoticeContent)
		displayEditnotice(builder, 'editnotice-protection-title', protectionTitleNoticeContent)
		
		if currentTitle.id ~= 0 then
			local protectionNoticeName = pseudoNs.prefixedText .. "/" .. enTypes["protection_id"] .. "/" .. currentTitle.id
			local protectionNoticeContent = noticeAction == "protect" and getNoticeContent(frame, protectionNoticeName, noticeArgs, noText) or ""
			editnotices = editnotices .. protectionNoticeContent
			makeLink(builder, links, protectionNoticeName, enNames["protection_id"], protectionNoticeContent)
			displayEditnotice(builder, 'editnotice-protection', protectionNoticeContent)
		end
	end

	if mw.site.namespaces[currentTitle.namespace].hasSubpages then
		local splitTitle = mw.text.split(currentTitle.fullText, "/")
		local groupTitle = ''
		for k,v in ipairs(splitTitle) do
			groupTitle = groupTitle .. v
			local groupNoticeName = pseudoNs.prefixedText .. "/" .. enTypes["group"] .. "/" ..  groupTitle
			local groupNoticeContent = getNoticeContent(frame, groupNoticeName, noticeArgs, noText)
			editnotices = editnotices .. groupNoticeContent
			makeLink(builder, links, groupNoticeName, enNames["group"] .. ' (' .. groupTitle .. ')', groupNoticeContent)
			displayEditnotice(builder, 'editnotice-group', groupNoticeContent)
			groupTitle = groupTitle .. '/'
		end
	end
	
	if cfg.user_editnotice ~= nil and ((currentTitle:hasSubjectNamespace(2) or currentTitle:hasSubjectNamespace(3)) and not currentTitle.isSubpage) then
		-- display user page notice
		local userPageNoticeName = currentTitle.prefixedText .. '/' .. cfg.user_editnotice
		local userPageNoticeContent = getNoticeContent(frame, userPageNoticeName, noticeArgs, noText)
		editnotices = editnotices .. userPageNoticeContent
		makeLink(builder, builder, userPageNoticeName, enNames["user"], userPageNoticeContent)
		displayEditnotice(builder, 'usernotice-page', userPageNoticeContent)
	end
	
	local titleNoticeName = pseudoNs.prefixedText .. "/" .. enTypes["title"] .. "/" ..  currentTitle.rootPageTitle.prefixedText
	local titleNoticeContent = getNoticeContent(frame, titleNoticeName, noticeArgs, noText)
	editnotices = editnotices .. titleNoticeContent
	makeLink(builder, links, titleNoticeName, enNames["title"], titleNoticeContent)
	displayEditnotice(builder, 'editnotice-title', titleNoticeContent)
	
	if currentTitle.id ~= 0 then
		local pageNoticeName = pseudoNs.prefixedText .. "/" .. enTypes["page"] .. "/" .. currentTitle.id
		local pageNoticeContent = getNoticeContent(frame, pageNoticeName, noticeArgs, noText)
		editnotices = editnotices .. pageNoticeContent
		makeLink(builder, links, pageNoticeName, enNames["page"], pageNoticeContent)
		displayEditnotice(builder, 'editnotice-page', pageNoticeContent)
	end
	
	local categories = mw.title.getCurrentTitle().categories
	mw.logObject(categories)
	for k,v in ipairs(categories) do
		local categoryNoticeName = pseudoNs.prefixedText .. "/" .. enTypes["category"] .. "/" .. v
		local categoryNoticeContent = getNoticeContent(frame, categoryNoticeName, noticeArgs, noText)
		editnotices = editnotices .. categoryNoticeContent
		makeLink(builder, links, categoryNoticeName, enNames["category"] .. ' (' .. v .. ')', categoryNoticeContent)
		displayEditnotice(builder, 'editnotice-category', categoryNoticeContent)
	end
	if editnotices == '' and not noText then return '' end
	builder:tag('div')
		:css('clear', 'both')
	return tostring( builder ) .. frame:preprocess('<templatestyles src="Template:Hlist/styles.css"/>')
end

return p