Module:If in category
Appearance
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This Lua module is used on approximately 121,000 pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
Due to a server limit of 500 "expensive parser function" calls (e.g. #ifexist:, PAGESIZE:, and PAGESINCATEGORY:), this module will only work properly if the page using it has not already exceeded the limit. When the limit is exceeded, the page using this template or module is categorised in Category:Pages with too many expensive parser function calls. (further information) |
This module implements {{if in category}}
Usage
{{if in category | [category] | [result if true] | [result if false] | page = [page] }}
If |page=
is omitted, the current page is used. If both the second and third unnamed parameters are omitted, the second unnamed parameter defaults to yes.
local p = {}
local TableTools = require('Module:TableTools')
function p.main(frame)
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
return p._main(args)
end
function p._main(args)
-- create a title object
local page = mw.title.new((args.page or '')) or mw.title.getCurrentTitle()
if TableTools.inArray(page.categories, string.gsub( args[1], '^[Cc]ategory:', '' )) then
if not args[3] then
-- if we are are not given anything to return, return 'yes' if it evalulates to true
return args[2] or 'yes'
else
return args[2]
end
else
return args[3]
end
end
return p