Creates a new Title instance.
Usage:
const title = new mwbot.Title('Page title');
The title of the page. If no namespace
is provided, this will be analyzed
to determine if it includes a namespace prefix.
Optional
namespace: numberThe default namespace to use for the given title. (Default: NS_MAIN
)
Readonly
existObject used by exists.
Keyed by title. Boolean true value indicates page does exist.
The setter function. Returns a boolean.
Example to declare existing titles:
Title.exist.set(['User:John_Doe', ...]);
Example to declare titles nonexistent:
Title.exist.set(['File:Foo_bar.jpg', ...], false);
Remove unicode bidirectional characters and trim a string.
"Unicode bidirectional characters" are characters that can slip into cut-and-pasted texts, represented as red dots in WikiEditor.
This method is exclusive to mwbot-ts
.
Input string.
Optional
trim: booleanWhether to trim the string. Defaults to true
.
Check whether this title exists on the wiki.
Prefixed DB title (string) or instance of Title.
Boolean if the information is available, otherwise null
.
Check if a given namespace is a talk namespace.
The namespace ID.
A boolean indicating whether the namespace is a talk namespace.
Converts all the alphabetic characters in a string to lowercase, as in PHP.
This method is exclusive to mwbot-ts
.
Constructor for Title objects with predefined namespace.
Unlike newFromText or the constructor, this method doesn't allow the given namespace
to be overridden by a namespace prefix in title
. See the class desciprtion
for details about this behavior.
The single exception to this is when namespace
is 0
, indicating the main namespace.
The function behaves like newFromText in that case.
Namespace to use for the title.
The unprefixed title.
Optional
fragment: stringThe link fragment (after the "#").
This parameter is exclusive to mwbot-ts
.
Optional
interwiki: stringThe interwiki prefix.
This parameter is exclusive to mwbot-ts
.
A valid Title object or null
if the title is invalid.
Sanitizes a file name as supplied by the user, originating in the user's file system so it is most likely a valid MediaWiki title and file name after processing.
The unclean file name including file extension but without namespace.
A valid Title object or null
if the title is invalid.
Constructor for Title objects with a null return instead of an exception for invalid titles.
Note that namespace
is the default namespace only, and can be overridden by a namespace
prefix in title
. If you do not want this behavior, use makeTitle. See
the class desciprtion for details.
Optional
namespace: numberDefault namespace. (Default: NS_MAIN
)
A valid Title object or null
if the title is invalid.
Constructor for Title objects from user input altering that input to produce a title that MediaWiki will accept as legal.
The title, optionally namespace-prefixed (NOTE: interwiki prefixes are disallowed).
Optional
defaultNamespace: numberIf given, used as default namespace for the given title.
Optional
options: { forUploading?: true }Additional options.
Optional
forUploading?: true(Default: true
)
Makes sure that a file is uploadable under the title returned. There are pages in the file namespace under which file upload is impossible. Automatically assumed if the title is created in the Media namespace.
A valid Title object or null
if the input cannot be turned into a valid title.
Normalizes a title to its canonical form.
This capitalizes the first character of the base (unprefixed) title and localizes the namespace according to the wiki’s configuration.
This method is exclusive to mwbot-ts
.
The title to normalize.
Optional
options: TitleNormalizeOptionsOptions that control how the title is normalized.
The normalized title as a string, or null
if the input is not a valid title.
Normalize a file extension to the common form, making it lowercase and checking some synonyms, and ensure it's clean. Extensions with non-alphanumeric characters will be discarded.
File extension (without the leading dot).
File extension in canonical form.
Normalizes a username by capitalizing its first letter, following MediaWiki conventions.
IP addresses are capitalized with all hexadecimal segments spelled out (e.g., 192.168.0.1
for IPv4 and FD12:3456:789A:1:0:0:0:0
for IPv6).
This method is exclusive to mwbot-ts
.
The username to normalize.
The normalized username, or null
if the input contains characters that are not
allowed in usernames.
De-capitalizes a character as in PHP.
This method handles the difference between PHP's strtolower
and JS's String.toLowerCase
(see phab:T147646).
This method is exclusive to mwbot-ts
.
Unicode character.
Unicode character, in lower case, according to the same rules as in PHP.
Capitalizes a character as in PHP.
This method handles the difference between PHP's strtoupper
and JS's String.toUpperCase
(see phab:T147646).
Unicode character.
Unicode character, in upper case, according to the same rules as in PHP.
Converts all the alphabetic characters in a string to uppercase, as in PHP.
This method is exclusive to mwbot-ts
.
This interface defines the static members of the
Title
class. For instance members, see Title (defined separately due to TypeScript limitations).Example:
Native specs
When using the constructor directly, passing invalid titles will result in an exception. Use newFromText instead to safely handle invalid titles, returning
null
instead of throwing an error.In both the constructor and newFromText,
namespace
acts only as the default namespace and can be overridden by a namespace prefix intitle
. If you want to enforce the provided namespace, use makeTitle instead. Compare:Mwbot enhancements
In addition to the native
mediawiki.Title
features,mwbot.Title
correctly parses interwiki prefixes. Compare:Here,
Main_page
is correctly extracted as the title inmwbot.Title
. To check whether a title contains an interwiki prefix, use Title.isExternal, and to retrieve the interwiki prefixes, use Title.getInterwiki.Interwiki Handling and Namespace Limitations
Titles with interwiki prefixes are always recognized as being in the main namespace, since there is no guarantee that the interwiki target site has an equivalent namespace. This also affects the casing of
title
, as the interwiki prefix is treated as part of the title rather than a namespace. Example:The casing of the title depends on the input string. This ensures compatibility with case-sensitive wikis — do not assume that
[[iw:foo]]
will be interpreted as[[iw:Foo]]
when dealing with interwiki titles. (This is howTitleParser
in PHP works.)Leading Colons
The handling of leading colons is improved in
mwbot.Title
. You can check if a title originally had a leading colon using Title.hadLeadingColon, and you can enforce its presence in the output of Title.getPrefixedDb. This makes it easier to differentiate e.g. pure links ([[:cat]]
) and category links ([[cat]]
).