27 use InvalidArgumentException;
35 use Wikimedia\IPUtils;
71 private $createMalformedTitleException;
96 $this->createMalformedTitleException =
static function (
99 $errorMessageParameters = []
111 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
112 throw new LogicException( __METHOD__ .
' can only be used in tests' );
115 $this->createMalformedTitleException = $callback;
128 if ( $this->language->needsGenderDistinction() &&
129 $this->nsInfo->hasGenderDistinction( $namespace )
132 $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
133 $name = $this->language->getGenderNsText( $namespace, $gender );
135 $name = $this->language->getNsText( $namespace );
138 if ( $name ===
false ) {
139 throw new InvalidArgumentException(
'Unknown namespace ID: ' . $namespace );
157 public function formatTitle( $namespace, $text, $fragment =
'', $interwiki =
'' ) {
159 if ( $interwiki !==
'' ) {
160 $out = $interwiki .
':';
163 if ( $namespace != 0 ) {
166 }
catch ( InvalidArgumentException $e ) {
168 $nsName = $this->language->getNsText(
NS_SPECIAL ) .
":Badtitle/NS{$namespace}";
171 $out .= $nsName .
':';
175 if ( $fragment !==
'' ) {
176 $out .=
'#' . $fragment;
179 $out = str_replace(
'_',
' ', $out );
221 if ( !$this->nsInfo->exists( $namespace ) ) {
225 $canonicalNs = $this->nsInfo->getCanonicalName( $namespace );
226 $fullText = $canonicalNs ==
'' ? $text :
"$canonicalNs:$text";
227 if ( strval( $interwiki ) !=
'' ) {
228 $fullText =
"$interwiki:$fullText";
230 if ( strval( $fragment ) !=
'' ) {
231 $fullText .=
'#' . $fragment;
241 $parts[
'namespace'], $parts[
'dbkey'], $parts[
'fragment'], $parts[
'interwiki'] );
253 return $title->getText();
255 return strtr( $title->getDBKey(),
'_',
' ' );
257 throw new InvalidArgumentException(
'$title has invalid type: ' . get_class( $title ) );
271 if ( !isset( $title->prefixedText ) ) {
273 $title->getNamespace(),
276 $title->getInterwiki()
279 return $title->prefixedText;
281 $title->assertWiki( PageReference::LOCAL );
283 $title->getNamespace(),
284 $this->getText( $title )
287 throw new InvalidArgumentException(
'$title has invalid type: ' . get_class( $title ) );
300 $target->getNamespace(),
303 $target->getInterwiki()
306 $target->assertWiki( PageReference::LOCAL );
308 $target->getNamespace(),
312 throw new InvalidArgumentException(
'$title has invalid type: ' . get_class( $target ) );
326 $title->getNamespace(),
328 $title->getFragment(),
329 $title->getInterwiki()
332 $title->assertWiki( PageReference::LOCAL );
334 $title->getNamespace(),
335 $this->getText( $title )
338 throw new InvalidArgumentException(
'$title has invalid type: ' . get_class( $title ) );
364 $dbkey = str_replace(
' ',
'_', $text );
369 'local_interwiki' =>
false,
371 'namespace' => (int)$defaultNamespace,
375 # Strip Unicode bidi override characters.
376 # Sometimes they slip into cut-n-pasted page titles, where the
377 # override chars get included in list displays.
378 $dbkey = preg_replace(
'/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u',
'', $dbkey );
380 if ( $dbkey ===
null ) {
381 # Regex had an error. Most likely this is caused by invalid UTF-8
382 $exception = ( $this->createMalformedTitleException )(
'title-invalid-utf8', $text );
386 # Clean up whitespace
387 $dbkey = preg_replace(
388 '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
392 $dbkey = trim( $dbkey,
'_' );
394 if ( strpos( $dbkey, \UtfNormal\Constants::UTF8_REPLACEMENT ) !==
false ) {
395 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
396 $exception = ( $this->createMalformedTitleException )(
'title-invalid-utf8', $text );
400 $parts[
'dbkey'] = $dbkey;
402 # Initial colon indicates main namespace rather than specified default
403 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
404 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
406 $dbkey = substr( $dbkey, 1 ); #
remove the colon but
continue processing
407 $dbkey = trim( $dbkey,
'_' ); #
remove any subsequent whitespace
410 if ( $dbkey ==
'' ) {
411 $exception = ( $this->createMalformedTitleException )(
'title-invalid-empty', $text );
415 # Namespace or interwiki prefix
416 $prefixRegexp =
"/^(.+?)_*:_*(.*)$/S";
419 if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
421 $ns = $this->language->getNsIndex( $p );
422 if ( $ns !==
false ) {
425 $parts[
'namespace'] = $ns;
426 # For Talk:X pages, check if X has a "namespace" prefix
427 if ( $ns ===
NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
428 if ( $this->language->getNsIndex( $x[1] ) ) {
429 # Disallow Talk:File:x type titles...
430 $exception = ( $this->createMalformedTitleException )(
431 'title-invalid-talk-namespace',
435 } elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
436 # Disallow Talk:Interwiki:x type titles...
437 $exception = ( $this->createMalformedTitleException )(
438 'title-invalid-talk-namespace',
444 } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
447 $parts[
'interwiki'] = $this->language->lc( $p );
449 # Redundant interwiki prefix to the local wiki
450 foreach ( $this->localInterwikis as $localIW ) {
451 if ( strcasecmp( $parts[
'interwiki'], $localIW ) == 0 ) {
452 if ( $dbkey ==
'' ) {
453 # Empty self-links should point to the Main Page, to ensure
454 # compatibility with cross-wiki transclusions and the like.
457 'interwiki' => $mainPage->getInterwiki(),
458 'local_interwiki' =>
true,
459 'fragment' => $mainPage->getFragment(),
460 'namespace' => $mainPage->getNamespace(),
461 'dbkey' => $mainPage->getDBkey(),
464 $parts[
'interwiki'] =
'';
465 # local interwikis should behave like initial-colon links
466 $parts[
'local_interwiki'] =
true;
468 # Do another namespace split...
473 # If there's an initial colon after the interwiki, that also
474 # resets the default namespace
475 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
477 $dbkey = substr( $dbkey, 1 );
478 $dbkey = trim( $dbkey,
'_' );
481 # If there's no recognized interwiki or namespace,
482 # then let the colon expression be part of the title.
487 $fragment = strstr( $dbkey,
'#' );
488 if ( $fragment !==
false ) {
489 $parts[
'fragment'] = str_replace(
'_',
' ', substr( $fragment, 1 ) );
490 $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
491 # remove whitespace again: prevents "Foo_bar_#"
492 # becoming "Foo_bar_"
493 $dbkey = rtrim( $dbkey,
"_" );
496 # Reject illegal characters.
499 if ( preg_match( $rxTc, $dbkey,
$matches ) ) {
500 $exception = ( $this->createMalformedTitleException )(
'title-invalid-characters', $text, [
$matches[0] ] );
504 # Pages with "/./" or "/../" appearing in the URLs will often be un-
505 # reachable due to the way web browsers deal with 'relative' URLs.
506 # Also, they conflict with subpage syntax. Forbid them explicitly.
508 str_contains( $dbkey,
'.' ) &&
510 $dbkey ===
'.' || $dbkey ===
'..' ||
511 str_starts_with( $dbkey,
'./' ) ||
512 str_starts_with( $dbkey,
'../' ) ||
513 str_contains( $dbkey,
'/./' ) ||
514 str_contains( $dbkey,
'/../' ) ||
515 str_ends_with( $dbkey,
'/.' ) ||
516 str_ends_with( $dbkey,
'/..' )
519 $exception = ( $this->createMalformedTitleException )(
'title-invalid-relative', $text );
523 # Magic tilde sequences? Nu-uh!
524 if ( strpos( $dbkey,
'~~~' ) !== false ) {
525 $exception = ( $this->createMalformedTitleException )(
'title-invalid-magic-tilde', $text );
529 # Limit the size of titles to 255 bytes. This is typically the size of the
530 # underlying database field. We make an exception for special pages, which
531 # don't need to be stored in the database, and may edge over 255 bytes due
532 # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
533 $maxLength = ( $parts[
'namespace'] !==
NS_SPECIAL ) ? 255 : 512;
534 if ( strlen( $dbkey ) > $maxLength ) {
535 $exception = ( $this->createMalformedTitleException )(
536 'title-invalid-too-long',
543 # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
544 # and [[Foo]] point to the same place. Don't force it for interwikis, since the
545 # other site might be case-sensitive.
546 if ( $parts[
'interwiki'] ===
'' && $this->nsInfo->isCapitalized( $parts[
'namespace'] ) ) {
547 $dbkey = $this->language->ucfirst( $dbkey );
550 # Can't make a link to a namespace alone... "empty" local links can only be
551 # self-links with a fragment identifier.
552 if ( $dbkey ==
'' && $parts[
'interwiki'] ===
'' && $parts[
'namespace'] !==
NS_MAIN ) {
553 $exception = ( $this->createMalformedTitleException )(
'title-invalid-empty', $text );
563 if ( $dbkey !==
'' && ( $parts[
'namespace'] ===
NS_USER || $parts[
'namespace'] ===
NS_USER_TALK ) ) {
564 $dbkey = IPUtils::sanitizeIP( $dbkey );
566 '@phan-var string $dbkey';
570 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
571 $exception = ( $this->createMalformedTitleException )(
'title-invalid-leading-colon', $text );
576 $parts[
'dbkey'] = $dbkey;
587 }
catch ( InvalidArgumentException $ex ) {
588 $exception = ( $this->createMalformedTitleException )(
'title-invalid', $text, [ $ex->getMessage() ] );
605 static $rxTc =
false;
607 # Matching titles will be held as illegal.
609 # Any character not allowed is forbidden...
611 # URL percent encoding sequences interfere with the ability
612 # to round-trip titles -- you can't link to them consistently.
614 # XML/HTML character references produce similar issues.
615 '|&[A-Za-z0-9\x80-\xff]+;' .
627 class_alias( MediaWikiTitleCodec::class,
'MediaWikiTitleCodec' );
Caches user genders when needed to use correct namespace aliases.
Base class for language-specific code.
The Message class deals with fetching and processing of interface message into a variety of formats.