FANDOM


-- <nowiki>
--------------------------------------------------------------------------------
-- Functions relating to Wikia's module system.
--------------------------------------------------------------------------------
local p = {}
 
-- Load dependencies
local getArgs = require("Dev:Arguments").getArgs
local normalize = require("Dev:NormalizeModuleName")
local userError = require("Dev:User error")
local removeArgs = require("Dev:FrameTools").removeArgs
local checkType = require("libraryUtil").checkType
 
--------------------------------------------------------------------------------
-- Invokes global modules from dev.wikia.com.
--
-- Syntax:
-- {{#invoke:Package|from_dev|<parameters>|#mod=<module-name>|#fun=<function-name>}}
--------------------------------------------------------------------------------
function p.from_dev(frame)
    local args = getArgs(frame)
    local moduleName = args["#mod"] or args["#modulename"]
    local functionName = args["#fun"] or args["#fname"] or "main"
    local success, _, invokedModule
 
    -- try to `normalize()` the given module name
    -- if this fails, then the user did not pass the right parameters
    success, _, moduleName = pcall(normalize, moduleName)
 
    if not success then
        return userError("no `#mod` parameter given " ..
                         "(should be the name of a module on dev.wikia)")
    end
 
    -- try to `require()` the given module
    -- if this fails, then the user gave an incorrect module name
    success, invokedModule = pcall(require, "Dev:" .. moduleName)
 
    if not success then
        return userError("could not find a module called `" .. moduleName ..
                         "` on dev.wikia")
    end
 
    -- make sure the module has a function called `functionName`
    -- if this fails, then the user gave an incorrect function name
    local func = invokedModule[functionName]
 
    if not func then
        return userError("that module does not have a function called `" ..
                         functionName .. "`")
    end
 
    -- create a frame-like object, without the Devmodule-exclusive arguments
    local pseudoFrame = removeArgs(frame, "#mod", "#modulename", "#fun",
                                   "#fname")
 
    -- call the function
    -- any errors from the function itself will be passed to the user
    return func(pseudoFrame)
end
 
--------------------------------------------------------------------------------
-- Determines if a module exists by checking if it has or can be loaded.
--------------------------------------------------------------------------------
function p._exists(name, foundVal, missingVal)
    checkType("Dev:Existsmod", 1, name, "string", true)
 
    if name then
        name = table.concat{normalize(name)}
 
        if package.loaded[name] then
            return foundVal
        else
            local _, val = pcall(package.loaders[2], name)
 
            if type(val) == "function" or type(val) == "string" then
                return foundVal 
            end
        end
    end
 
    return missingVal
end
 
--------------------------------------------------------------------------------
-- An inexpensive alternative to {{#ifexist}} which looks for a given module,
-- and returns one of two values depending on whether it was found.
--
-- Syntax:
-- {{#invoke:Package|exists|<module-name>|<value-if-found>|<value-if-missing>}}
--------------------------------------------------------------------------------
function p.exists(frame)
    local args = getArgs(frame)
 
    return p.module_exists(args[1], args[2], args[3])
end
 
return p
 
-- </nowiki>
-- (Add categories here.)

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.