MediaWiki master
Site.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Site;
22
23use InvalidArgumentException;
26use RuntimeException;
27use UnexpectedValueException;
28
36class 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
56 public const SERIAL_VERSION_ID = '2013-01-23';
57
63 protected $globalId = null;
64
71
78
85
91 protected $languageCode = null;
92
101 protected $localIds = [];
102
108 protected $extraData = [];
109
115 protected $extraConfig = [];
116
122 protected $forward = false;
123
129 protected $internalId = null;
130
136 public function __construct( $type = self::TYPE_UNKNOWN ) {
137 $this->type = $type;
138 }
139
147 public function getGlobalId() {
148 return $this->globalId;
149 }
150
157 public function setGlobalId( ?string $globalId ) {
158 $this->globalId = $globalId;
159 }
160
168 public function getType() {
169 return $this->type;
170 }
171
179 public function getGroup() {
180 return $this->group;
181 }
182
189 public function setGroup( string $group ) {
190 $this->group = $group;
191 }
192
200 public function getSource() {
201 return $this->source;
202 }
203
210 public function setSource( string $source ) {
211 $this->source = $source;
212 }
213
222 public function shouldForward() {
223 return $this->forward;
224 }
225
233 public function setForward( bool $shouldForward ) {
234 $this->forward = $shouldForward;
235 }
236
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
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 // Malformed URL
277 if ( $protocol === false ) {
278 throw new UnexpectedValueException( "failed to parse URL '$path'" );
279 }
280
281 // No schema
282 if ( $protocol === null ) {
283 // Used for protocol relative URLs
284 $protocol = '';
285 }
286
287 return $protocol;
288 }
289
298 public function setLinkPath( $fullUrl ) {
299 $type = $this->getLinkPathType();
300
301 if ( $type === null ) {
302 throw new RuntimeException( "This Site does not support link paths." );
303 }
304
305 $this->setPath( $type, $fullUrl );
306 }
307
315 public function getLinkPath() {
316 $type = $this->getLinkPathType();
317 return $type === null ? null : $this->getPath( $type );
318 }
319
332 public function getLinkPathType() {
333 return self::PATH_LINK;
334 }
335
350 public function getPageUrl( $pageName = false ) {
351 $url = $this->getLinkPath();
352
353 if ( $url === null ) {
354 return null;
355 }
356
357 if ( $pageName !== false ) {
358 $url = str_replace( '$1', rawurlencode( $pageName ), $url );
359 }
360
361 return $url;
362 }
363
381 public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
382 return $pageName;
383 }
384
392 public function getExtraData() {
393 return $this->extraData;
394 }
395
403 public function setExtraData( array $extraData ) {
404 $this->extraData = $extraData;
405 }
406
414 public function getExtraConfig() {
415 return $this->extraConfig;
416 }
417
425 public function setExtraConfig( array $extraConfig ) {
426 $this->extraConfig = $extraConfig;
427 }
428
437 public function getLanguageCode() {
438 return $this->languageCode;
439 }
440
448 public function setLanguageCode( $languageCode ) {
449 if ( $languageCode !== null &&
450 !MediaWikiServices::getInstance()->getLanguageNameUtils()->isValidCode( $languageCode ) ) {
451 throw new InvalidArgumentException( "$languageCode is not a valid language code." );
452 }
453 $this->languageCode = $languageCode;
454 }
455
463 public function getInternalId() {
464 return $this->internalId;
465 }
466
475 public function setInternalId( $internalId = null ) {
476 $this->internalId = $internalId;
477 }
478
487 public function addLocalId( $type, $identifier ) {
488 if ( $this->localIds === false ) {
489 $this->localIds = [];
490 }
491
492 if ( !array_key_exists( $type, $this->localIds ) ) {
493 $this->localIds[$type] = [];
494 }
495
496 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
497 $this->localIds[$type][] = $identifier;
498 }
499 }
500
508 public function addInterwikiId( $identifier ) {
509 $this->addLocalId( self::ID_INTERWIKI, $identifier );
510 }
511
519 public function addNavigationId( $identifier ) {
520 $this->addLocalId( self::ID_EQUIVALENT, $identifier );
521 }
522
530 public function getInterwikiIds() {
531 return array_key_exists( self::ID_INTERWIKI, $this->localIds )
532 ? $this->localIds[self::ID_INTERWIKI]
533 : [];
534 }
535
544 public function getNavigationIds() {
545 return array_key_exists( self::ID_EQUIVALENT, $this->localIds )
546 ? $this->localIds[self::ID_EQUIVALENT] :
547 [];
548 }
549
557 public function getLocalIds() {
558 return $this->localIds;
559 }
560
570 public function setPath( $pathType, string $fullUrl ) {
571 $this->extraData['paths'][$pathType] = $fullUrl;
572 }
573
583 public function getPath( $pathType ) {
584 $paths = $this->getAllPaths();
585 return $paths[$pathType] ?? null;
586 }
587
596 public function getAllPaths() {
597 return $this->extraData['paths'] ?? [];
598 }
599
607 public function removePath( $pathType ) {
608 if ( array_key_exists( 'paths', $this->extraData ) ) {
609 unset( $this->extraData['paths'][$pathType] );
610 }
611 }
612
620 public static function newForType( $siteType ) {
621 $siteTypes = MediaWikiServices::getInstance()->getMainConfig()->get(
623 );
624
625 if ( array_key_exists( $siteType, $siteTypes ) ) {
626 return new $siteTypes[$siteType]();
627 }
628
629 return new Site();
630 }
631
639 public function __serialize() {
640 return [
641 'globalid' => $this->globalId,
642 'type' => $this->type,
643 'group' => $this->group,
644 'source' => $this->source,
645 'language' => $this->languageCode,
646 'localids' => $this->localIds,
647 'config' => $this->extraConfig,
648 'data' => $this->extraData,
649 'forward' => $this->forward,
650 'internalid' => $this->internalId,
651 ];
652 }
653
661 public function __unserialize( $fields ) {
662 $this->__construct( $fields['type'] );
663
664 $this->setGlobalId( $fields['globalid'] );
665 $this->setGroup( $fields['group'] );
666 $this->setSource( $fields['source'] );
667 $this->setLanguageCode( $fields['language'] );
668 $this->localIds = $fields['localids'];
669 $this->setExtraConfig( $fields['config'] );
670 $this->setExtraData( $fields['data'] );
671 $this->setForward( $fields['forward'] );
672 $this->setInternalId( $fields['internalid'] );
673 }
674}
675
679class_alias( Site::class, 'Site' );
A class containing constants representing the names of configuration variables.
const SiteTypes
Name constant for the SiteTypes setting, for use with Config::get()
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:36
string null $globalId
Definition Site.php:63
setGlobalId(?string $globalId)
Sets the global site identifier (ie enwiktionary).
Definition Site.php:157
setForward(bool $shouldForward)
Sets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition Site.php:233
const ID_EQUIVALENT
Definition Site.php:43
getDomain()
Returns the domain of the site, ie en.wikipedia.org Or null if it's not known.
Definition Site.php:245
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition Site.php:530
getExtraConfig()
Returns the type specific config.
Definition Site.php:414
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition Site.php:315
getExtraData()
Returns the type specific fields.
Definition Site.php:392
setLinkPath( $fullUrl)
Set the path used to construct links with.
Definition Site.php:298
setPath( $pathType, string $fullUrl)
Set the path used to construct links with.
Definition Site.php:570
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition Site.php:544
setExtraData(array $extraData)
Sets the type specific fields.
Definition Site.php:403
setExtraConfig(array $extraConfig)
Sets the type specific config.
Definition Site.php:425
normalizePageName( $pageName, $followRedirect=MediaWikiPageNameNormalizer::FOLLOW_REDIRECT)
Attempt to normalize the page name in some fashion.
Definition Site.php:381
setGroup(string $group)
Sets the group of the site (ie wikipedia).
Definition Site.php:189
getGroup()
Gets the group of the site (ie wikipedia).
Definition Site.php:179
getPageUrl( $pageName=false)
Get the full URL for the given page on the site.
Definition Site.php:350
getAllPaths()
Returns the paths as associative array.
Definition Site.php:596
getLinkPathType()
Returns the main path type, that is the type of the path that should generally be used to construct l...
Definition Site.php:332
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition Site.php:448
addLocalId( $type, $identifier)
Adds a local identifier.
Definition Site.php:487
int null $internalId
Definition Site.php:129
setInternalId( $internalId=null)
Sets the internal identifier for the site.
Definition Site.php:475
__unserialize( $fields)
Definition Site.php:661
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition Site.php:222
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:200
const SOURCE_LOCAL
Definition Site.php:45
getProtocol()
Returns the protocol of the site.
Definition Site.php:267
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition Site.php:519
string[][] false $localIds
Holds the local ids for this site.
Definition Site.php:101
getType()
Returns the type of the site (ie mediawiki).
Definition Site.php:168
const ID_INTERWIKI
Definition Site.php:42
const TYPE_MEDIAWIKI
Definition Site.php:38
setSource(string $source)
Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:210
static newForType( $siteType)
Definition Site.php:620
getLocalIds()
Returns all local ids.
Definition Site.php:557
getInternalId()
Returns the set internal identifier for the site.
Definition Site.php:463
getLanguageCode()
Returns language code of the sites primary language.
Definition Site.php:437
string null $languageCode
Definition Site.php:91
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition Site.php:607
getPath( $pathType)
Returns the path of the provided type or null if there is no such path.
Definition Site.php:583
const TYPE_UNKNOWN
Definition Site.php:37
__construct( $type=self::TYPE_UNKNOWN)
Definition Site.php:136
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition Site.php:508
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:147
$source