Module:BlockMessages

From The Satanic Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:BlockMessages/doc

--| Module:BlockMessages (v2.2.0)
--by "The JoTS"

-- v3.0 (Planned) - Drop requirement for sub'd ":" for ":"
-- v2.2 - Support for message headers and footers
-- v2.1 - Preparation function for MediaWiki:Ipbreason-dropdown
-- v2.0 - Now only accepts named parameters. (To avoid errors with "=")

--- This module is meant to be used in conjunction to [[MessageBlock]] and
--- allows for customized block messages for every offense listed in the
--- Ipbreason-dropdown system message.

-- <nowiki>

-- [==[ Module Start ]==] ------
local bm = {}

function bm.main(frame)
    local reasons  = setmetatable({}, {
        __index = function(t,i)
            return rawget(t, "__DEFAULT__")
                or {"You have been banned for $2 because ", i, ":$1" }
        end
    })
    
    do local _reasons, currReason
        -- Get messages
        _reasons = mw.message.new("Custom-Ipbreason-messages")
        
        assert(_reasons:exists(),
            "MediaWiki:Custom-Ipbreason-messages does not exist.")
    
        -- Parse messages
        for type, msg in
                (tostring(_reasons)..'\n'):gmatch("([;:])(.-)\n") do
            if type == ";" then
                currReason = {}
                reasons[msg] = currReason
            elseif type == ":" and currReason then
                table.insert(currReason, msg)
            end
        end
    end
    
    -- Get selected message
    local args = frame.args
    local offense, notes = args["reason"]:match("^(.-):%s*(.+)")
    offense = offense or args["reason"]
    
    return frame:preprocess(mw.text.trim(
        string.gsub(
            table.concat(rawget(reasons, "__HEADER__") or {}, '\n') ..
            table.concat(reasons[offense], '\n') ..
            table.concat(rawget(reasons, "__FOOTER__") or {}, '\n'),
            "$%d", {
                    ["$1"] = notes or '',
                    ["$2"] = args["expiry"]
            }
        )
    ))
end

function bm.prep(frame)
    local page = mw.title.getCurrentTitle()
    local content = page:getContent()
    
    if page.fullText ~= "MediaWiki:Ipbreason-dropdown" then
        return content
    end
    
    return ( content:gsub(':', "&#58;") )
end

return bm