Jump to content

Module:Page assessment: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
check for disambigs
Assign a class rating based on namespace, for non-article pages not otherwise assessed
Line 4: Line 4:
local p = {}
local p = {}
local util = {}
local util = {}
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)
}


--[[
--[[
Line 43: Line 55:
function util.paramValuePattern(param)
function util.paramValuePattern(param)
return "{{[^}]*|%s*" .. param .. "%s*=([^|{}]+)"
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
end


Line 56: Line 81:
return "disambig"
return "disambig"
else
else
local classParam = mw.ustring.match(talkpageWikitext, "{{.-%|%s*class%s*=([^}%|]*)", 0) or ""
local classParam = mw.text.trim(
mw.ustring.match(talkpageWikitext, "{{.-%|%s*class%s*=([^}%|]*)", 0) or ""
)
return mw.text.trim(classParam)
if classParam == "" then
return util.classByNamespace(pageName)
else
return classParam
end
end
end
end
end

Revision as of 14:34, 22 December 2020

require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local mDisambiguation = require('Module:Disambiguation')
local p = {}
local util = {}
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)
}

--[[
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

function p.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, "{{.-%|%s*class%s*=([^}%|]*)", 0) or ""
		)
		if classParam == "" then
			return util.classByNamespace(pageName)
		else
			return classParam
		end
	end
end

function p.main(frame)
	local args = getArgs(frame, {
		parentFirst = true
	})
	return p.class(args[1])
end

p.util = util
return p