120 if ( $this->language->needsGenderDistinction() &&
121 $this->nsInfo->hasGenderDistinction( $namespace )
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 );
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] ==
':' ) {
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.
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] ==
':' ) {
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.
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',
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 );
555 if ( $dbkey !==
'' && ( $parts[
'namespace'] ===
NS_USER || $parts[
'namespace'] ===
NS_USER_TALK ) ) {
556 $dbkey = IPUtils::sanitizeIP( $dbkey );
558 '@phan-var string $dbkey';
562 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
563 $exception = ( $this->createMalformedTitleException )(
'title-invalid-leading-colon', $text );
568 $parts[
'dbkey'] = $dbkey;
579 }
catch ( InvalidArgumentException $ex ) {
580 $exception = ( $this->createMalformedTitleException )(
'title-invalid', $text, [ $ex->getMessage() ] );