Module:ParamValidate
More actions
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))
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