MediaWiki REL1_39
TitleValue Class Reference

Represents a page (or page fragment) title within MediaWiki. More...

Inheritance diagram for TitleValue:
Collaboration diagram for TitleValue:

Public Member Functions

 __construct ( $namespace, $title, $fragment='', $interwiki='')
 Construct a TitleValue.
 
 __toString ()
 Returns a string representation of the title, for logging.
 
 createFragmentTarget (string $fragment)
 Create a new LinkTarget with a different fragment on the same page.
 
 getDBkey ()
 Get the main part of the link target, in canonical database form.
 
 getFragment ()
 Get the link fragment in text form (i.e.
 
 getInterwiki ()
 The interwiki component of this LinkTarget.
 
 getNamespace ()
 Get the namespace index.
 
 getText ()
 Get the main part of the link target, in text form.
 
 hasFragment ()
 Whether the link target has a fragment.
 
 inNamespace (int $ns)
 Convenience function to check if the target is in a given namespace.
 
 isExternal ()
 Whether this LinkTarget has an interwiki component.
 
 isSameLinkAs (LinkTarget $other)
 Check whether the given LinkTarget refers to the same target as this LinkTarget.
 

Static Public Member Functions

static assertValidSpec ( $namespace, $title, $fragment='', $interwiki='')
 Assert that the given parameters could be used to construct a TitleValue object.
 
static castPageToLinkTarget (?PageReference $page)
 Casts a PageReference to a LinkTarget.
 
static newFromPage (PageReference $page)
 Create a TitleValue from a local PageReference.
 
static tryNew ( $namespace, $title, $fragment='', $interwiki='')
 Constructs a TitleValue, or returns null if the parameters are not valid.
 

Public Attributes

string $prefixedText = null
 Text form including namespace/interwiki, initialised on demand.
 

Detailed Description

Represents a page (or page fragment) title within MediaWiki.

Note
In contrast to Title, this is designed to be a plain value object. That is, it is immutable, does not use global state, and causes no side effects.
Stability: newable
See also
https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
Since
1.23

Definition at line 40 of file TitleValue.php.

Constructor & Destructor Documentation

◆ __construct()

TitleValue::__construct ( $namespace,
$title,
$fragment = '',
$interwiki = '' )

Construct a TitleValue.

Note
TitleValue expects a valid namespace and name; typically, a TitleValue is constructed either from a database entry, or by a TitleParser. For constructing a TitleValue from user input or external sources, use a TitleParser.
Stability: stable
to call
Parameters
int$namespaceThe namespace ID. This is not validated.
string$titleThe page title in either DBkey or text form. No normalization is applied beyond underscore/space conversion.
string$fragmentThe fragment title. Use '' to represent the whole page. No validation or normalization is applied.
string$interwikiThe interwiki component. No validation or normalization is applied.
Exceptions
InvalidArgumentException

Definition at line 143 of file TitleValue.php.

References $title.

Member Function Documentation

◆ __toString()

TitleValue::__toString ( )

Returns a string representation of the title, for logging.

This is purely informative and must not be used programmatically. Use the appropriate TitleFormatter to generate the correct string representation for a given use.

Since
1.23
Returns
string

Implements MediaWiki\Linker\LinkTarget.

Definition at line 243 of file TitleValue.php.

◆ assertValidSpec()

static TitleValue::assertValidSpec ( $namespace,
$title,
$fragment = '',
$interwiki = '' )
static

Assert that the given parameters could be used to construct a TitleValue object.

Performs basic syntax and consistency checks. Does not perform full validation, use TitleParser::makeTitleValueSafe() for that.

Parameters
int$namespace
string$title
string$fragment
string$interwiki
Exceptions
InvalidArgumentExceptionif the combination of parameters is not valid for constructing a TitleValue.

Definition at line 165 of file TitleValue.php.

References $title, and NS_MAIN.

◆ castPageToLinkTarget()

static TitleValue::castPageToLinkTarget ( ?PageReference $page)
static

Casts a PageReference to a LinkTarget.

If $page is null, null is returned. If $page is also an instance of LinkTarget, $page is returned unchanged.

See also
newFromPage()
Since
1.37
Parameters
PageReference | null$page
Returns
LinkTarget|null

Definition at line 118 of file TitleValue.php.

◆ createFragmentTarget()

TitleValue::createFragmentTarget ( string $fragment)

Create a new LinkTarget with a different fragment on the same page.

It is expected that the same type of object will be returned, but the only requirement is that it is a LinkTarget.

Since
1.27
Parameters
string$fragmentThe fragment override, or "" to remove it.
Returns
LinkTarget

Implements MediaWiki\Linker\LinkTarget.

Definition at line 218 of file TitleValue.php.

◆ getDBkey()

TitleValue::getDBkey ( )

Get the main part of the link target, in canonical database form.

The main part is the link target without namespace prefix or hash fragment. The database form means that spaces become underscores, this is also used for URLs.

Since
1.27
Returns
string

Implements MediaWiki\Linker\LinkTarget.

Definition at line 210 of file TitleValue.php.

Referenced by newFromPage().

◆ getFragment()

TitleValue::getFragment ( )

Get the link fragment in text form (i.e.

the bit after the hash #).

Since
1.27
Returns
string link fragment

Implements MediaWiki\Linker\LinkTarget.

Definition at line 202 of file TitleValue.php.

◆ getInterwiki()

TitleValue::getInterwiki ( )

The interwiki component of this LinkTarget.

Since
1.27
Returns
string

Implements MediaWiki\Linker\LinkTarget.

Definition at line 231 of file TitleValue.php.

◆ getNamespace()

TitleValue::getNamespace ( )

Get the namespace index.

Since
1.27
Returns
int Namespace index

Implements MediaWiki\Linker\LinkTarget.

Definition at line 194 of file TitleValue.php.

Referenced by newFromPage().

◆ getText()

TitleValue::getText ( )

Get the main part of the link target, in text form.

The main part is the link target without namespace prefix or hash fragment. The text form is used for display purposes.

This is computed from the DB key by replacing any underscores with spaces.

Note
To get a title string that includes the namespace and/or fragment, use a TitleFormatter.
Since
1.27
Returns
string

Implements MediaWiki\Linker\LinkTarget.

Definition at line 214 of file TitleValue.php.

◆ hasFragment()

TitleValue::hasFragment ( )

Whether the link target has a fragment.

Since
1.27
Returns
bool

Implements MediaWiki\Linker\LinkTarget.

Definition at line 206 of file TitleValue.php.

◆ inNamespace()

TitleValue::inNamespace ( int $ns)

Convenience function to check if the target is in a given namespace.

Since
1.27
Parameters
int$ns
Returns
bool

Implements MediaWiki\Linker\LinkTarget.

Definition at line 198 of file TitleValue.php.

◆ isExternal()

TitleValue::isExternal ( )

Whether this LinkTarget has an interwiki component.

Since
1.27
Returns
bool

Implements MediaWiki\Linker\LinkTarget.

Definition at line 227 of file TitleValue.php.

◆ isSameLinkAs()

TitleValue::isSameLinkAs ( LinkTarget $other)

Check whether the given LinkTarget refers to the same target as this LinkTarget.

Two link targets are considered the same if they have the same interwiki prefix, are in the same namespace, have the same main part, and the same fragment.

Since
1.36
Parameters
LinkTarget$other
Returns
bool

Implements MediaWiki\Linker\LinkTarget.

Definition at line 257 of file TitleValue.php.

◆ newFromPage()

static TitleValue::newFromPage ( PageReference $page)
static

Create a TitleValue from a local PageReference.

Note
The PageReference may belong to another wiki. In that case, the resulting TitleValue is also logically bound to that other wiki. No attempt is made to map the PageReference wiki ID to an interwiki prefix for the TitleValue.
Since
1.36
Parameters
PageReference$page
Returns
TitleValue

Definition at line 103 of file TitleValue.php.

References getDBkey(), and getNamespace().

◆ tryNew()

static TitleValue::tryNew ( $namespace,
$title,
$fragment = '',
$interwiki = '' )
static

Constructs a TitleValue, or returns null if the parameters are not valid.

Note
This does not perform any normalization, and only basic validation. For full normalization and validation, use TitleParser::makeTitleValueSafe().
Parameters
int$namespaceThe namespace ID. This is not validated.
string$titleThe page title in either DBkey or text form. No normalization is applied beyond underscore/space conversion.
string$fragmentThe fragment title. Use '' to represent the whole page. No validation or normalization is applied.
string$interwikiThe interwiki component. No validation or normalization is applied.
Returns
TitleValue|null
Exceptions
InvalidArgumentException

Definition at line 80 of file TitleValue.php.

References $title.

Member Data Documentation

◆ $prefixedText

string TitleValue::$prefixedText = null

Text form including namespace/interwiki, initialised on demand.

Only public to share cache with TitleFormatter

Access: internal

Definition at line 62 of file TitleValue.php.


The documentation for this class was generated from the following file: