Jump to content

Module:Page assessment

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 12:44, 22 December 2020 (:). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local p = {}
local util = {}

--[[
Gets the wikitext of a page and its related talk or subject page (nil if it does
not exist)

@param {string} pageName
@returns {string|nil, string|nil} subject page wikitext, talk page wikitex
]]--
function util.getWikitext(pageName)
	local title = mw.title.new(pageName)
	if not title then
		return nil, nil
	end
	local subjectTitle = title.isTalkPage and title.subjectPageTitle or title
	local talkpageTitle = title.isTalkPage and title or title.talkPageTitle
	return subjectTitle:getContent(), talkpageTitle:getContent()
end

--[[
Creates a pattern for finding the value given to a parameter within any template
call.

@param {string} param
@returns {string} pattern
]]--
function util.paramValuePattern(param)
	return "{{[^}]*|%s*" + param + "%s*=([^|{}]+)"
end

function p.class(pageName)
	local subjectWikitext, talkpageWikitext = util.getWikitext(pageName)
	if not subjectWikitext then -- page does not exist
		return "needed"
	elseif not subjectWikitext then -- talk page does not exist
		return "unassessed"
	else
		local classParam = mw.text.trim(
			mw.ustring.find(subjectWikitext, util.paramValuePattern(class)) or ""
		)
		return classParam == "" and "unassessed" or classParam
	end
end

function p.main(frame)
	local args = getArgs(frame, {
		parentFirst = true
	})
	local class = p.class(args[1])
	return frame:expandTemplate{title = 'class mask', args = { 'arg1', 'arg2', name = 'arg3' } }
end

p.util = util
return p