MediaWiki master
MediaWiki\Title\MediaWikiTitleCodec Class Reference

A codec for MediaWiki page titles. More...

Inherits MediaWiki\Title\TitleFormatter, and MediaWiki\Title\TitleParser.

Collaboration diagram for MediaWiki\Title\MediaWikiTitleCodec:

Public Member Functions

 __construct (Language $language, GenderCache $genderCache, $localInterwikis, InterwikiLookup $interwikiLookup, NamespaceInfo $nsInfo)
 
 formatTitle ( $namespace, $text, $fragment='', $interwiki='')
 
 getFullText ( $title)
 
 getNamespaceName ( $namespace, $text)
 
 getPrefixedDBkey ( $target)
 
 getPrefixedText ( $title)
 
 getText ( $title)
 
 makeTitleValueSafe ( $namespace, $text, $fragment='', $interwiki='')
 Given a namespace and title, return a TitleValue if valid, or null if invalid.
 
 overrideCreateMalformedTitleExceptionCallback (callable $callback)
 
 parseTitle ( $text, $defaultNamespace=NS_MAIN)
 Parses the given text and constructs a TitleValue.
 
 splitTitleString ( $text, $defaultNamespace=NS_MAIN)
 Validates, normalizes and splits a title string.
 

Static Public Member Functions

static getTitleInvalidRegex ()
 Returns a simple regex that will match on characters and sequences invalid in titles.
 

Protected Attributes

GenderCache $genderCache
 
InterwikiLookup $interwikiLookup
 
Language $language
 
array string[] $localInterwikis
 
NamespaceInfo $nsInfo
 

Detailed Description

A codec for MediaWiki page titles.

Note
Normalization and validation is applied while parsing, not when formatting. It's possible to construct a TitleValue with an invalid title, and use MediaWikiTitleCodec to generate an (invalid) title string from it. TitleValues should be constructed only via parseTitle() or from a (semi)trusted source, such as the database.
See also
https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
Since
1.23

Definition at line 48 of file MediaWikiTitleCodec.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Title\MediaWikiTitleCodec::__construct ( Language $language,
GenderCache $genderCache,
$localInterwikis,
InterwikiLookup $interwikiLookup,
NamespaceInfo $nsInfo )
Parameters
Language$languageThe language object to use for localizing namespace names, capitalization, etc.
GenderCache$genderCacheThe gender cache for generating gendered namespace names
string[] | string$localInterwikis
InterwikiLookup$interwikiLookup
NamespaceInfo$nsInfo

Definition at line 73 of file MediaWikiTitleCodec.php.

References MediaWiki\Title\MediaWikiTitleCodec\$genderCache, MediaWiki\Title\MediaWikiTitleCodec\$interwikiLookup, MediaWiki\Title\MediaWikiTitleCodec\$language, MediaWiki\Title\MediaWikiTitleCodec\$localInterwikis, and MediaWiki\Title\MediaWikiTitleCodec\$nsInfo.

Member Function Documentation

◆ formatTitle()

MediaWiki\Title\MediaWikiTitleCodec::formatTitle ( $namespace,
$text,
$fragment = '',
$interwiki = '' )
See also
TitleFormatter::formatTitle()
Parameters
int | false$namespaceThe namespace ID (or false, if the namespace should be ignored)
string$textThe page title. Should be valid. Only minimal normalization is applied. Underscores will be replaced.
string$fragmentThe fragment name (may be empty).
string$interwikiThe interwiki name (may be empty).
Exceptions
InvalidArgumentExceptionIf the namespace is invalid
Returns
string

Implements MediaWiki\Title\TitleFormatter.

Definition at line 149 of file MediaWikiTitleCodec.php.

References MediaWiki\Title\MediaWikiTitleCodec\getNamespaceName(), and NS_SPECIAL.

Referenced by MediaWiki\Title\MediaWikiTitleCodec\getFullText(), MediaWiki\Title\MediaWikiTitleCodec\getPrefixedDBkey(), and MediaWiki\Title\MediaWikiTitleCodec\getPrefixedText().

◆ getFullText()

MediaWiki\Title\MediaWikiTitleCodec::getFullText ( $title)
See also
TitleFormatter::getText()
Parameters
LinkTarget | PageReference$title
Returns
string

Implements MediaWiki\Title\TitleFormatter.

Definition at line 315 of file MediaWikiTitleCodec.php.

References MediaWiki\Title\MediaWikiTitleCodec\formatTitle().

◆ getNamespaceName()

MediaWiki\Title\MediaWikiTitleCodec::getNamespaceName ( $namespace,
$text )
See also
TitleFormatter::getNamespaceName()
Parameters
int$namespace
string$text
Exceptions
InvalidArgumentExceptionIf the namespace is invalid
Returns
string Namespace name with underscores (not spaces), e.g. 'User_talk'

Implements MediaWiki\Title\TitleFormatter.

Definition at line 119 of file MediaWikiTitleCodec.php.

Referenced by MediaWiki\Title\MediaWikiTitleCodec\formatTitle().

◆ getPrefixedDBkey()

MediaWiki\Title\MediaWikiTitleCodec::getPrefixedDBkey ( $target)
Since
1.27
See also
TitleFormatter::getPrefixedDBkey()
Parameters
LinkTarget | PageReference$target
Returns
string

