title: Convert binary regexp to use Unicode code points
[lhc/web/wiklou.git] / includes / title / MediaWikiTitleCodec.php
1 <?php
2 /**
3 * A codec for MediaWiki page titles.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Daniel Kinzler
22 */
23 use MediaWiki\Interwiki\InterwikiLookup;
24 use MediaWiki\MediaWikiServices;
25 use MediaWiki\Linker\LinkTarget;
26
27 /**
28 * A codec for MediaWiki page titles.
29 *
30 * @note Normalization and validation is applied while parsing, not when formatting.
31 * It's possible to construct a TitleValue with an invalid title, and use MediaWikiTitleCodec
32 * to generate an (invalid) title string from it. TitleValues should be constructed only
33 * via parseTitle() or from a (semi)trusted source, such as the database.
34 *
35 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
36 * @since 1.23
37 */
38 class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
39 /**
40 * @var Language
41 */
42 protected $language;
43
44 /**
45 * @var GenderCache
46 */
47 protected $genderCache;
48
49 /**
50 * @var string[]
51 */
52 protected $localInterwikis;
53
54 /**
55 * @var InterwikiLookup
56 */
57 protected $interwikiLookup;
58
59 /**
60 * @var NamespaceInfo
61 */
62 protected $nsInfo;
63
64 /**
65 * @param Language $language The language object to use for localizing namespace names.
66 * @param GenderCache $genderCache The gender cache for generating gendered namespace names
67 * @param string[]|string $localInterwikis
68 * @param InterwikiLookup|null $interwikiLookup
69 * @param NamespaceInfo|null $nsInfo
70 */
71 public function __construct( Language $language, GenderCache $genderCache,
72 $localInterwikis = [], InterwikiLookup $interwikiLookup = null,
73 NamespaceInfo $nsInfo = null
74 ) {
75 if ( !$interwikiLookup ) {
76 wfDeprecated( __METHOD__ . ' with no InterwikiLookup argument', '1.34' );
77 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
78 }
79 if ( !$nsInfo ) {
80 wfDeprecated( __METHOD__ . ' with no NamespaceInfo argument', '1.34' );
81 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
82 }
83 $this->language = $language;
84 $this->genderCache = $genderCache;
85 $this->localInterwikis = (array)$localInterwikis;
86 $this->interwikiLookup = $interwikiLookup;
87 $this->nsInfo = $nsInfo;
88 }
89
90 /**
91 * @see TitleFormatter::getNamespaceName()
92 *
93 * @param int $namespace
94 * @param string $text
95 *
96 * @throws InvalidArgumentException If the namespace is invalid
97 * @return string Namespace name with underscores (not spaces)
98 */
99 public function getNamespaceName( $namespace, $text ) {
100 if ( $this->language->needsGenderDistinction() &&
101 $this->nsInfo->hasGenderDistinction( $namespace )
102 ) {
103 // NOTE: we are assuming here that the title text is a user name!
104 $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
105 $name = $this->language->getGenderNsText( $namespace, $gender );
106 } else {
107 $name = $this->language->getNsText( $namespace );
108 }
109
110 if ( $name === false ) {
111 throw new InvalidArgumentException( 'Unknown namespace ID: ' . $namespace );
112 }
113
114 return $name;
115 }
116
117 /**
118 * @see TitleFormatter::formatTitle()
119 *
120 * @param int|bool $namespace The namespace ID (or false, if the namespace should be ignored)
121 * @param string $text The page title. Should be valid. Only minimal normalization is applied.
122 * Underscores will be replaced.
123 * @param string $fragment The fragment name (may be empty).
124 * @param string $interwiki The interwiki name (may be empty).
125 *
126 * @throws InvalidArgumentException If the namespace is invalid
127 * @return string
128 */
129 public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
130 $out = '';
131 if ( $interwiki !== '' ) {
132 $out = $interwiki . ':';
133 }
134
135 if ( $namespace != 0 ) {
136 try {
137 $nsName = $this->getNamespaceName( $namespace, $text );
138 } catch ( InvalidArgumentException $e ) {
139 // See T165149. Awkward, but better than erroneously linking to the main namespace.
140 $nsName = $this->language->getNsText( NS_SPECIAL ) . ":Badtitle/NS{$namespace}";
141 }
142
143 $out .= $nsName . ':';
144 }
145 $out .= $text;
146
147 if ( $fragment !== '' ) {
148 $out .= '#' . $fragment;
149 }
150
151 $out = str_replace( '_', ' ', $out );
152
153 return $out;
154 }
155
156 /**
157 * Parses the given text and constructs a TitleValue. Normalization
158 * is applied according to the rules appropriate for the form specified by $form.
159 *
160 * @param string $text The text to parse
161 * @param int $defaultNamespace Namespace to assume per default (usually NS_MAIN)
162 *
163 * @throws MalformedTitleException
164 * @return TitleValue
165 */
166 public function parseTitle( $text, $defaultNamespace = NS_MAIN ) {
167 // Convert things like &eacute; &#257; or &#x3017; into normalized (T16952) text
168 $filteredText = Sanitizer::decodeCharReferencesAndNormalize( $text );
169
170 // NOTE: this is an ugly cludge that allows this class to share the
171 // code for parsing with the old Title class. The parser code should
172 // be refactored to avoid this.
173 $parts = $this->splitTitleString( $filteredText, $defaultNamespace );
174
175 // Fragment-only is okay, but only with no namespace
176 if ( $parts['dbkey'] === '' &&
177 ( $parts['fragment'] === '' || $parts['namespace'] !== NS_MAIN ) ) {
178 throw new MalformedTitleException( 'title-invalid-empty', $text );
179 }
180
181 return new TitleValue(
182 $parts['namespace'],
183 $parts['dbkey'],
184 $parts['fragment'],
185 $parts['interwiki']
186 );
187 }
188
189 /**
190 * @see TitleFormatter::getText()
191 *
192 * @param LinkTarget $title
193 *
194 * @return string $title->getText()
195 */
196 public function getText( LinkTarget $title ) {
197 return $title->getText();
198 }
199
200 /**
201 * @see TitleFormatter::getText()
202 *
203 * @param LinkTarget $title
204 *
205 * @return string
206 */
207 public function getPrefixedText( LinkTarget $title ) {
208 if ( !isset( $title->prefixedText ) ) {
209 $title->prefixedText = $this->formatTitle(
210 $title->getNamespace(),
211 $title->getText(),
212 '',
213 $title->getInterwiki()
214 );
215 }
216
217 return $title->prefixedText;
218 }
219
220 /**
221 * @since 1.27
222 * @see TitleFormatter::getPrefixedDBkey()
223 * @param LinkTarget $target
224 * @return string
225 */
226 public function getPrefixedDBkey( LinkTarget $target ) {
227 return strtr( $this->formatTitle(
228 $target->getNamespace(),
229 $target->getDBkey(),
230 '',
231 $target->getInterwiki()
232 ), ' ', '_' );
233 }
234
235 /**
236 * @see TitleFormatter::getText()
237 *
238 * @param LinkTarget $title
239 *
240 * @return string
241 */
242 public function getFullText( LinkTarget $title ) {
243 return $this->formatTitle(
244 $title->getNamespace(),
245 $title->getText(),
246 $title->getFragment(),
247 $title->getInterwiki()
248 );
249 }
250
251 /**
252 * Normalizes and splits a title string.
253 *
254 * This function removes illegal characters, splits off the interwiki and
255 * namespace prefixes, sets the other forms, and canonicalizes
256 * everything.
257 *
258 * @todo this method is only exposed as a temporary measure to ease refactoring.
259 * It was copied with minimal changes from Title::secureAndSplit().
260 *
261 * @todo This method should be split up and an appropriate interface
262 * defined for use by the Title class.
263 *
264 * @param string $text
265 * @param int $defaultNamespace
266 *
267 * @throws MalformedTitleException If $text is not a valid title string.
268 * @return array A map with the fields 'interwiki', 'fragment', 'namespace',
269 * 'user_case_dbkey', and 'dbkey'.
270 */
271 public function splitTitleString( $text, $defaultNamespace = NS_MAIN ) {
272 $dbkey = str_replace( ' ', '_', $text );
273
274 # Initialisation
275 $parts = [
276 'interwiki' => '',
277 'local_interwiki' => false,
278 'fragment' => '',
279 'namespace' => $defaultNamespace,
280 'dbkey' => $dbkey,
281 'user_case_dbkey' => $dbkey,
282 ];
283
284 # Strip Unicode bidi override characters.
285 # Sometimes they slip into cut-n-pasted page titles, where the
286 # override chars get included in list displays.
287 $dbkey = preg_replace( '/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u', '', $dbkey );
288
289 # Clean up whitespace
290 # Note: use of the /u option on preg_replace here will cause
291 # input with invalid UTF-8 sequences to be nullified out in PHP 5.2.x,
292 # conveniently disabling them.
293 $dbkey = preg_replace(
294 '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
295 '_',
296 $dbkey
297 );
298 $dbkey = trim( $dbkey, '_' );
299
300 if ( strpos( $dbkey, UtfNormal\Constants::UTF8_REPLACEMENT ) !== false ) {
301 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
302 throw new MalformedTitleException( 'title-invalid-utf8', $text );
303 }
304
305 $parts['dbkey'] = $dbkey;
306
307 # Initial colon indicates main namespace rather than specified default
308 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
309 if ( $dbkey !== '' && $dbkey[0] == ':' ) {
310 $parts['namespace'] = NS_MAIN;
311 $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing
312 $dbkey = trim( $dbkey, '_' ); # remove any subsequent whitespace
313 }
314
315 if ( $dbkey == '' ) {
316 throw new MalformedTitleException( 'title-invalid-empty', $text );
317 }
318
319 # Namespace or interwiki prefix
320 $prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
321 do {
322 $m = [];
323 if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
324 $p = $m[1];
325 $ns = $this->language->getNsIndex( $p );
326 if ( $ns !== false ) {
327 # Ordinary namespace
328 $dbkey = $m[2];
329 $parts['namespace'] = $ns;
330 # For Talk:X pages, check if X has a "namespace" prefix
331 if ( $ns == NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
332 if ( $this->language->getNsIndex( $x[1] ) ) {
333 # Disallow Talk:File:x type titles...
334 throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
335 } elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
336 # Disallow Talk:Interwiki:x type titles...
337 throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
338 }
339 }
340 } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
341 # Interwiki link
342 $dbkey = $m[2];
343 $parts['interwiki'] = $this->language->lc( $p );
344
345 # Redundant interwiki prefix to the local wiki
346 foreach ( $this->localInterwikis as $localIW ) {
347 if ( strcasecmp( $parts['interwiki'], $localIW ) == 0 ) {
348 if ( $dbkey == '' ) {
349 # Empty self-links should point to the Main Page, to ensure
350 # compatibility with cross-wiki transclusions and the like.
351 $mainPage = Title::newMainPage();
352 return [
353 'interwiki' => $mainPage->getInterwiki(),
354 'local_interwiki' => true,
355 'fragment' => $mainPage->getFragment(),
356 'namespace' => $mainPage->getNamespace(),
357 'dbkey' => $mainPage->getDBkey(),
358 'user_case_dbkey' => $mainPage->getUserCaseDBKey()
359 ];
360 }
361 $parts['interwiki'] = '';
362 # local interwikis should behave like initial-colon links
363 $parts['local_interwiki'] = true;
364
365 # Do another namespace split...
366 continue 2;
367 }
368 }
369
370 # If there's an initial colon after the interwiki, that also
371 # resets the default namespace
372 if ( $dbkey !== '' && $dbkey[0] == ':' ) {
373 $parts['namespace'] = NS_MAIN;
374 $dbkey = substr( $dbkey, 1 );
375 $dbkey = trim( $dbkey, '_' );
376 }
377 }
378 # If there's no recognized interwiki or namespace,
379 # then let the colon expression be part of the title.
380 }
381 break;
382 } while ( true );
383
384 $fragment = strstr( $dbkey, '#' );
385 if ( $fragment !== false ) {
386 $parts['fragment'] = str_replace( '_', ' ', substr( $fragment, 1 ) );
387 $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
388 # remove whitespace again: prevents "Foo_bar_#"
389 # becoming "Foo_bar_"
390 $dbkey = preg_replace( '/_*$/', '', $dbkey );
391 }
392
393 # Reject illegal characters.
394 $rxTc = self::getTitleInvalidRegex();
395 $matches = [];
396 if ( preg_match( $rxTc, $dbkey, $matches ) ) {
397 throw new MalformedTitleException( 'title-invalid-characters', $text, [ $matches[0] ] );
398 }
399
400 # Pages with "/./" or "/../" appearing in the URLs will often be un-
401 # reachable due to the way web browsers deal with 'relative' URLs.
402 # Also, they conflict with subpage syntax. Forbid them explicitly.
403 if (
404 strpos( $dbkey, '.' ) !== false &&
405 (
406 $dbkey === '.' || $dbkey === '..' ||
407 strpos( $dbkey, './' ) === 0 ||
408 strpos( $dbkey, '../' ) === 0 ||
409 strpos( $dbkey, '/./' ) !== false ||
410 strpos( $dbkey, '/../' ) !== false ||
411 substr( $dbkey, -2 ) == '/.' ||
412 substr( $dbkey, -3 ) == '/..'
413 )
414 ) {
415 throw new MalformedTitleException( 'title-invalid-relative', $text );
416 }
417
418 # Magic tilde sequences? Nu-uh!
419 if ( strpos( $dbkey, '~~~' ) !== false ) {
420 throw new MalformedTitleException( 'title-invalid-magic-tilde', $text );
421 }
422
423 # Limit the size of titles to 255 bytes. This is typically the size of the
424 # underlying database field. We make an exception for special pages, which
425 # don't need to be stored in the database, and may edge over 255 bytes due
426 # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
427 $maxLength = ( $parts['namespace'] != NS_SPECIAL ) ? 255 : 512;
428 if ( strlen( $dbkey ) > $maxLength ) {
429 throw new MalformedTitleException( 'title-invalid-too-long', $text,
430 [ Message::numParam( $maxLength ) ] );
431 }
432
433 # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
434 # and [[Foo]] point to the same place. Don't force it for interwikis, since the
435 # other site might be case-sensitive.
436 $parts['user_case_dbkey'] = $dbkey;
437 if ( $parts['interwiki'] === '' ) {
438 $dbkey = Title::capitalize( $dbkey, $parts['namespace'] );
439 }
440
441 # Can't make a link to a namespace alone... "empty" local links can only be
442 # self-links with a fragment identifier.
443 if ( $dbkey == '' && $parts['interwiki'] === '' && $parts['namespace'] != NS_MAIN ) {
444 throw new MalformedTitleException( 'title-invalid-empty', $text );
445 }
446
447 // Allow IPv6 usernames to start with '::' by canonicalizing IPv6 titles.
448 // IP names are not allowed for accounts, and can only be referring to
449 // edits from the IP. Given '::' abbreviations and caps/lowercaps,
450 // there are numerous ways to present the same IP. Having sp:contribs scan
451 // them all is silly and having some show the edits and others not is
452 // inconsistent. Same for talk/userpages. Keep them normalized instead.
453 if ( $parts['namespace'] == NS_USER || $parts['namespace'] == NS_USER_TALK ) {
454 $dbkey = IP::sanitizeIP( $dbkey );
455 }
456
457 // Any remaining initial :s are illegal.
458 if ( $dbkey !== '' && $dbkey[0] == ':' ) {
459 throw new MalformedTitleException( 'title-invalid-leading-colon', $text );
460 }
461
462 # Fill fields
463 $parts['dbkey'] = $dbkey;
464
465 return $parts;
466 }
467
468 /**
469 * Returns a simple regex that will match on characters and sequences invalid in titles.
470 * Note that this doesn't pick up many things that could be wrong with titles, but that
471 * replacing this regex with something valid will make many titles valid.
472 * Previously Title::getTitleInvalidRegex()
473 *
474 * @return string Regex string
475 * @since 1.25
476 */
477 public static function getTitleInvalidRegex() {
478 static $rxTc = false;
479 if ( !$rxTc ) {
480 # Matching titles will be held as illegal.
481 $rxTc = '/' .
482 # Any character not allowed is forbidden...
483 '[^' . Title::legalChars() . ']' .
484 # URL percent encoding sequences interfere with the ability
485 # to round-trip titles -- you can't link to them consistently.
486 '|%[0-9A-Fa-f]{2}' .
487 # XML/HTML character references produce similar issues.
488 '|&[A-Za-z0-9\x80-\xff]+;' .
489 '|&#[0-9]+;' .
490 '|&#x[0-9A-Fa-f]+;' .
491 '/S';
492 }
493
494 return $rxTc;
495 }
496 }