Wookieepedia

READ MORE

Wookieepedia
Advertisement
Wookieepedia

Documentation for this module may be created at Module:InfoboxParamCheck/doc

local p = {}

local function makeCategoryLink(cat)
	-- "Category" is split out here so that the module isn't put into the
	-- category "%s" when the page is saved.
	return string.format('[[%s:%s]]', 'Category', cat)
end

local function buildWarning(s)
	return string.format('<span style="color: red">\'\'\'Warning: %s\'\'\'</span>\n\n', s)
end

local function concatKeys(map)
	local s = ''
	for k, v in pairs(map) do
		if s == '' then
			s = '' .. k
		else 
			s = s .. ', ' .. k
		end
	end
	return s
end

function p.main(frame)
    if mw.title.getCurrentTitle().namespace ~= 0 then
        return ''
    end
    local missing = {}
    local comboMap = {}
    local empty = false
    local extra = {}
    local unnamed = 0
    for _, k in ipairs(frame.args) do
        missing[k] = true
        if k:find(':') then
        	for s in k:gmatch('[^%:]+') do
        		comboMap[s] = k
        	end
        end
    end
    local ignore = {}
    if frame.args.ignore then
        for k in frame.args.ignore:gmatch('[^%,]+') do
            ignore[k] = true
        end
    end
    local optional = {}
    if frame.args.optional then
        for k in frame.args.optional:gmatch('[^%,]+') do
            optional[k] = true
        	if k:find(':') then
        		for s in k:gmatch('[^%:]+') do
        			comboMap[s] = k
        		end
        	end
        end
    end
    for k, v in pairs(frame:getParent().args) do
    	if type(k) == "number" then
    		unnamed = unnamed + 1
    	elseif comboMap[k] then
        	if not missing[comboMap[k]] and not missing[k] and not optional[comboMap[k]] then
        		extra[k] = true
        	elseif empty and not ignore[comboMap[k]] and not ignore[k] and #v > 0 then
        		empty = false
        	end
        	missing[comboMap[k]] = nil
        else
        	if not missing[k] and not optional[k] then
            	extra[k] = true
	        elseif empty and not ignore[k] and #v > 0 then
	            empty = false
	        end
			missing[k] = nil
        end
    end
	
	local missingParams = concatKeys(missing)
	local extraParams = concatKeys(extra)
    local ret = {''}
    if #missingParams > 0 then
        ret[#ret + 1] = makeCategoryLink('Infoboxes with missing parameters')
    end
    if empty then
        ret[#ret + 1] = makeCategoryLink('Articles with empty infoboxes') 
    end
    if #extraParams > 0 then
        ret[#ret + 1] = makeCategoryLink('Infoboxes with unrecognized parameters')
    end
	
	if #extraParams > 0 and #missingParams > 0 then
		ret[#ret + 1] = buildWarning('This infobox has missing parameters: ' .. missingParams .. ' and unrecognized parameters: ' .. extraParams)
	elseif #missingParams > 0 then
		ret[#ret + 1] = buildWarning('This infobox is missing the following parameters: ' .. missingParams)
	elseif #extraParams > 0 then
		ret[#ret + 1] = buildWarning('The following parameters in the infobox are unrecognized: ' .. extraParams)
	end
    return table.concat(ret)
end

return p
Advertisement