| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Title; |
| 24 |
|
| 25 |
use InvalidArgumentException; |
| 26 |
use MediaWiki\Config\ServiceOptions; |
| 27 |
use MediaWiki\HookContainer\HookContainer; |
| 28 |
use MediaWiki\HookContainer\HookRunner; |
| 29 |
use MediaWiki\Linker\LinkTarget; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MWException; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class NamespaceInfo { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private const ALWAYS_CAPITALIZED_NAMESPACES = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ]; |
| 47 |
|
| 48 |
|
| 49 |
private $canonicalNamespaces = null; |
| 50 |
|
| 51 |
|
| 52 |
private $namespaceIndexes = false; |
| 53 |
|
| 54 |
|
| 55 |
private $validNamespaces = null; |
| 56 |
|
| 57 |
private ServiceOptions $options; |
| 58 |
private HookRunner $hookRunner; |
| 59 |
private array $extensionNamespaces; |
| 60 |
private array $extensionImmovableNamespaces; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public const CANONICAL_NAMES = [ |
| 68 |
NS_MEDIA => 'Media', |
| 69 |
NS_SPECIAL => 'Special', |
| 70 |
NS_MAIN => '', |
| 71 |
NS_TALK => 'Talk', |
| 72 |
NS_USER => 'User', |
| 73 |
NS_USER_TALK => 'User_talk', |
| 74 |
NS_PROJECT => 'Project', |
| 75 |
NS_PROJECT_TALK => 'Project_talk', |
| 76 |
NS_FILE => 'File', |
| 77 |
NS_FILE_TALK => 'File_talk', |
| 78 |
NS_MEDIAWIKI => 'MediaWiki', |
| 79 |
NS_MEDIAWIKI_TALK => 'MediaWiki_talk', |
| 80 |
NS_TEMPLATE => 'Template', |
| 81 |
NS_TEMPLATE_TALK => 'Template_talk', |
| 82 |
NS_HELP => 'Help', |
| 83 |
NS_HELP_TALK => 'Help_talk', |
| 84 |
NS_CATEGORY => 'Category', |
| 85 |
NS_CATEGORY_TALK => 'Category_talk', |
| 86 |
]; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public const CONSTRUCTOR_OPTIONS = [ |
| 92 |
MainConfigNames::CanonicalNamespaceNames, |
| 93 |
MainConfigNames::CapitalLinkOverrides, |
| 94 |
MainConfigNames::CapitalLinks, |
| 95 |
MainConfigNames::ContentNamespaces, |
| 96 |
MainConfigNames::ExtraNamespaces, |
| 97 |
MainConfigNames::ExtraSignatureNamespaces, |
| 98 |
MainConfigNames::NamespaceContentModels, |
| 99 |
MainConfigNames::NamespacesWithSubpages, |
| 100 |
MainConfigNames::NonincludableNamespaces, |
| 101 |
]; |
| 102 |
|
| 103 |
public function __construct( |
| 104 |
ServiceOptions $options, |
| 105 |
HookContainer $hookContainer, |
| 106 |
array $extensionNamespaces, |
| 107 |
array $extensionImmovableNamespaces |
| 108 |
) { |
| 109 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 110 |
$this->options = $options; |
| 111 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 112 |
$this->extensionNamespaces = $extensionNamespaces; |
| 113 |
$this->extensionImmovableNamespaces = $extensionImmovableNamespaces; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private function isMethodValidFor( $index, $method ) { |
| 129 |
if ( $index < NS_MAIN ) { |
| 130 |
throw new MWException( "$method does not make any sense for given namespace $index" ); |
| 131 |
} |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private function makeValidNamespace( $index, $method ) { |
| 145 |
if ( !( |
| 146 |
is_int( $index ) |
| 147 |
|
| 148 |
|| ctype_digit( $index ) |
| 149 |
|
| 150 |
|| ( $index[0] === '-' && ctype_digit( substr( $index, 1 ) ) ) |
| 151 |
) ) { |
| 152 |
throw new InvalidArgumentException( |
| 153 |
"$method called with non-integer (" . get_debug_type( $index ) . ") namespace '$index'" |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
return intval( $index ); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function isMovable( $index ) { |
| 167 |
$result = $index >= NS_MAIN && !in_array( $index, $this->extensionImmovableNamespaces ); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
$this->hookRunner->onNamespaceIsMovable( $index, $result ); |
| 173 |
|
| 174 |
return $result; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function isSubject( $index ) { |
| 184 |
return !$this->isTalk( $index ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function isTalk( $index ) { |
| 194 |
$index = $this->makeValidNamespace( $index, __METHOD__ ); |
| 195 |
|
| 196 |
return $index > NS_MAIN |
| 197 |
&& $index % 2 === 1; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function getTalk( $index ) { |
| 209 |
$index = $this->makeValidNamespace( $index, __METHOD__ ); |
| 210 |
|
| 211 |
$this->isMethodValidFor( $index, __METHOD__ ); |
| 212 |
return $this->isTalk( $index ) |
| 213 |
? $index |
| 214 |
: $index + 1; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function getTalkPage( LinkTarget $target ): LinkTarget { |
| 227 |
if ( $target->getText() === '' ) { |
| 228 |
throw new MWException( 'Can\'t determine talk page associated with relative section link' ); |
| 229 |
} |
| 230 |
|
| 231 |
if ( $target->getInterwiki() !== '' ) { |
| 232 |
throw new MWException( 'Can\'t determine talk page associated with interwiki link' ); |
| 233 |
} |
| 234 |
|
| 235 |
if ( $this->isTalk( $target->getNamespace() ) ) { |
| 236 |
return $target; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
return new TitleValue( $this->getTalk( $target->getNamespace() ), $target->getDBkey() ); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function canHaveTalkPage( LinkTarget $target ) { |
| 255 |
return $target->getNamespace() >= NS_MAIN && |
| 256 |
!$target->isExternal() && |
| 257 |
$target->getText() !== ''; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function getSubject( $index ) { |
| 268 |
$index = $this->makeValidNamespace( $index, __METHOD__ ); |
| 269 |
|
| 270 |
|
| 271 |
if ( $index < NS_MAIN ) { |
| 272 |
return $index; |
| 273 |
} |
| 274 |
|
| 275 |
return $this->isTalk( $index ) |
| 276 |
? $index - 1 |
| 277 |
: $index; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function getSubjectPage( LinkTarget $target ): LinkTarget { |
| 285 |
if ( $this->isSubject( $target->getNamespace() ) ) { |
| 286 |
return $target; |
| 287 |
} |
| 288 |
return new TitleValue( $this->getSubject( $target->getNamespace() ), $target->getDBkey() ); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public function getAssociated( $index ) { |
| 301 |
$this->isMethodValidFor( $index, __METHOD__ ); |
| 302 |
|
| 303 |
if ( $this->isSubject( $index ) ) { |
| 304 |
return $this->getTalk( $index ); |
| 305 |
} |
| 306 |
return $this->getSubject( $index ); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function getAssociatedPage( LinkTarget $target ): LinkTarget { |
| 316 |
if ( $target->getText() === '' ) { |
| 317 |
throw new MWException( 'Can\'t determine talk page associated with relative section link' ); |
| 318 |
} |
| 319 |
|
| 320 |
if ( $target->getInterwiki() !== '' ) { |
| 321 |
throw new MWException( 'Can\'t determine talk page associated with interwiki link' ); |
| 322 |
} |
| 323 |
|
| 324 |
return new TitleValue( |
| 325 |
$this->getAssociated( $target->getNamespace() ), $target->getDBkey() ); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public function exists( $index ) { |
| 336 |
$nslist = $this->getCanonicalNamespaces(); |
| 337 |
return isset( $nslist[$index] ); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function equals( $ns1, $ns2 ) { |
| 354 |
return $ns1 == $ns2; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public function subjectEquals( $ns1, $ns2 ) { |
| 368 |
return $this->getSubject( $ns1 ) == $this->getSubject( $ns2 ); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public function getCanonicalNamespaces() { |
| 378 |
if ( $this->canonicalNamespaces === null ) { |
| 379 |
$this->canonicalNamespaces = |
| 380 |
[ NS_MAIN => '' ] + $this->options->get( MainConfigNames::CanonicalNamespaceNames ); |
| 381 |
$this->canonicalNamespaces += $this->extensionNamespaces; |
| 382 |
if ( is_array( $this->options->get( MainConfigNames::ExtraNamespaces ) ) ) { |
| 383 |
$this->canonicalNamespaces += $this->options->get( MainConfigNames::ExtraNamespaces ); |
| 384 |
} |
| 385 |
$this->hookRunner->onCanonicalNamespaces( $this->canonicalNamespaces ); |
| 386 |
} |
| 387 |
return $this->canonicalNamespaces; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
public function getCanonicalName( $index ) { |
| 397 |
$nslist = $this->getCanonicalNamespaces(); |
| 398 |
return $nslist[$index] ?? false; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public function getCanonicalIndex( $name ) { |
| 409 |
if ( $this->namespaceIndexes === false ) { |
| 410 |
$this->namespaceIndexes = []; |
| 411 |
foreach ( $this->getCanonicalNamespaces() as $i => $text ) { |
| 412 |
$this->namespaceIndexes[strtolower( $text )] = $i; |
| 413 |
} |
| 414 |
} |
| 415 |
if ( array_key_exists( $name, $this->namespaceIndexes ) ) { |
| 416 |
return $this->namespaceIndexes[$name]; |
| 417 |
} else { |
| 418 |
return null; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public function getValidNamespaces() { |
| 428 |
if ( $this->validNamespaces === null ) { |
| 429 |
$this->validNamespaces = []; |
| 430 |
foreach ( $this->getCanonicalNamespaces() as $ns => $_ ) { |
| 431 |
if ( $ns >= 0 ) { |
| 432 |
$this->validNamespaces[] = $ns; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
sort( $this->validNamespaces, SORT_NUMERIC ); |
| 437 |
} |
| 438 |
|
| 439 |
return $this->validNamespaces; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
public function hasTalkNamespace( $index ) { |
| 449 |
return $index >= NS_MAIN; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
public function isContent( $index ) { |
| 460 |
return $index == NS_MAIN || |
| 461 |
in_array( $index, $this->options->get( MainConfigNames::ContentNamespaces ) ); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
public function wantSignatures( $index ) { |
| 472 |
return $this->isTalk( $index ) || |
| 473 |
in_array( $index, $this->options->get( MainConfigNames::ExtraSignatureNamespaces ) ); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public function isWatchable( $index ) { |
| 483 |
return $index >= NS_MAIN; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
public function hasSubpages( $index ) { |
| 494 |
return !empty( $this->options->get( MainConfigNames::NamespacesWithSubpages )[$index] ); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public function getContentNamespaces() { |
| 502 |
$contentNamespaces = $this->options->get( MainConfigNames::ContentNamespaces ); |
| 503 |
if ( !is_array( $contentNamespaces ) || $contentNamespaces === [] ) { |
| 504 |
return [ NS_MAIN ]; |
| 505 |
} elseif ( !in_array( NS_MAIN, $contentNamespaces ) ) { |
| 506 |
|
| 507 |
return array_merge( [ NS_MAIN ], $contentNamespaces ); |
| 508 |
} else { |
| 509 |
return $contentNamespaces; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public function getSubjectNamespaces() { |
| 520 |
return array_filter( |
| 521 |
$this->getValidNamespaces(), |
| 522 |
[ $this, 'isSubject' ] |
| 523 |
); |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
public function getTalkNamespaces() { |
| 533 |
return array_filter( |
| 534 |
$this->getValidNamespaces(), |
| 535 |
[ $this, 'isTalk' ] |
| 536 |
); |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public function isCapitalized( $index ) { |
| 546 |
|
| 547 |
$index = $index === NS_MEDIA ? NS_FILE : $index; |
| 548 |
|
| 549 |
|
| 550 |
$index = $this->getSubject( $index ); |
| 551 |
|
| 552 |
|
| 553 |
if ( in_array( $index, self::ALWAYS_CAPITALIZED_NAMESPACES ) ) { |
| 554 |
return true; |
| 555 |
} |
| 556 |
$overrides = $this->options->get( MainConfigNames::CapitalLinkOverrides ); |
| 557 |
if ( isset( $overrides[$index] ) ) { |
| 558 |
|
| 559 |
return $overrides[$index]; |
| 560 |
} |
| 561 |
|
| 562 |
return $this->options->get( MainConfigNames::CapitalLinks ); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public function hasGenderDistinction( $index ) { |
| 573 |
return $index == NS_USER || $index == NS_USER_TALK; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
public function isNonincludable( $index ) { |
| 583 |
$namespaces = $this->options->get( MainConfigNames::NonincludableNamespaces ); |
| 584 |
return $namespaces && in_array( $index, $namespaces ); |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
public function getNamespaceContentModel( $index ) { |
| 598 |
return $this->options->get( MainConfigNames::NamespaceContentModels )[$index] ?? null; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
public function getCategoryLinkType( $index ) { |
| 611 |
$this->isMethodValidFor( $index, __METHOD__ ); |
| 612 |
|
| 613 |
if ( $index == NS_CATEGORY ) { |
| 614 |
return 'subcat'; |
| 615 |
} elseif ( $index == NS_FILE ) { |
| 616 |
return 'file'; |
| 617 |
} else { |
| 618 |
return 'page'; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
public static function getCommonNamespaces() { |
| 630 |
return array_keys( self::CANONICAL_NAMES ); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
class_alias( NamespaceInfo::class, 'NamespaceInfo' ); |