Module:Validate gadgets
Appearance
![]() | This Lua module is used in system messages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
{{#invoke:Validate gadgets|validate}}
This module checks the gadget definitions in MediaWiki:Gadgets-definition for errors and other issues.
No output is produced if there are no warnings. But during previews, a message with a green check will be shown.
local p = {}
-- Database of valid options
local VALID_SKINS = {'vector', 'vector-2022', 'minerva', 'timeless', 'monobook', 'modern', 'cologneblue'}
local VALID_CONTENT_MODELS = {'wikitext', 'javascript', 'css', 'json', 'MassMessage', 'Scribunto', 'sanitized-css'}
local VALID_RIGHTS = {'minoredit', 'viewmywatchlist'}
local VALID_ACTIONS = {'view', 'edit', 'history', 'info'}
p.validate = function ()
-- local text = mw.title.getCurrentTitle():getContent()
local text = mw.title.new('MediaWiki:Gadgets-definition'):getContent()
local lines = mw.text.split(text, '\n', false)
for _, line in ipairs(lines) do
if line:sub(1, 1) == '*' then
return p.parse_line(line)
end
end
return lines[1]
end
p.parse_line = function(def)
local pattern = "^%*%s*(%S+)%s*(%b[])%s*(.-)$"
local name, opts, fileList = string.match(def, pattern)
if name and options then
-- Extracting the options without square brackets and trimming spaces
opts = opts:sub(2, -2):gsub("%s+", "")
end
local warnings = {}
if string.len(name) > 255 then
table.insert(warnings, 'Gadget name exceeds 255 bytes')
end
-- Process options string into a Lua table
local options = {}
for option in opts:gmatch("[^|]+") do
local key, value = option:match("%s*(%w+)%s*=%s*(%w+)%s*")
if not key then
-- Handling boolean options where no '=' is present
key = option:gsub("%s+", "")
value = true -- Setting a default value for boolean options
end
end
-- Process file list into an array
local files = {}
for file in fileList:gmatch("[^|]+") do
files[#files + 1] = file:gsub("%s+", "") -- Remove spaces
end
if options.ResourceLoader == nil then
table.insert(warnings, 'ResourceLoader option must be set')
end
if options.namespaces ~= nil then
for _, id in ipairs(mw.text.split(options.namespaces, ',', false)) do
id = tonumber(id)
if mw.site.namespaces[id] == nil then
table.insert(warnings, 'Namespace '..id..' is invalid')
end
end
end
for _, page in ipairs(files) do
page = 'MediaWiki:Gadget-' .. page
local title = mw.title.new(page)
if title == nil or not title.exists then
table.insert(warnings, 'Page '..page..' does not exist')
end
local ext = title.text:match("%.([^%.]+)$")
if ext == 'js' and title.contentModel ~= 'javascript' then
table.insert(warnings, 'Page '..page..' is not of JavaScript content model')
end
if ext == 'css' and title.contentModel ~= 'css' then
table.insert(warnings, 'Page '..page..' is not of CSS content model')
end
if ext == 'json' and title.contentModel ~= 'json' then
table.insert(warnings, 'Page '..page..' is not of JSON content model')
end
end
return options
end
return p