Module:Portal maintenance status

From blackwiki
Revision as of 04:02, 11 June 2018 by test>Evad37 (historical function, to find {{Historical}} on a project page.)
Jump to navigation Jump to search

This module searches a Portal_talk: page's portal page, and returns an appropriate message based if {{Portal maintenance status}} is found in the page's wikitext, and which parameters are used. An error is reported if this module is used outside of the Portal_talk namespace. It can be demonstrated, in any namespace, with the |demo= and |demo2= parameters, which take the name of templates to "find" (instead of actually searching a page).

Usage

{{#invoke:Portal maintenance status|main}}
Looks for {{Portal maintenance status}} on a Portal_talk: page's related portal page.
Returns an appropriate message string if found or an empty string if not found, or an error if used in the wrong namespace.
{{#invoke:Portal maintenance status|historical}}
Looks for {{Historical}} on a Wikipedia_talk: page's related project page.
Returns yes if found or an empty string if not found, or an error if used in the wrong namespace.
{{#invoke:Portal maintenance status|featured}}
Looks for {{Featured portal}} on a Wikipedia_talk: page's related project page.
Returns yes if found or an empty string if not found, or an error if used in the wrong namespace.

Examples

If the portal contains {{Portal maintenance status|date=June 2026|manual=yes}}

This portal is manually maintained.
This portal's subpages should be checked. Superfluous subpages should be nominated for deletion.
Please take care when editing, especially if using automated editing software, and seek consensus before making major changes.


If the portal contains {{Portal maintenance status|date=June 2026|nonstandard=yes}}

This portal has a non-standard layout.
This portal's subpages should be checked. Superfluous subpages should be nominated for deletion.
Please take care when editing, especially if using automated editing software.


If the portal contains {{Portal maintenance status|date=June 2026|incomplete=yes}}

This portal's subpages should be checked. Superfluous subpages should be nominated for deletion.
Please take care when editing, especially if using automated editing software.


If the portal contains {{Portal maintenance status|date=June 2026|subpages=single}}

This portal has a single page layout. Any subpages are likely superfluous.
Please take care when editing, especially if using automated editing software.


If the portal contains {{Portal maintenance status|date=June 2026|subpages=checked}}

This portal's subpages have been checked by an editor, and found not to be superfluous.
Please take care when editing, especially if using automated editing software.


If the portal contains {{Portal maintenance status|date=June 2026|note=This is an example.}}

This portal's subpages should be checked. Superfluous subpages should be nominated for deletion.
Please take care when editing, especially if using automated editing software.


If the portal contains {{Portal maintenance status|date=June 2026|manual=yes|nonstandard=yes|subpages=single|incomplete=yes|note=Lorem ipsum dolor sit amet.}}

This portal is manually maintained and has a non-standard layout.
This portal has a single page layout. Any subpages are likely superfluous.
Please take care when editing, especially if using automated editing software, and seek consensus before making major changes.


If the portal does not contain the template, there should be no output



If used in the wrong namespace, an error should be reported

Lua error: Wrong namespace.


See also

  • Script error: No such module "Section link".



local p = {}

function cleanupArgs(argsTable)
	local cleanArgs = {}
	for key, val in pairs(argsTable) do
		if type(val) == 'string' then
			val = val:match('^%s*(.-)%s*$')
			if val ~= '' then
				cleanArgs[key] = val
			end
		else
			cleanArgs[key] = val
		end
	end
	return cleanArgs
end

local content = {}

function makeTemplatePattern(template)
	local first = string.sub(template, 1, 1)
	local rest = string.sub(template, 2)
	local pattern = mw.ustring.format('%s[%s%s]%s%s', '%{%{%s*', mw.ustring.upper(first), mw.ustring.lower(first), rest, '%s*|?[^%}]*%}%}')
	return pattern
end

function makeParameterPattern(parameter)
	return mw.ustring.format('%s%s%s%s', '|%s*', parameter, '%s*=%s*', '([^|%}]*)', '%s*[|%}]')
end

function contentContains(content, template, subpattern, leadOnly)
	if leadOnly then
		content = mw.ustring.gsub(content, "%c%s*==.*","") -- remove first ==Heading== and everything after it
	end
	local templateWikitext = string.match(content, makeTemplatePattern(template))
	if not templateWikitext then
		return false
	end
	if subpattern then
		return string.match(templateWikitext, subpattern) or false
	else
		return templateWikitext or false
	end
end

function getSubjectPageContent(contentNamespaceNumber)
	local namespace = mw.site.namespaces[contentNamespaceNumber] ["name"]
	local talkTitle = mw.title.getCurrentTitle()
	if talkTitle.namespace ~= contentNamespaceNumber + 1 then
		return error('Wrong namespace', 0)
	end
	local subjectTitle = mw.title.new(namespace .. ":" .. talkTitle.text)
	return subjectTitle:getContent()
end

-- historical function
--   Looks for {{Historical}} on a Wikipedia_talk: page's related project page.
--   Returns 'yes' if found or '' (empty string) if not found, or an error if used in the wrong namespace.
p.historical = function(frame)
	local parent = frame.getParent(frame)
	local args = cleanupArgs(frame.args)
	local demo = args.demo and true or false
	local content
	if demo then
		local demoText = mw.ustring.gsub(args.demo, '%{%{%!%}%}', '|')
		content = '{{' .. demoText .. '}}'
		if args.demo2 then
			local demo2Text = mw.ustring.gsub(args.demo2, '%{%{%!%}%}', '|')
			content= portalContent  .. '{{' .. demo2Text .. '}}'
		end
	else
		content = getSubjectPageContent(4)
	end

	content = mw.ustring.gsub(content, "<!%-%-.-%-%->","") -- remove HTML comments
	content = mw.ustring.gsub(content, "%c%s*==.*","") -- remove first ==Heading== and everything after it
	content = mw.ustring.gsub(content, "<noinclude>.-</noinclude>", "") -- remove noinclude bits

	local isHistorical = mw.ustring.match(content, makeTemplatePattern('Historical')) and true or false
	return isHistorical and 'yes' or ''
end

-- main function
--   Looks for {{Portal maintenance status}} (or earlier deprecated templates) on a Portal_talk: page's related portal page.
--   Returns an appropriate message string if found or '' (empty string) if not found, or an error if used in the wrong namespace.
p.main = function(frame)
	local parent = frame.getParent(frame)
	local args = cleanupArgs(frame.args)
	local demo = args.demo and true or false
	local portalContent
	if demo then
		local demoText = mw.ustring.gsub(args.demo, '%{%{%!%}%}', '|')
		portalContent = '{{' .. demoText .. '}}'
		if args.demo2 then
			local demo2Text = mw.ustring.gsub(args.demo2, '%{%{%!%}%}', '|')
			portalContent = portalContent  .. '{{' .. demo2Text .. '}}'
		end
	else
		portalContent = getSubjectPageContent(100)
	end

	local status = mw.ustring.match(portalContent, makeTemplatePattern('Portal maintenance status')) or mw.ustring.match(portalContent, makeTemplatePattern('Portal flag'))
	if not status then
		-- Legacy code -- remove once there are no more uses of {{Maintained portal flag}} and {{Non-standard portal flag}} --
		local isMaintained = mw.ustring.find(portalContent, '%{%{%s*[Mm]aintained portal flag%s*%}%}') and true or false
		local isNonstandard = mw.ustring.find(portalContent, '%{%{%s*[Nn]on%-standard portal flag%s*%}%}') and true or false

		if (not isMaintained) and (not isNonstandard) then
			return ''
		end

		local text = "This portal " .. ( isMaintained and "is '''manually [[Wikipedia:WikiProject Portals#Specific portal maintainers|maintained]]'''" or '' ) .. ( ( isMaintained and isNonstandard ) and ' and ' or '' ) .. ( isNonstandard and "has a '''non-standard layout'''" or '' ) .. '.'
		local subtext = "Please [[WP:CAREFUL|take care]] when editing, especially if using [[WP:ASSISTED|automated editing software]]" .. ( isMaintained and ", and seek [[Wikipedia:Consensus|consensus]] before making major changes." or '.')

		return text .. '<br>' .. '<span style="font-size:90%">' .. subtext .. '</span>'
		-- end of legacy code --

		-- return '' -- use this line when legacy code is removed
	end

	local isManuallyMaintained = mw.ustring.match(status, makeParameterPattern('manual')) and true or false
	local isNonstandard = mw.ustring.match(status, makeParameterPattern('nonstandard')) and true or false
	local text = ''
	if isManuallyMaintained or isNonstandard then
		text = "This portal " .. ( isManuallyMaintained and "is '''manually [[Wikipedia:WikiProject Portals#Specific portal maintainers|maintained]]'''" or '' ) .. ( ( isManuallyMaintained and isNonstandard ) and ' and ' or '' ) .. ( isNonstandard and "has a '''non-standard layout'''" or '' ) .. '.<br>'
	end

	local subpages = mw.ustring.match(status, makeParameterPattern('subpages'))
	local subpagesText
	if subpages and ( subpages == 'none' or subpages == 'single' or subpage == 'singlepage' ) then
		subpagesText = frame:preprocess("This portal has a '''single page layout'''. Any [[Special:PrefixIndex/{{SUBJECTSPACE}}:{{ROOTPAGENAME}}/|subpages]] are likely superfluous.<br>")
	elseif not subpages or subpages == '' or subpages == 'untriaged' or subpages == 'unchecked' then
		subpagesText = frame:preprocess("This portal's '''[[Special:PrefixIndex/{{SUBJECTSPACE}}:{{ROOTPAGENAME}}/|subpages]] should be checked'''. Superfluous subpages should be nominated for deletion.<br>")
	else
		subpagesText = frame:preprocess("This portal's [[Special:PrefixIndex/{{SUBJECTSPACE}}:{{ROOTPAGENAME}}/|subpages]] have been checked by an editor, and found '''not to be superfluous'''.<br>") 
	end

	local bottomText = "Please [[WP:CAREFUL|take care]] when editing, especially if using [[WP:ASSISTED|automated editing software]]" .. ( isManuallyMaintained and ", and seek [[Wikipedia:Consensus|consensus]] before making major changes." or '.')

	local output = mw.ustring.format('%s%s<span style="font-size:90%%">%s</span>', text, subpagesText, bottomText)
	return output
end

return p