Zelda Wiki

Want to contribute to this wiki?
Sign up for an account, and get started!

Come join the Zelda Wiki community Discord server!

READ MORE

Zelda Wiki
Advertisement
This module exports the following functions.

exists

exists(fullPageName, [noRedirect])

Check whether a page exists. Unlike mw.title, this function does not register a link in Special:WantedPages. It also does not count as an "expensive parser function."

Parameters

Returns

  • Boolean indicating whether the page exists.

Examples

#InputOutputResult
1
exists("OoT")
true
Green check
2
exists("OoT", true)
false
Green check

dpl

dpl(args)

This function is wrapper for the DPL parser function.

Returns

  • Array of results

Examples

#InputOutputResult
3
dpl({
  namespace = "Category",
  titlematch = "Link|Zelda",
})
{"Category:Link", "Category:Zelda"}
Green check

inCategory

inCategory(category, fullPageName)

Parameters

  • category
    Category name with or without namespace prefix.
  • fullPageName
    Full page name with namespace prefix.

Returns

  • A boolean indicating whether the given page is a member of the given category.

Examples

#InputOutputResult
Works with or without the namespace prefix.
4
inCategory("Characters in Breath of the Wild", "Link")
true
Green check
5
inCategory("Category:Characters", "Link")
true
Green check
6
inCategory("Items", "Link")
false
Green check
7
inCategory("Fakecategory", "Link")
false
Green check
For pages not in the main namespace, the namespace prefix is required.
8
inCategory(
  "Characters by Game",
  "Characters in Breath of the Wild"
)
false
Green check
9
inCategory(
  "Characters by Game",
  "Category:Characters in Breath of the Wild"
)
true
Green check

inNamespace

inNamespace(namespaces, [fullPageName])

Parameters

Returns

  • true if and only if fullPageName (or the current page) has a namespace prefix that is one of namespaces, regardless of whether the page actually exists.

Examples

#InputOutputResult
Main namespace is the empty string.
10
inNamespace("", "Link")
true
Green check
11
inNamespace("Category", "Link")
false
Green check
12
inNamespace("Category", "Category:Link")
true
Green check
Can evaluate to true even when page does not exist.
13
inNamespace("Category", "Category:Flippityfloppityfloo")
true
Green check
Current page
14
inNamespace("Module")
true
Green check
Multiple namespaces
15
inNamespace({"User", "MediaWiki"}, "Princess Zelda")
false
Green check
16
inNamespace({"User", "MediaWiki"}, "User:Abdullah")
true
Green check

local p = {}

local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")

local function stripNamespace(category)
	return string.gsub(category, "Category:%s+", "")
end

function p.exists(fullPageName, noRedirect)
	local title = mw.title.new(fullPageName)
	local isMainNamespace = utilsString.isEmpty(title.nsText)
	local result = p.dpl({
		title = title.text,
		namespace = title.nsText,
		notnamespace = (not isMainNamespace) and "" or nil, -- For some reason it'll return results for the main namespace without this line
		redirects = noRedirect and "exclude" or "include"
	})
	return #result > 0
end

local SEPARATOR = "#"
function p.dpl(args)
	if args.format then
		mw.addWarning("<code>format</code> argument cannot be used here. Format the resulting Lua table instead.")
	end
	local dplArgs = utilsTable.merge({}, args, {
		format = ",,%PAGE%" .. SEPARATOR .. ","
	})
	local result = mw.getCurrentFrame():callParserFunction('#dpl:', dplArgs)
	if not utilsString._endsWith(SEPARATOR)(result) then
		return {}
	end
	result = utilsString.trim(result, SEPARATOR)
	result = utilsString.split(result, SEPARATOR)
	result = utilsTable.map(result, utilsString._trim(":"))
	return result
end

function p.inCategory(category, fullPageName)
	if (not category) or (not fullPageName) then
		return false
	end
	local title = mw.title.new(fullPageName)
	local dplResult = p.dpl({
		category= stripNamespace(category),
		namespace= title.nsText,
		title= title.text,
	})
	return #dplResult ~= 0
