MediaWiki REL1_32
MediaWikiTitleCodec.php
Go to the documentation of this file.
1<?php
26
42 protected $language;
43
47 protected $genderCache;
48
53
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() &&
86 MWNamespace::hasGenderDistinction( $namespace )
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 ( 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 $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 ( false !== $fragment ) {
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.
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'] === '' ) {
425 if ( $parts['namespace'] != NS_MAIN ) {
426 throw new MalformedTitleException( 'title-invalid-empty', $text );
427 }
428 }
429
430 // Allow IPv6 usernames to start with '::' by canonicalizing IPv6 titles.
431 // IP names are not allowed for accounts, and can only be referring to
432 // edits from the IP. Given '::' abbreviations and caps/lowercaps,
433 // there are numerous ways to present the same IP. Having sp:contribs scan
434 // them all is silly and having some show the edits and others not is
435 // inconsistent. Same for talk/userpages. Keep them normalized instead.
436 if ( $parts['namespace'] == NS_USER || $parts['namespace'] == NS_USER_TALK ) {
437 $dbkey = IP::sanitizeIP( $dbkey );
438 }
439
440 // Any remaining initial :s are illegal.
441 if ( $dbkey !== '' && ':' == $dbkey[0] ) {
442 throw new MalformedTitleException( 'title-invalid-leading-colon', $text );
443 }
444
445 # Fill fields
446 $parts['dbkey'] = $dbkey;
447
448 return $parts;
449 }
450
460 public static function getTitleInvalidRegex() {
461 static $rxTc = false;
462 if ( !$rxTc ) {
463 # Matching titles will be held as illegal.
464 $rxTc = '/' .
465 # Any character not allowed is forbidden...
466 '[^' . Title::legalChars() . ']' .
467 # URL percent encoding sequences interfere with the ability
468 # to round-trip titles -- you can't link to them consistently.
469 '|%[0-9A-Fa-f]{2}' .
470 # XML/HTML character references produce similar issues.
471 '|&[A-Za-z0-9\x80-\xff]+;' .
472 '|&#[0-9]+;' .
473 '|&#x[0-9A-Fa-f]+;' .
474 '/S';
475 }
476
477 return $rxTc;
478 }
479}
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
Caches user genders when needed to use correct namespace aliases.
Internationalisation code.
Definition Language.php:35
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
A codec for MediaWiki page titles.
static getTitleInvalidRegex()
Returns a simple regex that will match on characters and sequences invalid in titles.
splitTitleString( $text, $defaultNamespace=NS_MAIN)
Normalizes and splits a title string.
getPrefixedDBkey(LinkTarget $target)
formatTitle( $namespace, $text, $fragment='', $interwiki='')
getFullText(LinkTarget $title)
InterwikiLookup $interwikiLookup
getNamespaceName( $namespace, $text)
getText(LinkTarget $title)
__construct(Language $language, GenderCache $genderCache, $localInterwikis=[], $interwikiLookup=null)
getPrefixedText(LinkTarget $title)
parseTitle( $text, $defaultNamespace=NS_MAIN)
Parses the given text and constructs a TitleValue.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a page (or page fragment) title within MediaWiki.
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:2063
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:894
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
returning false will NOT prevent logging $e
Definition hooks.txt:2226
const NS_MAIN
Definition Defines.php:64
const NS_SPECIAL
Definition Defines.php:53
const NS_TALK
Definition Defines.php:65
const NS_USER_TALK
Definition Defines.php:67
Service interface for looking up Interwiki records.
getInterwiki()
The interwiki component of this LinkTarget.
getNamespace()
Get the namespace index.
getDBkey()
Get the main part with underscores.
A title formatter service for MediaWiki.
A title parser service for MediaWiki.
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))