Module:Message box: Difference between revisions

rewrite with a "box" object to make the code a little less spaghetti-like
m>Mr. Stradivarius
(pass nocat, page, and demospace parameters to Module:Category handler, and add a choice for which arguments to allow as blank)
m>Mr. Stradivarius
(rewrite with a "box" object to make the code a little less spaghetti-like)
Line 14: Line 14:
local tconcat = table.concat
local tconcat = table.concat


local p = {}
local box = {}


local function getTitleObject(page)
local function getTitleObject(page)
Line 34: Line 34:
         return false
         return false
     end
     end
end
local function formatCategory(cat, date, all)
    local ret = {}
    cat = type(cat) == 'string' and cat
    date = type(date) == 'string' and date
    all = type(all) == 'string' and all
    local preposition = 'from'
    if cat and date then
        local catTitle = format('Category:%s %s %s', cat, preposition, date)
        tinsert(ret, format('[[%s]]', catTitle))
        catTitle = getTitleObject(catTitle)
        if not catTitle or not catTitle.exists then
            tinsert(ret, '[[Category:Articles with invalid date parameter in template]]')
        end
    elseif cat and not date then
        tinsert(ret, format('[[Category:%s]]', cat))
    end
    if all then
        tinsert(ret, format('[[Category:%s]]', all))
    end
    return tconcat(ret)
end
end


Line 87: Line 65:
end
end


local function getNamespaceId(ns)
function box.getNamespaceId(ns)
     if not ns then return end
     if not ns then return end
     if type(ns) == 'string' then
     if type(ns) == 'string' then
Line 101: Line 79:
end
end


local function getMboxType(nsid)
function box.getMboxType(nsid)
     -- Gets the mbox type from a namespace number.
     -- Gets the mbox type from a namespace number.
     if nsid == 0 then
     if nsid == 0 then
Line 119: Line 97:
end
end


function p.build(boxType, args)
function box:addCat(ns, cat, sort)
     if type(args) ~= 'table' then
     if type(cat) ~= 'string' then return end
        error(format('invalid "args" parameter type; expected type "table", got type "%s"', type(args)), 2)
    local nsVals = {'main', 'template', 'all'}
    local tname
    for i, val in ipairs(nsVals) do
        if ns == val then
            tname = ns .. 'Cats'
        end
     end
     end
    if not tname then
        for i, val in ipairs(nsVals) do
            nsVals[i] = format('"%s"', val)
        end
        error('invalid ns parameter passed to box:addCat; valid values are ' .. mw.text.listToText(nsVals, nil, ' or '))
    end
    self[tname] = self[tname] or {}
    if type(sort) == 'string' then
        tinsert(self[tname], format('[[Category:%s|%s]]', cat, sort))
    else
        tinsert(self[tname], format('[[Category:%s]]', cat))
    end
end


function box:addClass(class)
    if type(class) ~= 'string' then return end
    self.classes = self.classes or {}
    tinsert(self.classes, class)
end
function box:setTitle(args)
     -- Get the title object and the namespace.
     -- Get the title object and the namespace.
     local pageTitle = getTitleObject(args.page ~= '' and args.page)
     local pageTitle = getTitleObject(args.page ~= '' and args.page)
     local title = pageTitle or mw.title.getCurrentTitle()
     self.title = pageTitle or mw.title.getCurrentTitle()
     local demospace = getNamespaceId(args.demospace ~= '' and args.demospace)
     local demospace = box.getNamespaceId(args.demospace ~= '' and args.demospace)
     local nsid = demospace or title.namespace
     self.nsid = demospace or self.title.namespace
end