Jump to content

Module:Portal maintenance status/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 02:07, 10 June 2018 (start making ready for {{Portal maintenance status}}). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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, value)
	return mw.ustring.format('%s%s%s%s', '|%s*', parameter, '%s*=%s*', value or '', '%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 ~= namespaceNumber + 1 then
		return error('Wrong namespace', 0)
	end
	local subjectTitle = mw.title.new(namespace .. ":" .. talkTitle.text)
	return subjectTitle:getContent()
end

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
		portalContent = '{{' .. args.demo .. '}}'
		if args.demo2 then
			portalContent = portalContent  .. '{{' .. args.demo2 .. '}}'
		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
		return ''
	end

	local isManuallyMaintained = mw.ustring.match(status, makeParameterPattern(manual, '[~|%}]')) and true or false
	local isNonstandard = mw.ustring.match(status, makeParameterPattern(manual, '[~|%}]')) and true or false

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

	local text = "This portal " .. ( isManuallyMaintained and "'''is [[Wikipedia:WikiProject Portals#Specific portal maintainers|maintained]]'''" or '' ) .. ( ( isManuallyMaintained 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]]" .. ( isManuallyMaintained and ", and seek [[Wikipedia:Consensus|consensus]] before making major changes." or '.')

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

return p