Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Page assessment

From Wikipedia, the free encyclopedia

-- Dependencies
require('strict')
local getArgs = require('Module:Arguments').getArgs
local mDisambiguation = require('Module:Disambiguation')

-- Packapge to export
local p = {}

-- Namespace for utlity functions
local util = {}

-- Table to look up class rating from namespace number
local classByNamespace = {
	[710] = "File", -- TimedText namespace
    [6]   = "File",
    [14]  = "Category",
    [100] = "Portal",
    [828] = "Template", -- Module namespace
    [10]  = "Template",
    [4]   = "Project", -- Wikipedia namespace
    [118] = "Draft",
    [108] = "Book",
    [0]   = "" -- Mainspace (leave unassessed)
}

-- Table to look up standard class names from aliases,
-- based on the source code of Template:Class_mask (as of 30 Dec 2020)
local classByAlias = { 
	image = "File",
	img   = "File",
	
	cat   = "Category",
	categ = "Category",
	
	disambiguation = "Disambig",
	disamb         = "Disambig",
	dab            = "Disambig",
	
	red   = "Redirect",
	redir = "Redirect",
	
	temp  = "Template",
	tpl   = "Template",
	templ = "Template",
}

--[[
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.subjectPageTitle
	local talkpageTitle = title.talkPageTitle
	return subjectTitle:getContent(), talkpageTitle:getContent()
end

--[[
Checks if a page is a redirect without using the expensive mw.title.isRedirect

@param {string} wikitext - page wikitext, from mw.title:getContent()
@returns {boolean}
--]]
function util.isRedirect(wikitext)
	return string.match(
		wikitext,
		"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]"
	) and true or false
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

--[[
Assigns a class rating based on namespace, for non-article pages

@param {string} pageName - name of page, or its talk page
@returns {string} class or empty string
]]--
function util.classByNamespace(pageName)
	local title = mw.title.new(pageName)
	if not title then return "" end
	local nsNumber = title.subjectPageTitle.namespace
	return classByNamespace[nsNumber] or "NA"
end

--[[
Normalises the capitalisation of class rating, e.g. "fa" to "FA", or "redirect"
to "Redirect". Also converts aliases to standard class names, e.g. from "Image"
to "File".

@param {string} class
@returns {string} normalisedClass
]]--
function util.normaliseRating(class)
	if not class or class == "" then
		return ""
	else
		class = mw.text.trim(class)
	end
	class = classByAlias[mw.ustring.lower(class)] or class
	if mw.ustring.len(class) <= 3 then
		-- Uppercase, e.g. "FA"
		return mw.ustring.upper(class)
	else
		-- Sentence case, e.g. "Redirect"
		return mw.ustring.upper(mw.ustring.sub(class, 1, 1 )) .. mw.ustring.lower(mw.ustring.sub(class, 2)) 
	end
end

--[[
Gets the class rating for a page

@param {string} pageName - either subject or talk page name
@returns {string} class rating, or "Unassessed" if none found
]]--
function util.class(pageName)
	local subjectPage = mw.title.new(pageName)                   -- create new object for given page title
	local subjectAssessments = subjectPage.pageAssessments
	-- save first-indexed pageAssessments 
	local _subjectProject, subjectAssessment = next(subjectAssessments)

    if subjectAssessment ~= nil then                             -- if there are assessments in pageAssessments
        return subjectAssessment["class"]                        -- return class parameter from pageAssessments
    else
    	local subjectWikitext, talkpageWikitext = util.getWikitext(pageName)
		if not subjectWikitext then                              -- page does not exist
			return "Needed"
		elseif util.isRedirect(subjectWikitext) then
			return "Redirect"
		elseif mDisambiguation.isDisambiguation(subjectWikitext) then
			return "Disambig"
		end
	end

	return "Unassessed"
end

--[[
Entry point for invoking the module.
Gets the class rating as text.
]]--
function p.main(frame)
	local args = getArgs(frame, {
		parentFirst = true
	})
	return util.class(args[1])
end

--[[
Entry point for invoking the module.
Gets the class rating as an icon.
]]--
function p.icon(frame)
	local args = getArgs(frame, {
		parentFirst = true
	})
	local class = util.class(args[1])
	local wikitext = mw.ustring.format("{{class/icon|%s}}", class)
	return frame:preprocess(wikitext)
end

--[[
Entry point for invoking the module.
Gets the class rating as an icon, followed by a link to the page.
]]--
function p.iconLink(frame)
	local args = getArgs(frame, {
		parentFirst = true
	})
	local class = util.class(args[1])
	local wikitext = mw.ustring.format("{{class/icon|%s}} [[%s]]", class, args[1])
	return frame:preprocess(wikitext)
end

-- Export util, for testing purposes
p.test = util
return p