Fallout Wiki
Advertisement
Fallout Wiki

This module is primarily intended for validating fields of infoboxes, although it is also usable for automatically performing other tasks based on fields in infoboxes.

Currently, it contains a single function, validateFields, which will insert text where invoked based on checks performed on single fields. This is most obviously useful for automatically adding categories based on infobox fields, but this isn't the only type of text that can be inserted. As of right now, only a single field can be checked in a single way to determine whether to insert a particular piece of text. If more complex functionality is required in the future, it will be implemented on an as-needed basis.

There are currently four types of checks that can be performed: present, absent, equals, and contains. As many of these checks as desired can be performed with a single invoke, simply by appending each one after the previous one. A single check has either three or four arguments. The first is the type of check (present, absent, equals, or contains). The second is the name of the field. The last is the text to insert if the check evaluates to true. If the check is either equals or contains, a fourth argument immediately after the name of the field is required, which contains what you want to check the field either equals or contains.

Usage[]

Presence of field:

{{#invoke:Validation|validateFields|present|field name|text to be inserted}}

Absence of field:

{{#invoke:Validation|validateFields|absent|field name|text to be inserted}}

Field equals value:

{{#invoke:Validation|validateFields|equals|field name|text field value should be equal to|text to be inserted}}

Field contains value:

{{#invoke:Validation|validateFields|contains|field name|text field value should contain|text to be inserted}}

Multiple checks:

{{#invoke:Validation|validateFields|first check|args...|second check|args...|third check...}}

Example usage[]

Let's say we have an infobox where we have a deprecated field id, required field name, and field birthday. We want to add pages with the deprecated field and pages missing the required field to the relevant categories, and we want to add all instances of a particular birthday (September 13) to another category. We can take care of all of these at once with a single invoke in the infobox template after the </infobox> tag:

{{#invoke:Validation|validateFields|present|id|[[Category:Infoboxes with id field]]|absent|name|[[Category:Infoboxes without name field]]|equals|birthday|September 13|[[Category:September 13 birthdays]]}}

local p = {};
local str = require( "Module:String" )

function present(field, comp)
    return field ~= nil
end

function absent(field, comp)
    return field == nil
end

function equals(field, comp)
    return field ~= nil and field:lower() == comp:lower()
end

function contains(field, comp)
    return field ~= nil and string.find(field:lower(), comp:lower()) ~= nil
end

function categoryGeneration(baseCat, pageName, categoryType)
	return "[[Category:"..baseCat.." ("..pageName..")/"..categoryType.."]][[Category:"..baseCat.." ("..categoryType..")]]"
end

function check(field, comp, func, ret)
    if func(field, comp) then
    	if string.find(ret, "catgen") ~= nil then
    		if (str.split(ret,"!!",3) == nil) then 
    			return categoryGeneration("Template Fixes", str.split(ret,"!!",1), str.split(ret,"!!",2)) .. " " 
    		end
    		return categoryGeneration(str.split(ret,"!!",1), str.split(ret,"!!",2), str.split(ret,"!!",3)) .. " "
    	else
        	return ret .. " "
        end
    else
        return ""
    end
end

function handleErrors(funcName, func, comp, checkRet)
    missingComp = ((funcName == "equals") or (funcName == "contains")) and (comp == nil)
    if (missingComp) or (func == nil) or (checkRet == nil) then
        error("Invalid args passed to validateFields. Check [[Module:Validation/doc]] for proper usage.", 0)
    end
end

-- This function will insert text into an infobox based on the properties of a field.
-- Currently, this function can only look at one field for a given text insertion,
-- but it is possible to check multiple fields and insert text separately for each one.
function p.validateFields(frame)
    functions = {["present"]=present, ["absent"]=absent, ["equals"]=equals, ["contains"]=contains}
    infoboxArgs = frame:getParent().args
    args = frame.args
    i = 1
    ret = ""

    while (args[i] ~= nil) do
        inc = 3
        field = infoboxArgs[args[i + 1]]
        comp = nil
        func = functions[args[i]]
        checkRet = args[i + 2]

        if (args[i] == "equals") or (args[i] == "contains") then
            inc = 4
            comp = args[i + 2]
            checkRet = args[i + 3]
        end

        errRet, errMsg = pcall(handleErrors, args[i], func, comp, checkRet)
        if (not errRet) then
            return errMsg
        end

        ret = ret .. check(field, comp, func, checkRet)
        i = i + inc
    end

    return ret
end

return p
Advertisement