Module:Page assessment: Difference between revisions
Appearance
Content deleted Content added
add icon and iconLink functions |
Normalise ratings |
||
Line 1: | Line 1: | ||
-- Dependencies |
|||
require('Module:No globals') |
require('Module:No globals') |
||
local getArgs = require('Module:Arguments').getArgs |
local getArgs = require('Module:Arguments').getArgs |
||
local mDisambiguation = require('Module:Disambiguation') |
local mDisambiguation = require('Module:Disambiguation') |
||
-- Packapge to export |
|||
local p = {} |
local p = {} |
||
-- Namespace for utlity functions |
|||
local util = {} |
local util = {} |
||
-- Table to look up class rating from namespace number |
|||
local classByNamespace = { |
local classByNamespace = { |
||
[710] = "File", -- TimedText namespace |
[710] = "File", -- TimedText namespace |
||
Line 15: | Line 22: | ||
[108] = "Book", |
[108] = "Book", |
||
[0] = "" -- Mainspace (leave unassessed) |
[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", |
|||
} |
} |
||
Line 70: | Line 98: | ||
end |
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) |
function util.class(pageName) |
||
local subjectWikitext, talkpageWikitext = util.getWikitext(pageName) |
local subjectWikitext, talkpageWikitext = util.getWikitext(pageName) |
||
if not subjectWikitext then -- page does not exist |
if not subjectWikitext then -- page does not exist |
||
return " |
return "Needed" |
||
elseif not subjectWikitext then -- talk page does not exist |
elseif not subjectWikitext then -- talk page does not exist |
||
return " |
return "Unassessed" |
||
elseif util.isRedirect(subjectWikitext) then |
elseif util.isRedirect(subjectWikitext) then |
||
return " |
return "Redirect" |
||
elseif mDisambiguation.isDisambiguation(subjectWikitext) then |
elseif mDisambiguation.isDisambiguation(subjectWikitext) then |
||
return " |
return "Disambig" |
||
else |
else |
||
local classParam = mw.text.trim( |
local classParam = mw.text.trim( |
||
Line 87: | Line 145: | ||
return util.classByNamespace(pageName) |
return util.classByNamespace(pageName) |
||
else |
else |
||
return classParam |
return util.normaliseRating(classParam) |
||
end |
end |
||
end |
end |
||
end |
end |
||
--[[ |
|||
Entry point for invoking the module. |
|||
Gets the class rating as text. |
|||
]]-- |
|||
function p.main(frame) |
function p.main(frame) |
||
local args = getArgs(frame, { |
local args = getArgs(frame, { |
||
Line 99: | Line 161: | ||
end |
end |
||
--[[ |
|||
Entry point for invoking the module. |
|||
Gets the class rating as an icon. |
|||
]]-- |
|||
function p.icon(frame) |
function p.icon(frame) |
||
local args = getArgs(frame, { |
local args = getArgs(frame, { |
||
Line 108: | Line 174: | ||
end |
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) |
function p.iconLink(frame) |
||
local args = getArgs(frame, { |
local args = getArgs(frame, { |
||
Line 117: | Line 187: | ||
end |
end |
||
-- Export util, for testing purposes |
|||
p. |
p.test = util |
||
return p |
return p |
Revision as of 05:15, 30 December 2020
![]() | 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
text
Returns the class as text.
{{#invoke:Page assessment|main|page name}}
- Examples
{{#invoke:Page assessment|main|Wikipedia}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Page assessment|main|Wiktionary}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Page assessment|main|Wikt}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
icon
Returns the class as an icon.
{{#invoke:Page assessment|icon|page name}}
- Examples
{{#invoke:Page assessment|icon|Wikipedia}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Page assessment|icon|Wiktionary}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Page assessment|icon|Wikt}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iconLink
Returns the class as an icon and a link to the page.
{{#invoke:Page assessment|iconLink|page name}}
- Examples
{{#invoke:Page assessment|iconLink|Wikipedia}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Page assessment|iconLink|Wiktionary}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.{{#invoke:Page assessment|iconLink|Wikt}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
In other templates
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 |
---|---|
??? | Wiktionary |
-- 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 = 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