Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Paramvalidate: Difference between revisions

From Sunday Campaign
Leo created the page Module:Paramvalidate using a non-default content model "Scribunto module"
 
No edit summary
 
Line 1: Line 1:
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local mArguments -- initialize lazily
local p = {}
local categories = {}


--- Format table of strings into category wikitext
--- @return string
local function tableToCategory()
local wikitext = {}
for _, category in ipairs(categories) do
table.insert(wikitext, string.format('[[Category:%s]]', category))
end
return table.concat(wikitext)
end
--- Validates if the given image file extension is one of the supported types (gif, png, jpg, jpeg, webp).
--- Return true if the extension is valid, false if isn't (the input parameter is likely incorrect)
--- @param image string - The image argument of the template
--- @return boolean
local function checkImageExtension(image)
local EXTENSIONS = {
'gif',
'png',
'jpg',
'JPG',
'jpeg',
'svg',
'webp'
}
for _, extension in ipairs(EXTENSIONS) do
local pattern = "%." .. extension .. "$"
if string.match(image, pattern) then
return true
end
end
table.insert(categories, 'Pages with invalid image argument in template')
return false
end
--- Check if the image actually exists and whether it has the sufficient width for the infobox
--- @param image string - The image argument of the template
--- @return nil
local function checkImageMetadata(image)
local MIN_WIDTH = 640 -- 320px is the infobox width, 2x for HiDPI screens
local MAX_HEIGHT = 960
local title = mw.title.makeTitle('File', image)
local file = title.file
if not title.exists or not file or not file.exists then
table.insert(categories, 'Pages with non-existent  image in template')
return
end
if file.height < MAX_HEIGHT and file.width < MIN_WIDTH then
table.insert(categories, 'Pages with non-sufficient width image in infobox')
end
end
--- Performs checks on the image argument
--- @param image string - The image argument of the template
--- @return nil
function p.checkImage(image)
local isImageParameterValid = checkImageExtension(image)
-- Only continue if the parameter is valid
if isImageParameterValid then
checkImageMetadata(image)
end
end
--- Retrieves template arguments from the frame invoke function and pass to the main function
function p.paramvalidate(frame)
mArguments = require('Module:Arguments')
return p._paramvalidate(mArguments.getArgs(frame))
end
--- Validate template parameters and return the wikitext
--- @param args table - Template arguments to be validated
--- @return string|nil
function p._paramvalidate(args)
checkType('_paramvalidate', 1, args, 'table')
if args['image'] and args['image'] ~= '' then
checkTypeForNamedArg('_paramvalidate', 'image', args.image, 'string', false)
p.checkImage(args['image'])
end
-- Only add category if it is content pages
if mw.title.getCurrentTitle().isContentPage then
return tableToCategory()
end
end
return p

Latest revision as of 10:43, 27 June 2026

This module is used to validate template inputs.

Supported parameters

Parameter Check Result Function
{{{image}}} Check if the string ends with .extension Category:Pages with invalid image argument in template checkImageExtension
{{{image}}} Check if the image exists as a file Category:Pages with non-existent image in template checkImageMetadata
{{{image}}} Check if the image has sufficient width (>=640px) for the infobox Category:Pages with non-sufficient width image in infobox checkImageMetadata

local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local mArguments -- initialize lazily
local p = {}
local categories = {}


--- Format table of strings into category wikitext
--- @return string
local function tableToCategory()
	local wikitext = {}
	for _, category in ipairs(categories) do
		table.insert(wikitext, string.format('[[Category:%s]]', category))
	end
	return table.concat(wikitext)
end

--- Validates if the given image file extension is one of the supported types (gif, png, jpg, jpeg, webp).
--- Return true if the extension is valid, false if isn't (the input parameter is likely incorrect)
--- @param image string - The image argument of the template
--- @return boolean
local function checkImageExtension(image)
	local EXTENSIONS = {
		'gif',
		'png',
		'jpg',
		'JPG',
		'jpeg',
		'svg',
		'webp'
	}

	for _, extension in ipairs(EXTENSIONS) do
		local pattern = "%." .. extension .. "$"
		if string.match(image, pattern) then
			return true
		end
	end

	table.insert(categories, 'Pages with invalid image argument in template')
	return false
end

--- Check if the image actually exists and whether it has the sufficient width for the infobox
--- @param image string - The image argument of the template
--- @return nil
local function checkImageMetadata(image)
	local MIN_WIDTH = 640 -- 320px is the infobox width, 2x for HiDPI screens
	local MAX_HEIGHT = 960

	local title = mw.title.makeTitle('File', image)
	local file = title.file

	if not title.exists or not file or not file.exists then
		table.insert(categories, 'Pages with non-existent  image in template')
		return
	end

	if file.height < MAX_HEIGHT and file.width < MIN_WIDTH then
		table.insert(categories, 'Pages with non-sufficient width image in infobox')
	end
end

--- Performs checks on the image argument
--- @param image string - The image argument of the template
--- @return nil
function p.checkImage(image)
	local isImageParameterValid = checkImageExtension(image)
	-- Only continue if the parameter is valid
	if isImageParameterValid then
		checkImageMetadata(image)
	end
end

--- Retrieves template arguments from the frame invoke function and pass to the main function
function p.paramvalidate(frame)
	mArguments = require('Module:Arguments')
	return p._paramvalidate(mArguments.getArgs(frame))
end

--- Validate template parameters and return the wikitext
--- @param args table - Template arguments to be validated
--- @return string|nil
function p._paramvalidate(args)
	checkType('_paramvalidate', 1, args, 'table')

	if args['image'] and args['image'] ~= '' then
		checkTypeForNamedArg('_paramvalidate', 'image', args.image, 'string', false)
		p.checkImage(args['image'])
	end

	-- Only add category if it is content pages
	if mw.title.getCurrentTitle().isContentPage then
		return tableToCategory()
	end
end

return p