Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / title / MediaWikiTitleCodec.php
blob190bfa1ca89051be45f5884252f01b04496cbe0c
1 <?php
2 /**
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
20 * @file
21 * @author Daniel Kinzler
24 namespace MediaWiki\Title;
26 use InvalidArgumentException;
27 use LogicException;
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;
37 /**
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
46 * @since 1.23
48 class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
49 protected Language $language;
50 protected GenderCache $genderCache;
51 /** @var string[] */
52 protected array $localInterwikis;
53 protected InterwikiLookup $interwikiLookup;
54 protected NamespaceInfo $nsInfo;
56 /**
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.
61 * @var callable
63 private $createMalformedTitleException;
65 /**
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(
74 Language $language,
75 GenderCache $genderCache,
76 $localInterwikis,
77 InterwikiLookup $interwikiLookup,
78 NamespaceInfo $nsInfo
79 ) {
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 (
89 $errorMessage,
90 $titleText = null,
91 $errorMessageParameters = []
92 ): MalformedTitleException {
93 return new MalformedTitleException( $errorMessage, $titleText, $errorMessageParameters );
97 /**
98 * @internal
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 );
126 } else {
127 $name = $this->language->getNsText( $namespace );
130 if ( $name === false ) {
131 throw new InvalidArgumentException( 'Unknown namespace ID: ' . $namespace );
134 return $name;
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
147 * @return string
149 public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
150 $out = '';
151 if ( $interwiki !== '' ) {
152 $out = $interwiki . ':';
155 if ( $namespace != 0 ) {
156 try {
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 . ':';
165 $out .= $text;
167 if ( $fragment !== '' ) {
168 $out .= '#' . $fragment;
171 $out = str_replace( '_', ' ', $out );
173 return $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
183 * @return TitleValue
185 public function parseTitle( $text, $defaultNamespace = NS_MAIN ) {
186 // Convert things like &eacute; &#257; or &#x3017; 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(
195 $parts['namespace'],
196 $parts['dbkey'],
197 $parts['fragment'],
198 $parts['interwiki']
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 ) ) {
214 return null;
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;
226 try {
227 $parts = $this->splitTitleString( $fullText );
228 } catch ( MalformedTitleException $e ) {
229 return null;
232 return new TitleValue(
233 $parts['namespace'], $parts['dbkey'], $parts['fragment'], $parts['interwiki'] );
237 * @see TitleFormatter::getText()
239 * @param LinkTarget|PageReference $title
241 * @return string
243 public function getText( $title ) {
244 if ( $title instanceof LinkTarget ) {
245 return $title->getText();
246 } elseif ( $title instanceof PageReference ) {
247 return strtr( $title->getDBKey(), '_', ' ' );
248 } else {
249 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
254 * @see TitleFormatter::getText()
256 * @param LinkTarget|PageReference $title
258 * @return string
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(),
266 $title->getText(),
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 )
278 } else {
279 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) );
284 * @since 1.27
285 * @see TitleFormatter::getPrefixedDBkey()
286 * @param LinkTarget|PageReference $target
287 * @return string
289 public function getPrefixedDBkey( $target ) {
290 if ( $target instanceof LinkTarget ) {
291 return strtr( $this->formatTitle(
292 $target->getNamespace(),
293 $target->getDBkey(),
295 $target->getInterwiki()
296 ), ' ', '_' );
297 } elseif ( $target instanceof PageReference ) {
298 $target->assertWiki( PageReference::LOCAL );
299 return strtr( $this->formatTitle(
300 $target->getNamespace(),
301 $target->getDBkey()
302 ), ' ', '_' );
303 } else {
304 throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $target ) );
309 * @see TitleFormatter::getText()
311 * @param LinkTarget|PageReference $title
313 * @return string
315 public function getFullText( $title ) {
316 if ( $title instanceof LinkTarget ) {
317 return $this->formatTitle(
318 $title->getNamespace(),
319 $title->getText(),
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 )
329 } else {
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
340 * everything.
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
351 * @internal
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 );
358 # Initialisation
359 $parts = [
360 'interwiki' => '',
361 'local_interwiki' => false,
362 'fragment' => '',
363 'namespace' => (int)$defaultNamespace,
364 'dbkey' => $dbkey,
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 );
375 throw $exception;
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',
381 '_',
382 $dbkey
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 );
389 throw $exception;
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 );
404 throw $exception;
407 # Namespace or interwiki prefix
408 $prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
409 do {
410 $m = [];
411 if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
412 $p = $m[1];
413 $ns = $this->language->getNsIndex( $p );
414 if ( $ns !== false ) {
415 # Ordinary namespace
416 $dbkey = $m[2];
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',
424 $text
426 throw $exception;
427 } elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
428 # Disallow Talk:Interwiki:x type titles...
429 $exception = ( $this->createMalformedTitleException )(
430 'title-invalid-talk-interwiki',
431 $text
433 throw $exception;
436 } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
437 # Interwiki link
438 $dbkey = $m[2];
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();
448 return [
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...
461 continue 2;
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.
476 break;
477 } while ( true );
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();
490 $matches = [];
491 if ( preg_match( $rxTc, $dbkey, $matches ) ) {
492 $exception = ( $this->createMalformedTitleException )( 'title-invalid-characters', $text, [ $matches[0] ] );
493 throw $exception;
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.
499 if (
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 );
512 throw $exception;
515 # Magic tilde sequences? Nu-uh!
516 if ( strpos( $dbkey, '~~~' ) !== false ) {
517 $exception = ( $this->createMalformedTitleException )( 'title-invalid-magic-tilde', $text );
518 throw $exception;
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',
529 $text,
530 [ Message::numParam( $maxLength ), Message::numParam( strlen( $dbkey ) ) ]
532 throw $exception;
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 );
546 throw $exception;
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 );
564 throw $exception;
567 // Fill fields
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.
572 try {
573 TitleValue::assertValidSpec(
574 $parts['namespace'],
575 $parts['dbkey'],
576 $parts['fragment'],
577 $parts['interwiki']
579 } catch ( InvalidArgumentException $ex ) {
580 $exception = ( $this->createMalformedTitleException )( 'title-invalid', $text, [ $ex->getMessage() ] );
581 throw $exception;
584 return $parts;
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
594 * @since 1.25
596 public static function getTitleInvalidRegex() {
597 static $rxTc = false;
598 if ( !$rxTc ) {
599 # Matching titles will be held as illegal.
600 $rxTc = '/' .
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.
605 '|%[0-9A-Fa-f]{2}' .
606 # XML/HTML character references produce similar issues.
607 '|&[A-Za-z0-9\x80-\xff]+;' .
608 '/S';
611 return $rxTc;
615 /** @deprecated class alias since 1.41 */
616 class_alias( MediaWikiTitleCodec::class, 'MediaWikiTitleCodec' );