Implements MediaWiki\Title\TitleFormatter.

Definition at line 289 of file MediaWikiTitleCodec.php.

References MediaWiki\Title\MediaWikiTitleCodec\formatTitle().

◆ getPrefixedText()

MediaWiki\Title\MediaWikiTitleCodec::getPrefixedText ( $title)
See also
TitleFormatter::getText()
Parameters
LinkTarget | PageReference$title
Returns
string

Implements MediaWiki\Title\TitleFormatter.

Definition at line 261 of file MediaWikiTitleCodec.php.

References MediaWiki\Title\MediaWikiTitleCodec\formatTitle().

◆ getText()

MediaWiki\Title\MediaWikiTitleCodec::getText ( $title)
See also
TitleFormatter::getText()
Parameters
LinkTarget | PageReference$title
Returns
string

Implements MediaWiki\Title\TitleFormatter.

Definition at line 243 of file MediaWikiTitleCodec.php.

◆ getTitleInvalidRegex()

static MediaWiki\Title\MediaWikiTitleCodec::getTitleInvalidRegex ( )
static

Returns a simple regex that will match on characters and sequences invalid in titles.

Note that this doesn't pick up many things that could be wrong with titles, but that replacing this regex with something valid will make many titles valid. Previously Title::getTitleInvalidRegex()

Returns
string Regex string
Since
1.25

Definition at line 596 of file MediaWikiTitleCodec.php.

References MediaWiki\Title\Title\legalChars().

Referenced by MediaWiki\Languages\LanguageNameUtils\isValidCode(), and MediaWiki\Title\MediaWikiTitleCodec\splitTitleString().

◆ makeTitleValueSafe()

MediaWiki\Title\MediaWikiTitleCodec::makeTitleValueSafe ( $namespace,
$text,
$fragment = '',
$interwiki = '' )

Given a namespace and title, return a TitleValue if valid, or null if invalid.

Parameters
int$namespace
string$text
string$fragment
string$interwiki
Returns
TitleValue|null

Implements MediaWiki\Title\TitleParser.

Definition at line 212 of file MediaWikiTitleCodec.php.

References MediaWiki\Title\MediaWikiTitleCodec\splitTitleString().

◆ overrideCreateMalformedTitleExceptionCallback()

MediaWiki\Title\MediaWikiTitleCodec::overrideCreateMalformedTitleExceptionCallback ( callable $callback)
Access: internal
Parameters
callable$callback

Definition at line 101 of file MediaWikiTitleCodec.php.

◆ parseTitle()

MediaWiki\Title\MediaWikiTitleCodec::parseTitle ( $text,
$defaultNamespace = NS_MAIN )

Parses the given text and constructs a TitleValue.

Parameters
string$textThe text to parse
int$defaultNamespaceNamespace to assume by default (usually NS_MAIN)
Exceptions
MalformedTitleException
Returns
TitleValue

Implements MediaWiki\Title\TitleParser.

Definition at line 185 of file MediaWikiTitleCodec.php.

References MediaWiki\Title\MediaWikiTitleCodec\splitTitleString().

◆ splitTitleString()

MediaWiki\Title\MediaWikiTitleCodec::splitTitleString ( $text,
$defaultNamespace = NS_MAIN )

Validates, normalizes and splits a title string.

This is the "source of truth" for title validity.

This function removes illegal characters, splits off the interwiki and namespace prefixes, sets the other forms, and canonicalizes everything.

Todo
this method is only exposed as a temporary measure to ease refactoring. It was copied with minimal changes from Title::secureAndSplit().
Todo
This method should be split up and an appropriate interface defined for use by the Title class.
Parameters
string$text
int$defaultNamespace
Access: internal
Exceptions
MalformedTitleExceptionIf $text is not a valid title string.
Returns
array A map with the fields 'interwiki', 'fragment', 'namespace', and 'dbkey'.

Definition at line 355 of file MediaWikiTitleCodec.php.

References $matches, MediaWiki\Title\TitleValue\assertValidSpec(), MediaWiki\Title\MediaWikiTitleCodec\getTitleInvalidRegex(), MediaWiki\Title\Title\newMainPage(), NS_MAIN, NS_SPECIAL, NS_TALK, NS_USER, NS_USER_TALK, and MediaWiki\Message\Message\numParam().

Referenced by MediaWiki\Title\MediaWikiTitleCodec\makeTitleValueSafe(), and MediaWiki\Title\MediaWikiTitleCodec\parseTitle().

Member Data Documentation

◆ $genderCache

GenderCache MediaWiki\Title\MediaWikiTitleCodec::$genderCache
protected

◆ $interwikiLookup

InterwikiLookup MediaWiki\Title\MediaWikiTitleCodec::$interwikiLookup
protected

◆ $language

Language MediaWiki\Title\MediaWikiTitleCodec::$language
protected

◆ $localInterwikis

array string [] MediaWiki\Title\MediaWikiTitleCodec::$localInterwikis
protected

◆ $nsInfo

NamespaceInfo MediaWiki\Title\MediaWikiTitleCodec::$nsInfo
protected

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