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 // Used for protocol relative URLs
282 return $protocol ?? '';
283 }
284
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
310 public function getLinkPath() {
311 $type = $this->getLinkPathType();
312 return $type === null ? null : $this->getPath( $type );
313 }
314
327 public function getLinkPathType() {
328 return self::PATH_LINK;
329 }
330
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
376 public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
377 return $pageName;
378 }
379
387 public function getExtraData() {
388 return $this->extraData;
389 }
390
398 public function setExtraData( array $extraData ) {
399 $this->extraData = $extraData;
400 }
401
409 public function getExtraConfig() {
410 return $this->extraConfig;
411 }
412
420 public function setExtraConfig( array $extraConfig ) {
421 $this->extraConfig = $extraConfig;
422 }
423
432 public function getLanguageCode() {
433 return $this->languageCode;
434 }
435
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
458 public function getInternalId() {
459 return $this->internalId;
460 }
461
470 public function setInternalId( $internalId = null ) {
471 $this->internalId = $internalId;
472 }
473
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
501 public function addInterwikiId( $identifier ) {
502 $this->addLocalId( self::ID_INTERWIKI, $identifier );
503 }
504
512 public function addNavigationId( $identifier ) {
513 $this->addLocalId( self::ID_EQUIVALENT, $identifier );
514 }
515
523 public function getInterwikiIds() {
524 return $this->localIds[self::ID_INTERWIKI] ?? [];
525 }
526
535 public function getNavigationIds() {
536 return $this->localIds[self::ID_EQUIVALENT] ?? [];
537 }
538
546 public function getLocalIds() {
547 return $this->localIds;
548 }
549
559 public function setPath( $pathType, string $fullUrl ) {
560 $this->extraData['paths'][$pathType] = $fullUrl;
561 }
562
572 public function getPath( $pathType ) {
573 $paths = $this->getAllPaths();
574 return $paths[$pathType] ?? null;
575 }
576
585 public function getAllPaths() {
586 return $this->extraData['paths'] ?? [];
587 }
588
596 public function removePath( $pathType ) {
597 if ( array_key_exists( 'paths', $this->extraData ) ) {
598 unset( $this->extraData['paths'][$pathType] );
599 }
600 }
601
609 public static function newForType( $siteType ) {
611 $siteTypes = MediaWikiServices::getInstance()->getMainConfig()->get(
613 );
614
615 if ( array_key_exists( $siteType, $siteTypes ) ) {
616 return new $siteTypes[$siteType]();
617 }
618
619 return new Site();
620 }
621
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
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
667class_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:523
getExtraConfig()
Returns the type specific config.
Definition Site.php:409
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition Site.php:310
getExtraData()
Returns the type specific fields.
Definition Site.php:387
setLinkPath( $fullUrl)
Set the path used to construct links with.
Definition Site.php:293
setPath( $pathType, string $fullUrl)
Set the path used to construct links with.
Definition Site.php:559
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition Site.php:535
setExtraData(array $extraData)
Sets the type specific fields.
Definition Site.php:398
setExtraConfig(array $extraConfig)
Sets the type specific config.
Definition Site.php:420
normalizePageName( $pageName, $followRedirect=MediaWikiPageNameNormalizer::FOLLOW_REDIRECT)
Attempt to normalize the page name in some fashion.
Definition Site.php:376
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:345
getAllPaths()
Returns the paths as associative array.
Definition Site.php:585
getLinkPathType()
Returns the main path type, that is the type of the path that should generally be used to construct l...
Definition Site.php:327
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition Site.php:443
addLocalId( $type, $identifier)
Adds a local identifier.
Definition Site.php:482
int null $internalId
Definition Site.php:129
setInternalId( $internalId=null)
Sets the internal identifier for the site.
Definition Site.php:470
__unserialize( $fields)
Definition Site.php:651
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:512
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:609
getLocalIds()
Returns all local ids.
Definition Site.php:546
getInternalId()
Returns the set internal identifier for the site.
Definition Site.php:458
getLanguageCode()
Returns language code of the sites primary language.
Definition Site.php:432
string null $languageCode
Definition Site.php:91
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition Site.php:596
getPath( $pathType)
Returns the path of the provided type or null if there is no such path.
Definition Site.php:572
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:501
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:147
$source