mw.Title(title, namespaceopt)

Library for constructing MediaWiki titles.

Example

new mw.Title( 'Foo', NS_TEMPLATE ).getPrefixedText();
// => 'Template:Foo'
mw.Title.newFromText( 'Foo', NS_TEMPLATE ).getPrefixedText();
// => 'Template:Foo'
mw.Title.makeTitle( NS_TEMPLATE, 'Foo' ).getPrefixedText();
// => 'Template:Foo'

new mw.Title( 'Category:Foo', NS_TEMPLATE ).getPrefixedText();
// => 'Category:Foo'
mw.Title.newFromText( 'Category:Foo', NS_TEMPLATE ).getPrefixedText();
// => 'Category:Foo'
mw.Title.makeTitle( NS_TEMPLATE, 'Category:Foo' ).getPrefixedText();
// => 'Template:Category:Foo'

new mw.Title( 'Template:Foo', NS_TEMPLATE ).getPrefixedText();
// => 'Template:Foo'
mw.Title.newFromText( 'Template:Foo', NS_TEMPLATE ).getPrefixedText();
// => 'Template:Foo'
mw.Title.makeTitle( NS_TEMPLATE, 'Template:Foo' ).getPrefixedText();
// => 'Template:Template:Foo'

Constructor

new mw.Title(title, namespaceopt) #

Parse titles into an object structure. Note that when using the constructor directly, passing invalid titles will result in an exception. Use newFromText to use the logic directly and get null for invalid titles which is easier to work with.

Note that in the constructor and newFromText method, 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.

Parameters:

Name Type Attributes Default Description
title string

Title of the page. If no second argument given, this will be searched for a namespace

namespace number <optional>
NS_MAIN

If given, will used as default namespace for the given title

Source:

Throws:

When the title is invalid

Type
Error
Parse titles into an object structure.

Properties

Methods

canHaveTalkPage() → {boolean} #

Check the title can have an associated talk page.

Source:

Returns:

The title can have an associated talk page

Type
boolean
Check the title can have an associated talk page.

exists() → {boolean|null} #

Whether this title exists on the wiki.

Source:
See:

Returns:

Boolean if the information is available, otherwise null

Type
boolean | null
Whether this title exists on the wiki.

getExtension() → {string|null} #

Get the extension of the page name (if any).

Source:

Returns:

Name extension or null if there is none

Type
string | null
Get the extension of the page name (if any).

getFileNameTextWithoutExtension() → {string} #

Get the page name as if it is a file name, without extension or namespace prefix, in the human-readable form with spaces instead of underscores. For example, the title File:Example_image.svg will be returned as "Example image".

Note that this method will work for non-file titles but probably give nonsensical results. A title like User:Dr._J._Fail will be returned as Dr. J! Use getMainText instead.

Source:

Returns:

Type
string

Get the page name as if it is a file name, without extension or namespace prefix, in the human-readable form with spaces instead of underscores.

getFileNameWithoutExtension() → {string} #

Get the page name as if it is a file name, without extension or namespace prefix, in the canonical form with underscores instead of spaces. For example, the title File:Example_image.svg will be returned as Example_image.

Note that this method will work for non-file titles but probably give nonsensical results. A title like User:Dr._J._Fail will be returned as Dr._J! Use getMain instead.

Source:

Returns:

Type
string

Get the page name as if it is a file name, without extension or namespace prefix, in the canonical form with underscores instead of spaces.

getFragment() → {string|null} #

Get the fragment (if any).

Note that this method (by design) does not include the hash character and the value is not url encoded.

Source:

Returns:

Type
string | null
Get the fragment (if any).

getMain() → {string} #

Get the main page name.

Example: Example_image.svg for File:Example_image.svg.

Source:

Returns:

Type
string
Get the main page name.

getMainText() → {string} #

Get the main page name (transformed by text()).

Example: Example image.svg for File:Example_image.svg.

Source:

Returns:

Type
string
Get the main page name (transformed by text()).

getName() → {string} #

Get the page name as if it is a file name, without extension or namespace prefix. Warning, this is usually not what you want! A title like User:Dr._J._Fail will be returned as Dr. J! Use getMain or getMainText for the actual page name.

Deprecated:
Source:

Returns:

File name without file extension, in the canonical form with underscores instead of spaces. For example, the title File:Example_image.svg will be returned as Example_image.

Type
string
Get the page name as if it is a file name, without extension or namespace prefix.

getNameText() → {string} #

Get the page name as if it is a file name, without extension or namespace prefix. Warning, this is usually not what you want! A title like User:Dr._J._Fail will be returned as Dr. J! Use getMainText for the actual page name.

Deprecated:
Source:

Returns:

File name without file extension, formatted with spaces instead of underscores. For example, the title File:Example_image.svg will be returned as Example image.

Type
string
Get the page name as if it is a file name, without extension or namespace prefix.

getNamespaceId() → {number} #

Get the namespace number.

Example: 6 for "File:Example_image.svg".

Source:

Returns:

Type
number
Get the namespace number.

getNamespacePrefix() → {string} #

Get the namespace prefix (in the content language).

Example: "File:" for "File:Example_image.svg". In #NS_MAIN this is '', otherwise namespace name plus ':'

Source:

Returns:

Type
string
Get the namespace prefix (in the content language).

getPrefixedDb() → {string} #

Get the full page name.

Example: File:Example_image.svg. Most useful for API calls, anything that must identify the "title".

Source:

Returns:

Type
string
Get the full page name.

getPrefixedText() → {string} #

Get the full page name (transformed by text).

Example: File:Example image.svg for File:Example_image.svg.

Source:

Returns:

