Module:Database report

MyWikiBiz, Author Your Legacy — Wednesday May 01, 2024
Jump to navigationJump to search

This module is used from Template:T, Template:T and Template:T. Template:Sandbox other


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

local p = {}

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

local function arr_contains(array, val)
    for _, 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 is_integer(str) 
	if str:match("%D") then 
		return false
	end 
	return true
end

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

-- Called from {{Database report}}
function p.check_syntax(frame)
	local args = getArgs(frame)
	
	-- check that required parameters are provided
	if not args.sql then
		return error('Invalid config: required parameter "sql" is missing')
	end
	
	if not args.row_template then
		if args.skip_table then 
			return error('Invalid config: skip_table can only be used with row_template')
		end 
		if args.header_template then 
			return error('header_template can be used only when row_template is being used')
		end 
	end 
	
	-- check for invalid parameters
	for param, _ in pairs(args) do
		if not arr_contains({
			'sql', 'wikilinks', 'excerpts', 'comments', 'widths', 'hide',
			'table_style', 'remove_underscores', 'autoupdate', 'pagination',
			'max_pages', 'frequency', 'row_template', 'skip_table', 
			'header_template'
		}, param) then
			return error('Unknown parameter "' .. param .. '" used, ignoring')
		end
	end

	-- check wikilinks config
	if args.wikilinks then
		local configs = str_split(args.wikilinks, ',')
		for _, config in ipairs(configs) do
			local parts = str_split(trim(config), ':')
			local srcColNum = parts[1]
			if not is_integer(srcColNum) 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 _, config in ipairs(configs) do
			local parts = str_split(trim(config), ':')
			local srcColNum = parts[1]
			if not is_integer(srcColNum) then
				return error('Invalid excerpts config: Non-numeral source column number: '..parts[1]..'. Will be ignored.')
			end
			local dstColNum = parts[2]
			if dstColNum and not is_integer(dstColNum) 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 not is_integer(charLimit) then 
				return error('Invalid excerpts config: Non-numeral in charLimit: '..parts[4]..'. Will be ignored.')
			end 
			local hardCharLimit = parts[5]
			if hardCharLimit and not is_integer(hardCharLimit) 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 not is_integer(column) 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 not is_integer(trim(column)) then
				return error('Invalid comments parameter: Non-numeral column number: '..column..'. 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 not is_integer(trim(column)) then
				return error('Invalid remove_underscores parameter: Non-numeral column number: '..column..'. Will be ignored.')
			end
		end
	end
	if args.hide then
		local columns = str_split(args.hide, ',')
		for _, column in ipairs(columns) do
			if not is_integer(trim(column)) then
				return error('Invalid hide parameter: Non-numeral column number: '..column..'. Will be ignored.')
			end
		end
	end
	if args.pagination and not is_integer(args.pagination) then 
		return error('pagination should be an integer. Will be ignored.')
	end 
	if args.max_pages and not is_integer(args.max_pages) then
		return error('max_pages should be an integer. Will be ignored.')
	end 
	if args.frequency and not is_integer(args.frequency) then
		return error('frequency should be an integer. Will be ignored.')
	end 
	
	return ''
end

-- Called from {{Database report/footer}} and {{Database report/subpage}} 
function p.navlinks(frame)
	local args = getArgs(frame)
	
	if args.page then
		local page = tonumber(args.page)
		local num_pages = tonumber(args.num_pages)
		
		local title = mw.title.getCurrentTitle()
		
		local nextPage = title.nsText..':'..title.baseText..'/'..(page + 1)
		local prevPage = title.nsText..':'..title.baseText..'/'..(page - 1)
		-- page 1 has no subpage name 
		if page == 1 then 
			nextPage = title.nsText..':'..title.text..'/2'
		elseif page == 2 then
			prevPage = title.nsText..':'..title.baseText 
		end 
		
		local prevPageText = '<< Previous page'
		local nextPageText = 'Next page >>'
		
		local prevPageLink = (page == 1 or page > num_pages + 1) and '<span style="color:grey">'..prevPageText..'</span>'
			or '[['..prevPage..'|'..prevPageText..']]'
		local nextPageLink = page >= num_pages and '<span style="color:grey">'..nextPageText..'</span>'
			or '[['..nextPage..'|'..nextPageText..']]'
		
		return prevPageLink..' || '..nextPageLink
	end
	return ''
end

return p