Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Page assessment
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
-- 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 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
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Wikipedia
(
edit
)
Wikt
(
edit
)
Wiktionary
(
edit
)
Talk:Wikipedia
(
edit
)
Talk:Wikt
(
edit
)
Talk:Wiktionary
(
edit
)
Template:Class
(
edit
)
Template:Class/icon
(
edit
)
Template:Module other
(
edit
)
Template:Module rating
(
edit
)
Template:Ombox
(
edit
)
Template:Para
(
edit
)
Module:Arguments
(
edit
)
Module:Disambiguation
(
edit
)
Module:Message box
(
edit
)
Module:Message box/configuration
(
edit
)
Module:Message box/ombox.css
(
edit
)
Module:Page assessment
(
edit
)
Module:Page assessment/doc
(
edit
)
Module:Redirect
(
edit
)
Module:Yesno
(
edit
)