Difference between revisions of "Module:Portal maintenance status/sandbox"

From blackwiki
Jump to navigation Jump to search
test>Evad37
(make pipe optional)
m (21 revisions imported)
 
(9 intermediate revisions by 2 users not shown)
Line 29: Line 29:
 
end
 
end
  
function contentContains(content, template, subpattern, leadOnly)
+
function getMatchingTemplateWikitext(content, template, leadOnly)
 
if leadOnly then
 
if leadOnly then
 
content = mw.ustring.gsub(content, "%c%s*==.*","") -- remove first ==Heading== and everything after it
 
content = mw.ustring.gsub(content, "%c%s*==.*","") -- remove first ==Heading== and everything after it
 
end
 
end
local templateWikitext = string.match(content, makeTemplatePattern(template))
+
for templateWikitext in mw.ustring.gmatch(content, '%b{}') do
if not templateWikitext then
+
local isCorrectTemplate = mw.ustring.match(templateWikitext, makeTemplatePattern(template))
return false
+
if isCorrectTemplate then
end
+
return templateWikitext
if subpattern then
+
end
return string.match(templateWikitext, subpattern) or false
 
else
 
return templateWikitext or false
 
 
end
 
end
 +
return false
 
end
 
end
  
Line 54: Line 52:
 
end
 
end
  
p.main = function(frame)
+
-- 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 parent = frame.getParent(frame)
 
local args = cleanupArgs(frame.args)
 
local args = cleanupArgs(frame.args)
 
local demo = args.demo and true or false
 
local demo = args.demo and true or false
local portalContent
+
local content
 
if demo then
 
if demo then
 
local demoText = mw.ustring.gsub(args.demo, '%{%{%!%}%}', '|')
 
local demoText = mw.ustring.gsub(args.demo, '%{%{%!%}%}', '|')
portalContent = '{{' .. demoText .. '}}'
+
content = '{{' .. demoText .. '}}'
 
if args.demo2 then
 
if args.demo2 then
 
local demo2Text = mw.ustring.gsub(args.demo2, '%{%{%!%}%}', '|')
 
local demo2Text = mw.ustring.gsub(args.demo2, '%{%{%!%}%}', '|')
portalContent = portalContent  .. '{{' .. demo2Text .. '}}'
+
content= portalContent  .. '{{' .. demo2Text .. '}}'
 
end
 
end
 
else
 
else
portalContent = getSubjectPageContent(100)
+
content = getSubjectPageContent(4)
 
end
 
end
  
local status = mw.ustring.match(portalContent, makeTemplatePattern('Portal maintenance status')) or mw.ustring.match(portalContent, makeTemplatePattern('Portal flag'))
+
content = mw.ustring.gsub(content, "<!%-%-.-%-%->","") -- remove HTML comments
if not status then
+
content = mw.ustring.gsub(content, "%c%s*==.*","") -- remove first ==Heading== and everything after it
-- Legacy code -- remove once there are no more uses of {{Maintained portal flag}} and {{Non-standard portal flag}} --
+
content = mw.ustring.gsub(content, "<noinclude>.-</noinclude>", "") -- remove noinclude bits
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
+
local isHistorical = mw.ustring.match(content, makeTemplatePattern('Historical')) and true or false
 +
return isHistorical and 'yes' or ''
 +
end
  
if (not isMaintained) and (not isNonstandard) then
+
-- featured function
return ''
+
--  Looks for {{Featured portal}} on a Portal_talk: page's related portal page.
 +
--  Returns 'yes' if found or '' (empty string) if not found, or an error if used in the wrong namespace.
 +
p.featured = 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
 
end
 +
else
 +
content = getSubjectPageContent(100)
 +
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 '' ) .. '.'
+
content = mw.ustring.gsub(content, "<!%-%-.-%-%->","") -- remove HTML comments
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 '.')
+
content = mw.ustring.gsub(content, "<noinclude>.-</noinclude>", "") -- remove noinclude bits
  
return text .. '<br>' .. '<span style="font-size:90%">' .. subtext .. '</span>'
+
local isFeatured = mw.ustring.match(content, makeTemplatePattern('Featured portal')) and true or false
-- end of legacy code --
+
return isFeatured and 'yes' or ''
 +
end
  
-- return '' -- use this line when legacy code is removed
+
-- 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
 
end
  
local isManuallyMaintained = mw.ustring.match(status, makeParameterPattern('manual')) and true or false
+
local status = getMatchingTemplateWikitext(portalContent, 'Portal maintenance status') or getMatchingTemplateWikitext(portalContent, 'Portal flag')
local isNonstandard = mw.ustring.match(status, makeParameterPattern('nonstandard')) and true or false
+
if not status then
local text = ''
+
return ''
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
 
end
  
local subpages = mw.ustring.match(status, makeParameterPattern('subpages'))
+
local output = mw.ustring.sub(status, 0, -3) .. '|embed=yes}}'  
local subpagesText
+
return frame:preprocess(output)
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
 
end
  
 
return p
 
return p

Latest revision as of 17:26, 8 September 2020

Documentation for this module may be created at Module:Portal maintenance status/sandbox/doc

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 getMatchingTemplateWikitext(content, template, leadOnly)
	if leadOnly then
		content = mw.ustring.gsub(content, "%c%s*==.*","") -- remove first ==Heading== and everything after it
	end
	for templateWikitext in mw.ustring.gmatch(content, '%b{}') do
		local isCorrectTemplate = mw.ustring.match(templateWikitext, makeTemplatePattern(template))
		if isCorrectTemplate then
			return templateWikitext
		end
	end
	return false
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

-- featured function
--   Looks for {{Featured portal}} on a Portal_talk: page's related portal page.
--   Returns 'yes' if found or '' (empty string) if not found, or an error if used in the wrong namespace.
p.featured = 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(100)
	end

	content = mw.ustring.gsub(content, "<!%-%-.-%-%->","") -- remove HTML comments
	content = mw.ustring.gsub(content, "<noinclude>.-</noinclude>", "") -- remove noinclude bits

	local isFeatured = mw.ustring.match(content, makeTemplatePattern('Featured portal')) and true or false
	return isFeatured 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 = getMatchingTemplateWikitext(portalContent, 'Portal maintenance status') or getMatchingTemplateWikitext(portalContent, 'Portal flag')
	if not status then
		return ''
	end

	local output = mw.ustring.sub(status, 0, -3) .. '|embed=yes}}' 
	return frame:preprocess(output)
end

return p