Difference between revisions of "Module:Category handler"

From Game Detectives Wiki
Jump to: navigation, search
(add blacklist check)
m (24 revisions imported)
 
(27 intermediate revisions by 9 users not shown)
Line 1: Line 1:
-- Configuration data.
+
--------------------------------------------------------------------------------
local cfg = {}
+
--                                                                            --
 +
--                              CATEGORY HANDLER                              --
 +
--                                                                            --
 +
--      This module implements the {{category handler}} template in Lua,      --
 +
--      with a few improvements: all namespaces and all namespace aliases    --
 +
--      are supported, and namespace names are detected automatically for    --
 +
--      the local wiki. This module requires [[Module:Namespace detect]]      --
 +
--      and [[Module:Yesno]] to be available on the local wiki. It can be    --
 +
--      configured for different wikis by altering the values in              --
 +
--      [[Module:Category handler/config]], and pages can be blacklisted      --
 +
--      from categorisation by using [[Module:Category handler/blacklist]].  --
 +
--                                                                            --
 +
--------------------------------------------------------------------------------
  
cfg.nocat = 'nocat'   
+
-- Load required modules
cfg.categories = 'categories'
+
local yesno = require('Module:Yesno')
cfg.subpage = 'subpage'
 
cfg.page = 'page'
 
cfg.category2 = 'category2'
 
cfg.all = 'all'
 
cfg.main = 'main'
 
cfg.other = 'other'
 
  
-- The categorisation blacklist. Pages that match Lua patterns in this
+
-- Lazily load things we don't always need
-- list will not be categorised unless the appropriate options are set.
+
local mShared, mappings
-- If the namespace name has a space in, it must be written with an
 
-- underscore, e.g. "Wikipedia_talk". Other parts of the title can have
 
-- either underscores or spaces.
 
cfg.blacklist = {
 
    '^Main Page$', -- don't categorise the main page.
 
   
 
    -- Don't categorise the following pages or their subpages.
 
    '^Wikipedia:Cascade%-protected items$',
 
    '^Wikipedia:Cascade%-protected items/.*$',
 
    '^User:UBX$', -- The userbox "template" space.
 
    '^User:UBX/.*$',
 
    '^User_talk:UBX$',
 
    '^User_talk:UBX/.*$',
 
   
 
    -- Don't categorise subpages of these pages, but allow
 
    -- categorisation of the base page.
 
    '^Wikipedia:Template messages/.*$',
 
   
 
    '/[aA]rchive' -- Don't categorise archives.
 
}
 
  
-- Module start.
 
 
local p = {}
 
local p = {}
local args = {}
 
  
-- Get the page object. This will return the page object for the page
+
--------------------------------------------------------------------------------
-- specified, or nil if there are errors in the title or if the
+
-- Helper functions
-- expensive function count has been exceeded.
+
--------------------------------------------------------------------------------
local function getPageObject()
+
 
    -- Get the title object for args.page if it is specified. Otherwise
+
local function trimWhitespace(s, removeBlanks)
    -- get the title object for the current page.
+
if type(s) ~= 'string' then
    if args[cfg.page] then
+
return s
        -- Get the page object, passing the function through pcall
+
end
        -- in case we are over the expensive function count limit.
+
s = s:match('^%s*(.-)%s*$')
        local noError, pageObject = pcall(mw.title.new, args[cfg.page])
+
if removeBlanks then
        if not noError then
+
if s ~= '' then
            return nil
+
return s
        else
+
else
            return pageObject
+
return nil
        end
+
end
    else
+
else
        return mw.title.getCurrentTitle()
+
return s
    end  
+
end
 
end
 
end
  
-- Find whether we need to return a category or not.
+
--------------------------------------------------------------------------------
local function needsCategory( pageObject )
+
-- CategoryHandler class
    if not pageObject then return end
+
--------------------------------------------------------------------------------
    if args[cfg.nocat] == 'true'
+
 
        or ( args[cfg.category2] and args[cfg.category2] ~= 'yes' )
+
local CategoryHandler = {}
        or ( args[cfg.subpage] == 'no' and pageObject.isSubpage )
+
CategoryHandler.__index = CategoryHandler
        or ( args[cfg.subpage] == 'only' and not pageObject.isSubpage ) then
