This module provides the Context class for Module:WikiProjectBanner. It stores data about the title we are being called from, provides access to the config and banner config modules, generates data commonly used by subclasses (e.g. project name), and provides a place to store data generated by subclasses that needs to be reused (e.g. Grade objects). This data is separate from the Banner class so that we can accurately generate documentation and JSON data about a banner without having to have a banner object available.
UsageEdit
Load the Context class:
local Context = require('Module:WikiProjectBanner/Context')
Once the class is loaded, initialise a context object:
local contextObj = Context.new(bannerName, args, cfg, bannerCfg)
- bannerName is the name of the banner template we were called from, minus the "Template:" prefix. Often this is the full project name, e.g. "WikiProject Tulips". This parameter is required.
- args is a table representing the arguments passed to the banner template by end users. This is optional.
- cfg is a config module to use instead of the default module. This is optional, and usually only used for testing.
- bannerCfg is a banner config module to use instead of the one at Module:WikiProjectBanner/banners/bannerName. This is optional, and usually only used for testing.
PropertiesEdit
Context objects have several properties.
Arguments
- args - the arguments that end users pass to banner templates, represented as a Lua table.
Configuration tables:
- cfg - the configuration table for all WikiProject banners.
- bannerCfg - the configuration table for the specific banner being generated.
Title data:
- currentTitle - the Scribunto title object for the page currently being displayed.
- subjectTitle - the Scribunto title object for the subject page of the page currently being displayed. For example, if the page currently being viewed was "Talk:Tulips", the subject page would be "Tulips".
Project data:
- bannerName - the name of the banner being generated, e.g. "WikiProject Tulips".
- project - the project name, e.g. "Tulips".
- projectLink - a link to the project page, without square brackets, e.g. "Wikipedia:WikiProject Tulips".
- projectName - the name of the project, without a namespace prefix, e.g. "WikiProject Tulips". This may not be the same as bannerName.
- projectScope - a link to an article defining the project's scope, *with* square brackets, e.g. "[[Tulips]]".
- projectLinkTalk - a link to the project talk page, without square brackets, e.g. "Wikipedia talk:WikiProject Tulips".
- assessmentLink - the page containing the project's assessment scales, without square brackets, e.g. "Wikipedia:WikiProject Tulips/Assessment".
Display data:
- isSmall - whether we are outputting a small banner. (boolean)
- isDemo - whether we are outputting a demonstration banner. (boolean)
-------------------------------------------------------------------------------
-- Context class --
-- This module contains the Context class used in Module:WikiProjectBanner. --
-- It stores data about the title we are being called from, provides access --
-- to the config and banner config modules, generates data commonly used by --
-- subclasses (e.g. project name), and provides a place to store data --
-- generated by subclasses that needs to be reused (e.g. Grade objects) --
-------------------------------------------------------------------------------
-- Load required modules
local mShared = require('Module:WikiProjectBanner/shared')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
-- Lazily load modules we don't always need.
local yesno
local Context = {}
function Context.new(bannerName, args, cfg, bannerCfg)
local obj = {}
-- Check the input set default values.
checkType('Context.new', 1, project, 'string')
checkType('Context.new', 2, args, 'table', true)
checkType('Context.new', 3, cfg, 'table', true)
checkType('Context.new', 4, bannerCfg, 'table', true)
obj.bannerName = bannerName
obj.args = args or {}
obj.cfg = cfg or mw.loadData('Module:WikiProjectBanner/config')
obj.bannerCfg = bannerCfg or mShared.maybeRequire(
'Module:WikiProjectBanner/banners/' .. data.bannerName
)
if not obj.bannerCfg then
error(
'banner data page [[Module:WikiProjectBanner/banners/' ..
bannerName ..
']] does not exist',
0
)
end
-- Set properties that we will need every time or that are inexpensive.
obj.currentTitle = mw.title.getCurrentTitle()
obj.subjectTitle = obj.currentTitle.subjectPageTitle
-- Set up the metatable and define functions for properties to be
-- generated on the fly. These functions are used for properties that are
-- expensive to generate and that we might not need.
local propertyFuncs = {}
setmetatable(obj, {
__index = function (t, key)
local func = propertyFuncs[key]
if func then
local val = func()
obj[key] = val
return val
else
return Context[key]
end
end
})
function propertyFuncs.pageType()
return require('Module:Pagetype')._main{}
end
function propertyFuncs.project()
-- Gets the project name from the banner name.
-- For example, for "WikiProject Tulips", the project name would be
-- "Tulips".
if obj.bannerCfg.project then
return obj.bannerData.project
else
local pattern = obj.cfg.msg['project-page-name-pattern']
return mw.ustring.match(obj.bannerName, pattern) or obj.bannerName
end
end
function propertyFuncs.projectLink()
-- The project page, e.g. "Wikipedia:WikiProject Tulips".
if obj.bannerCfg.projectLink then
return obj.bannerCfg.projectLink
else
local msg = obj.cfg.msg['project-link']
return mShared.substituteParams(msg, obj.project)
end
end
function propertyFuncs.projectName()
-- The project name without a namespace prefix,
-- e.g. "WikiProject Tulips".
if obj.bannerCfg.projectName then
return obj.bannerCfg.projectName
else
local msg = obj.cfg.msg['project-name']
return mShared.substituteParams(msg, obj.project)
end
end
function propertyFuncs.projectScope()
-- A link to an article that encapsulates the project's scope,
-- e.g. "[[Tulips]]". The link will have square brackets added if they
-- are not already present.
local scope = obj.bannerCfg.projectScope
if scope and scope:find('^%[%[') then
return scope
elseif scope then
return mShared.makeWikilink(scope)
else
return mShared.makeWikilink(obj.project)
end
end
function propertyFuncs.projectLinkTalk()
-- A link to the project talk page, without square brackets, e.g.
-- "Wikipedia talk:WikiProject Tulips".
return mw.title.new(obj.projectLink).talkPageTitle.prefixedText
end
function propertyFuncs.assessmentLink()
-- The page containing the project's assessment scales.
if obj.bannerCfg.assessmentLink ~= nil then
-- Custom link or false for no link
return bannerCfg.assessmentLink
else
local assessmentPage = obj.projectLink .. '/Assessment'
local assessmentTitle = mw.title.new(assessmentPage)
return assessmentTitle
and assessmentTitle.exists
and assessmentTitle.prefixedText
or false
end
end
function propertyFuncs.isSmall()
-- Whether we are outputting a small banner or not.
yesno = yesno or require('Module:Yesno')
return yesno(obj.args.small)
end
function propertyFuncs.isDemo()
-- Whether we are outputting a demo page.
local currentPage = obj.currentTitle.prefixedText
local template = mw.site.namespaces[10].name .. ':' .. obj.bannerName
local sandboxSubpage = obj.cfg.msg['sandbox-subpage']
return currentPage == template
or currentPage == template .. '/' .. sandboxSubpage
end
return obj
end
return Context