Module:InfoboxParamCheck: Difference between revisions

From SW420
Jump to navigation Jump to search
No edit summary
Removing the hack done in order to find infoboxes with missing images. It worked by, if an image was set in the 'image' field of the infobox, checking if the File existed. If not, it would repeat it out into the body of the article so that it could be found via the WantedFiles special page.
 
(13 intermediate revisions by the same user not shown)
Line 5: Line 5:
-- category "%s" when the page is saved.
-- category "%s" when the page is saved.
return string.format('[[%s:%s]]', 'Category', cat)
return string.format('[[%s:%s]]', 'Category', cat)
end
local function makeImageLink(img)
-- This is to allow images missing from infoboxes to be seen by MissingFiles.
-- We're forcing any missing images to get added plainly onto the page.
return string.format('[[%s:%s]]', 'File', img)
end
end


Line 21: Line 15:
     local extra = false
     local extra = false
     local arglist = {}
     local arglist = {}
     local imagearg = nil
     local parentargs = frame:getParent().args
     for _, k in ipairs(frame.args) do
     for _, k in ipairs(frame.args) do
         arglist[k] = true
         arglist[k] = true
Line 37: Line 31:
         end
         end
     end
     end
     for k, v in pairs(frame:getparent().args) do
     for k, v in pairs(parentargs ) do
         if not arglist[k] and not optional[k] then
         if not arglist[k] and not optional[k] then
             extra = true
             extra = true
         elseif empty and not ignore[k] and #v > 0 then
         elseif empty and not ignore[k] and #v > 0 then
             empty = false
             empty = false
        end
        if string.match(arglist[k],image) then
            imagearg = frame:getparent().args[k]
         end
         end
         arglist[k] = nil
         arglist[k] = nil
Line 54: Line 45:
     end
     end
     local ret = {''}
     local ret = {''}
    if imagearg then
        ret[#ret + 1] = makeImageLink(tostring(imagearg))
    end
     if missing then
     if missing then
         ret[#ret + 1] = makeCategoryLink('Infoboxes with missing parameters')
         ret[#ret + 1] = makeCategoryLink('Infoboxes with missing parameters')

Latest revision as of 04:32, 21 February 2023

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

function p.main(frame)
    if mw.title.getCurrentTitle().namespace ~= 0 then
        return ''
    end
    local missing = false
    local empty = true
    local extra = false
    local arglist = {}
    local parentargs = frame:getParent().args
    for _, k in ipairs(frame.args) do
        arglist[k] = true
    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
        end
    end
    for k, v in pairs(parentargs ) do
        if not arglist[k] and not optional[k] then
            extra = true
        elseif empty and not ignore[k] and #v > 0 then
            empty = false
        end
        arglist[k] = nil
    end
    if next(arglist) then
        -- nil if the table is empty, or a string key
        -- that evaluates to true if not empty
        missing = true
    end
    local ret = {''}
    if missing then
        ret[#ret + 1] = makeCategoryLink('Infoboxes with missing parameters')
    end
    if empty then
        ret[#ret + 1] = makeCategoryLink('Articles with empty infoboxes')
    end
    if extra then
        ret[#ret + 1] = makeCategoryLink('Infoboxes with unrecognized parameters')
    end
    return table.concat(ret)
end

return p