3 * A codec for MediaWiki page titles.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Daniel Kinzler
24 namespace MediaWiki\Title
;
26 use InvalidArgumentException
;
28 use MediaWiki\Cache\GenderCache
;
29 use MediaWiki\Interwiki\InterwikiLookup
;
30 use MediaWiki\Language\Language
;
31 use MediaWiki\Linker\LinkTarget
;
32 use MediaWiki\Message\Message
;
33 use MediaWiki\Page\PageReference
;
34 use MediaWiki\Parser\Sanitizer
;
35 use Wikimedia\IPUtils
;
38 * A codec for MediaWiki page titles.
40 * @note Normalization and validation is applied while parsing, not when formatting.
41 * It's possible to construct a TitleValue with an invalid title, and use MediaWikiTitleCodec
42 * to generate an (invalid) title string from it. TitleValues should be constructed only
43 * via parseTitle() or from a (semi)trusted source, such as the database.
45 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
48 class MediaWikiTitleCodec
implements TitleFormatter
, TitleParser
{
49 protected Language
$language;
50 protected GenderCache
$genderCache;
52 protected array $localInterwikis;
53 protected InterwikiLookup
$interwikiLookup;
54 protected NamespaceInfo
$nsInfo;
57 * The code here can throw MalformedTitleException, which cannot be created in
58 * unit tests (see T281935). Until that changes, we use this helper callback
59 * that can be overridden in unit tests to return a mock instead.
63 private $createMalformedTitleException;
66 * @param Language $language The language object to use for localizing namespace names,
67 * capitalization, etc.
68 * @param GenderCache $genderCache The gender cache for generating gendered namespace names
69 * @param string[]|string $localInterwikis
70 * @param InterwikiLookup $interwikiLookup
71 * @param NamespaceInfo $nsInfo
73 public function __construct(
75 GenderCache
$genderCache,
77 InterwikiLookup
$interwikiLookup,
80 $this->language
= $language;
81 $this->genderCache
= $genderCache;
82 $this->localInterwikis
= (array)$localInterwikis;
83 $this->interwikiLookup
= $interwikiLookup;
84 $this->nsInfo
= $nsInfo;
86 // Default callback is to return a real MalformedTitleException,
87 // callback signature matches constructor
88 $this->createMalformedTitleException
= static function (
91 $errorMessageParameters = []
92 ): MalformedTitleException
{
93 return new MalformedTitleException( $errorMessage, $titleText, $errorMessageParameters );
99 * @param callable $callback
101 public function overrideCreateMalformedTitleExceptionCallback( callable
$callback ) {
102 // @codeCoverageIgnoreStart
103 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
104 throw new LogicException( __METHOD__
. ' can only be used in tests' );
106 // @codeCoverageIgnoreEnd
107 $this->createMalformedTitleException
= $callback;
111 * @see TitleFormatter::getNamespaceName()
113 * @param int $namespace
114 * @param string $text
116 * @throws InvalidArgumentException If the namespace is invalid
117 * @return string Namespace name with underscores (not spaces), e.g. 'User_talk'
119 public function getNamespaceName( $namespace, $text ) {
120 if ( $this->language
->needsGenderDistinction() &&
121 $this->nsInfo
->hasGenderDistinction( $namespace )
123 // NOTE: we are assuming here that the title text is a user name!
124 $gender = $this->genderCache
->getGenderOf( $text, __METHOD__
);
125 $name = $this->language
->getGenderNsText( $namespace, $gender );
127 $name = $this->language
->getNsText( $namespace );
130 if ( $name === false ) {
131 throw new InvalidArgumentException( 'Unknown namespace ID: ' . $namespace );
138 * @see TitleFormatter::formatTitle()
140 * @param int|false $namespace The namespace ID (or false, if the namespace should be ignored)
141 * @param string $text The page title. Should be valid. Only minimal normalization is applied.
142 * Underscores will be replaced.
143 * @param string $fragment The fragment name (may be empty).
144 * @param string $interwiki The interwiki name (may be empty).
146 * @throws InvalidArgumentException If the namespace is invalid
149 public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
151 if ( $interwiki !== '' ) {
152 $out = $interwiki . ':';
155 if ( $namespace != 0 ) {
157 $nsName = $this->getNamespaceName( $namespace, $text );
158 } catch ( InvalidArgumentException
$e ) {
159 // See T165149. Awkward, but better than erroneously linking to the main namespace.
160 $nsName = $this->language
->getNsText( NS_SPECIAL
) . ":Badtitle/NS{$namespace}";
163 $out .= $nsName . ':';
167 if ( $fragment !== '' ) {
168 $out .= '#' . $fragment;
171 $out = str_replace( '_', ' ', $out );
177 * Parses the given text and constructs a TitleValue.
179 * @param string $text The text to parse
180 * @param int $defaultNamespace Namespace to assume by default (usually NS_MAIN)
182 * @throws MalformedTitleException
185 public function parseTitle( $text, $defaultNamespace = NS_MAIN
) {
186 // Convert things like é ā or 〗 into normalized (T16952) text
187 $filteredText = Sanitizer
::decodeCharReferencesAndNormalize( $text );
189 // NOTE: this is an ugly kludge that allows this class to share the
190 // code for parsing with the old Title class. The parser code should
191 // be refactored to avoid this.
192 $parts = $this->splitTitleString( $filteredText, $defaultNamespace );
194 return new TitleValue(
203 * Given a namespace and title, return a TitleValue if valid, or null if invalid.
205 * @param int $namespace
206 * @param string $text
207 * @param string $fragment
208 * @param string $interwiki
210 * @return TitleValue|null
212 public function makeTitleValueSafe( $namespace, $text, $fragment = '', $interwiki = '' ) {
213 if ( !$this->nsInfo
->exists( $namespace ) ) {
217 $canonicalNs = $this->nsInfo
->getCanonicalName( $namespace );
218 $fullText = $canonicalNs == '' ?
$text : "$canonicalNs:$text";
219 if ( strval( $interwiki ) != '' ) {
220 $fullText = "$interwiki:$fullText";
222 if ( strval( $fragment ) != '' ) {
223 $fullText .= '#' . $fragment;
227 $parts = $this->splitTitleString( $fullText );
228 } catch ( MalformedTitleException
$e ) {
232 return new TitleValue(
233 $parts['namespace'], $parts['dbkey'], $parts['fragment'], $parts['interwiki'] );
237 * @see TitleFormatter::getText()
239 * @param LinkTarget|PageReference $title
243 public function getText( $title ) {
244 if ( $title instanceof LinkTarget
) {
245 return $title->getText();
246 } elseif ( $title instanceof PageReference
) {
247 return strtr( $title->getDBKey(), '_', ' ' );
249 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
254 * @see TitleFormatter::getText()
256 * @param LinkTarget|PageReference $title
259 * @suppress PhanUndeclaredProperty
261 public function getPrefixedText( $title ) {
262 if ( $title instanceof LinkTarget
) {
263 if ( !isset( $title->prefixedText
) ) {
264 $title->prefixedText
= $this->formatTitle(
265 $title->getNamespace(),
268 $title->getInterwiki()
271 return $title->prefixedText
;
272 } elseif ( $title instanceof PageReference
) {
273 $title->assertWiki( PageReference
::LOCAL
);
274 return $this->formatTitle(
275 $title->getNamespace(),
276 $this->getText( $title )
279 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
285 * @see TitleFormatter::getPrefixedDBkey()
286 * @param LinkTarget|PageReference $target
289 public function getPrefixedDBkey( $target ) {
290 if ( $target instanceof LinkTarget
) {
291 return strtr( $this->formatTitle(
292 $target->getNamespace(),
295 $target->getInterwiki()
297 } elseif ( $target instanceof PageReference
) {
298 $target->assertWiki( PageReference
::LOCAL
);
299 return strtr( $this->formatTitle(
300 $target->getNamespace(),
304 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $target ) );
309 * @see TitleFormatter::getText()
311 * @param LinkTarget|PageReference $title
315 public function getFullText( $title ) {
316 if ( $title instanceof LinkTarget
) {
317 return $this->formatTitle(
318 $title->getNamespace(),
320 $title->getFragment(),
321 $title->getInterwiki()
323 } elseif ( $title instanceof PageReference
) {
324 $title->assertWiki( PageReference
::LOCAL
);
325 return $this->formatTitle(
326 $title->getNamespace(),
327 $this->getText( $title )
330 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
335 * Validates, normalizes and splits a title string.
336 * This is the "source of truth" for title validity.
338 * This function removes illegal characters, splits off the interwiki and
339 * namespace prefixes, sets the other forms, and canonicalizes
342 * @todo this method is only exposed as a temporary measure to ease refactoring.
343 * It was copied with minimal changes from Title::secureAndSplit().
345 * @todo This method should be split up and an appropriate interface
346 * defined for use by the Title class.
348 * @param string $text
349 * @param int $defaultNamespace
352 * @throws MalformedTitleException If $text is not a valid title string.
353 * @return array A map with the fields 'interwiki', 'fragment', 'namespace', and 'dbkey'.
355 public function splitTitleString( $text, $defaultNamespace = NS_MAIN
) {
356 $dbkey = str_replace( ' ', '_', $text );
361 'local_interwiki' => false,
363 'namespace' => (int)$defaultNamespace,
367 # Strip Unicode bidi override characters.
368 # Sometimes they slip into cut-n-pasted page titles, where the
369 # override chars get included in list displays.
370 $dbkey = preg_replace( '/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u', '', $dbkey );
372 if ( $dbkey === null ) {
373 # Regex had an error. Most likely this is caused by invalid UTF-8
374 $exception = ( $this->createMalformedTitleException
)( 'title-invalid-utf8', $text );
378 # Clean up whitespace
379 $dbkey = preg_replace(
380 '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
384 $dbkey = trim( $dbkey, '_' );
386 if ( strpos( $dbkey, \UtfNormal\Constants
::UTF8_REPLACEMENT
) !== false ) {
387 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
388 $exception = ( $this->createMalformedTitleException
)( 'title-invalid-utf8', $text );
392 $parts['dbkey'] = $dbkey;
394 # Initial colon indicates main namespace rather than specified default
395 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
396 if ( $dbkey !== '' && $dbkey[0] == ':' ) {
397 $parts['namespace'] = NS_MAIN
;
398 $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing
399 $dbkey = trim( $dbkey, '_' ); # remove any subsequent whitespace
402 if ( $dbkey == '' ) {
403 $exception = ( $this->createMalformedTitleException
)( 'title-invalid-empty', $text );
407 # Namespace or interwiki prefix
408 $prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
411 if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
413 $ns = $this->language
->getNsIndex( $p );
414 if ( $ns !== false ) {
417 $parts['namespace'] = $ns;
418 # For Talk:X pages, check if X has a "namespace" prefix
419 if ( $ns === NS_TALK
&& preg_match( $prefixRegexp, $dbkey, $x ) ) {
420 if ( $this->language
->getNsIndex( $x[1] ) ) {
421 # Disallow Talk:File:x type titles...
422 $exception = ( $this->createMalformedTitleException
)(
423 'title-invalid-talk-namespace',
427 } elseif ( $this->interwikiLookup
->isValidInterwiki( $x[1] ) ) {
428 # Disallow Talk:Interwiki:x type titles...
429 $exception = ( $this->createMalformedTitleException
)(
430 'title-invalid-talk-interwiki',
436 } elseif ( $this->interwikiLookup
->isValidInterwiki( $p ) ) {
439 $parts['interwiki'] = $this->language
->lc( $p );
441 # Redundant interwiki prefix to the local wiki
442 foreach ( $this->localInterwikis
as $localIW ) {
443 if ( strcasecmp( $parts['interwiki'], $localIW ) == 0 ) {
444 if ( $dbkey == '' ) {
445 # Empty self-links should point to the Main Page, to ensure
446 # compatibility with cross-wiki transclusions and the like.
447 $mainPage = Title
::newMainPage();
449 'interwiki' => $mainPage->getInterwiki(),
450 'local_interwiki' => true,
451 'fragment' => $mainPage->getFragment(),
452 'namespace' => $mainPage->getNamespace(),
453 'dbkey' => $mainPage->getDBkey(),
456 $parts['interwiki'] = '';
457 # local interwikis should behave like initial-colon links
458 $parts['local_interwiki'] = true;
460 # Do another namespace split...
465 # If there's an initial colon after the interwiki, that also
466 # resets the default namespace
467 if ( $dbkey !== '' && $dbkey[0] == ':' ) {
468 $parts['namespace'] = NS_MAIN
;
469 $dbkey = substr( $dbkey, 1 );
470 $dbkey = trim( $dbkey, '_' );
473 # If there's no recognized interwiki or namespace,
474 # then let the colon expression be part of the title.
479 $fragment = strstr( $dbkey, '#' );
480 if ( $fragment !== false ) {
481 $parts['fragment'] = str_replace( '_', ' ', substr( $fragment, 1 ) );
482 $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
483 # remove whitespace again: prevents "Foo_bar_#"
484 # becoming "Foo_bar_"
485 $dbkey = rtrim( $dbkey, "_" );
488 # Reject illegal characters.
489 $rxTc = self
::getTitleInvalidRegex();
491 if ( preg_match( $rxTc, $dbkey, $matches ) ) {
492 $exception = ( $this->createMalformedTitleException
)( 'title-invalid-characters', $text, [ $matches[0] ] );
496 # Pages with "/./" or "/../" appearing in the URLs will often be un-
497 # reachable due to the way web browsers deal with 'relative' URLs.
498 # Also, they conflict with subpage syntax. Forbid them explicitly.
500 str_contains( $dbkey, '.' ) &&
502 $dbkey === '.' ||
$dbkey === '..' ||
503 str_starts_with( $dbkey, './' ) ||
504 str_starts_with( $dbkey, '../' ) ||
505 str_contains( $dbkey, '/./' ) ||
506 str_contains( $dbkey, '/../' ) ||
507 str_ends_with( $dbkey, '/.' ) ||
508 str_ends_with( $dbkey, '/..' )
511 $exception = ( $this->createMalformedTitleException
)( 'title-invalid-relative', $text );
515 # Magic tilde sequences? Nu-uh!
516 if ( strpos( $dbkey, '~~~' ) !== false ) {
517 $exception = ( $this->createMalformedTitleException
)( 'title-invalid-magic-tilde', $text );
521 # Limit the size of titles to 255 bytes. This is typically the size of the
522 # underlying database field. We make an exception for special pages, which
523 # don't need to be stored in the database, and may edge over 255 bytes due
524 # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
525 $maxLength = ( $parts['namespace'] !== NS_SPECIAL
) ?
255 : 512;
526 if ( strlen( $dbkey ) > $maxLength ) {
527 $exception = ( $this->createMalformedTitleException
)(
528 'title-invalid-too-long',
530 [ Message
::numParam( $maxLength ), Message
::numParam( strlen( $dbkey ) ) ]
535 # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
536 # and [[Foo]] point to the same place. Don't force it for interwikis, since the
537 # other site might be case-sensitive.
538 if ( $parts['interwiki'] === '' && $this->nsInfo
->isCapitalized( $parts['namespace'] ) ) {
539 $dbkey = $this->language
->ucfirst( $dbkey );
542 # Can't make a link to a namespace alone... "empty" local links can only be
543 # self-links with a fragment identifier.
544 if ( $dbkey == '' && $parts['interwiki'] === '' && $parts['namespace'] !== NS_MAIN
) {
545 $exception = ( $this->createMalformedTitleException
)( 'title-invalid-empty', $text );
549 // Allow IPv6 usernames to start with '::' by canonicalizing IPv6 titles.
550 // IP names are not allowed for accounts, and can only be referring to
551 // edits from the IP. Given '::' abbreviations and caps/lowercaps,
552 // there are numerous ways to present the same IP. Having sp:contribs scan
553 // them all is silly and having some show the edits and others not is
554 // inconsistent. Same for talk/userpages. Keep them normalized instead.
555 if ( $dbkey !== '' && ( $parts['namespace'] === NS_USER ||
$parts['namespace'] === NS_USER_TALK
) ) {
556 $dbkey = IPUtils
::sanitizeIP( $dbkey );
557 // IPUtils::sanitizeIP return null only for bad input
558 '@phan-var string $dbkey';
561 // Any remaining initial :s are illegal.
562 if ( $dbkey !== '' && $dbkey[0] == ':' ) {
563 $exception = ( $this->createMalformedTitleException
)( 'title-invalid-leading-colon', $text );
568 $parts['dbkey'] = $dbkey;
570 // Check to ensure that the return value can be used to construct a TitleValue.
571 // All issues should in theory be caught above, this is here to enforce consistency.
573 TitleValue
::assertValidSpec(
579 } catch ( InvalidArgumentException
$ex ) {
580 $exception = ( $this->createMalformedTitleException
)( 'title-invalid', $text, [ $ex->getMessage() ] );
588 * Returns a simple regex that will match on characters and sequences invalid in titles.
589 * Note that this doesn't pick up many things that could be wrong with titles, but that
590 * replacing this regex with something valid will make many titles valid.
591 * Previously Title::getTitleInvalidRegex()
593 * @return string Regex string
596 public static function getTitleInvalidRegex() {
597 static $rxTc = false;
599 # Matching titles will be held as illegal.
601 # Any character not allowed is forbidden...
602 '[^' . Title
::legalChars() . ']' .
603 # URL percent encoding sequences interfere with the ability
604 # to round-trip titles -- you can't link to them consistently.
606 # XML/HTML character references produce similar issues.
607 '|&[A-Za-z0-9\x80-\xff]+;' .
615 /** @deprecated class alias since 1.41 */
616 class_alias( MediaWikiTitleCodec
::class, 'MediaWikiTitleCodec' );