+
 
        return false
+
function CategoryHandler.new(data, args)
    else
+
local obj = setmetatable({ _data = data, _args = args }, CategoryHandler)
        return true
+
    end
+
-- Set the title object
 +
do
 +
local pagename = obj:parameter('demopage')
 +
local success, titleObj
 +
if pagename then
 +
success, titleObj = pcall(mw.title.new, pagename)
 +
end
 +
if success and titleObj then
 +
obj.title = titleObj
 +
if titleObj == mw.title.getCurrentTitle() then
 +
obj._usesCurrentTitle = true
 +
end
 +
else
 +
obj.title = mw.title.getCurrentTitle()
 +
obj._usesCurrentTitle = true
 +
end
 +
end
 +
 
 +
-- Set suppression parameter values
 +
for _, key in ipairs{'nocat', 'categories'} do
 +
local value = obj:parameter(key)
 +
value = trimWhitespace(value, true)
 +
obj['_' .. key] = yesno(value)
 +
end
 +
do
 +
local subpage = obj:parameter('subpage')
 +
local category2 = obj:parameter('category2')
 +
if type(subpage) == 'string' then
 +
subpage = mw.ustring.lower(subpage)
 +
end
 +
if type(category2) == 'string' then
 +
subpage = mw.ustring.lower(category2)
 +
end
 +
obj._subpage = trimWhitespace(subpage, true)
 +
obj._category2 = trimWhitespace(category2) -- don't remove blank values
 +
end
 +
return obj
 
end
 
end
  
-- Find whether we need to check the blacklist or not.
+
function CategoryHandler:parameter(key)
local function needsBlacklistCheck()
+
local parameterNames = self._data.parameters[key]
    if args[cfg.nocat] == 'false'
+
local pntype = type(parameterNames)
        or args[cfg.categories] == 'yes'
+
if pntype == 'string' or pntype == 'number' then
        or args[cfg.category2] == 'yes' then
+
return self._args[parameterNames]
        return false
+
elseif pntype == 'table' then
    else
+
for _, name in ipairs(parameterNames) do
        return true
+
local value = self._args[name]
    end
+
if value ~= nil then
 +
return value
 +
end
 +
end
 +
return nil
 +
else
 +
error(string.format(
 +
'invalid config key "%s"',
 +
tostring(key)
 +
), 2)
 +
end
 
end
 
end
  
-- Searches the blacklist to find a match with the page object. The
+
function CategoryHandler:isSuppressedByArguments()
-- string searched is the namespace plus the title, including subpages.
+
return
-- Returns true if there is a match, otherwise returns false.
+
-- See if a category suppression argument has been set.
local function findBlacklistMatch(pageObject)
+
self._nocat == true
    if not pageObject then return end
+
or self._categories == false
   
+
or (
    -- Get the title to check.
+
self._category2
    local title = pageObject.nsText -- Get the namespace.
+
and self._category2 ~= self._data.category2Yes
    -- Append a colon if the namespace isn't the blank string.
+
and self._category2 ~= self._data.category2Negative
    if #title > 0 then
+
)
        title = title .. ':' .. pageObject.text
+
 
    else
+
-- Check whether we are on a subpage, and see if categories are
        title = pageObject.text
+
-- suppressed based on our subpage status.
    end
+
or self._subpage == self._data.subpageNo and self.title.isSubpage
   
+
or self._subpage == self._data.subpageOnly and not self.title.isSubpage
    -- Check the blacklist.
 
    for i, pattern in ipairs( cfg.blacklist ) do
 
        if mw.ustring.match( title, pattern ) then
 
            return true
 
        end
 
    end
 
    return false
 
 
end
 
end
  
local function _main()
+
function CategoryHandler:shouldSkipBlacklistCheck()
    local pageObject = getPageObject()
+
-- Check whether the category suppression arguments indicate we
    if not needsCategory( pageObject ) then return end
+
-- should skip the blacklist check.
    if needsBlacklistCheck() then
+
return self._nocat == false
        return findBlacklistMatch( pageObject )
+
or self._categories == true
    end
+
or self._category2 == self._data.category2Yes
 
end
 
end
  
