MediaWiki master
Site.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Site;
8
9use InvalidArgumentException;
12use RuntimeException;
13use UnexpectedValueException;
14
22class Site {
23 public const TYPE_UNKNOWN = 'unknown';
24 public const TYPE_MEDIAWIKI = 'mediawiki';
25
26 public const GROUP_NONE = 'none';
27
28 public const ID_INTERWIKI = 'interwiki';
29 public const ID_EQUIVALENT = 'equivalent';
30
31 public const SOURCE_LOCAL = 'local';
32
33 public const PATH_LINK = 'link';
34
42 public const SERIAL_VERSION_ID = '2013-01-23';
43
49 protected $globalId = null;
50
57
64
71
77 protected $languageCode = null;
78
87 protected $localIds = [];
88
94 protected $extraData = [];
95
101 protected $extraConfig = [];
102
108 protected $forward = false;
109
115 protected $internalId = null;
116
122 public function __construct( $type = self::TYPE_UNKNOWN ) {
123 $this->type = $type;
124 }
125
133 public function getGlobalId() {
134 return $this->globalId;
135 }
136
143 public function setGlobalId( ?string $globalId ) {
144 $this->globalId = $globalId;
145 }
146
154 public function getType() {
155 return $this->type;
156 }
157
165 public function getGroup() {
166 return $this->group;
167 }
168
175 public function setGroup( string $group ) {
176 $this->group = $group;
177 }
178
186 public function getSource() {
187 return $this->source;
188 }
189
196 public function setSource( string $source ) {
197 $this->source = $source;
198 }
199
208 public function shouldForward() {
209 return $this->forward;
210 }
211
219 public function setForward( bool $shouldForward ) {
220 $this->forward = $shouldForward;
221 }
222
231 public function getDomain(): ?string {
232 $path = $this->getLinkPath();
233
234 if ( $path === null ) {
235 return null;
236 }
237
238 $domain = parse_url( $path, PHP_URL_HOST );
239
240 if ( $domain === false ) {
241 $domain = null;
242 }
243
244 return $domain;
245 }
246
253 public function getProtocol() {
254 $path = $this->getLinkPath();
255
256 if ( $path === null ) {
257 return '';
258 }
259
260 $protocol = parse_url( $path, PHP_URL_SCHEME );
261
262 // Malformed URL
263 if ( $protocol === false ) {
264 throw new UnexpectedValueException( "failed to parse URL '$path'" );
265 }
266
267 // Used for protocol relative URLs
268 return $protocol ?? '';
269 }
270
279 public function setLinkPath( $fullUrl ) {
280 $type = $this->getLinkPathType();
281
282 if ( $type === null ) {
283 throw new RuntimeException( "This Site does not support link paths." );
284 }
285
286 $this->setPath( $type, $fullUrl );
287 }
288
296 public function getLinkPath() {
297 $type = $this->getLinkPathType();
298 return $type === null ? null : $this->getPath( $type );
299 }
300
313 public function getLinkPathType() {
314 return self::PATH_LINK;
315 }
316
331 public function getPageUrl( $pageName = false ) {
332 $url = $this->getLinkPath();
333
334 if ( $url === null ) {
335 return null;
336 }
337
338 if ( $pageName !== false ) {
339 $url = str_replace( '$1', rawurlencode( $pageName ), $url );
340 }
341
342 return $url;
343 }
344
362 public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
363 return $pageName;
364 }
365
373 public function getExtraData() {
374 return $this->extraData;
375 }
376
384 public function setExtraData( array $extraData ) {
385 $this->extraData = $extraData;
386 }
387
395 public function getExtraConfig() {
396 return $this->extraConfig;
397 }
398
406 public function setExtraConfig( array $extraConfig ) {
407 $this->extraConfig = $extraConfig;
408 }
409
418 public function getLanguageCode() {
419 return $this->languageCode;
420 }
421
429 public function setLanguageCode( $languageCode ) {
430 if ( $languageCode !== null &&
431 !MediaWikiServices::getInstance()->getLanguageNameUtils()->isValidCode( $languageCode ) ) {
432 throw new InvalidArgumentException( "$languageCode is not a valid language code." );
433 }
434 $this->languageCode = $languageCode;
435 }
436
444 public function getInternalId() {
445 return $this->internalId;
446 }
447
456 public function setInternalId( $internalId = null ) {
457 $this->internalId = $internalId;
458 }
459
468 public function addLocalId( $type, $identifier ) {
469 if ( $this->localIds === false ) {
470 $this->localIds = [];
471 }
472
473 $this->localIds[$type] ??= [];
474
475 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
476 $this->localIds[$type][] = $identifier;
477 }
478 }
479
487 public function addInterwikiId( $identifier ) {
488 $this->addLocalId( self::ID_INTERWIKI, $identifier );
489 }
490
498 public function addNavigationId( $identifier ) {
499 $this->addLocalId( self::ID_EQUIVALENT, $identifier );
500 }
501
509 public function getInterwikiIds() {
510 return $this->localIds[self::ID_INTERWIKI] ?? [];
511 }
512
521 public function getNavigationIds() {
522 return $this->localIds[self::ID_EQUIVALENT] ?? [];
523 }
524
532 public function getLocalIds() {
533 return $this->localIds;
534 }
535
545 public function setPath( $pathType, string $fullUrl ) {
546 $this->extraData['paths'][$pathType] = $fullUrl;
547 }
548
558 public function getPath( $pathType ) {
559 $paths = $this->getAllPaths();
560 return $paths[$pathType] ?? null;
561 }
562
571 public function getAllPaths() {
572 return $this->extraData['paths'] ?? [];
573 }
574
582 public function removePath( $pathType ) {
583 if ( array_key_exists( 'paths', $this->extraData ) ) {
584 unset( $this->extraData['paths'][$pathType] );
585 }
586 }
587
595 public static function newForType( $siteType ) {
597 $siteTypes = MediaWikiServices::getInstance()->getMainConfig()->get(
599 );
600
601 if ( array_key_exists( $siteType, $siteTypes ) ) {
602 return new $siteTypes[$siteType]();
603 }
604
605 return new Site();
606 }
607
615 public function __serialize() {
616 return [
617 'globalid' => $this->globalId,
618 'type' => $this->type,
619 'group' => $this->group,
620 'source' => $this->source,
621 'language' => $this->languageCode,
622 'localids' => $this->localIds,
623 'config' => $this->extraConfig,
624 'data' => $this->extraData,
625 'forward' => $this->forward,
626 'internalid' => $this->internalId,
627 ];
628 }
629
637 public function __unserialize( $fields ) {
638 $this->__construct( $fields['type'] );
639
640 $this->setGlobalId( $fields['globalid'] );
641 $this->setGroup( $fields['group'] );
642 $this->setSource( $fields['source'] );
643 $this->setLanguageCode( $fields['language'] );
644 $this->localIds = $fields['localids'];
645 $this->setExtraConfig( $fields['config'] );
646 $this->setExtraData( $fields['data'] );
647 $this->setForward( $fields['forward'] );
648 $this->setInternalId( $fields['internalid'] );
649 }
650}
651
653class_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:22
string null $globalId
Definition Site.php:49
setGlobalId(?string $globalId)
Sets the global site identifier (ie enwiktionary).
Definition Site.php:143
setForward(bool $shouldForward)
Sets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition Site.php:219
const ID_EQUIVALENT
Definition Site.php:29
getDomain()
Returns the domain of the site, ie en.wikipedia.org Or null if it's not known.
Definition Site.php:231
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition Site.php:509
getExtraConfig()
Returns the type specific config.
Definition Site.php:395
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition Site.php:296
getExtraData()
Returns the type specific fields.
Definition Site.php:373
setLinkPath( $fullUrl)
Set the path used to construct links with.
Definition Site.php:279
setPath( $pathType, string $fullUrl)
Set the path used to construct links with.
Definition Site.php:545
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition Site.php:521
setExtraData(array $extraData)
Sets the type specific fields.
Definition Site.php:384
setExtraConfig(array $extraConfig)
Sets the type specific config.
Definition Site.php:406
normalizePageName( $pageName, $followRedirect=MediaWikiPageNameNormalizer::FOLLOW_REDIRECT)
Attempt to normalize the page name in some fashion.
Definition Site.php:362
setGroup(string $group)
Sets the group of the site (ie wikipedia).
Definition Site.php:175
getGroup()
Gets the group of the site (ie wikipedia).
Definition Site.php:165
getPageUrl( $pageName=false)
Get the full URL for the given page on the site.
Definition Site.php:331
getAllPaths()
Returns the paths as associative array.
Definition Site.php:571
getLinkPathType()
Returns the main path type, that is the type of the path that should generally be used to construct l...
Definition Site.php:313
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition Site.php:429
addLocalId( $type, $identifier)
Adds a local identifier.
Definition Site.php:468
int null $internalId
Definition Site.php:115
setInternalId( $internalId=null)
Sets the internal identifier for the site.
Definition Site.php:456
__unserialize( $fields)
Definition Site.php:637
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition Site.php:208
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:186
const SOURCE_LOCAL
Definition Site.php:31
getProtocol()
Returns the protocol of the site.
Definition Site.php:253
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition Site.php:498
string[][] false $localIds
Holds the local ids for this site.
Definition Site.php:87
getType()
Returns the type of the site (ie mediawiki).
Definition Site.php:154
const ID_INTERWIKI
Definition Site.php:28
const TYPE_MEDIAWIKI
Definition Site.php:24
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:595
getLocalIds()
Returns all local ids.
Definition Site.php:532
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:418
string null $languageCode
Definition Site.php:77
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition Site.php:582
getPath( $pathType)
Returns the path of the provided type or null if there is no such path.
Definition Site.php:558
const TYPE_UNKNOWN
Definition Site.php:23
__construct( $type=self::TYPE_UNKNOWN)
Definition Site.php:122
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition Site.php:487
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:133
$source