MediaWiki  1.27.2
MediaWikiTitleCodec.php
Go to the documentation of this file.
1 <?php
25 
41  protected $language;
42 
46  protected $genderCache;
47 
51  protected $localInterwikis;
52 
59  $localInterwikis = []
60  ) {
61  $this->language = $language;
62  $this->genderCache = $genderCache;
63  $this->localInterwikis = (array)$localInterwikis;
64  }
65 
75  public function getNamespaceName( $namespace, $text ) {
76  if ( $this->language->needsGenderDistinction() &&
78  ) {
79 
80  // NOTE: we are assuming here that the title text is a user name!
81  $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
82  $name = $this->language->getGenderNsText( $namespace, $gender );
83  } else {
84  $name = $this->language->getNsText( $namespace );
85  }
86 
87  if ( $name === false ) {
88  throw new InvalidArgumentException( 'Unknown namespace ID: ' . $namespace );
89  }
90 
91  return $name;
92  }
93 
106  public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
107  if ( $namespace !== false ) {
108  $namespace = $this->getNamespaceName( $namespace, $text );
109 
110  if ( $namespace !== '' ) {
111  $text = $namespace . ':' . $text;
112  }
113  }
114 
115  if ( $fragment !== '' ) {
116  $text = $text . '#' . $fragment;
117  }
118 
119  if ( $interwiki !== '' ) {
120  $text = $interwiki . ':' . $text;
121  }
122 
123  $text = str_replace( '_', ' ', $text );
124 
125  return $text;
126  }
127 
138  public function parseTitle( $text, $defaultNamespace ) {
139  // NOTE: this is an ugly cludge that allows this class to share the
140  // code for parsing with the old Title class. The parser code should
141  // be refactored to avoid this.
142  $parts = $this->splitTitleString( $text, $defaultNamespace );
143 
144  // Relative fragment links are not supported by TitleValue
145  if ( $parts['dbkey'] === '' ) {
146  throw new MalformedTitleException( 'title-invalid-empty', $text );
147  }
148 
149  return new TitleValue(
150  $parts['namespace'],
151  $parts['dbkey'],
152  $parts['fragment'],
153  $parts['interwiki']
154  );
155  }
156 
164  public function getText( LinkTarget $title ) {
165  return $this->formatTitle( false, $title->getText(), '' );
166  }
167 
175  public function getPrefixedText( LinkTarget $title ) {
176  return $this->formatTitle(
177  $title->getNamespace(),
178  $title->getText(),
179  '',
180  $title->getInterwiki()
181  );
182  }
183 
190  public function getPrefixedDBkey( LinkTarget $target ) {
191  $key = '';
192  if ( $target->isExternal() ) {
193  $key .= $target->getInterwiki() . ':';
194  }
195  // Try to get a namespace name, but fallback
196  // to empty string if it doesn't exist
197  try {
198  $nsName = $this->getNamespaceName(
199  $target->getNamespace(),
200  $target->getText()
201  );
202  } catch ( InvalidArgumentException $e ) {
203  $nsName = '';
204  }
205 
206  if ( $target->getNamespace() !== 0 ) {
207  $key .= $nsName . ':';
208  }
209 
210  $key .= $target->getText();
211 
212  return strtr( $key, ' ', '_' );
213  }
214 
222  public function getFullText( LinkTarget $title ) {
223  return $this->formatTitle(
224  $title->getNamespace(),
225  $title->getText(),
226  $title->getFragment(),
227  $title->getInterwiki()
228  );
229  }
230 
251  public function splitTitleString( $text, $defaultNamespace = NS_MAIN ) {
252  $dbkey = str_replace( ' ', '_', $text );
253 
254  # Initialisation
255  $parts = [
256  'interwiki' => '',
257  'local_interwiki' => false,
258  'fragment' => '',
259  'namespace' => $defaultNamespace,
260  'dbkey' => $dbkey,
261  'user_case_dbkey' => $dbkey,
262  ];
263 
264  # Strip Unicode bidi override characters.
265  # Sometimes they slip into cut-n-pasted page titles, where the
266  # override chars get included in list displays.
267  $dbkey = preg_replace( '/\xE2\x80[\x8E\x8F\xAA-\xAE]/S', '', $dbkey );
268 
269  # Clean up whitespace
270  # Note: use of the /u option on preg_replace here will cause
271  # input with invalid UTF-8 sequences to be nullified out in PHP 5.2.x,
272  # conveniently disabling them.
273  $dbkey = preg_replace(
274  '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
275  '_',
276  $dbkey
277  );
278  $dbkey = trim( $dbkey, '_' );
279 
280  if ( strpos( $dbkey, UtfNormal\Constants::UTF8_REPLACEMENT ) !== false ) {
281  # Contained illegal UTF-8 sequences or forbidden Unicode chars.
282  throw new MalformedTitleException( 'title-invalid-utf8', $text );
283  }
284 
285  $parts['dbkey'] = $dbkey;
286 
287  # Initial colon indicates main namespace rather than specified default
288  # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
289  if ( $dbkey !== '' && ':' == $dbkey[0] ) {
290  $parts['namespace'] = NS_MAIN;
291  $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing
292  $dbkey = trim( $dbkey, '_' ); # remove any subsequent whitespace
293  }
294 
295  if ( $dbkey == '' ) {
296  throw new MalformedTitleException( 'title-invalid-empty', $text );
297  }
298 
299  # Namespace or interwiki prefix
300  $prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
301  do {
302  $m = [];
303  if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
304  $p = $m[1];
305  $ns = $this->language->getNsIndex( $p );
306  if ( $ns !== false ) {
307  # Ordinary namespace
308  $dbkey = $m[2];
309  $parts['namespace'] = $ns;
310  # For Talk:X pages, check if X has a "namespace" prefix
311  if ( $ns == NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
312  if ( $this->language->getNsIndex( $x[1] ) ) {
313  # Disallow Talk:File:x type titles...
314  throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
315  } elseif ( Interwiki::isValidInterwiki( $x[1] ) ) {
316  // TODO: get rid of global state!
317  # Disallow Talk:Interwiki:x type titles...
318  throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
319  }
320  }
321  } elseif ( Interwiki::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 ( 0 == strcasecmp( $parts['interwiki'], $localIW ) ) {
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  }
357  }
358  # If there's no recognized interwiki or namespace,
359  # then let the colon expression be part of the title.
360  }
361  break;
362  } while ( true );
363 
364  $fragment = strstr( $dbkey, '#' );
365  if ( false !== $fragment ) {
366  $parts['fragment'] = str_replace( '_', ' ', substr( $fragment, 1 ) );
367  $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
368  # remove whitespace again: prevents "Foo_bar_#"
369  # becoming "Foo_bar_"
370  $dbkey = preg_replace( '/_*$/', '', $dbkey );
371  }
372 
373  # Reject illegal characters.
374  $rxTc = self::getTitleInvalidRegex();
375  $matches = [];
376  if ( preg_match( $rxTc, $dbkey, $matches ) ) {
377  throw new MalformedTitleException( 'title-invalid-characters', $text, [ $matches[0] ] );
378  }
379 
380  # Pages with "/./" or "/../" appearing in the URLs will often be un-
381  # reachable due to the way web browsers deal with 'relative' URLs.
382  # Also, they conflict with subpage syntax. Forbid them explicitly.
383  if (
384  strpos( $dbkey, '.' ) !== false &&
385  (
386  $dbkey === '.' || $dbkey === '..' ||
387  strpos( $dbkey, './' ) === 0 ||
388  strpos( $dbkey, '../' ) === 0 ||
389  strpos( $dbkey, '/./' ) !== false ||
390  strpos( $dbkey, '/../' ) !== false ||
391  substr( $dbkey, -2 ) == '/.' ||
392  substr( $dbkey, -3 ) == '/..'
393  )
394  ) {
395  throw new MalformedTitleException( 'title-invalid-relative', $text );
396  }
397 
398  # Magic tilde sequences? Nu-uh!
399  if ( strpos( $dbkey, '~~~' ) !== false ) {
400  throw new MalformedTitleException( 'title-invalid-magic-tilde', $text );
401  }
402 
403  # Limit the size of titles to 255 bytes. This is typically the size of the
404  # underlying database field. We make an exception for special pages, which
405  # don't need to be stored in the database, and may edge over 255 bytes due
406  # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
407  $maxLength = ( $parts['namespace'] != NS_SPECIAL ) ? 255 : 512;
408  if ( strlen( $dbkey ) > $maxLength ) {
409  throw new MalformedTitleException( 'title-invalid-too-long', $text,
410  [ Message::numParam( $maxLength ) ] );
411  }
412 
413  # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
414  # and [[Foo]] point to the same place. Don't force it for interwikis, since the
415  # other site might be case-sensitive.
416  $parts['user_case_dbkey'] = $dbkey;
417  if ( $parts['interwiki'] === '' ) {
418  $dbkey = Title::capitalize( $dbkey, $parts['namespace'] );
419  }
420 
421  # Can't make a link to a namespace alone... "empty" local links can only be
422  # self-links with a fragment identifier.
423  if ( $dbkey == '' && $parts['interwiki'] === '' ) {
424  if ( $parts['namespace'] != NS_MAIN ) {
425  throw new MalformedTitleException( 'title-invalid-empty', $text );
426  }
427  }
428 
429  // Allow IPv6 usernames to start with '::' by canonicalizing IPv6 titles.
430  // IP names are not allowed for accounts, and can only be referring to
431  // edits from the IP. Given '::' abbreviations and caps/lowercaps,
432  // there are numerous ways to present the same IP. Having sp:contribs scan
433  // them all is silly and having some show the edits and others not is
434  // inconsistent. Same for talk/userpages. Keep them normalized instead.
435  if ( $parts['namespace'] == NS_USER || $parts['namespace'] == NS_USER_TALK ) {
436  $dbkey = IP::sanitizeIP( $dbkey );
437  }
438 
439  // Any remaining initial :s are illegal.
440  if ( $dbkey !== '' && ':' == $dbkey[0] ) {
441  throw new MalformedTitleException( 'title-invalid-leading-colon', $text );
442  }
443 
444  # Fill fields
445  $parts['dbkey'] = $dbkey;
446 
447  return $parts;
448  }
449 
459  public static function getTitleInvalidRegex() {
460  static $rxTc = false;
461  if ( !$rxTc ) {
462  # Matching titles will be held as illegal.
463  $rxTc = '/' .
464  # Any character not allowed is forbidden...
465  '[^' . Title::legalChars() . ']' .
466  # URL percent encoding sequences interfere with the ability
467  # to round-trip titles -- you can't link to them consistently.
468  '|%[0-9A-Fa-f]{2}' .
469  # XML/HTML character references produce similar issues.
470  '|&[A-Za-z0-9\x80-\xff]+;' .
471  '|&#[0-9]+;' .
472  '|&#x[0-9A-Fa-f]+;' .
473  '/S';
474  }
475 
476  return $rxTc;
477  }
478 }
#define the
table suitable for use with IDatabase::select()
A codec for MediaWiki page titles.
static hasGenderDistinction($index)
Does the namespace (potentially) have different aliases for different genders.
the array() calling protocol came about after MediaWiki 1.4rc1.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
static getTitleInvalidRegex()
Returns a simple regex that will match on characters and sequences invalid in titles.
static sanitizeIP($ip)
Convert an IP into a verbose, uppercase, normalized form.
Definition: IP.php:140
const NS_MAIN
Definition: Defines.php:69
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:569
getText(LinkTarget $title)
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
splitTitleString($text, $defaultNamespace=NS_MAIN)
Normalizes and splits a title string.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:1932
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like please read the documentation for except in special pages derived from QueryPage It s a common pitfall for new developers to submit code containing SQL queries which examine huge numbers of rows Remember that COUNT * is(N), counting rows in atable is like counting beans in a bucket.------------------------------------------------------------------------Replication------------------------------------------------------------------------The largest installation of MediaWiki, Wikimedia, uses a large set ofslave MySQL servers replicating writes made to a master MySQL server.Itis important to understand the issues associated with this setup if youwant to write code destined for Wikipedia.It's often the case that the best algorithm to use for a given taskdepends on whether or not replication is in use.Due to our unabashedWikipedia-centrism, we often just use the replication-friendly version, but if you like, you can use wfGetLB() ->getServerCount() > 1 tocheck to see if replication is in use.===Lag===Lag primarily occurs when large write queries are sent to the master.Writes on the master are executed in parallel, but they are executed inserial when they are replicated to the slaves.The master writes thequery to the binlog when the transaction is committed.The slaves pollthe binlog and start executing the query as soon as it appears.They canservice reads while they are performing a write query, but will not readanything more from the binlog and thus will perform no more writes.Thismeans that if the write query runs for a long time, the slaves will lagbehind the master for the time it takes for the write query to complete.Lag can be exacerbated by high read load.MediaWiki's load balancer willstop sending reads to a slave when it is lagged by more than 30 seconds.If the load ratios are set incorrectly, or if there is too much loadgenerally, this may lead to a slave permanently hovering around 30seconds lag.If all slaves are lagged by more than 30 seconds, MediaWiki will stopwriting to the database.All edits and other write operations will berefused, with an error returned to the user.This gives the slaves achance to catch up.Before we had this mechanism, the slaves wouldregularly lag by several minutes, making review of recent editsdifficult.In addition to this, MediaWiki attempts to ensure that the user seesevents occurring on the wiki in chronological order.A few seconds of lagcan be tolerated, as long as the user sees a consistent picture fromsubsequent requests.This is done by saving the master binlog positionin the session, and then at the start of each request, waiting for theslave to catch up to that position before doing any reads from it.Ifthis wait times out, reads are allowed anyway, but the request isconsidered to be in"lagged slave mode".Lagged slave mode can bechecked by calling wfGetLB() ->getLaggedSlaveMode().The onlypractical consequence at present is a warning displayed in the pagefooter.===Lag avoidance===To avoid excessive lag, queries which write large numbers of rows shouldbe split up, generally to write one row at a time.Multi-row INSERT...SELECT queries are the worst offenders should be avoided altogether.Instead do the select first and then the insert.===Working with lag===Despite our best efforts, it's not practical to guarantee a low-lagenvironment.Lag will usually be less than one second, but mayoccasionally be up to 30 seconds.For scalability, it's very importantto keep load on the master low, so simply sending all your queries tothe master is not the answer.So when you have a genuine need forup-to-date data, the following approach is advised:1) Do a quick query to the master for a sequence number or timestamp 2) Run the full query on the slave and check if it matches the data you gotfrom the master 3) If it doesn't, run the full query on the masterTo avoid swamping the master every time the slaves lag, use of thisapproach should be kept to a minimum.In most cases you should just readfrom the slave and let the user deal with the delay.------------------------------------------------------------------------Lock contention------------------------------------------------------------------------Due to the high write rate on Wikipedia(and some other wikis), MediaWiki developers need to be very careful to structure their writesto avoid long-lasting locks.By default, MediaWiki opens a transactionat the first query, and commits it before the output is sent.Locks willbe held from the time when the query is done until the commit.So youcan reduce lock time by doing as much processing as possible before youdo your write queries.Often this approach is not good enough, and it becomes necessary toenclose small groups of queries in their own transaction.Use thefollowing syntax:$dbw=wfGetDB(DB_MASTER
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
const NS_SPECIAL
Definition: Defines.php:58
const UTF8_REPLACEMENT
formatTitle($namespace, $text, $fragment= '', $interwiki= '')
getNamespace()
Get the namespace index.
getFragment()
Get the link fragment (i.e.
isExternal()
Whether this LinkTarget has an interwiki component.
Unicode normalization routines for working with UTF-8 strings.
Definition: UtfNormal.php:48
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:1798
__construct(Language $language, GenderCache $genderCache, $localInterwikis=[])
A title parser service for MediaWiki.
Definition: TitleParser.php:34
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
getPrefixedText(LinkTarget $title)
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
parseTitle($text, $defaultNamespace)
Parses the given text and constructs a TitleValue.
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
getNamespaceName($namespace, $text)
Caches user genders when needed to use correct namespace aliases.
Definition: GenderCache.php:30
getFullText(LinkTarget $title)
static numParam($num)
Definition: Message.php:986
A title formatter service for MediaWiki.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain URL
Definition: design.txt:25
getInterwiki()
The interwiki component of this LinkTarget.
getText()
Returns the link in text form, without namespace prefix or fragment.
static legalChars()
Get a regex character class describing the legal characters in a link.
Definition: Title.php:606
const NS_TALK
Definition: Defines.php:70
const NS_USER_TALK
Definition: Defines.php:72
static capitalize($text, $ns=NS_MAIN)
Capitalize a text string for a title if it belongs to a namespace that capitalizes.
Definition: Title.php:3345
static isValidInterwiki($prefix)
Check whether an interwiki prefix exists.
Definition: Interwiki.php:73
getPrefixedDBkey(LinkTarget $target)
$matches
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310