Jump to content

Module:Database report

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 18:05, 17 June 2021 (more; fixes). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local getArgs = require('Module:Arguments').getArgs

local p = {}

function p.main(frame)
	local args = getArgs(frame)
	return p.check_syntax(args)
end

local function error(text)
	return '*' .. require('Module:Error').error{ text, tag = 'p' }
end 

local function has_value(array, val)
    for index, value in ipairs(array) do
        if value == val then
            return true
        end
    end
    return false
end

local function str_split(inputstr, sep)
    local t = {}
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
		table.insert(t, str)
    end
    return t
end

local function trim(s)
   return s:match("^%s*(.-)%s*$")
end

function p.check_syntax(args)
	
	-- check for invalid parameters
	for param, value in pairs(args) do
		if not has_value({'sql', 'wikilinks', 'excerpts', 'comments', 'widths', 'table_style', 'remove_underscores', 'update'}, param) then
			return error('Unknown parameter "' .. param .. '" used, ignoring')
		end
	end
	
	-- check that required parameters are provided
	if not args.sql then
		return error('Invalid config: required parameter "sql" is missing')
	end
	
	-- check wikilinks config
	if args.wikilinks then
		local configs = str_split(args.wikilinks, ',')
		for idx, config in ipairs(configs) do
			local parts = str_split(trim(config), ':')
			local srcColNum = parts[1]
			if srcColNum:match("%D") then
				return error('Invalid wikilink config: Non-numeral source column number: '..parts[1]..'. Will be ignored.')
			end
			local ns = parts[2]
			if ns and ns:match("^c?%d+$") == nil then 
				return error('Invalid namespace number "'.. ns..'" in wikilinks parameter: refer to [[WP:NS]] for namespace numbers, or use "cN" to take namespace number from column number N')
			end 
		end 
	end
	
	-- check excerpts config
	if args.excerpts then
		local configs = str_split(args.excerpts, ',')
		for idx, config in ipairs(configs) do
			local parts = str_split(trim(config), ':')
			local srcColNum = parts[1]
			if srcColNum:match("%D") then
				return error('Invalid excerpts config: Non-numeral source column number: '..parts[1]..'. Will be ignored.')
			end
			local dstColNum = parts[2]
			if dstColNum and dstColNum:match("%D") then
				return error('Invalid excerpts config: Non-numeral destination column number: '..parts[2]..'. Will be ignored.')
			end
			local ns = parts[3]
			if ns and ns:match("^c?%d+$") == nil then 
				return error('Invalid namespace number "'.. ns..'" in excerpts parameter: refer to [[WP:NS]] for namespace numbers, or use "cN" to take namespace number from column number N')
			end 
			local charLimit = parts[4]
			if charLimit and charLimit:match("%D") then 
				return error('Invalid excerpts config: Non-numeral in charLimit: '..parts[4]..'. Will be ignored.')
			end 
			local hardCharLimit = parts[5]
			if hardCharLimit and hardCharLimit:match("%D") then 
				return error('Invalid excerpts config: Non-numeral in hardCharLimit: '..parts[5]..'. Will be ignored.')
			end 
		end 
	end
	
	-- check column numbers in widths param
	if args.widths then
		local configs = str_split(args.widths, ',')
		for _, config in ipairs(configs) do 
			local parts = str_split(trim(config), ':')
			local column = parts[1]
			if column:match("%D") then
				return error('Invalid widths config: Non-numeral column number: '..parts[1]..'. Will be ignored.')
			end
		end 
	end
	
	-- check numeric configs
	if args.comments then
		local columns = str_split(args.comments, ',')
		for _, column in ipairs(columns) do
			if column:match("%D") then
				return error('Invalid comments parameter: Non-numeral column number: '..parts[1]..'. Will be ignored.')
			end
		end
	end
	if args.remove_underscores then
		local columns = str_split(args.remove_underscores, ',')
		for _, column in ipairs(columns) do
			if column:match("%D") then
				return error('Invalid remove_underscores parameter: Non-numeral column number: '..parts[1]..'. Will be ignored.')
			end
		end
	end
	
	return ''
end

return p