Module:Page assessment

MyWikiBiz, Author Your Legacy — Monday May 20, 2024
Jump to navigationJump to search

Template:Module rating

This module returns a page's assessment (class rating), by:

  • Detecting non-existent pages (Needed class); or
  • Detecting non-existent talk pages (Unassessed class); or
  • Detecting redirects and disambiguation pages (Redirect or Disambig class); or
  • Looking at the talk page for the value in the first Template:Para parameter; or
  • Looking at the namespace of the page



Returns the class as text. {{#invoke:Page assessment|main|page name}}

{{#invoke:Page assessment|main|Wikipedia}} → Redirect
{{#invoke:Page assessment|main|Wiktionary}} → Needed
{{#invoke:Page assessment|main|Wikt}} → Needed


Returns the class as an icon. {{#invoke:Page assessment|icon|page name}}

{{#invoke:Page assessment|icon|Wikipedia}}Template:Class/icon
{{#invoke:Page assessment|icon|Wiktionary}}Template:Class/icon
{{#invoke:Page assessment|icon|Wikt}}Template:Class/icon


Returns the class as an icon and a link to the page. {{#invoke:Page assessment|iconLink|page name}}

{{#invoke:Page assessment|iconLink|Wikipedia}}Template:Class/icon Wikipedia
{{#invoke:Page assessment|iconLink|Wiktionary}}Template:Class/icon Wiktionary
{{#invoke:Page assessment|iconLink|Wikt}}Template:Class/icon Wikt

In other templates

The output can be passed into other templates or modules, for example to generate table cells:

! Class !! Article
{{class|{{#invoke:Page assessment|main|Wiktionary}}}}<td>[[Wiktionary]]</td>
Class Article

-- Dependencies
require('Module:No globals')
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 =
	if not title then
		return nil, nil
	local subjectTitle = title.subjectPageTitle
	local talkpageTitle = title.talkPageTitle
	return subjectTitle:getContent(), talkpageTitle:getContent()

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(
	) and true or false
Creates a pattern for finding the value given to a parameter within any template

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

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 =
	if not title then return "" end
	local nsNumber = title.subjectPageTitle.namespace
	return classByNamespace[nsNumber] or "NA"

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 ""
		class = mw.text.trim(class)
	class = classByAlias[mw.ustring.lower(class)] or class
	if mw.ustring.len(class) <= 3 then
		-- Uppercase, e.g. "FA"
		return mw.ustring.upper(class)
		-- Sentence case, e.g. "Redirect"
		return mw.ustring.upper(mw.ustring.sub(class, 1, 1 )) .. mw.ustring.lower(mw.ustring.sub(class, 2)) 

Gets the class rating for a page

@param {string} pageName - either subject or talk page name
@returns {string} class rating, or empty string if none found
function util.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"
	elseif util.isRedirect(subjectWikitext) then
		return "Redirect"
	elseif mDisambiguation.isDisambiguation(subjectWikitext) then
		return "Disambig"
		local classParam = mw.text.trim(
			mw.ustring.match(talkpageWikitext, util.paramValuePattern("class"), 0) or ""
		if classParam == "" then
			return util.classByNamespace(pageName)
			return util.normaliseRating(classParam)

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])

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)

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)

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