Module:Devmodule/sandbox

--

-- Invokes global modules from dev.wikia.com -- Syntax: --

local p = {}

local checkType = require("libraryUtil").checkType local getArgs = require("Dev:Arguments").getArgs local userError = require("Dev:User error")

-- Frame methods are protected by a `checkSelf` function, which makes them more -- difficult to copy.

local function cloneFrame(frame) local clone = mw.clone(frame)

-- Point methods on `clone` to their `frame` counterparts for methodName in pairs(frame) do       local method = frame[methodName]

local function callFrameMethod(clone, ...) if methodName ~= "getParent" then return method(frame, ...) end

local parent = frame:getParent

if parent then return cloneFrame(parent) end end

if type(method) == "function" then clone[methodName] = callFrameMethod end end

return clone end

-- Creates a frame-like object, with all Devmodule-exclusive arguments removed.

local function makePseudoFrame(frame) local pseudoFrame = cloneFrame(frame) local metatable = getmetatable(pseudoFrame.args)

metatable.__index = nil metatable.__pairs = nil

pseudoFrame.args["#modulename"] = nil pseudoFrame.args["#mod"] = nil pseudoFrame.args["#fname"] = nil

return pseudoFrame end

-- Takes the name of a module, removes any prefixes, and makes it -- case-insensitive.

local function normalize(name) checkType("normalize", 1, name, "string")

local ustring = mw.ustring local uLower = ustring.lower local uSub = ustring.sub

-- When loading a module, these prefixes will work: local prefixes = { "Dev",                      -- added by Wikia "Module",                   -- canonical namespace name mw.site.namespaces[828].name -- namespace name in your language }

-- Reduce iterations when the canonical and local namespace names are equal. -- I.e.: avoid checking the same thing twice. if prefixes[2] == prefixes[3] then prefixes[3] = nil end

for _, prefix in ipairs(prefixes) do       prefix = uLower(prefix) .. ":"

local prefixLength = ustring.len(prefix) local lowerName = uLower(name)

-- If `name` starts with a prefix, remove it       if uSub(lowerName, 1, prefixLength) == prefix then name = uSub(name, prefixLength + 1)

-- Only remove the first prefix, e.g.: -- "Dev:Module:Foo" => "Module:Foo" break end end

return mw.getContentLanguage:ucfirst(name) end

-- Describe the following function here.

function p.main(frame) checkType("Dev:Devmodule.main", 1, frame, "table")

local args = getArgs(frame) local moduleName = args["#mod"] or args["#modulename"] local functionName = args["#fname"] or "main" local pseudoFrame = makePseudoFrame(frame)

-- error handling local _, response = xpcall(       function             moduleName = normalize(modulename)

return require("Dev:" .. moduleName)[functionName](pseudoFrame) end,

function (err) local message

if err:find("bad argument #1 to 'normalize'") then message = "missing or invalid `#mod` parameter (should be the name of a module on dev.wikia)" elseif err:find("module `Dev:" .. moduleName .. "' not found") then message = "module `" .. moduleName .. "` does not exist on dev.wikia" end

-- @TODO uncomment once the QUESTION MARK BUG is solved. --           if err:find("attempt to call field '" .. functionName .. "' (a nil value)") then                message = "module `" .. moduleName .. "` does not have a function called `" .. functionName "`"            end            --

if not message then return err end

return userError(message) end )

return response end

return p

-- -- (Add categories here.)