Module:Protection banner: Difference between revisions

    From Nonbinary Wiki
    (add no globals now, to catch any problems sooner rather than later)
    (export classes for testing purposes and a few other tweaks)
    Line 88: Line 88:


    function Config:getConfigTable(key)
    function Config:getConfigTable(key)
    local whitelisted = {
    local blacklist = {
    images = true,
    banners = true,
    categories = true,
    defaultBanners = true
    categoryNamespaces = true,
    pagetypeNamespaces = true,
    errorCategories = true
    }
    }
    if whitelisted[key] then
    if not blacklist[key] then
    return self._cfg[key]
    return self._cfg[key]
    else
    else
    Line 187: Line 184:
    local ProtectionBanner = {}
    local ProtectionBanner = {}


    function ProtectionBanner._newBannerTemplate(args)
    function ProtectionBanner.exportToWiki(frame, title)
    -- Makes a new banner template object. This will be a new instance of
    -- the most suitable subclass of BannerTemplate, e.g. Banner or Padlock.
    if yesno(args.small) then
    return Padlock:new(args)
    else
    return Banner:new(args)
    end
    end
     
    function ProtectionBanner.exportToWiki(frame, titleObj)
    local args = mArguments.getArgs(frame)
    local args = mArguments.getArgs(frame)
    return ProtectionBanner.exportToLua(args, titleObj)
    return ProtectionBanner.exportToLua(args, title)
    end
    end


    function ProtectionBanner.exportToLua(args, title)
    function ProtectionBanner.exportToLua(args, title)
    title = title or mw.title.getCurrentTitle()
    title = title or mw.title.getCurrentTitle()
    local pstatus = ProtectionStatus.new(args, title)
    local cfg = Config:new()
    -- Get the banner template object
    local banner
    do
    local bannerClass
    if yesno(args.small) then
    bannerClass = Padlock
    else
    bannerClass = Banner
    end
    banner = bannerClass:new()
    end
    end
    function ProtectionBanner._exportClasses()
    return {
    ProtectionStatus = ProtectionStatus,
    Config = Config,
    Image = Image,
    Blurb = Blurb,
    BannerTemplate = BannerTemplate,
    Banner = Banner,
    Padlock = Padlock,
    Category = Category,
    ProtectionCategory = ProtectionCategory,
    ErrorCategory = ErrorCategory,
    ExpiryCategory = ExpiryCategory
    }
    end
    end


    return ProtectionBanner
    return ProtectionBanner

    Revision as of 16:17, 8 June 2014

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

    -- This module implements {{pp-meta}} and its daughter templates such as
    -- {{pp-dispute}}, {{pp-vandalism}} and {{pp-sock}}.
    
    -- Initialise necessary modules.
    require('Module:No globals')
    local class = require('Module:Middleclass').class
    local mArguments = require('Module:Arguments')
    local mFileLink = require('Module:File link')
    local mProtectionLevel = require('Module:Effective protection level')
    local yesno = require('Module:Yesno')
    
    --------------------------------------------------------------------------------
    -- ProtectionStatus class
    --------------------------------------------------------------------------------
    
    local ProtectionStatus = class('ProtectionStatus')
    
    function ProtectionStatus:initialize(args, titleObj)
    	-- Set action
    	do
    		local actions = {
    			create = true,
    			edit = true,
    			move = true,
    			autoreview = true
    		}
    		if args.action and actions[args.action] then
    			self._action = args.action
    		else
    			self._action = 'edit'
    		end
    	end
    
    	-- Set level
    	do
    		local level = mProtectionLevel._main(self._action, titleObj)
    		if level == 'accountcreator' then
    			-- Lump titleblacklisted pages in with template-protected pages,
    			-- since templateeditors can do both.
    			level = 'templateeditor'
    		end
    		self._level = level or '*'
    	end
    
    	-- Set reason
    	self._reason = args.reason
    
    	-- Set expiry
    	self._expiry = args.expiry
    end
    
    function ProtectionStatus:getAction()
    	return self._action
    end
    
    function ProtectionStatus:getLevel()
    	return self._level
    end
    
    function ProtectionStatus:getReason()
    	return self._reason
    end
    
    function ProtectionStatus:getExpiry()
    	return self._expiry
    end
    
    --------------------------------------------------------------------------------
    -- Config class
    --------------------------------------------------------------------------------
    
    local Config = class('Config')
    
    function Config:initialize()
    	self._cfg = mw.loadData('Module:Protection banner/config')
    end
    
    function Config:getBannerConfig(protectionStatusObj)
    	local cfg = self._cfg
    	local action = protectionStatusObj:getAction()
    	local reason = protectionStatusObj:getReason()
    	if cfg.banners[action][reason] then
    		return cfg.banners[action][reason]
    	else
    		return cfg.defaultBanners[action]
    	end
    end
    
    function Config:getConfigTable(key)
    	local blacklist = {
    		banners = true,
    		defaultBanners = true
    	}
    	if not blacklist[key] then
    		return self._cfg[key]
    	else
    		return nil
    	end
    end
    
    --------------------------------------------------------------------------------
    -- Image class
    --------------------------------------------------------------------------------
    
    local Image = class('Image')
    
    --------------------------------------------------------------------------------
    -- Blurb class
    --------------------------------------------------------------------------------
    
    local Blurb = class('Blurb')
    
    function Blurb:initialize(bannerConfig)
    	self._config = bannerConfig
    end
    
    --------------------------------------------------------------------------------
    -- BannerTemplate class
    --------------------------------------------------------------------------------
    
    local BannerTemplate = class('BannerTemplate')
    
    function BannerTemplate:initialize()
    end
    
    function BannerTemplate:render()
    end
    
    --------------------------------------------------------------------------------
    -- Banner class
    --------------------------------------------------------------------------------
    
    local Banner = BannerTemplate:subclass('Banner')
    
    --------------------------------------------------------------------------------
    -- Padlock class
    --------------------------------------------------------------------------------
    
    local Padlock = BannerTemplate:subclass('Padlock')
    
    --------------------------------------------------------------------------------
    -- Category class
    --------------------------------------------------------------------------------
    
    local Category = class('Category')
    
    function Category:initialize()
    end
    
    function Category:export()
    	if self._categoryName then
    		return string.format(
    			'[[%s:%s]]',
    			mw.site.namespaces[14].name,
    			self._categoryName
    		)
    	else
    		return ''
    	end
    end
    
    --------------------------------------------------------------------------------
    -- ProtectionCategory class
    --------------------------------------------------------------------------------
    
    local ProtectionCategory = Category:subclass('ProtectionCategory')
    
    --------------------------------------------------------------------------------
    -- ErrorCategory class
    --------------------------------------------------------------------------------
    
    local ErrorCategory = Category:subclass('ErrorCategory')
    
    --------------------------------------------------------------------------------
    -- ExpiryCategory class
    --------------------------------------------------------------------------------
    
    local ExpiryCategory = Category:subclass('ExpiryCategory')
    
    --------------------------------------------------------------------------------
    -- ProtectionBanner class
    --------------------------------------------------------------------------------
    
    local ProtectionBanner = {}
    
    function ProtectionBanner.exportToWiki(frame, title)
    	local args = mArguments.getArgs(frame)
    	return ProtectionBanner.exportToLua(args, title)
    end
    
    function ProtectionBanner.exportToLua(args, title)
    	title = title or mw.title.getCurrentTitle()
    	local pstatus = ProtectionStatus.new(args, title)
    	local cfg = Config:new()
    
    	-- Get the banner template object
    	local banner
    	do
    		local bannerClass
    		if yesno(args.small) then
    			bannerClass = Padlock
    		else
    			bannerClass = Banner
    		end
    		banner = bannerClass:new()
    	end
    end
    
    function ProtectionBanner._exportClasses()
    	return {
    		ProtectionStatus = ProtectionStatus,
    		Config = Config,
    		Image = Image,
    		Blurb = Blurb,
    		BannerTemplate = BannerTemplate,
    		Banner = Banner,
    		Padlock = Padlock,
    		Category = Category,
    		ProtectionCategory = ProtectionCategory,
    		ErrorCategory = ErrorCategory,
    		ExpiryCategory = ExpiryCategory
    	}
    end
    
    return ProtectionBanner