355 $dbkey = str_replace(
' ',
'_', $text );
360 'local_interwiki' =>
false,
362 'namespace' => (int)$defaultNamespace,
366 # Strip Unicode bidi override characters.
367 # Sometimes they slip into cut-n-pasted page titles, where the
368 # override chars get included in list displays.
369 $dbkey = preg_replace(
'/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u',
'', $dbkey );
371 if ( $dbkey ===
null ) {
372 # Regex had an error. Most likely this is caused by invalid UTF-8
373 $exception = ( $this->createMalformedTitleException )(
'title-invalid-utf8', $text );
377 # Clean up whitespace
378 # Note: use of the /u option on preg_replace here will cause
379 # input with invalid UTF-8 sequences to be nullified out in PHP 5.2.x,
380 # conveniently disabling them.
381 $dbkey = preg_replace(
382 '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
386 $dbkey = trim( $dbkey,
'_' );
388 if ( strpos( $dbkey, UtfNormal\Constants::UTF8_REPLACEMENT ) !==
false ) {
389 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
390 $exception = ( $this->createMalformedTitleException )(
'title-invalid-utf8', $text );
394 $parts[
'dbkey'] = $dbkey;
396 # Initial colon indicates main namespace rather than specified default
397 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
398 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
400 $dbkey = substr( $dbkey, 1 ); #
remove the colon but
continue processing
401 $dbkey = trim( $dbkey,
'_' ); #
remove any subsequent whitespace
404 if ( $dbkey ==
'' ) {
405 $exception = ( $this->createMalformedTitleException )(
'title-invalid-empty', $text );
409 # Namespace or interwiki prefix
410 $prefixRegexp =
"/^(.+?)_*:_*(.*)$/S";
413 if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
415 $ns = $this->language->getNsIndex( $p );
416 if ( $ns !==
false ) {
419 $parts[
'namespace'] = $ns;
420 # For Talk:X pages, check if X has a "namespace" prefix
421 if ( $ns ===
NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
422 if ( $this->language->getNsIndex( $x[1] ) ) {
423 # Disallow Talk:File:x type titles...
424 $exception = ( $this->createMalformedTitleException )(
425 'title-invalid-talk-namespace',
429 } elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
430 # Disallow Talk:Interwiki:x type titles...
431 $exception = ( $this->createMalformedTitleException )(
432 'title-invalid-talk-namespace',
438 } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
441 $parts[
'interwiki'] = $this->language->lc( $p );
443 # Redundant interwiki prefix to the local wiki
444 foreach ( $this->localInterwikis as $localIW ) {
445 if ( strcasecmp( $parts[
'interwiki'], $localIW ) == 0 ) {
446 if ( $dbkey ==
'' ) {
447 # Empty self-links should point to the Main Page, to ensure
448 # compatibility with cross-wiki transclusions and the like.
449 $mainPage = Title::newMainPage();
451 'interwiki' => $mainPage->getInterwiki(),
452 'local_interwiki' =>
true,
453 'fragment' => $mainPage->getFragment(),
454 'namespace' => $mainPage->getNamespace(),
455 'dbkey' => $mainPage->getDBkey(),
458 $parts[
'interwiki'] =
'';
459 # local interwikis should behave like initial-colon links
460 $parts[
'local_interwiki'] =
true;
462 # Do another namespace split...
467 # If there's an initial colon after the interwiki, that also
468 # resets the default namespace
469 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
471 $dbkey = substr( $dbkey, 1 );
472 $dbkey = trim( $dbkey,
'_' );
475 # If there's no recognized interwiki or namespace,
476 # then let the colon expression be part of the title.
481 $fragment = strstr( $dbkey,
'#' );
482 if ( $fragment !==
false ) {
483 $parts[
'fragment'] = str_replace(
'_',
' ', substr( $fragment, 1 ) );
484 $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
485 # remove whitespace again: prevents "Foo_bar_#"
486 # becoming "Foo_bar_"
487 $dbkey = preg_replace(
'/_*$/',
'', $dbkey );
490 # Reject illegal characters.
491 $rxTc = self::getTitleInvalidRegex();
493 if ( preg_match( $rxTc, $dbkey,
$matches ) ) {
494 $exception = ( $this->createMalformedTitleException )(
'title-invalid-characters', $text, [
$matches[0] ] );
498 # Pages with "/./" or "/../" appearing in the URLs will often be un-
499 # reachable due to the way web browsers deal with 'relative' URLs.
500 # Also, they conflict with subpage syntax. Forbid them explicitly.
502 strpos( $dbkey,
'.' ) !==
false &&
504 $dbkey ===
'.' || $dbkey ===
'..' ||
505 strpos( $dbkey,
'./' ) === 0 ||
506 strpos( $dbkey,
'../' ) === 0 ||
507 strpos( $dbkey,
'/./' ) !==
false ||
508 strpos( $dbkey,
'/../' ) !==
false ||
509 substr( $dbkey, -2 ) ==
'/.' ||
510 substr( $dbkey, -3 ) ==
'/..'
513 $exception = ( $this->createMalformedTitleException )(
'title-invalid-relative', $text );
517 # Magic tilde sequences? Nu-uh!
518 if ( strpos( $dbkey,
'~~~' ) !== false ) {
519 $exception = ( $this->createMalformedTitleException )(
'title-invalid-magic-tilde', $text );
523 # Limit the size of titles to 255 bytes. This is typically the size of the
524 # underlying database field. We make an exception for special pages, which
525 # don't need to be stored in the database, and may edge over 255 bytes due
526 # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
527 $maxLength = ( $parts[
'namespace'] !==
NS_SPECIAL ) ? 255 : 512;
528 if ( strlen( $dbkey ) > $maxLength ) {
529 $exception = ( $this->createMalformedTitleException )(
530 'title-invalid-too-long',
537 # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
538 # and [[Foo]] point to the same place. Don't force it for interwikis, since the
539 # other site might be case-sensitive.
540 if ( $parts[
'interwiki'] ===
'' && $this->nsInfo->isCapitalized( $parts[
'namespace'] ) ) {
541 $dbkey = $this->language->ucfirst( $dbkey );
544 # Can't make a link to a namespace alone... "empty" local links can only be
545 # self-links with a fragment identifier.
546 if ( $dbkey ==
'' && $parts[
'interwiki'] ===
'' && $parts[
'namespace'] !==
NS_MAIN ) {
547 $exception = ( $this->createMalformedTitleException )(
'title-invalid-empty', $text );
558 $dbkey = IPUtils::sanitizeIP( $dbkey );
562 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
563 $exception = ( $this->createMalformedTitleException )(
'title-invalid-leading-colon', $text );
568 $parts[
'dbkey'] = $dbkey;
573 TitleValue::assertValidSpec(
579 }
catch ( InvalidArgumentException $ex ) {
580 $exception = ( $this->createMalformedTitleException )(
'title-invalid', $text, [ $ex->getMessage() ] );