| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Site; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use RuntimeException; |
| 27 |
use UnexpectedValueException; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class Site { |
| 37 |
public const TYPE_UNKNOWN = 'unknown'; |
| 38 |
public const TYPE_MEDIAWIKI = 'mediawiki'; |
| 39 |
|
| 40 |
public const GROUP_NONE = 'none'; |
| 41 |
|
| 42 |
public const ID_INTERWIKI = 'interwiki'; |
| 43 |
public const ID_EQUIVALENT = 'equivalent'; |
| 44 |
|
| 45 |
public const SOURCE_LOCAL = 'local'; |
| 46 |
|
| 47 |
public const PATH_LINK = 'link'; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public const SERIAL_VERSION_ID = '2013-01-23'; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected $globalId = null; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
protected $type = self::TYPE_UNKNOWN; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
protected $group = self::GROUP_NONE; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
protected $source = self::SOURCE_LOCAL; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected $languageCode = null; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
protected $localIds = []; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
protected $extraData = []; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected $extraConfig = []; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
protected $forward = false; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
protected $internalId = null; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function __construct( $type = self::TYPE_UNKNOWN ) { |
| 137 |
$this->type = $type; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function getGlobalId() { |
| 148 |
return $this->globalId; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function setGlobalId( ?string $globalId ) { |
| 158 |
$this->globalId = $globalId; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function getType() { |
| 169 |
return $this->type; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function getGroup() { |
| 180 |
return $this->group; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function setGroup( string $group ) { |
| 190 |
$this->group = $group; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function getSource() { |
| 201 |
return $this->source; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function setSource( string $source ) { |
| 211 |
$this->source = $source; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function shouldForward() { |
| 223 |
return $this->forward; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function setForward( bool $shouldForward ) { |
| 234 |
$this->forward = $shouldForward; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function getDomain(): ?string { |
| 246 |
$path = $this->getLinkPath(); |
| 247 |
|
| 248 |
if ( $path === null ) { |
| 249 |
return null; |
| 250 |
} |
| 251 |
|
| 252 |
$domain = parse_url( $path, PHP_URL_HOST ); |
| 253 |
|
| 254 |
if ( $domain === false ) { |
| 255 |
$domain = null; |
| 256 |
} |
| 257 |
|
| 258 |
return $domain; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function getProtocol() { |
| 268 |
$path = $this->getLinkPath(); |
| 269 |
|
| 270 |
if ( $path === null ) { |
| 271 |
return ''; |
| 272 |
} |
| 273 |
|
| 274 |
$protocol = parse_url( $path, PHP_URL_SCHEME ); |
| 275 |
|
| 276 |
|
| 277 |
if ( $protocol === false ) { |
| 278 |
throw new UnexpectedValueException( "failed to parse URL '$path'" ); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
return $protocol ?? ''; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public function setLinkPath( $fullUrl ) { |
| 294 |
$type = $this->getLinkPathType(); |
| 295 |
|
| 296 |
if ( $type === null ) { |
| 297 |
throw new RuntimeException( "This Site does not support link paths." ); |
| 298 |
} |
| 299 |
|
| 300 |
$this->setPath( $type, $fullUrl ); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public function getLinkPath() { |
| 311 |
$type = $this->getLinkPathType(); |
| 312 |
return $type === null ? null : $this->getPath( $type ); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public function getLinkPathType() { |
| 328 |
return self::PATH_LINK; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public function getPageUrl( $pageName = false ) { |
| 346 |
$url = $this->getLinkPath(); |
| 347 |
|
| 348 |
if ( $url === null ) { |
| 349 |
return null; |
| 350 |
} |
| 351 |
|
| 352 |
if ( $pageName !== false ) { |
| 353 |
$url = str_replace( '$1', rawurlencode( $pageName ), $url ); |
| 354 |
} |
| 355 |
|
| 356 |
return $url; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) { |
| 377 |
return $pageName; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public function getExtraData() { |
| 388 |
return $this->extraData; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public function setExtraData( array $extraData ) { |
| 399 |
$this->extraData = $extraData; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public function getExtraConfig() { |
| 410 |
return $this->extraConfig; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public function setExtraConfig( array $extraConfig ) { |
| 421 |
$this->extraConfig = $extraConfig; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public function getLanguageCode() { |
| 433 |
return $this->languageCode; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public function setLanguageCode( $languageCode ) { |
| 444 |
if ( $languageCode !== null && |
| 445 |
!MediaWikiServices::getInstance()->getLanguageNameUtils()->isValidCode( $languageCode ) ) { |
| 446 |
throw new InvalidArgumentException( "$languageCode is not a valid language code." ); |
| 447 |
} |
| 448 |
$this->languageCode = $languageCode; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
public function getInternalId() { |
| 459 |
return $this->internalId; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public function setInternalId( $internalId = null ) { |
| 471 |
$this->internalId = $internalId; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public function addLocalId( $type, $identifier ) { |
| 483 |
if ( $this->localIds === false ) { |
| 484 |
$this->localIds = []; |
| 485 |
} |
| 486 |
|
| 487 |
$this->localIds[$type] ??= []; |
| 488 |
|
| 489 |
if ( !in_array( $identifier, $this->localIds[$type] ) ) { |
| 490 |
$this->localIds[$type][] = $identifier; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public function addInterwikiId( $identifier ) { |
| 502 |
$this->addLocalId( self::ID_INTERWIKI, $identifier ); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
public function addNavigationId( $identifier ) { |
| 513 |
$this->addLocalId( self::ID_EQUIVALENT, $identifier ); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public function getInterwikiIds() { |
| 524 |
return $this->localIds[self::ID_INTERWIKI] ?? []; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
public function getNavigationIds() { |
| 536 |
return $this->localIds[self::ID_EQUIVALENT] ?? []; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
public function getLocalIds() { |
| 547 |
return $this->localIds; |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
public function setPath( $pathType, string $fullUrl ) { |
| 560 |
$this->extraData['paths'][$pathType] = $fullUrl; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public function getPath( $pathType ) { |
| 573 |
$paths = $this->getAllPaths(); |
| 574 |
return $paths[$pathType] ?? null; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
public function getAllPaths() { |
| 586 |
return $this->extraData['paths'] ?? []; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public function removePath( $pathType ) { |
| 597 |
if ( array_key_exists( 'paths', $this->extraData ) ) { |
| 598 |
unset( $this->extraData['paths'][$pathType] ); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
public static function newForType( $siteType ) { |
| 610 |
|
| 611 |
$siteTypes = MediaWikiServices::getInstance()->getMainConfig()->get( |
| 612 |
MainConfigNames::SiteTypes |
| 613 |
); |
| 614 |
|
| 615 |
if ( array_key_exists( $siteType, $siteTypes ) ) { |
| 616 |
return new $siteTypes[$siteType](); |
| 617 |
} |
| 618 |
|
| 619 |
return new Site(); |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
public function __serialize() { |
| 630 |
return [ |
| 631 |
'globalid' => $this->globalId, |
| 632 |
'type' => $this->type, |
| 633 |
'group' => $this->group, |
| 634 |
'source' => $this->source, |
| 635 |
'language' => $this->languageCode, |
| 636 |
'localids' => $this->localIds, |
| 637 |
'config' => $this->extraConfig, |
| 638 |
'data' => $this->extraData, |
| 639 |
'forward' => $this->forward, |
| 640 |
'internalid' => $this->internalId, |
| 641 |
]; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
public function __unserialize( $fields ) { |
| 652 |
$this->__construct( $fields['type'] ); |
| 653 |
|
| 654 |
$this->setGlobalId( $fields['globalid'] ); |
| 655 |
$this->setGroup( $fields['group'] ); |
| 656 |
$this->setSource( $fields['source'] ); |
| 657 |
$this->setLanguageCode( $fields['language'] ); |
| 658 |
$this->localIds = $fields['localids']; |
| 659 |
$this->setExtraConfig( $fields['config'] ); |
| 660 |
$this->setExtraData( $fields['data'] ); |
| 661 |
$this->setForward( $fields['forward'] ); |
| 662 |
$this->setInternalId( $fields['internalid'] ); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
class_alias( Site::class, 'Site' ); |