MediaWiki  1.33.0
MediaWikiTitleCodec.php
Go to the documentation of this file.
1 <?php
26 
42  protected $language;
43 
47  protected $genderCache;
48 
52  protected $localInterwikis;
53 
57  protected $interwikiLookup;
58 
67  ) {
68  $this->language = $language;
69  $this->genderCache = $genderCache;
70  $this->localInterwikis = (array)$localInterwikis;
71  $this->interwikiLookup = $interwikiLookup ?:
72  MediaWikiServices::getInstance()->getInterwikiLookup();
73  }
74 
84  public function getNamespaceName( $namespace, $text ) {
85  if ( $this->language->needsGenderDistinction() &&
87  ) {
88  // NOTE: we are assuming here that the title text is a user name!
89  $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
90  $name = $this->language->getGenderNsText( $namespace, $gender );
91  } else {
92  $name = $this->language->getNsText( $namespace );
93  }
94 
95  if ( $name === false ) {
96  throw new InvalidArgumentException( 'Unknown namespace ID: ' . $namespace );
97  }
98 
99  return $name;
100  }
101 
114  public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
115  $out = '';
116  if ( $interwiki !== '' ) {
117  $out = $interwiki . ':';
118  }
119 
120  if ( $namespace != 0 ) {
121  try {
122  $nsName = $this->getNamespaceName( $namespace, $text );
123  } catch ( InvalidArgumentException $e ) {
124  // See T165149. Awkward, but better than erroneously linking to the main namespace.
125  $nsName = $this->language->getNsText( NS_SPECIAL ) . ":Badtitle/NS{$namespace}";
126  }
127 
128  $out .= $nsName . ':';
129  }
130  $out .= $text;
131 
132  if ( $fragment !== '' ) {
133  $out .= '#' . $fragment;
134  }
135 
136  $out = str_replace( '_', ' ', $out );
137 
138  return $out;
139  }
140 
151  public function parseTitle( $text, $defaultNamespace = NS_MAIN ) {
152  // NOTE: this is an ugly cludge that allows this class to share the
153  // code for parsing with the old Title class. The parser code should
154  // be refactored to avoid this.
155  $parts = $this->splitTitleString( $text, $defaultNamespace );
156 
157  // Relative fragment links are not supported by TitleValue
158  if ( $parts['dbkey'] === '' ) {
159  throw new MalformedTitleException( 'title-invalid-empty', $text );
160  }
161 
162  return new TitleValue(
163  $parts['namespace'],
164  $parts['dbkey'],
165  $parts['fragment'],
166  $parts['interwiki']
167  );
168  }
169 
177  public function getText( LinkTarget $title ) {
178  return $title->getText();
179  }
180 
188  public function getPrefixedText( LinkTarget $title ) {
189  if ( !isset( $title->prefixedText ) ) {
190  $title->prefixedText = $this->formatTitle(
191  $title->getNamespace(),
192  $title->getText(),
193  '',
194  $title->getInterwiki()
195  );
196  }
197 
198  return $title->prefixedText;
199  }
200 
207  public function getPrefixedDBkey( LinkTarget $target ) {
208  return strtr( $this->formatTitle(
209  $target->getNamespace(),
210  $target->getDBkey(),
211  '',
212  $target->getInterwiki()
213  ), ' ', '_' );
214  }
215 
223  public function getFullText( LinkTarget $title ) {
224  return $this->formatTitle(
225  $title->getNamespace(),
226  $title->getText(),
227  $title->getFragment(),
228  $title->getInterwiki()
229  );
230  }
231 
252  public function splitTitleString( $text, $defaultNamespace = NS_MAIN ) {
253  $dbkey = str_replace( ' ', '_', $text );
254 
255  # Initialisation
256  $parts = [
257  'interwiki' => '',
258  'local_interwiki' => false,
259  'fragment' => '',
260  'namespace' => $defaultNamespace,
261  'dbkey' => $dbkey,
262  'user_case_dbkey' => $dbkey,
263  ];
264 
265  # Strip Unicode bidi override characters.
266  # Sometimes they slip into cut-n-pasted page titles, where the
267  # override chars get included in list displays.
268  $dbkey = preg_replace( '/\xE2\x80[\x8E\x8F\xAA-\xAE]/S', '', $dbkey );
269 
270  # Clean up whitespace
271  # Note: use of the /u option on preg_replace here will cause
272  # input with invalid UTF-8 sequences to be nullified out in PHP 5.2.x,
273  # conveniently disabling them.
274  $dbkey = preg_replace(
275  '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
276  '_',
277  $dbkey
278  );
279  $dbkey = trim( $dbkey, '_' );
280 
281  if ( strpos( $dbkey, UtfNormal\Constants::UTF8_REPLACEMENT ) !== false ) {
282  # Contained illegal UTF-8 sequences or forbidden Unicode chars.
283  throw new MalformedTitleException( 'title-invalid-utf8', $text );
284  }
285 
286  $parts['dbkey'] = $dbkey;
287 
288  # Initial colon indicates main namespace rather than specified default
289  # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
290  if ( $dbkey !== '' && $dbkey[0] == ':' ) {
291  $parts['namespace'] = NS_MAIN;
292  $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing
293  $dbkey = trim( $dbkey, '_' ); # remove any subsequent whitespace
294  }
295 
296  if ( $dbkey == '' ) {
297  throw new MalformedTitleException( 'title-invalid-empty', $text );
298  }
299 
300  # Namespace or interwiki prefix
301  $prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
302  do {
303  $m = [];
304  if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
305  $p = $m[1];
306  $ns = $this->language->getNsIndex( $p );
307  if ( $ns !== false ) {
308  # Ordinary namespace
309  $dbkey = $m[2];
310  $parts['namespace'] = $ns;
311  # For Talk:X pages, check if X has a "namespace" prefix
312  if ( $ns == NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
313  if ( $this->language->getNsIndex( $x[1] ) ) {
314  # Disallow Talk:File:x type titles...
315  throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
316  } elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
317  # Disallow Talk:Interwiki:x type titles...
318  throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
319  }
320  }
321  } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
322  # Interwiki link
323  $dbkey = $m[2];
324  $parts['interwiki'] = $this->language->lc( $p );
325 
326  # Redundant interwiki prefix to the local wiki
327  foreach ( $this->localInterwikis as $localIW ) {
328  if ( strcasecmp( $parts['interwiki'], $localIW ) == 0 ) {
329  if ( $dbkey == '' ) {
330  # Empty self-links should point to the Main Page, to ensure
331  # compatibility with cross-wiki transclusions and the like.
332  $mainPage = Title::newMainPage();
333  return [
334  'interwiki' => $mainPage->getInterwiki(),
335  'local_interwiki' => true,
336  'fragment' => $mainPage->getFragment(),
337  'namespace' => $mainPage->getNamespace(),
338  'dbkey' => $mainPage->getDBkey(),
339  'user_case_dbkey' => $mainPage->getUserCaseDBKey()
340  ];
341  }
342  $parts['interwiki'] = '';
343  # local interwikis should behave like initial-colon links
344  $parts['local_interwiki'] = true;
345 
346  # Do another namespace split...
347  continue 2;
348  }
349  }
350 
351  # If there's an initial colon after the interwiki, that also
352  # resets the default namespace
353  if ( $dbkey !== '' && $dbkey[0] == ':' ) {
354  $parts['namespace'] = NS_MAIN;
355  $dbkey = substr( $dbkey, 1 );
356  $dbkey = trim( $dbkey, '_' );
357  }
358  }
359  # If there's no recognized interwiki or namespace,
360  # then let the colon expression be part of the title.
361  }
362  break;
363  } while ( true );
364 
365  $fragment = strstr( $dbkey, '#' );
366  if ( $fragment !== false ) {
367  $parts['fragment'] = str_replace( '_', ' ', substr( $fragment, 1 ) );
368  $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
369  # remove whitespace again: prevents "Foo_bar_#"
370  # becoming "Foo_bar_"
371  $dbkey = preg_replace( '/_*$/', '', $dbkey );
372  }
373 
374  # Reject illegal characters.
375  $rxTc = self::getTitleInvalidRegex();
376  $matches = [];
377  if ( preg_match( $rxTc, $dbkey, $matches ) ) {
378  throw new MalformedTitleException( 'title-invalid-characters', $text, [ $matches[0] ] );
379  }
380 
381  # Pages with "/./" or "/../" appearing in the URLs will often be un-
382  # reachable due to the way web browsers deal with 'relative' URLs.
383  # Also, they conflict with subpage syntax. Forbid them explicitly.
384  if (
385  strpos( $dbkey, '.' ) !== false &&
386  (
387  $dbkey === '.' || $dbkey === '..' ||
388  strpos( $dbkey, './' ) === 0 ||
389  strpos( $dbkey, '../' ) === 0 ||
390  strpos( $dbkey, '/./' ) !== false ||
391  strpos( $dbkey, '/../' ) !== false ||
392  substr( $dbkey, -2 ) == '/.' ||
393  substr( $dbkey, -3 ) == '/..'
394  )
395  ) {
396  throw new MalformedTitleException( 'title-invalid-relative', $text );
397  }
398 
399  # Magic tilde sequences? Nu-uh!
400  if ( strpos( $dbkey, '~~~' ) !== false ) {
401  throw new MalformedTitleException( 'title-invalid-magic-tilde', $text );
402  }
403 
404  # Limit the size of titles to 255 bytes. This is typically the size of the
405  # underlying database field. We make an exception for special pages, which
406  # don't need to be stored in the database, and may edge over 255 bytes due
407  # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
408  $maxLength = ( $parts['namespace'] != NS_SPECIAL ) ? 255 : 512;
409  if ( strlen( $dbkey ) > $maxLength ) {
410  throw new MalformedTitleException( 'title-invalid-too-long', $text,
411  [ Message::numParam( $maxLength ) ] );
412  }
413 
414  # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
415  # and [[Foo]] point to the same place. Don't force it for interwikis, since the
416  # other site might be case-sensitive.
417  $parts['user_case_dbkey'] = $dbkey;
418  if ( $parts['interwiki'] === '' ) {
419  $dbkey = Title::capitalize( $dbkey, $parts['namespace'] );
420  }
421 
422  # Can't make a link to a namespace alone... "empty" local links can only be
423  # self-links with a fragment identifier.
424  if ( $dbkey == '' && $parts['interwiki'] === '' && $parts['namespace'] != NS_MAIN ) {
425  throw new MalformedTitleException( 'title-invalid-empty', $text );
426  }
427 
428  // Allow IPv6 usernames to start with '::' by canonicalizing IPv6 titles.
429  // IP names are not allowed for accounts, and can only be referring to
430  // edits from the IP. Given '::' abbreviations and caps/lowercaps,
431  // there are numerous ways to present the same IP. Having sp:contribs scan
432  // them all is silly and having some show the edits and others not is
433  // inconsistent. Same for talk/userpages. Keep them normalized instead.
434  if ( $parts['namespace'] == NS_USER || $parts['namespace'] == NS_USER_TALK ) {
435  $dbkey = IP::sanitizeIP( $dbkey );
436  }
437 
438  // Any remaining initial :s are illegal.
439  if ( $dbkey !== '' && $dbkey[0] == ':' ) {
440  throw new MalformedTitleException( 'title-invalid-leading-colon', $text );
441  }
442 
443  # Fill fields
444  $parts['dbkey'] = $dbkey;
445 
446  return $parts;
447  }
448 
458  public static function getTitleInvalidRegex() {
459  static $rxTc = false;
460  if ( !$rxTc ) {
461  # Matching titles will be held as illegal.
462  $rxTc = '/' .
463  # Any character not allowed is forbidden...
464  '[^' . Title::legalChars() . ']' .
465  # URL percent encoding sequences interfere with the ability
466  # to round-trip titles -- you can't link to them consistently.
467  '|%[0-9A-Fa-f]{2}' .
468  # XML/HTML character references produce similar issues.
469  '|&[A-Za-z0-9\x80-\xff]+;' .
470  '|&#[0-9]+;' .
471  '|&#x[0-9A-Fa-f]+;' .
472  '/S';
473  }
474 
475  return $rxTc;
476  }
477 }
MediaWikiTitleCodec\getPrefixedText
getPrefixedText(LinkTarget $title)
Definition: MediaWikiTitleCodec.php:188
MediaWiki\Linker\LinkTarget\getInterwiki
getInterwiki()
The interwiki component of this LinkTarget.
MediaWikiTitleCodec
A codec for MediaWiki page titles.
Definition: MediaWikiTitleCodec.php:38
MWNamespace\hasGenderDistinction
static hasGenderDistinction( $index)
Does the namespace (potentially) have different aliases for different genders.
Definition: MWNamespace.php:447
Title\newMainPage
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:632
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:780
MediaWikiTitleCodec\getTitleInvalidRegex
static getTitleInvalidRegex()
Returns a simple regex that will match on characters and sequences invalid in titles.
Definition: MediaWikiTitleCodec.php:458
GenderCache
Caches user genders when needed to use correct namespace aliases.
Definition: GenderCache.php:31
MediaWikiTitleCodec\splitTitleString
splitTitleString( $text, $defaultNamespace=NS_MAIN)
Normalizes and splits a title string.
Definition: MediaWikiTitleCodec.php:252
MediaWikiTitleCodec\getNamespaceName
getNamespaceName( $namespace, $text)
Definition: MediaWikiTitleCodec.php:84
is
This document provides an overview of the usage of PageUpdater and that is
Definition: pageupdater.txt:3
MediaWikiTitleCodec\parseTitle
parseTitle( $text, $defaultNamespace=NS_MAIN)
Parses the given text and constructs a TitleValue.
Definition: MediaWikiTitleCodec.php:151
MediaWikiTitleCodec\getFullText
getFullText(LinkTarget $title)
Definition: MediaWikiTitleCodec.php:223
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
processing
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after processing
Definition: hooks.txt:1985
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
$matches
$matches
Definition: NoLocalSettings.php:24
not
if not
Definition: COPYING.txt:307
MediaWiki\Interwiki\InterwikiLookup
Service interface for looking up Interwiki records.
Definition: InterwikiLookup.php:31
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWikiTitleCodec\formatTitle
formatTitle( $namespace, $text, $fragment='', $interwiki='')
Definition: MediaWikiTitleCodec.php:114
MediaWikiTitleCodec\$localInterwikis
string[] $localInterwikis
Definition: MediaWikiTitleCodec.php:52
TitleParser
A title parser service for MediaWiki.
Definition: TitleParser.php:33
MediaWikiTitleCodec\__construct
__construct(Language $language, GenderCache $genderCache, $localInterwikis=[], $interwikiLookup=null)
Definition: MediaWikiTitleCodec.php:65
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:67
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
MediaWikiTitleCodec\$interwikiLookup
InterwikiLookup $interwikiLookup
Definition: MediaWikiTitleCodec.php:57
language
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of either verbatim or with modifications and or translated into another language(Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
MediaWikiTitleCodec\$genderCache
GenderCache $genderCache
Definition: MediaWikiTitleCodec.php:47
IP\sanitizeIP
static sanitizeIP( $ip)
Convert an IP into a verbose, uppercase, normalized form.
Definition: IP.php:152
MediaWikiTitleCodec\getText
getText(LinkTarget $title)
Definition: MediaWikiTitleCodec.php:177
MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Definition: MalformedTitleException.php:25
Title\capitalize
static capitalize( $text, $ns=NS_MAIN)
Capitalize a text string for a title if it belongs to a namespace that capitalizes.
Definition: Title.php:3110
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MediaWikiTitleCodec\getPrefixedDBkey
getPrefixedDBkey(LinkTarget $target)
Definition: MediaWikiTitleCodec.php:207
NS_USER
const NS_USER
Definition: Defines.php:66
NS_TALK
const NS_TALK
Definition: Defines.php:65
Title\legalChars
static legalChars()
Get a regex character class describing the legal characters in a link.
Definition: Title.php:669
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
Language
Internationalisation code.
Definition: Language.php:36
MediaWikiTitleCodec\$language
Language $language
Definition: MediaWikiTitleCodec.php:42
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36