-- Process the arguments.
+
function CategoryHandler:matchesBlacklist()
function p.main(frame)
+
if self._usesCurrentTitle then
    -- If called via #invoke, use the args passed into the invoking
+
return self._data.currentTitleMatchesBlacklist
    -- template, or the args passed to #invoke if any exist. Otherwise
+
else
    -- assume args are being passed directly in.
+
mShared = mShared or require('Module:Category handler/shared')
    local origArgs
+
return mShared.matchesBlacklist(
    if frame == mw.getCurrentFrame() then
+
self.title.prefixedText,
        origArgs = frame:getParent().args
+
mw.loadData('Module:Category handler/blacklist')
        for k, v in pairs( frame.args ) do
+
)
            origArgs = frame.args
+
end
            break
+
end
        end
+
 
    else
+
function CategoryHandler:isSuppressed()
        origArgs = frame
+
-- Find if categories are suppressed by either the arguments or by
    end
+
-- matching the blacklist.
   
+
return self:isSuppressedByArguments()
    -- The following don't need blank values preserved:
+
or not self:shouldSkipBlacklistCheck() and self:matchesBlacklist()
    -- nocat
+
end
    -- categories
+
 
    -- subpage
+
function CategoryHandler:getNamespaceParameters()
    -- page
+
if self._usesCurrentTitle then
    -- positional parameters (1-10)
+
return self._data.currentTitleNamespaceParameters
   
+
else
    -- The following *do* need blank values preserved
+
if not mappings then
    -- category2
+
mShared = mShared or require('Module:Category handler/shared')
    -- all
+
mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData
    -- other
+
end
    -- main
+
return mShared.getNamespaceParameters(
    -- all the namespace parameters
+
self.title,
 +
mappings
 +
)
 +
end
 +
end
 +
 
 +
function CategoryHandler:namespaceParametersExist()
 +
-- Find whether any namespace parameters have been specified.
 +
-- We use the order "all" --> namespace params --> "other" as this is what
 +
-- the old template did.
 +
if self:parameter('all') then
 +
return true
 +
end
 +
if not mappings then
 +
mShared = mShared or require('Module:Category handler/shared')
 +
mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData
 +
end
 +
for ns, params in pairs(mappings) do
 +
for i, param in ipairs(params) do
 +
if self._args[param] then
 +
return true
 +
end
 +
end
 +
end
 +
if self:parameter('other') then
 +
return true
 +
end
 +
return false
 +
end
 +
 
 +
function CategoryHandler:getCategories()
 +
local params = self:getNamespaceParameters()
 +
local nsCategory
 +
for i, param in ipairs(params) do
 +
local value = self._args[param]
 +
if value ~= nil then
 +
nsCategory = value
 +
break
 +
end
 +
end
 +
if nsCategory ~= nil or self:namespaceParametersExist() then
 +
-- Namespace parameters exist - advanced usage.
 +
if nsCategory == nil then
 +
nsCategory = self:parameter('other')
 +
end
 +
local ret = {self:parameter('all')}
 +
local numParam = tonumber(nsCategory)
 +
if numParam and numParam >= 1 and math.floor(numParam) == numParam then
 +
-- nsCategory is an integer
 +
