Module:Taxon page control

From HopperWiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Taxon page control/doc

local cargo = mw.ext.cargo
local getArgs = require('Module:Arguments').getArgs -- for processing arguments
local encode_json = mw.text.jsonEncode -- use this to output tables to see there format
local cf = require("Module:Custom_functions")
local ibf = require("Module:Infobox_functions")



local p = {}



function p.sci_needs_redirect()
    -- Query the Species Cargo table to get all Species values.
    local tables = "Species"
    local fields = "_pageName, Species"
    local cargo_args = {
            limit = 10000
        }
    local cargo_results = cargo.query(tables, fields, cargo_args)

    -- Extract species values from the table
    local cargo_species = {}
    for _, entry in ipairs(cargo_results) do
        local species = entry["Species"]
        if species then
            table.insert(cargo_species, species)
        end
    end
    
    -- Remove duplicates
    local hash = {}
    local cargo_unique_species = {}
    for _, v in ipairs(cargo_species) do
        if (not hash[v]) then
            table.insert(cargo_unique_species, v)
            hash[v] = true
        end
    end
    
    -- Get all pages in the wiki that are in the Species category using DPL
    local species_pages = mw.ext.dpl.getPagenames{namespace = "Main", category = "Species"}
    
    -- Check for missing pages
    missing_pages = cf.in_a_not_b(cargo_unique_species, species_pages)
    function contains(array, target)
	    for _, value in ipairs(array) do
	        if value == target then
	            return true
	        end
	    end
	    return false
    end
    
    -- return contains(species_pages, "Acorypha glaucopsis")
    -- return table.concat(cargo_unique_species, "\n\n")
    
    if #missing_pages > 0 then
        local output = {}
        for _, v in ipairs(missing_pages) do
            table.insert(output, "*[[" .. v .. "]]")
        end
        -- Return output as a bulleted list
        return table.concat(output, "\n")
        
    else
        return "All necessary species pages seem to be created correctly."
    end




end



return p