Module:Page assessment/sandbox
Appearance
This is the module sandbox page for Module:Page assessment (diff). See also the companion subpage for test cases (run). |
This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
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
|class=
parameter; or - Looking at the namespace of the page
Usage
[edit]text
[edit]Returns the class as text.
{{#invoke:Page assessment|main|page name}}
- Examples
{{#invoke:Page assessment|main|Wikipedia}}
→ B{{#invoke:Page assessment|main|Wiktionary}}
→ C{{#invoke:Page assessment|main|Wikt}}
→ Redirect
icon
[edit]Returns the class as an icon.
{{#invoke:Page assessment|icon|page name}}
- Examples
{{#invoke:Page assessment|icon|Wikipedia}}
→{{#invoke:Page assessment|icon|Wiktionary}}
→{{#invoke:Page assessment|icon|Wikt}}
→
iconLink
[edit]Returns the class as an icon and a link to the page.
{{#invoke:Page assessment|iconLink|page name}}
- Examples
{{#invoke:Page assessment|iconLink|Wikipedia}}
→ Wikipedia{{#invoke:Page assessment|iconLink|Wiktionary}}
→ Wiktionary{{#invoke:Page assessment|iconLink|Wikt}}
→ Wikt
In other templates
[edit]The output can be passed into other templates or modules, for example to generate table cells:
{|class=wikitable ! Class !! Article |- {{class|{{#invoke:Page assessment|main|Wiktionary}}}}<td>[[Wiktionary]]</td> |}
Class | Article |
---|---|
C | Wiktionary |
-- Dependencies
require('strict')
local getArgs = require('Module:Arguments').getArgs
local mDisambiguation = require('Module:Disambiguation')
-- Packapge to export
local p = {}
-- Namespace for utlity function
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 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"
else
local classParam = mw.text.trim(
mw.ustring.match(talkpageWikitext, util.paramValuePattern("class"), 0) or ""
)
if classParam == "" then
return util.classByNamespace(pageName)
else
return util.normaliseRating(classParam)
end
end
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