Module:Class mask

From Safer nicotine wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Class mask/doc

local p = {}

local function getDocWarning(title)
end

local function ucfirst(s)
	-- Returns the given string with the first character in upper case.
	-- Should not be used with non-ascii strings.
	return s:sub(1, 1):upper() .. s:sub(2, -1)
end

local function isTruthyBParam(s)
	s = s and s:lower()
	return not s or s == 'yes' or s == 'y' or s == '1' or s == 'pass' or s == 'na' or s == 'n/a' or s == '¬' or s == 'unused'
end

local function resolveFQSgrade(grade, args)
	if (args[grade] or args.FQS) == 'yes' then
		return ucfirst(grade)
	else
		return 'NA'
	end
end

local function resolveExtraGrade(grade, args)
	if args[grade] == 'yes' then
		return ucfirst(grade)
	else
		return 'NA'
	end
end

local function resolveDefaultGrade(args, title, talkDefault)
	if args.ignorenamespace then
		return nil
	end
	if title.subjectPageTitle.isRedirect then
		return resolveExtraGrade('redirect',args)
	end
	local ns = title.namespace
	if ns==1 then -- Talk
		return talkDefault
	elseif ns==7 or ns==711 then -- File talk
		return resolveFQSgrade('file', args)
	elseif ns==15 then -- Category talk
		return resolveFQSgrade('category', args)
	elseif ns==101 then -- Portal talk
		return resolveFQSgrade('portal', args)
	elseif ns==11 or ns==829 then -- Template talk
		return resolveFQSgrade('template', args)
	elseif ns==5 then -- Wikipedia talk
		return resolveFQSgrade('project', args)
	elseif ns==119 then -- Draft talk
		return resolveFQSgrade('draft', args)
	else
		return 'NA'
	end
end

local function getGrade(args, title)
	local grade = args[1]
	grade = grade and grade:match('^%s*(.-)%s*$'):lower()
	local ret

	if not grade or grade == '¬' then -- undefined
		ret = '¬'

	-- Ucfirst
	elseif grade == 'start' or grade == 'stub' or grade == 'list' then
		if args[grade] ~= 'no' then
			ret = ucfirst(grade)
		end

	-- B
	elseif grade == 'b' then
		local bParams = {'b1', 'b2', 'b3', 'b4', 'b5', 'b6'}
		local isExtended = false
		for _, param in ipairs(bParams) do
			if args[param] then
				isExtended = true
				break
			end
		end
		if isExtended then
			local isB = true
			for _, param in ipairs(bParams) do
				if not isTruthyBParam(args[param]) then
					isB = false
					break
				end
			end
			ret = isB and 'B' or 'C'
		elseif args.b ~= 'no' then
			ret = 'B'
		end

	-- Upper-case
	elseif grade == 'fa' or grade == 'fl' or grade == 'a' or grade == 'ga' or grade == 'c' then
		if args[grade] ~= 'no' then
			ret = grade:upper()
		end
	elseif grade == 'na' then
		if args.forceNA == 'yes' then
			ret = resolveDefaultGrade(args, title, 'NA')
		else
			ret = 'NA'
		end
	elseif grade == 'file' or grade == 'image' or grade == 'img' then
		ret = resolveFQSgrade('file', args)
	elseif grade == 'category' or grade == 'cat' or grade == 'categ' then
		ret = resolveFQSgrade('category', args)
	elseif grade == 'dab' or grade == 'disambig' or grade == 'disambiguation' or grade == 'disamb' then
		ret = resolveFQSgrade('disambig', args)
	elseif grade == 'redirect' or grade == 'red' or grade == 'redir' then
		ret = resolveExtraGrade('redirect', args)
	elseif grade == 'portal' or grade == 'project' or grade == 'draft' then
		ret = resolveFQSgrade(grade, args)
	elseif grade == 'template' or grade == 'temp' or grade == 'tpl' or grade == 'templ' then
		ret = resolveFQSgrade('template', args)
	elseif grade == 'fm' then
		if args.fm == 'yes' then
			ret = 'FM'
		else
			ret = resolveFQSgrade('file', args)
		end

	else
		-- Upper-case syntax
		ret = args[grade:upper()]

		-- Lower-case syntax
		if not ret then
			if args[grade:lower()] == 'yes' then
				ret = mw.language.getContentLanguage():ucfirst(grade:lower())
			end
		end

		-- Defaults
		if not ret then
			ret = resolveDefaultGrade(args, title)
		end
	end

	return ret
end

function p._main(args, title)
	title = title or mw.title.getCurrentTitle()
	local out = ''
	if title.namespace == 10 and title.subpageText == 'class' then
		out = mw.getCurrentFrame():expandTemplate{title = 'Class mask/doc warning'}
	end
	out = out .. (getGrade(args, title) or '')
	return out
end

function p.main(frame)
	local args = frame.args or frame:getParent().args
	return p._main(args)
end

return p