Type
string
Get the full page name (transformed by text).

getRelativeText(namespace) → {string} #

Get the page name relative to a namespace.

Example:

  • "Foo:Bar" relative to the Foo namespace becomes "Bar".
  • "Bar" relative to any non-main namespace becomes ":Bar".
  • "Foo:Bar" relative to any namespace other than Foo stays "Foo:Bar".

Parameters:

Name Type Description
namespace number

The namespace to be relative to

Source:

Returns:

Type
string
Get the page name relative to a namespace.

getSubjectPage() → {mw.Title|null} #

Get the title for the subject page of a talk page.

Source:

Returns:

The title for the subject page of a talk page, null if not available

Type
mw.Title | null
Get the title for the subject page of a talk page.

getTalkPage() → {mw.Title|null} #

Get the title for the associated talk page.

Source:

Returns:

The title for the associated talk page, null if not available

Type
mw.Title | null
Get the title for the associated talk page.

getUrl(paramsopt) → {string} #

Get the URL to this title.

Parameters:

Name Type Attributes Description
params Object <optional>

A mapping of query parameter names to values, e.g. { action: 'edit' }.

Source:
See:

Returns:

Type
string
Get the URL to this title.

isTalkPage() → {boolean} #

Check if the title is in a talk namespace.

Source:

Returns:

The title is in a talk namespace

Type
boolean
Check if the title is in a talk namespace.

toString() #

Alias of getPrefixedDb.

toText() #

Alias of getPrefixedText.

exists(title) → {boolean|null}static #

Whether this title exists on the wiki.

Parameters:

Name Type Description
title string | mw.Title

prefixed db-key name (string) or instance of Title

Source:

Throws:

If title is not a string or mw.Title

Type
Error

Returns:

Boolean if the information is available, otherwise null

Type
boolean | null
Whether this title exists on the wiki.

isTalkNamespace(namespaceId) → {boolean}static #

Check if a given namespace is a talk namespace.

See NamespaceInfo::isTalk in PHP

Parameters:

Name Type Description
namespaceId number

Namespace ID

Source:

Returns:

Namespace is a talk namespace

Type
boolean
Check if a given namespace is a talk namespace.

makeTitle(namespace, title) → {mw.Title|null}static #

Constructor for Title objects with predefined namespace.

Unlike newFromText or the constructor, this function doesn't allow the given namespace to be overridden by a namespace prefix in title. See the constructor documentation 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.

Parameters:

Name Type Description
namespace number

Namespace to use for the title

title string
Source:

Returns:

A valid Title object or null if the title is invalid

Type
mw.Title | null
Constructor for Title objects with predefined namespace.

newFromFileName(uncleanName) → {mw.Title|null}static #

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. Returns null on fatal errors.

Parameters:

Name Type Description
uncleanName string

The unclean file name including file extension but without namespace

Source:

Returns:

A valid Title object or null if the title is invalid

Type
mw.Title | null

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.

newFromImg(img) → {mw.Title|null}static #

Get the file title from an image element.

Example

var title = mw.Title.newFromImg( imageNode );

Parameters:

Name Type Description
img HTMLElement | jQuery

The image to use as a base

Source:

Returns:

The file title or null if unsuccessful

Type
mw.Title | null
Get the file title from an image element.

newFromText(title, namespaceopt) → {mw.Title|null}static #

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 #constructor for details.

Parameters:

Name Type Attributes Default Description
title string
namespace number <optional>
NS_MAIN

Default namespace

Source:

Returns:

A valid Title object or null if the title is invalid

Type
mw.Title | null
Constructor for Title objects with a null return instead of an exception for invalid titles.

newFromUserInput(title, defaultNamespaceopt, optionsopt) → {mw.Title|null}static #

Constructor for Title objects from user input altering that input to produce a title that MediaWiki will accept as legal.

Parameters:

Name Type Attributes Default Description
title string
defaultNamespace number <optional>
NS_MAIN

If given, will used as default namespace for the given title.

options Object <optional>

additional options

Properties
Name Type Attributes Default Description
forUploading boolean <optional>
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.

Source:

Returns:

A valid Title object or null if the input cannot be turned into a valid title

Type
mw.Title | null

Constructor for Title objects from user input altering that input to produce a title that MediaWiki will accept as legal.

normalizeExtension(extension) → {string}static #

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. Keep in sync with File::normalizeExtension() in PHP.

Parameters:

Name Type Description
extension string

File extension (without the leading dot)

Source:

Returns:

File extension in canonical form

Type
string

Normalize a file extension to the common form, making it lowercase and checking some synonyms, and ensure it's clean.

phpCharToUpper(chr) → {string}static #

PHP's strtoupper differs from String.toUpperCase in a number of cases (T147646).

Parameters:

Name Type Description
chr string

Unicode character

Source:

Returns:

Unicode character, in upper case, according to the same rules as in PHP

Type
string
PHP's strtoupper differs from String.toUpperCase in a number of cases (T147646).

wantSignaturesNamespace(namespaceId) → {boolean}static #

Check if signature buttons should be shown in a given namespace.

See NamespaceInfo::wantSignatures in PHP

Parameters:

Name Type Description
namespaceId number

Namespace ID

Source:

Returns:

Namespace is a signature namespace

Type
boolean
Check if signature buttons should be shown in a given namespace.

Type Definitions

TitleExistenceStore #

Type:

  • Object

Properties:

Name Type Description
pages Object

Keyed by title. Boolean true value indicates page does exist.

set function

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 );
Properties
Name Type Attributes Default Description
titles string | Array.<string>

Title(s) in strict prefixedDb title form

state boolean <optional>
true

State of the given titles

Source: