MediaWiki master
Site.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Site;
8
10use RuntimeException;
11use UnexpectedValueException;
12
20class Site {
21 public const TYPE_UNKNOWN = 'unknown';
22 public const TYPE_MEDIAWIKI = 'mediawiki';
23
24 public const GROUP_NONE = 'none';
25
26 public const ID_INTERWIKI = 'interwiki';
27 public const ID_EQUIVALENT = 'equivalent';
28
29 public const SOURCE_LOCAL = 'local';
30
31 public const PATH_LINK = 'link';
32
40 public const SERIAL_VERSION_ID = '2013-01-23';
41
47 protected $globalId = null;
48
55
62
69
75 protected $languageCode = null;
76
85 protected $localIds = [];
86
92 protected $extraData = [];
93
99 protected $extraConfig = [];
100
106 protected $forward = false;
107
113 protected $internalId = null;
114
120 public function __construct( $type = self::TYPE_UNKNOWN ) {
121 $this->type = $type;
122 }
123
131 public function getGlobalId() {
132 return $this->globalId;
133 }
134
141 public function setGlobalId( ?string $globalId ) {
142 $this->globalId = $globalId;
143 }
144
152 public function getType() {
153 return $this->type;
154 }
155
163 public function getGroup() {
164 return $this->group;
165 }
166
173 public function setGroup( string $group ) {
174 $this->group = $group;
175 }
176
185 public function getSource() {
186 return $this->source;
187 }
188
196 public function setSource( string $source ) {
197 $this->source = $source;
198 }
199
209 public function shouldForward() {
210 return $this->forward;
211 }
212
221 public function setForward( bool $shouldForward ) {
222 $this->forward = $shouldForward;
223 }
224
233 public function getDomain(): ?string {
234 $path = $this->getLinkPath();
235
236 if ( $path === null ) {
237 return null;
238 }
239
240 $domain = parse_url( $path, PHP_URL_HOST );
241
242 if ( $domain === false ) {
243 $domain = null;
244 }
245
246 return $domain;
247 }
248
255 public function getProtocol() {
256 $path = $this->getLinkPath();
257
258 if ( $path === null ) {
259 return '';
260 }
261
262 $protocol = parse_url( $path, PHP_URL_SCHEME );
263
264 // Malformed URL
265 if ( $protocol === false ) {
266 throw new UnexpectedValueException( "failed to parse URL '$path'" );
267 }
268
269 // Used for protocol relative URLs
270 return $protocol ?? '';
271 }
272
281 public function setLinkPath( $fullUrl ) {
282 $type = $this->getLinkPathType();
283
284 if ( $type === null ) {
285 throw new RuntimeException( "This Site does not support link paths." );
286 }
287
288 $this->setPath( $type, $fullUrl );
289 }
290
298 public function getLinkPath() {
299 return $this->getPath( $this->getLinkPathType() );
300 }
301
314 public function getLinkPathType() {
315 return self::PATH_LINK;
316 }
317
332 public function getPageUrl( $pageName = false ) {
333 $url = $this->getLinkPath();
334
335 if ( $url === null ) {
336 return null;
337 }
338
339 if ( $pageName !== false ) {
340 $url = str_replace( '$1', rawurlencode( $pageName ), $url );
341 }
342
343 return $url;
344 }
345
363 public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
364 return $pageName;
365 }
366
374 public function getExtraData() {
375 return $this->extraData;
376 }
377
385 public function setExtraData( array $extraData ) {
386 $this->extraData = $extraData;
387 }
388
397 public function getExtraConfig() {
398 return $this->extraConfig;
399 }
400
409 public function setExtraConfig( array $extraConfig ) {
410 $this->extraConfig = $extraConfig;
411 }
412
421 public function getLanguageCode() {
422 return $this->languageCode;
423 }
424
432 public function setLanguageCode( $languageCode ) {
433 $this->languageCode = $languageCode;
434 }
435
444 public function getInternalId() {
445 return $this->internalId;
446 }
447
457 public function setInternalId( $internalId = null ) {
458 $this->internalId = $internalId;
459 }
460
469 public function addLocalId( $type, $identifier ) {
470 if ( $this->localIds === false ) {
471 $this->localIds = [];
472 }
473
474 $this->localIds[$type] ??= [];
475
476 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
477 $this->localIds[$type][] = $identifier;
478 }
479 }
480
488 public function addInterwikiId( $identifier ) {
489 $this->addLocalId( self::ID_INTERWIKI, $identifier );
490 }
491
499 public function addNavigationId( $identifier ) {
500 $this->addLocalId( self::ID_EQUIVALENT, $identifier );
501 }
502
510 public function getInterwikiIds() {
511 return $this->localIds[self::ID_INTERWIKI] ?? [];
512 }
513
522 public function getNavigationIds() {
523 return $this->localIds[self::ID_EQUIVALENT] ?? [];
524 }
525
533 public function getLocalIds() {
534 return $this->localIds;
535 }
536
546 public function setPath( $pathType, string $fullUrl ) {
547 $this->extraData['paths'][$pathType] = $fullUrl;
548 }
549
559 public function getPath( $pathType ) {
560 $paths = $this->getAllPaths();
561 return $paths[$pathType] ?? null;
562 }
563
572 public function getAllPaths() {
573 return $this->extraData['paths'] ?? [];
574 }
575
583 public function removePath( $pathType ) {
584 if ( array_key_exists( 'paths', $this->extraData ) ) {
585 unset( $this->extraData['paths'][$pathType] );
586 }
587 }
588
597 public static function newForType( $siteType ) {
598 return MediaWikiServices::getInstance()->getSiteSanitizer()->newSiteForType( $siteType );
599 }
600
608 public function __serialize() {
609 return [
610 'globalid' => $this->globalId,
611 'type' => $this->type,
612 'group' => $this->group,
613 'source' => $this->source,
614 'language' => $this->languageCode,
615 'localids' => $this->localIds,
616 'config' => $this->extraConfig,
617 'data' => $this->extraData,
618 'forward' => $this->forward,
619 'internalid' => $this->internalId,
620 ];
621 }
622
630 public function __unserialize( $fields ) {
631 $this->__construct( $fields['type'] );
632
633 $this->setGlobalId( $fields['globalid'] );
634 $this->setGroup( $fields['group'] );
635 $this->setSource( $fields['source'] );
636 $this->setLanguageCode( $fields['language'] );
637 $this->localIds = $fields['localids'];
638 $this->setExtraConfig( $fields['config'] );
639 $this->setExtraData( $fields['data'] );
640 $this->setForward( $fields['forward'] );
641 $this->setInternalId( $fields['internalid'] );
642 }
643}
644
646class_alias( Site::class, 'Site' );
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Represents a single site.
Definition Site.php:20
string null $globalId
Definition Site.php:47
setGlobalId(?string $globalId)
Sets the global site identifier (ie enwiktionary).
Definition Site.php:141
setForward(bool $shouldForward)
Sets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition Site.php:221
const ID_EQUIVALENT
Definition Site.php:27
getDomain()
Returns the domain of the site, ie en.wikipedia.org Or null if it's not known.
Definition Site.php:233
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition Site.php:510
getExtraConfig()
Returns the type specific config.
Definition Site.php:397
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition Site.php:298
getExtraData()
Returns the type specific fields.
Definition Site.php:374
setLinkPath( $fullUrl)
Set the path used to construct links with.
Definition Site.php:281
setPath( $pathType, string $fullUrl)
Set the path used to construct links with.
Definition Site.php:546
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition Site.php:522
array $extraConfig
Definition Site.php:99
setExtraData(array $extraData)
Sets the type specific fields.
Definition Site.php:385
setExtraConfig(array $extraConfig)
Sets the type specific config.
Definition Site.php:409
normalizePageName( $pageName, $followRedirect=MediaWikiPageNameNormalizer::FOLLOW_REDIRECT)
Attempt to normalize the page name in some fashion.
Definition Site.php:363
setGroup(string $group)
Sets the group of the site (ie wikipedia).
Definition Site.php:173
getGroup()
Gets the group of the site (ie wikipedia).
Definition Site.php:163
getPageUrl( $pageName=false)
Get the full URL for the given page on the site.
Definition Site.php:332
getAllPaths()
Returns the paths as associative array.
Definition Site.php:572
getLinkPathType()
Returns the main path type, that is the type of the path that should generally be used to construct l...
Definition Site.php:314
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition Site.php:432
addLocalId( $type, $identifier)
Adds a local identifier.
Definition Site.php:469
int null $internalId
Definition Site.php:113
setInternalId( $internalId=null)
Sets the internal identifier for the site.
Definition Site.php:457
__unserialize( $fields)
Definition Site.php:630
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition Site.php:209
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:185
const SOURCE_LOCAL
Definition Site.php:29
getProtocol()
Returns the protocol of the site.
Definition Site.php:255
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition Site.php:499
string[][] false $localIds
Holds the local ids for this site.
Definition Site.php:85
getType()
Returns the type of the site (ie mediawiki).
Definition Site.php:152
const ID_INTERWIKI
Definition Site.php:26
const TYPE_MEDIAWIKI
Definition Site.php:22
setSource(string $source)
Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:196
static newForType( $siteType)
Definition Site.php:597
getLocalIds()
Returns all local ids.
Definition Site.php:533
getInternalId()
Returns the set internal identifier for the site.
Definition Site.php:444
getLanguageCode()
Returns language code of the sites primary language.
Definition Site.php:421
string null $languageCode
Definition Site.php:75
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition Site.php:583
getPath( $pathType)
Returns the path of the provided type or null if there is no such path.
Definition Site.php:559
const TYPE_UNKNOWN
Definition Site.php:21
__construct( $type=self::TYPE_UNKNOWN)
Definition Site.php:120
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition Site.php:488
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:131
$source