FANDOM


-- <nowiki>
local p = {}
local getArgs = require("Dev:Arguments").getArgs
local userError = require("Dev:User error")
 
--------------------------------------------------------------------------------
-- HELPER FUNCTIONS
--------------------------------------------------------------------------------
 
--% Checks whether a given string is a valid bulleted list in wikitext.
--@ str (string) 
--: (boolean) 
local function isBulletedList(str)
    local tmp = mw.text.trim(str) -- Module:Arguments trims automatically, but
                                  -- DPL does not
 
    for line in mw.text.gsplit(tmp, "\n") do
        if not line:find("^*") then
            return false
        end
    end
 
    return true
end
 
--% Checks whether a given string is one of a list of valid options.
--@ str (string)
--@ values (table)
--: (boolean)
local function isOneOf(str, values)
    local tmp = mw.text.trim(str) -- Module:Arguments trims automatically, but
                                  -- DPL does not
 
    for _, value in ipairs(values) do
        if tmp:lower() == value:lower() then
            return true
        end
    end
 
    return false
end
 
--------------------------------------------------------------------------------
-- VALIDATION FUNCTIONS
--------------------------------------------------------------------------------
 
--% Checks whether a given string is a valid name.
--@ str (string)
--: (boolean)
function p.name(str)
    return type(str) == "string"
end
 
--% Checks whether a given string is a valid description.
--@ str (string) 
--: (boolean) 
function p.description(str)
    if type(str) ~= "string" then
        return false
    end
 
    return not str:find("\n") and true or false
end
 
--% Checks whether a given string is a valid list of categories.
--@ str (string) 
--: (boolean) 
function p.categories(str)
    local values = {
        "user tools",
        "page and file management",
        "site enhancements",
        "editing tools",
        "user management",
        "oasis skin enhancements",
        "chat",
        "site integration",
        "development tools",
        "inadvisable installations"
    }
 
    if type(str) ~= "string" or not isBulletedList(str) then
        return false
    end
 
    for line in mw.text.gsplit(str, "\n") do
        local tmp = line:sub(2)
 
        if not isOneOf(tmp, values) then
            return false
        end
    end
 
    return true
end
 
--% Checks whether a given string is a valid scope.
--@ str (string) 
--: (boolean) 
function p.scope(str)
    if type(str) ~= "string" then
        return false
    end
 
    return isOneOf(str, {
        "general",
        "personal",
        "site-wide"
    })
end
 
--% Checks whether a given string is a valid skins.
--@ str (string) 
--: (boolean) 
function p.skins(str)
    if type(str) ~= "string" then
        return false
    end
 
    return isOneOf(str, {
        "general",
        "monobook",
        "oasis"
    })
end
 
--% Checks whether a given string is a valid updated date.
--@ str (string)
--: (boolean)
function p.updated(str)
    local success, tmp = pcall(function ()
        return mw.language.new("en"):formatDate("Y-m-d", str)
    end)
 
    return (success == true) and (tmp == str)
end
 
--% Checks whether a given string is a valid list of authors.
--@ str (string) 
--: (boolean) 
function p.authors(str)
    if type(str) ~= "string" then
        return false
    end
 
    return isBulletedList(str)
end
 
--% Checks whether a given string is a valid list of languages.
--@ str (string) 
--: (boolean) 
function p.languages(str)
    if type(str) ~= "string" then
        return false
    end
 
    return isBulletedList(str)
end
 
--------------------------------------------------------------------------------
-- MAIN FUNCTION
--
-- This is invoked on [[Template:Infobox JavaScript/sandbox]].
--------------------------------------------------------------------------------
 
--% Describe the following function here.
--@ frame (table) The
--- [[Lua reference manual/Scribunto libraries#Frame object|frame object]].
--: (string) 
function p.main(frame)
    local args = getArgs(frame)
    local lang = args.uselang or "en"
    local i18n = {
        ["name"] = {
            en = "the <code>%s</code> parameter must be used"
        },
 
        ["description"] = {
            en = "the <code>%s</code> parameter must be a single paragraph"
        },
 
        ["categories"] = {
            en = "the <code>%s</code> parameter must be a bulleted list, " ..
                 "where each item is one of the following values " ..
                 "(case insensitive): " ..
                 "<code>user tools</code>, " ..
                 "<code>page and file management</code>, " ..
                 "<code>site enhancements</code>, " ..
                 "<code>editing tools</code>, " ..
                 "<code>user management</code>, " ..
                 "<code>oasis skin enhancements</code>, " ..
                 "<code>chat</code>, " ..
                 "<code>site integration</code>, " ..
                 "<code>development tools</code>, or " ..
                 "<code>inadvisable installations</code>"
        },
 
        ["scope"] = {
            en = "the <code>%s</code> parameter must be one of the " ..
                 "following values (case insensitive): " ..
                 "<code>general</code>, " ..
                 "<code>personal</code>, or " ..
                 "<code>site-wide</code>"
        },
 
        ["skins"] = {
            en = "the <code>%s</code> parameter must be one of the " ..
                 "following values (case insensitive): " ..
                 "<code>general</code>, " ..
                 "<code>monobook</code>, or " ..
                 "<code>oasis</code>"
        },
 
        ["updated"] = {
            en = "the <code>%s</code> parameter must be an " ..
                 "[[Wikipedia:ISO 8601|ISO 8601]] date " ..
                 "(<code>YYYY-MM-DD</code>)"
        },
 
        ["authors"] = {
            en = "the <code>%s</code> parameter must be a bulleted list"
        },
 
        ["languages"] = {
            en = "the <code>%s</code> parameter must be a bulleted list"
        }
    }
 
    for key, messages in pairs(i18n) do
        local message = (messages[lang] or messages.en):format(key)
        local category = "Improper use of Infobox JavaScript — invalid " .. key
 
        -- could be a problem if the param and method have different names
        if not p[key](args[key]) then
            return userError(message, category)
        end
    end
 
    return args[1]
end
 
return p
-- </nowiki>

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.