Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[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 * @param Language $language The language object to use for localizing namespace names.
61 * @param GenderCache $genderCache The gender cache for generating gendered namespace names
62 * @param string[]|string $localInterwikis
63 * @param InterwikiLookup|null $interwikiLookup
64 */
65 public function __construct( Language $language, GenderCache $genderCache,
66 $localInterwikis = [], $interwikiLookup = null
67 ) {
68 $this->language = $language;
69 $this->genderCache = $genderCache;
70 $this->localInterwikis = (array)$localInterwikis;
71 $this->interwikiLookup = $interwikiLookup ?:
72 MediaWikiServices::getInstance()->getInterwikiLookup();
73 }
74
75 /**
76 * @see TitleFormatter::getNamespaceName()
77 *
78 * @param int $namespace
79 * @param string $text
80 *
81 * @throws InvalidArgumentException If the namespace is invalid
82 * @return string Namespace name with underscores (not spaces)
83 */
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
102 /**
103 * @see TitleFormatter::formatTitle()
104 *
105 * @param int|bool $namespace The namespace ID (or false, if the namespace should be ignored)
106 * @param string $text The page title. Should be valid. Only minimal normalization is applied.
107 * Underscores will be replaced.
108 * @param string $fragment The fragment name (may be empty).
109 * @param string $interwiki The interwiki name (may be empty).
110 *
111 * @throws InvalidArgumentException If the namespace is invalid
112 * @return string
113 */
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
141 /**
142 * Parses the given text and constructs a TitleValue. Normalization
143 * is applied according to the rules appropriate for the form specified by $form.
144 *
145 * @param string $text The text to parse
146 * @param int $defaultNamespace Namespace to assume per default (usually NS_MAIN)
147 *
148 * @throws MalformedTitleException
149 * @return TitleValue
150 */
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
170 /**
171 * @see TitleFormatter::getText()
172 *
173 * @param LinkTarget $title
174 *
175 * @return string $title->getText()
176 */
177 public function getText( LinkTarget $title ) {
178 return $title->getText();
179 }
180
181 /**
182 * @see TitleFormatter::getText()
183 *
184 * @param LinkTarget $title
185 *
186 * @return string
187 */
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
201 /**
202 * @since 1.27
203 * @see TitleFormatter::getPrefixedDBkey()
204 * @param LinkTarget $target
205 * @return string
206 */
207 public function getPrefixedDBkey( LinkTarget $target ) {
208 return strtr( $this->formatTitle(
209 $target->getNamespace(),
210 $target->getDBkey(),
211 '',
212 $target->getInterwiki()
213 ), ' ', '_' );
214 }
215
216 /**
217 * @see TitleFormatter::getText()
218 *
219 * @param LinkTarget $title
220 *
221 * @return string
222 */
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
232 /**
233 * Normalizes and splits a title string.
234 *
235 * This function removes illegal characters, splits off the interwiki and
236 * namespace prefixes, sets the other forms, and canonicalizes
237 * everything.
238 *
239 * @todo this method is only exposed as a temporary measure to ease refactoring.
240 * It was copied with minimal changes from Title::secureAndSplit().
241 *
242 * @todo This method should be split up and an appropriate interface
243 * defined for use by the Title class.
244 *
245 * @param string $text
246 * @param int $defaultNamespace
247 *
248 * @throws MalformedTitleException If $text is not a valid title string.
249 * @return array A map with the fields 'interwiki', 'fragment', 'namespace',
250 * 'user_case_dbkey', and 'dbkey'.
251 */
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.
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'] === '' ) {
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
451 /**
452 * Returns a simple regex that will match on characters and sequences invalid in titles.
453 * Note that this doesn't pick up many things that could be wrong with titles, but that
454 * replacing this regex with something valid will make many titles valid.
455 * Previously Title::getTitleInvalidRegex()
456 *
457 * @return string Regex string
458 * @since 1.25
459 */
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 }