| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Title; |
| 25 |
|
| 26 |
use InvalidArgumentException; |
| 27 |
use LogicException; |
| 28 |
use MediaWiki\Cache\GenderCache; |
| 29 |
use MediaWiki\Interwiki\InterwikiLookup; |
| 30 |
use MediaWiki\Language\Language; |
| 31 |
use MediaWiki\Linker\LinkTarget; |
| 32 |
use MediaWiki\Message\Message; |
| 33 |
use MediaWiki\Page\PageReference; |
| 34 |
use MediaWiki\Parser\Sanitizer; |
| 35 |
use Wikimedia\IPUtils; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class MediaWikiTitleCodec implements TitleFormatter, TitleParser { |
| 49 |
protected Language $language; |
| 50 |
protected GenderCache $genderCache; |
| 51 |
|
| 52 |
protected array $localInterwikis; |
| 53 |
protected InterwikiLookup $interwikiLookup; |
| 54 |
protected NamespaceInfo $nsInfo; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private $createMalformedTitleException; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function __construct( |
| 74 |
Language $language, |
| 75 |
GenderCache $genderCache, |
| 76 |
$localInterwikis, |
| 77 |
InterwikiLookup $interwikiLookup, |
| 78 |
NamespaceInfo $nsInfo |
| 79 |
) { |
| 80 |
$this->language = $language; |
| 81 |
$this->genderCache = $genderCache; |
| 82 |
$this->localInterwikis = (array)$localInterwikis; |
| 83 |
$this->interwikiLookup = $interwikiLookup; |
| 84 |
$this->nsInfo = $nsInfo; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
$this->createMalformedTitleException = static function ( |
| 89 |
$errorMessage, |
| 90 |
$titleText = null, |
| 91 |
$errorMessageParameters = [] |
| 92 |
): MalformedTitleException { |
| 93 |
return new MalformedTitleException( $errorMessage, $titleText, $errorMessageParameters ); |
| 94 |
}; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function overrideCreateMalformedTitleExceptionCallback( callable $callback ) { |
| 102 |
|
| 103 |
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
| 104 |
throw new LogicException( __METHOD__ . ' can only be used in tests' ); |
| 105 |
} |
| 106 |
|
| 107 |
$this->createMalformedTitleException = $callback; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getNamespaceName( $namespace, $text ) { |
| 120 |
if ( $this->language->needsGenderDistinction() && |
| 121 |
$this->nsInfo->hasGenderDistinction( $namespace ) |
| 122 |
) { |
| 123 |
|
| 124 |
$gender = $this->genderCache->getGenderOf( $text, __METHOD__ ); |
| 125 |
$name = $this->language->getGenderNsText( $namespace, $gender ); |
| 126 |
} else { |
| 127 |
$name = $this->language->getNsText( $namespace ); |
| 128 |
} |
| 129 |
|
| 130 |
if ( $name === false ) { |
| 131 |
throw new InvalidArgumentException( 'Unknown namespace ID: ' . $namespace ); |
| 132 |
} |
| 133 |
|
| 134 |
return $name; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) { |
| 150 |
$out = ''; |
| 151 |
if ( $interwiki !== '' ) { |
| 152 |
$out = $interwiki . ':'; |
| 153 |
} |
| 154 |
|
| 155 |
if ( $namespace != 0 ) { |
| 156 |
try { |
| 157 |
$nsName = $this->getNamespaceName( $namespace, $text ); |
| 158 |
} catch ( InvalidArgumentException $e ) { |
| 159 |
|
| 160 |
$nsName = $this->language->getNsText( NS_SPECIAL ) . ":Badtitle/NS{$namespace}"; |
| 161 |
} |
| 162 |
|
| 163 |
$out .= $nsName . ':'; |
| 164 |
} |
| 165 |
$out .= $text; |
| 166 |
|
| 167 |
if ( $fragment !== '' ) { |
| 168 |
$out .= '#' . $fragment; |
| 169 |
} |
| 170 |
|
| 171 |
$out = str_replace( '_', ' ', $out ); |
| 172 |
|
| 173 |
return $out; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function parseTitle( $text, $defaultNamespace = NS_MAIN ) { |
| 186 |
|
| 187 |
$filteredText = Sanitizer::decodeCharReferencesAndNormalize( $text ); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
$parts = $this->splitTitleString( $filteredText, $defaultNamespace ); |
| 193 |
|
| 194 |
return new TitleValue( |
| 195 |
$parts['namespace'], |
| 196 |
$parts['dbkey'], |
| 197 |
$parts['fragment'], |
| 198 |
$parts['interwiki'] |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function makeTitleValueSafe( $namespace, $text, $fragment = '', $interwiki = '' ) { |
| 213 |
if ( !$this->nsInfo->exists( $namespace ) ) { |
| 214 |
return null; |
| 215 |
} |
| 216 |
|
| 217 |
$canonicalNs = $this->nsInfo->getCanonicalName( $namespace ); |
| 218 |
$fullText = $canonicalNs == '' ? $text : "$canonicalNs:$text"; |
| 219 |
if ( strval( $interwiki ) != '' ) { |
| 220 |
$fullText = "$interwiki:$fullText"; |
| 221 |
} |
| 222 |
if ( strval( $fragment ) != '' ) { |
| 223 |
$fullText .= '#' . $fragment; |
| 224 |
} |
| 225 |
|
| 226 |
try { |
| 227 |
$parts = $this->splitTitleString( $fullText ); |
| 228 |
} catch ( MalformedTitleException $e ) { |
| 229 |
return null; |
| 230 |
} |
| 231 |
|
| 232 |
return new TitleValue( |
| 233 |
$parts['namespace'], $parts['dbkey'], $parts['fragment'], $parts['interwiki'] ); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function getText( $title ) { |
| 244 |
if ( $title instanceof LinkTarget ) { |
| 245 |
return $title->getText(); |
| 246 |
} elseif ( $title instanceof PageReference ) { |
| 247 |
return strtr( $title->getDBKey(), '_', ' ' ); |
| 248 |
} else { |
| 249 |
throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) ); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function getPrefixedText( $title ) { |
| 262 |
if ( $title instanceof LinkTarget ) { |
| 263 |
if ( !isset( $title->prefixedText ) ) { |
| 264 |
$title->prefixedText = $this->formatTitle( |
| 265 |
$title->getNamespace(), |
| 266 |
$title->getText(), |
| 267 |
'', |
| 268 |
$title->getInterwiki() |
| 269 |
); |
| 270 |
} |
| 271 |
return $title->prefixedText; |
| 272 |
} elseif ( $title instanceof PageReference ) { |
| 273 |
$title->assertWiki( PageReference::LOCAL ); |
| 274 |
return $this->formatTitle( |
| 275 |
$title->getNamespace(), |
| 276 |
$this->getText( $title ) |
| 277 |
); |
| 278 |
} else { |
| 279 |
throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) ); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function getPrefixedDBkey( $target ) { |
| 290 |
if ( $target instanceof LinkTarget ) { |
| 291 |
return strtr( $this->formatTitle( |
| 292 |
$target->getNamespace(), |
| 293 |
$target->getDBkey(), |
| 294 |
'', |
| 295 |
$target->getInterwiki() |
| 296 |
), ' ', '_' ); |
| 297 |
} elseif ( $target instanceof PageReference ) { |
| 298 |
$target->assertWiki( PageReference::LOCAL ); |
| 299 |
return strtr( $this->formatTitle( |
| 300 |
$target->getNamespace(), |
| 301 |
$target->getDBkey() |
| 302 |
), ' ', '_' ); |
| 303 |
} else { |
| 304 |
throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $target ) ); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function getFullText( $title ) { |
| 316 |
if ( $title instanceof LinkTarget ) { |
| 317 |
return $this->formatTitle( |
| 318 |
$title->getNamespace(), |
| 319 |
$title->getText(), |
| 320 |
$title->getFragment(), |
| 321 |
$title->getInterwiki() |
| 322 |
); |
| 323 |
} elseif ( $title instanceof PageReference ) { |
| 324 |
$title->assertWiki( PageReference::LOCAL ); |
| 325 |
return $this->formatTitle( |
| 326 |
$title->getNamespace(), |
| 327 |
$this->getText( $title ) |
| 328 |
); |
| 329 |
} else { |
| 330 |
throw new InvalidArgumentException( '$title has invalid type: ' . get_class( $title ) ); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public function splitTitleString( $text, $defaultNamespace = NS_MAIN ) { |
| 356 |
$dbkey = str_replace( ' ', '_', $text ); |
| 357 |
|
| 358 |
|
| 359 |
$parts = [ |
| 360 |
'interwiki' => '', |
| 361 |
'local_interwiki' => false, |
| 362 |
'fragment' => '', |
| 363 |
'namespace' => (int)$defaultNamespace, |
| 364 |
'dbkey' => $dbkey, |
| 365 |
]; |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
$dbkey = preg_replace( '/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u', '', $dbkey ); |
| 371 |
|
| 372 |
if ( $dbkey === null ) { |
| 373 |
|
| 374 |
$exception = ( $this->createMalformedTitleException )( 'title-invalid-utf8', $text ); |
| 375 |
throw $exception; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
$dbkey = preg_replace( |
| 380 |
'/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u', |
| 381 |
'_', |
| 382 |
$dbkey |
| 383 |
); |
| 384 |
$dbkey = trim( $dbkey, '_' ); |
| 385 |
|
| 386 |
if ( strpos( $dbkey, \UtfNormal\Constants::UTF8_REPLACEMENT ) !== false ) { |
| 387 |
|
| 388 |
$exception = ( $this->createMalformedTitleException )( 'title-invalid-utf8', $text ); |
| 389 |
throw $exception; |
| 390 |
} |
| 391 |
|
| 392 |
$parts['dbkey'] = $dbkey; |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
if ( $dbkey !== '' && $dbkey[0] == ':' ) { |
| 397 |
$parts['namespace'] = NS_MAIN; |
| 398 |
$dbkey = substr( $dbkey, 1 ); |
| 399 |
$dbkey = trim( $dbkey, '_' ); |
| 400 |
} |
| 401 |
|
| 402 |
if ( $dbkey == '' ) { |
| 403 |
$exception = ( $this->createMalformedTitleException )( 'title-invalid-empty', $text ); |
| 404 |
throw $exception; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
$prefixRegexp = "/^(.+?)_*:_*(.*)$/S"; |
| 409 |
do { |
| 410 |
$m = []; |
| 411 |
if ( preg_match( $prefixRegexp, $dbkey, $m ) ) { |
| 412 |
$p = $m[1]; |
| 413 |
$ns = $this->language->getNsIndex( $p ); |
| 414 |
if ( $ns !== false ) { |
| 415 |
|
| 416 |
$dbkey = $m[2]; |
| 417 |
$parts['namespace'] = $ns; |
| 418 |
|
| 419 |
if ( $ns === NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) { |
| 420 |
if ( $this->language->getNsIndex( $x[1] ) ) { |
| 421 |
|
| 422 |
$exception = ( $this->createMalformedTitleException )( |
| 423 |
'title-invalid-talk-namespace', |
| 424 |
$text |
| 425 |
); |
| 426 |
throw $exception; |
| 427 |
} elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) { |
| 428 |
|
| 429 |
$exception = ( $this->createMalformedTitleException )( |
| 430 |
'title-invalid-talk-namespace', |
| 431 |
$text |
| 432 |
); |
| 433 |
throw $exception; |
| 434 |
} |
| 435 |
} |
| 436 |
} elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) { |
| 437 |
|
| 438 |
$dbkey = $m[2]; |
| 439 |
$parts['interwiki'] = $this->language->lc( $p ); |
| 440 |
|
| 441 |
|
| 442 |
foreach ( $this->localInterwikis as $localIW ) { |
| 443 |
if ( strcasecmp( $parts['interwiki'], $localIW ) == 0 ) { |
| 444 |
if ( $dbkey == '' ) { |
| 445 |
|
| 446 |
|
| 447 |
$mainPage = Title::newMainPage(); |
| 448 |
return [ |
| 449 |
'interwiki' => $mainPage->getInterwiki(), |
| 450 |
'local_interwiki' => true, |
| 451 |
'fragment' => $mainPage->getFragment(), |
| 452 |
'namespace' => $mainPage->getNamespace(), |
| 453 |
'dbkey' => $mainPage->getDBkey(), |
| 454 |
]; |
| 455 |
} |
| 456 |
$parts['interwiki'] = ''; |
| 457 |
|
| 458 |
$parts['local_interwiki'] = true; |
| 459 |
|
| 460 |
|
| 461 |
continue 2; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
if ( $dbkey !== '' && $dbkey[0] == ':' ) { |
| 468 |
$parts['namespace'] = NS_MAIN; |
| 469 |
$dbkey = substr( $dbkey, 1 ); |
| 470 |
$dbkey = trim( $dbkey, '_' ); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
} |
| 476 |
break; |
| 477 |
} while ( true ); |
| 478 |
|
| 479 |
$fragment = strstr( $dbkey, '#' ); |
| 480 |
if ( $fragment !== false ) { |
| 481 |
$parts['fragment'] = str_replace( '_', ' ', substr( $fragment, 1 ) ); |
| 482 |
$dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) ); |
| 483 |
|
| 484 |
|
| 485 |
$dbkey = rtrim( $dbkey, "_" ); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
$rxTc = self::getTitleInvalidRegex(); |
| 490 |
$matches = []; |
| 491 |
if ( preg_match( $rxTc, $dbkey, $matches ) ) { |
| 492 |
$exception = ( $this->createMalformedTitleException )( 'title-invalid-characters', $text, [ $matches[0] ] ); |
| 493 |
throw $exception; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
if ( |
| 500 |
str_contains( $dbkey, '.' ) && |
| 501 |
( |
| 502 |
$dbkey === '.' || $dbkey === '..' || |
| 503 |
str_starts_with( $dbkey, './' ) || |
| 504 |
str_starts_with( $dbkey, '../' ) || |
| 505 |
str_contains( $dbkey, '/./' ) || |
| 506 |
str_contains( $dbkey, '/../' ) || |
| 507 |
str_ends_with( $dbkey, '/.' ) || |
| 508 |
str_ends_with( $dbkey, '/..' ) |
| 509 |
) |
| 510 |
) { |
| 511 |
$exception = ( $this->createMalformedTitleException )( 'title-invalid-relative', $text ); |
| 512 |
throw $exception; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
if ( strpos( $dbkey, '~~~' ) !== false ) { |
| 517 |
$exception = ( $this->createMalformedTitleException )( 'title-invalid-magic-tilde', $text ); |
| 518 |
throw $exception; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
$maxLength = ( $parts['namespace'] !== NS_SPECIAL ) ? 255 : 512; |
| 526 |
if ( strlen( $dbkey ) > $maxLength ) { |
| 527 |
$exception = ( $this->createMalformedTitleException )( |
| 528 |
'title-invalid-too-long', |
| 529 |
$text, |
| 530 |
[ Message::numParam( $maxLength ) ] |
| 531 |
); |
| 532 |
throw $exception; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
if ( $parts['interwiki'] === '' && $this->nsInfo->isCapitalized( $parts['namespace'] ) ) { |
| 539 |
$dbkey = $this->language->ucfirst( $dbkey ); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
if ( $dbkey == '' && $parts['interwiki'] === '' && $parts['namespace'] !== NS_MAIN ) { |
| 545 |
$exception = ( $this->createMalformedTitleException )( 'title-invalid-empty', $text ); |
| 546 |
throw $exception; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
if ( $dbkey !== '' && ( $parts['namespace'] === NS_USER || $parts['namespace'] === NS_USER_TALK ) ) { |
| 556 |
$dbkey = IPUtils::sanitizeIP( $dbkey ); |
| 557 |
|
| 558 |
'@phan-var string $dbkey'; |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
if ( $dbkey !== '' && $dbkey[0] == ':' ) { |
| 563 |
$exception = ( $this->createMalformedTitleException )( 'title-invalid-leading-colon', $text ); |
| 564 |
throw $exception; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
$parts['dbkey'] = $dbkey; |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
try { |
| 573 |
TitleValue::assertValidSpec( |
| 574 |
$parts['namespace'], |
| 575 |
$parts['dbkey'], |
| 576 |
$parts['fragment'], |
| 577 |
$parts['interwiki'] |
| 578 |
); |
| 579 |
} catch ( InvalidArgumentException $ex ) { |
| 580 |
$exception = ( $this->createMalformedTitleException )( 'title-invalid', $text, [ $ex->getMessage() ] ); |
| 581 |
throw $exception; |
| 582 |
} |
| 583 |
|
| 584 |
return $parts; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public static function getTitleInvalidRegex() { |
| 597 |
static $rxTc = false; |
| 598 |
if ( !$rxTc ) { |
| 599 |
|
| 600 |
$rxTc = '/' . |
| 601 |
|
| 602 |
'[^' . Title::legalChars() . ']' . |
| 603 |
|
| 604 |
|
| 605 |
'|%[0-9A-Fa-f]{2}' . |
| 606 |
|
| 607 |
'|&[A-Za-z0-9\x80-\xff]+;' . |
| 608 |
'/S'; |
| 609 |
} |
| 610 |
|
| 611 |
return $rxTc; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
class_alias( MediaWikiTitleCodec::class, 'MediaWikiTitleCodec' ); |