Module:WatchlistNotice

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

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

Code

local p = {}

-- Powers [[Template:WatchlistNotice]]
-- =p.noteStyle({ args={ languages="en:es:de", userlang="de-formal", ignoreOn="Abc" } })
-- =p.noteStyle({ args={ languages="en:es:de", userlang="notinlist", ignoreOn="Abc" } })
function p.noteStyle(frame)
    local args = frame.args
    local dNow, dFrom, dUntil = tonumber(args.now), tonumber(args.from), tonumber(args['until'])
    local onPage, ignoreOn = args.onPage, args.ignoreOn
    local groups, condition = args.groups, mw.text.trim(args.condition)
    local langs, userLang = args.languages, args.userlang
    local setNone = function(important)
        if important then
            important = "!important"
        else
            important = ""
        end
        return ';display:none' .. important .. ";";
    end
    
    if onPage == ignoreOn then
        return ''
    end
    
    if dFrom then
        if dFrom > dNow then
            return setNone(true) -- current date is smaller than the start date -> don't show note yet
        end
    end
    if dUntil then
        if dNow > dUntil then
            return setNone(true) -- note expired
        end
    end
    
    if #condition then
    	if '0' == condition then
    		return setNone(true) -- custom condition not fulfilled
    	end
    end

    if langs and #langs > 0 then
        langs = (':' .. mw.text.trim( langs ) .. ':'):lower()
        userLang = userLang:lower()
        local userLangBase = ":" .. userLang:match( '[^%-]+' ) .. ":"
        local userLangFull = ":" .. userLang .. ":"
        if not langs:find( userLangFull, 1, true ) and not langs:find( userLangBase, 1, true ) then
            return setNone(true) -- user language not in supplied list
        end
    end

    --Must be processed as last
    if groups and #groups > 0 then
        return setNone()
    end
end

return p