Changes

MyWikiBiz, Author Your Legacy — Wednesday May 22, 2024
Jump to navigationJump to search
Pywikibot 6.4.0
-------------------------------------------------------------------------------
-- 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

Navigation menu