Jump to content

Module:Validate gadgets

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 08:34, 2 January 2024 (wip). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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