78 wfDeprecated( __METHOD__ .
' with no InterwikiLookup argument',
'1.34' );
82 wfDeprecated( __METHOD__ .
' with no NamespaceInfo argument',
'1.34' );
83 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
85 $this->language = $language;
86 $this->genderCache = $genderCache;
102 if ( $this->language->needsGenderDistinction() &&
103 $this->nsInfo->hasGenderDistinction( $namespace )
106 $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
107 $name = $this->language->getGenderNsText( $namespace, $gender );
109 $name = $this->language->getNsText( $namespace );
112 if ( $name ===
false ) {
113 throw new InvalidArgumentException(
'Unknown namespace ID: ' . $namespace );
131 public function formatTitle( $namespace, $text, $fragment =
'', $interwiki =
'' ) {
133 if ( $interwiki !==
'' ) {
134 $out = $interwiki .
':';
137 if ( $namespace != 0 ) {
140 }
catch ( InvalidArgumentException $e ) {
142 $nsName = $this->language->getNsText(
NS_SPECIAL ) .
":Badtitle/NS{$namespace}";
145 $out .= $nsName .
':';
149 if ( $fragment !==
'' ) {
150 $out .=
'#' . $fragment;
153 $out = str_replace(
'_',
' ', $out );
169 $filteredText = Sanitizer::decodeCharReferencesAndNormalize( $text );
195 if ( !$this->nsInfo->exists( $namespace ) ) {
199 $canonicalNs = $this->nsInfo->getCanonicalName( $namespace );
200 $fullText = $canonicalNs ==
'' ? $text :
"$canonicalNs:$text";
201 if ( strval( $interwiki ) !=
'' ) {
202 $fullText =
"$interwiki:$fullText";
204 if ( strval( $fragment ) !=
'' ) {
205 $fullText .=
'#' . $fragment;
215 $parts[
'namespace'], $parts[
'dbkey'], $parts[
'fragment'], $parts[
'interwiki'] );
238 if ( !isset(
$title->prefixedText ) ) {
247 return $title->prefixedText;
303 $dbkey = str_replace(
' ',
'_', $text );
308 'local_interwiki' =>
false,
310 'namespace' => $defaultNamespace,
314 # Strip Unicode bidi override characters.
315 # Sometimes they slip into cut-n-pasted page titles, where the
316 # override chars get included in list displays.
317 $dbkey = preg_replace(
'/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u',
'', $dbkey );
319 # Clean up whitespace
320 # Note: use of the /u option on preg_replace here will cause
321 # input with invalid UTF-8 sequences to be nullified out in PHP 5.2.x,
322 # conveniently disabling them.
323 $dbkey = preg_replace(
324 '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
328 $dbkey = trim( $dbkey,
'_' );
330 if ( strpos( $dbkey, UtfNormal\Constants::UTF8_REPLACEMENT ) !==
false ) {
331 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
335 $parts[
'dbkey'] = $dbkey;
337 # Initial colon indicates main namespace rather than specified default
338 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
339 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
341 $dbkey = substr( $dbkey, 1 ); #
remove the colon but
continue processing
342 $dbkey = trim( $dbkey,
'_' ); #
remove any subsequent whitespace
345 if ( $dbkey ==
'' ) {
349 # Namespace or interwiki prefix
350 $prefixRegexp =
"/^(.+?)_*:_*(.*)$/S";
353 if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
355 $ns = $this->language->getNsIndex( $p );
356 if ( $ns !==
false ) {
359 $parts[
'namespace'] = $ns;
360 # For Talk:X pages, check if X has a "namespace" prefix
361 if ( $ns ==
NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
362 if ( $this->language->getNsIndex( $x[1] ) ) {
363 # Disallow Talk:File:x type titles...
365 } elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
366 # Disallow Talk:Interwiki:x type titles...
370 } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
373 $parts[
'interwiki'] = $this->language->lc( $p );
375 # Redundant interwiki prefix to the local wiki
376 foreach ( $this->localInterwikis as $localIW ) {
377 if ( strcasecmp( $parts[
'interwiki'], $localIW ) == 0 ) {
378 if ( $dbkey ==
'' ) {
379 # Empty self-links should point to the Main Page, to ensure
380 # compatibility with cross-wiki transclusions and the like.
381 $mainPage = Title::newMainPage();
383 'interwiki' => $mainPage->getInterwiki(),
384 'local_interwiki' =>
true,
385 'fragment' => $mainPage->getFragment(),
386 'namespace' => $mainPage->getNamespace(),
387 'dbkey' => $mainPage->getDBkey(),
390 $parts[
'interwiki'] =
'';
391 # local interwikis should behave like initial-colon links
392 $parts[
'local_interwiki'] =
true;
394 # Do another namespace split...
399 # If there's an initial colon after the interwiki, that also
400 # resets the default namespace
401 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
403 $dbkey = substr( $dbkey, 1 );
404 $dbkey = trim( $dbkey,
'_' );
407 # If there's no recognized interwiki or namespace,
408 # then let the colon expression be part of the title.
413 $fragment = strstr( $dbkey,
'#' );
414 if ( $fragment !==
false ) {
415 $parts[
'fragment'] = str_replace(
'_',
' ', substr( $fragment, 1 ) );
416 $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
417 # remove whitespace again: prevents "Foo_bar_#"
418 # becoming "Foo_bar_"
419 $dbkey = preg_replace(
'/_*$/',
'', $dbkey );
422 # Reject illegal characters.
423 $rxTc = self::getTitleInvalidRegex();
425 if ( preg_match( $rxTc, $dbkey,
$matches ) ) {
429 # Pages with "/./" or "/../" appearing in the URLs will often be un-
430 # reachable due to the way web browsers deal with 'relative' URLs.
431 # Also, they conflict with subpage syntax. Forbid them explicitly.
433 strpos( $dbkey,
'.' ) !==
false &&
435 $dbkey ===
'.' || $dbkey ===
'..' ||
436 strpos( $dbkey,
'./' ) === 0 ||
437 strpos( $dbkey,
'../' ) === 0 ||
438 strpos( $dbkey,
'/./' ) !==
false ||
439 strpos( $dbkey,
'/../' ) !==
false ||
440 substr( $dbkey, -2 ) ==
'/.' ||
441 substr( $dbkey, -3 ) ==
'/..'
447 # Magic tilde sequences? Nu-uh!
448 if ( strpos( $dbkey,
'~~~' ) !==
false ) {
452 # Limit the size of titles to 255 bytes. This is typically the size of the
453 # underlying database field. We make an exception for special pages, which
454 # don't need to be stored in the database, and may edge over 255 bytes due
455 # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
456 $maxLength = ( $parts[
'namespace'] !=
NS_SPECIAL ) ? 255 : 512;
457 if ( strlen( $dbkey ) > $maxLength ) {
462 # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
463 # and [[Foo]] point to the same place. Don't force it for interwikis, since the
464 # other site might be case-sensitive.
465 if ( $parts[
'interwiki'] ===
'' && $this->nsInfo->isCapitalized( $parts[
'namespace'] ) ) {
466 $dbkey = $this->language->ucfirst( $dbkey );
469 # Can't make a link to a namespace alone... "empty" local links can only be
470 # self-links with a fragment identifier.
471 if ( $dbkey ==
'' && $parts[
'interwiki'] ===
'' && $parts[
'namespace'] !=
NS_MAIN ) {
482 $dbkey = IPUtils::sanitizeIP( $dbkey );
486 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
491 $parts[
'dbkey'] = $dbkey;
502 }
catch ( InvalidArgumentException $ex ) {
519 static $rxTc =
false;
521 # Matching titles will be held as illegal.
523 # Any character not allowed is forbidden...
524 '[^' . Title::legalChars() .
']' .
525 # URL percent encoding sequences interfere with the ability
526 # to round-trip titles -- you can't link to them consistently.
528 # XML/HTML character references produce similar issues.
529 '|&[A-Za-z0-9\x80-\xff]+;' .
531 '|&#x[0-9A-Fa-f]+;' .
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Caches user genders when needed to use correct namespace aliases.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents a page (or page fragment) title within MediaWiki.
static assertValidSpec( $namespace, $title, $fragment='', $interwiki='')
Asserts that the given parameters could be used to construct a TitleValue object.
A title parser service for MediaWiki.