ret[#ret + 1] = self._args[numParam]
 +
else
 +
ret[#ret + 1] = nsCategory
 +
end
 +
if #ret < 1 then
 +
return nil
 +
else
 +
return table.concat(ret)
 +
end
 +
elseif self._data.defaultNamespaces[self.title.namespace] then
 +
-- Namespace parameters don't exist, simple usage.
 +
return self._args[1]
 +
end
 +
return nil
 +
end
 +
 
 +
--------------------------------------------------------------------------------
 +
-- Exports
 +
--------------------------------------------------------------------------------
 +
 
 +
local p = {}
 +
 
 +
function p._exportClasses()
 +
-- Used for testing purposes.
 +
return {
 +
CategoryHandler = CategoryHandler
 +
}
 +
end
 +
 
 +
function p._main(args, data)
 +
data = data or mw.loadData('Module:Category handler/data')
 +
local handler = CategoryHandler.new(data, args)
 +
if handler:isSuppressed() then
 +
return nil
 +
end
 +
return handler:getCategories()
 +
end
  
    -- Trim whitespace and remove blank arguments for the following args:
+
function p.main(frame, data)
    -- 1, 2, 3 etc., "nocat", "categories", "subpage", and "page".
+
data = data or mw.loadData('Module:Category handler/data')
    for k, v in pairs(origArgs) do
+
local args = require('Module:Arguments').getArgs(frame, {
        v = mw.text.trim(v) -- Trim whitespace.
+
wrappers = data.wrappers,
        if type(k) == 'number'
+
valueFunc = function (k, v)
            or k == cfg.nocat
+
v = trimWhitespace(v)
            or k == cfg.categories
+
if type(k) == 'number' then
            or k == cfg.subpage
+
if v ~= '' then
            or k == cfg.page then
+
return v
            if v ~= '' then
+
else
                args[k] = v
+
return nil
            end
+
end
        else
+
else
            args[k] = v
+
return v
        end
+
end
    end
+
end
   
+
})
    -- Lower-case "nocat", "categories", "category2", and "subpage".
+
return p._main(args, data)
    local lowercase = { cfg.nocat, cfg.categories, cfg.category2, cfg.subpage }
 
    for _, v in ipairs( lowercase ) do
 
        if args[v] then
 
            args[v] = mw.ustring.lower( args[v] )
 
        end
 
    end
 
   
 
    return _main()
 
 
end
 
end
  
 
return p
 
return p

Latest revision as of 02:45, 18 May 2018

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

--------------------------------------------------------------------------------
--                                                                            --
--                              CATEGORY HANDLER                              --
--                                                                            --
--      This module implements the {{category handler}} template in Lua,      --
--      with a few improvements: all namespaces and all namespace aliases     --
--      are supported, and namespace names are detected automatically for     --
--      the local wiki. This module requires [[Module:Namespace detect]]      --
--      and [[Module:Yesno]] to be available on the local wiki. It can be     --
--      configured for different wikis by altering the values in              --
--      [[Module:Category handler/config]], and pages can be blacklisted      --
--      from categorisation by using [[Module:Category handler/blacklist]].   --
--                                                                            --
--------------------------------------------------------------------------------

-- Load required modules
local yesno = require('Module:Yesno')

-- Lazily load things we don't always need
local mShared, mappings

local p = {}

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

local function trimWhitespace(s, removeBlanks)
	if type(s) ~= 'string' then
		return s
	end
	s = s:match('^%s*(.-)%s*$')
	if removeBlanks then
		if s ~= '' then
			return s
		else
			return nil
		end
	else
		return s
	end
end

--------------------------------------------------------------------------------
-- CategoryHandler class
--------------------------------------------------------------------------------

local CategoryHandler = {}
CategoryHandler.__index = CategoryHandler

function CategoryHandler.new(data, args)
	local obj = setmetatable({ _data = data, _args = args }, CategoryHandler)
	
	-- Set the title object
	do
		local pagename = obj:parameter('demopage')
		local success, titleObj
		if pagename then
			success, titleObj = pcall(mw.title.new, pagename)
		end
		if success and titleObj then
			obj.title = titleObj
			if titleObj == mw.title.getCurrentTitle() then
				obj._usesCurrentTitle = true
			end
		else
			obj.title = mw.title.getCurrentTitle()
			obj._usesCurrentTitle = true
		end
	end

	-- Set suppression parameter values
	for _, key in ipairs{'nocat', 'categories'} do
		local value = obj:parameter(key)
		value = trimWhitespace(value, true)
		obj['_' .. key] = yesno(value)
	end
	do
		local subpage = obj:parameter('subpage')
		local category2 = obj:parameter('category2')
		if type(subpage) == 'string' then
			subpage = mw.ustring.lower(subpage)
		end
		if type(category2) == 'string' then
			subpage = mw.ustring.lower(category2)
		end
		obj._subpage = trimWhitespace(subpage, true)
		obj._category2 = trimWhitespace(category2) -- don't remove blank values
	end
	return obj
end

function CategoryHandler:parameter(key)
	local parameterNames = self._data.parameters[key]
	local pntype = type(parameterNames)
	if pntype == 'string' or pntype == 'number' then
		return self._args[parameterNames]
	elseif pntype == 'table' then
		for _, name in ipairs(parameterNames) do
			local value = self._args[name]
			if value ~= nil then
				return value
			end
		end
		return nil
	else
		error(string.format(
			'invalid config key "%s"',
			tostring(key)
		), 2)
	end
end

function CategoryHandler:isSuppressedByArguments()
	return
		-- See if a category suppression argument has been set.
		self._nocat == true
		or self._categories == false
		or (
			self._category2
			and self._category2 ~= self._data.category2Yes
			and self._category2 ~= self._data.category2Negative
		)

		-- Check whether we are on a subpage, and see if categories are
		-- suppressed based on our subpage status.
		or self._subpage == self._data.subpageNo and self.title.isSubpage
		or self._subpage == self._data.subpageOnly and not self.title.isSubpage
end

function CategoryHandler:shouldSkipBlacklistCheck()
	-- Check whether the category suppression arguments indicate we
	-- should skip the blacklist check.
	return self._nocat == false
		or self._categories == true
		or self._category2 == self._data.category2Yes
end

function CategoryHandler:matchesBlacklist()
	if self._usesCurrentTitle then
		return self._data.currentTitleMatchesBlacklist
	else
		mShared = mShared or require('Module:Category handler/shared')
		return mShared.matchesBlacklist(
			self.title.prefixedText,
			mw.loadData('Module:Category handler/blacklist')
		)
	end
end

function CategoryHandler:isSuppressed()
	-- Find if categories are suppressed by either the arguments or by
	-- matching the blacklist.
	return self:isSuppressedByArguments()
		or not self:shouldSkipBlacklistCheck() and self:matchesBlacklist()
end

function CategoryHandler:getNamespaceParameters()
	if self._usesCurrentTitle then
		return self._data.currentTitleNamespaceParameters
	else
		if not mappings then
			mShared = mShared or require('Module:Category handler/shared')
			mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData
		end
		return mShared.getNamespaceParameters(
			self.title,
			mappings
		)
	end
end

function CategoryHandler:namespaceParametersExist()
	-- Find whether any namespace parameters have been specified.
	-- We use the order "all" --> namespace params --> "other" as this is what
	-- the old template did.
	if self:parameter('all') then
		return true
	end
	if not mappings then
		mShared = mShared or require('Module:Category handler/shared')
		mappings = mShared.getParamMappings(true) -- gets mappings with mw.loadData
	end
	for ns, params in pairs(mappings) do
		for i, param in ipairs(params) do
			if self._args[param] then
				return true
			end
		end
	end
	if self:parameter('other') then
		return true
	end
	return false
end

function CategoryHandler:getCategories()
	local params = self:getNamespaceParameters()
	local nsCategory
	for i, param in ipairs(params) do
		local value = self._args[param]
		if value ~= nil then
			nsCategory = value
			break
		end
	end
	if nsCategory ~= nil or self:namespaceParametersExist() then
		-- Namespace parameters exist - advanced usage.
		if nsCategory == nil then
			nsCategory = self:parameter('other')
		end
		local ret = {self:parameter('all')}
		local numParam = tonumber(nsCategory)
		if numParam and numParam >= 1 and math.floor(numParam) == numParam then
			-- nsCategory is an integer
			ret[#ret + 1] = self._args[numParam]
		else
			ret[#ret + 1] = nsCategory
		end
		if #ret < 1 then
			return nil
		else
			return table.concat(ret)
		end
	elseif self._data.defaultNamespaces[self.title.namespace] then
		-- Namespace parameters don't exist, simple usage.
		return self._args[1]
	end
	return nil
end

--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------

local p = {}

function p._exportClasses()
	-- Used for testing purposes.
	return {
		CategoryHandler = CategoryHandler
	}
end

function p._main(args, data)
	data = data or mw.loadData('Module:Category handler/data')
	local handler = CategoryHandler.new(data, args)
	if handler:isSuppressed() then
		return nil
	end
	return handler:getCategories()
end

function p.main(frame, data)
	data = data or mw.loadData('Module:Category handler/data')
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = data.wrappers,
		valueFunc = function (k, v)
			v = trimWhitespace(v)
			if type(k) == 'number' then
				if v ~= '' then
					return v
				else
					return nil
				end
			else
				return v
			end
		end
	})
	return p._main(args, data)
end

return p