end

function p.inNamespace(namespaces, fullPageName)
	if type(namespaces) == "string" then
		namespaces = {namespaces}
	end
	local title = fullPageName and mw.title.new(fullPageName) or mw.title.getCurrentTitle()
	return utilsTable.includes(namespaces, title.nsText)
end

p.Schemas = {
	exists = {
		fullPageName = {
			type = "string",
			required = true,
			desc = "Full page name with namespace prefix.",
		},
		noRedirect = {
			type = "boolean",
			desc = "If true, redirects are not considered."
		},
	},
	inCategory = {
		category = {
			type = "string",
			required = true,
			desc = "Category name with or without namespace prefix.",
		},
		fullPageName = {
			type = "string",
			required = true,
			desc = "Full page name with namespace prefix."
		},
	},
	inNamespace = {
		namespaces = {
			desc = "A namespace or array of namespaces.",
			required = true,
			oneOf = {
				{ type = "string" },
				{ type = "array", items = { type = "string" } }
			},
		},
		fullPageName = {
			type = "string",
			desc = "Full pagename. Defaults to the name of the current page."
		}
	}
}

p.Documentation = {
	exists = {
		desc = 'Check whether a page exists. Unlike {{Scribunto Manual|lib=mw.title}}, this function does not register a link in [[Special:WantedPages]]. It also does not count as an "expensive parser function."',
		params = {"fullPageName", "noRedirect"},
		returns = "Boolean indicating whether the page exists.",
		cases = {
			{
				args = {"OoT"},
				expect = true,
			},
			{
				args = {"OoT", true},
				expect = false,
			},
		},
	},
	dpl = {
		desc = "This function is wrapper for the [[gphelp:Extension:DPL3/Manual|DPL]] parser function.",
		params = {"args"},
		returns = "Array of results",
		cases = {
			{
				args = { {titlematch = "Link|Zelda", namespace = "Category"} },
				expect = {"Category:Link", "Category:Zelda"}
			}
		}
	},
	inCategory = {
		params = {"category", "fullPageName"},
		returns = "A boolean indicating whether the given page is a member of the given category.",
		cases = {
			{
				desc = "Works with or without the namespace prefix.",
				args = {"Characters in Breath of the Wild", "Link"},
				expect = true,
			},
			{
				args = {"Category:Characters", "Link"},
				expect = true,
			},
			{
				args = {"Items", "Link"},
				expect = false,
			},
			{
				args = {"Fakecategory", "Link"},
				expect = false,
			},
			{
				desc = "For pages not in the main namespace, the namespace prefix is required.",
				args = {"Characters by Game", "Characters in Breath of the Wild"},
				expect = false,
			},
			{
				args = {"Characters by Game", "Category:Characters in Breath of the Wild"},
				expect = true,
			},
		},
	},
	inNamespace = {
		params = {"namespaces", "fullPageName"},
		returns = "<code>true</code> if and only if <code>fullPageName</code> (or the current page) has a namespace prefix that is one of <code>namespaces</code>, regardless of whether the page actually exists.",
		cases = {
			{
				desc = "Main namespace is the empty string.",
				args = {"", "Link"},
				expect = true,
			},
			{
				args = {"Category", "Link"},
				expect = false,
			},
			{
				args = {"Category", "Category:Link"},
				expect = true,
			},
			{
				desc = "Can evaluate to true even when page does not exist.",
				args = {"Category", "Category:Flippityfloppityfloo"},
				expect = true,
			},
			{
				desc = "Current page",
				args = {"Module"},
				expect = true,
			},
			{
				desc = "Multiple namespaces",
				args = {{"User", "MediaWiki"}, "Princess Zelda"},
				expect = false,
			},
			{
				args = {{"User", "MediaWiki"}, "User:Abdullah"},
				expect = true,
			},
		},
	},
}

return p
Advertisement