MediaWiki  master
Site.php
Go to the documentation of this file.
1 <?php
24 
32 class Site {
33  public const TYPE_UNKNOWN = 'unknown';
34  public const TYPE_MEDIAWIKI = 'mediawiki';
35 
36  public const GROUP_NONE = 'none';
37 
38  public const ID_INTERWIKI = 'interwiki';
39  public const ID_EQUIVALENT = 'equivalent';
40 
41  public const SOURCE_LOCAL = 'local';
42 
43  public const PATH_LINK = 'link';
44 
52  public const SERIAL_VERSION_ID = '2013-01-23';
53 
59  protected $globalId = null;
60 
67 
73  protected $group = self::GROUP_NONE;
74 
81 
87  protected $languageCode = null;
88 
97  protected $localIds = [];
98 
104  protected $extraData = [];
105 
111  protected $extraConfig = [];
112 
118  protected $forward = false;
119 
125  protected $internalId = null;
126 
132  public function __construct( $type = self::TYPE_UNKNOWN ) {
133  $this->type = $type;
134  }
135 
143  public function getGlobalId() {
144  return $this->globalId;
145  }
146 
153  public function setGlobalId( ?string $globalId ) {
154  $this->globalId = $globalId;
155  }
156 
164  public function getType() {
165  return $this->type;
166  }
167 
175  public function getGroup() {
176  return $this->group;
177  }
178 
185  public function setGroup( string $group ) {
186  $this->group = $group;
187  }
188 
196  public function getSource() {
197  return $this->source;
198  }
199 
206  public function setSource( string $source ) {
207  $this->source = $source;
208  }
209 
218  public function shouldForward() {
219  return $this->forward;
220  }
221 
229  public function setForward( bool $shouldForward ) {
230  $this->forward = $shouldForward;
231  }
232 
241  public function getDomain(): ?string {
242  $path = $this->getLinkPath();
243 
244  if ( $path === null ) {
245  return null;
246  }
247 
248  $domain = parse_url( $path, PHP_URL_HOST );
249 
250  if ( $domain === false ) {
251  $domain = null;
252  }
253 
254  return $domain;
255  }
256 
263  public function getProtocol() {
264  $path = $this->getLinkPath();
265 
266  if ( $path === null ) {
267  return '';
268  }
269 
270  $protocol = parse_url( $path, PHP_URL_SCHEME );
271 
272  // Malformed URL
273  if ( $protocol === false ) {
274  throw new UnexpectedValueException( "failed to parse URL '$path'" );
275  }
276 
277  // No schema
278  if ( $protocol === null ) {
279  // Used for protocol relative URLs
280  $protocol = '';
281  }
282 
283  return $protocol;
284  }
285 
294  public function setLinkPath( $fullUrl ) {
295  $type = $this->getLinkPathType();
296 
297  if ( $type === null ) {
298  throw new RuntimeException( "This Site does not support link paths." );
299  }
300 
301  $this->setPath( $type, $fullUrl );
302  }
303 
311  public function getLinkPath() {
312  $type = $this->getLinkPathType();
313  return $type === null ? null : $this->getPath( $type );
314  }
315 
328  public function getLinkPathType() {
329  return self::PATH_LINK;
330  }
331 
346  public function getPageUrl( $pageName = false ) {
347  $url = $this->getLinkPath();
348 
349  if ( $url === null ) {
350  return null;
351  }
352 
353  if ( $pageName !== false ) {
354  $url = str_replace( '$1', rawurlencode( $pageName ), $url );
355  }
356 
357  return $url;
358  }
359 
377  public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
378  return $pageName;
379  }
380 
388  public function getExtraData() {
389  return $this->extraData;
390  }
391 
399  public function setExtraData( array $extraData ) {
400  $this->extraData = $extraData;
401  }
402 
410  public function getExtraConfig() {
411  return $this->extraConfig;
412  }
413 
421  public function setExtraConfig( array $extraConfig ) {
422  $this->extraConfig = $extraConfig;
423  }
424 
433  public function getLanguageCode() {
434  return $this->languageCode;
435  }
436 
444  public function setLanguageCode( $languageCode ) {
445  if ( $languageCode !== null
446  && !MediaWikiServices::getInstance()
447  ->getLanguageNameUtils()
448  ->isValidCode( $languageCode )
449  ) {
450  throw new InvalidArgumentException( "$languageCode is not a valid language code." );
451  }
452  $this->languageCode = $languageCode;
453  }
454 
462  public function getInternalId() {
463  return $this->internalId;
464  }
465 
474  public function setInternalId( $internalId = null ) {
475  $this->internalId = $internalId;
476  }
477 
486  public function addLocalId( $type, $identifier ) {
487  if ( $this->localIds === false ) {
488  $this->localIds = [];
489  }
490 
491  if ( !array_key_exists( $type, $this->localIds ) ) {
492  $this->localIds[$type] = [];
493  }
494 
495  if ( !in_array( $identifier, $this->localIds[$type] ) ) {
496  $this->localIds[$type][] = $identifier;
497  }
498  }
499 
507  public function addInterwikiId( $identifier ) {
508  $this->addLocalId( self::ID_INTERWIKI, $identifier );
509  }
510 
518  public function addNavigationId( $identifier ) {
519  $this->addLocalId( self::ID_EQUIVALENT, $identifier );
520  }
521 
529  public function getInterwikiIds() {
530  return array_key_exists( self::ID_INTERWIKI, $this->localIds )
531  ? $this->localIds[self::ID_INTERWIKI]
532  : [];
533  }
534 
543  public function getNavigationIds() {
544  return array_key_exists( self::ID_EQUIVALENT, $this->localIds )
545  ? $this->localIds[self::ID_EQUIVALENT] :
546  [];
547  }
548 
556  public function getLocalIds() {
557  return $this->localIds;
558  }
559 
569  public function setPath( $pathType, string $fullUrl ) {
570  if ( !array_key_exists( 'paths', $this->extraData ) ) {
571  $this->extraData['paths'] = [];
572  }
573 
574  $this->extraData['paths'][$pathType] = $fullUrl;
575  }
576 
586  public function getPath( $pathType ) {
587  $paths = $this->getAllPaths();
588  return $paths[$pathType] ?? null;
589  }
590 
599  public function getAllPaths() {
600  return $this->extraData['paths'] ?? [];
601  }
602 
610  public function removePath( $pathType ) {
611  if ( array_key_exists( 'paths', $this->extraData ) ) {
612  unset( $this->extraData['paths'][$pathType] );
613  }
614  }
615 
623  public static function newForType( $siteType ) {
624  $siteTypes = MediaWikiServices::getInstance()->getMainConfig()->get(
625  MainConfigNames::SiteTypes );
626 
627  if ( array_key_exists( $siteType, $siteTypes ) ) {
628  return new $siteTypes[$siteType]();
629  }
630 
631  return new Site();
632  }
633 
641  public function __serialize() {
642  return [
643  'globalid' => $this->globalId,
644  'type' => $this->type,
645  'group' => $this->group,
646  'source' => $this->source,
647  'language' => $this->languageCode,
648  'localids' => $this->localIds,
649  'config' => $this->extraConfig,
650  'data' => $this->extraData,
651  'forward' => $this->forward,
652  'internalid' => $this->internalId,
653  ];
654  }
655 
663  public function __unserialize( $fields ) {
664  $this->__construct( $fields['type'] );
665 
666  $this->setGlobalId( $fields['globalid'] );
667  $this->setGroup( $fields['group'] );
668  $this->setSource( $fields['source'] );
669  $this->setLanguageCode( $fields['language'] );
670  $this->localIds = $fields['localids'];
671  $this->setExtraConfig( $fields['config'] );
672  $this->setExtraData( $fields['data'] );
673  $this->setForward( $fields['forward'] );
674  $this->setInternalId( $fields['internalid'] );
675  }
676 }
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Service for normalizing a page name via a MediaWiki action API.
Represents a single site.
Definition: Site.php:32
normalizePageName( $pageName, $followRedirect=MediaWikiPageNameNormalizer::FOLLOW_REDIRECT)
Attempt to normalize the page name in some fashion.
Definition: Site.php:377
__unserialize( $fields)
Definition: Site.php:663
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:218
const TYPE_MEDIAWIKI
Definition: Site.php:34
getExtraData()
Returns the type specific fields.
Definition: Site.php:388
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition: Site.php:507
getPath( $pathType)
Returns the path of the provided type or null if there is no such path.
Definition: Site.php:586
const GROUP_NONE
Definition: Site.php:36
string[][] false $localIds
Holds the local ids for this site.
Definition: Site.php:97
string $type
Definition: Site.php:66
array $extraConfig
Definition: Site.php:111
array $extraData
Definition: Site.php:104
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:196
__serialize()
Definition: Site.php:641
static newForType( $siteType)
Definition: Site.php:623
getProtocol()
Returns the protocol of the site.
Definition: Site.php:263
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:164
int null $internalId
Definition: Site.php:125
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition: Site.php:610
addLocalId( $type, $identifier)
Adds a local identifier.
Definition: Site.php:486
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition: Site.php:444
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:143
setSource(string $source)
Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:206
getLinkPathType()
Returns the main path type, that is the type of the path that should generally be used to construct l...
Definition: Site.php:328
const PATH_LINK
Definition: Site.php:43
const TYPE_UNKNOWN
Definition: Site.php:33
const ID_EQUIVALENT
Definition: Site.php:39
getExtraConfig()
Returns the type specific config.
Definition: Site.php:410
setGlobalId(?string $globalId)
Sets the global site identifier (ie enwiktionary).
Definition: Site.php:153
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition: Site.php:518
getPageUrl( $pageName=false)
Get the full URL for the given page on the site.
Definition: Site.php:346
string null $languageCode
Definition: Site.php:87
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:599
getLocalIds()
Returns all local ids.
Definition: Site.php:556
const ID_INTERWIKI
Definition: Site.php:38
getGroup()
Gets the group of the site (ie wikipedia).
Definition: Site.php:175
setInternalId( $internalId=null)
Sets the internal identifier for the site.
Definition: Site.php:474
string $source
Definition: Site.php:80
getLanguageCode()
Returns language code of the sites primary language.
Definition: Site.php:433
getInternalId()
Returns the set internal identifier for the site.
Definition: Site.php:462
bool $forward
Definition: Site.php:118
const SOURCE_LOCAL
Definition: Site.php:41
setExtraConfig(array $extraConfig)
Sets the type specific config.
Definition: Site.php:421
__construct( $type=self::TYPE_UNKNOWN)
Definition: Site.php:132
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition: Site.php:543
string null $globalId
Definition: Site.php:59
setForward(bool $shouldForward)
Sets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:229
string $group
Definition: Site.php:73
setPath( $pathType, string $fullUrl)
Set the path used to construct links with.
Definition: Site.php:569
getDomain()
Returns the domain of the site, ie en.wikipedia.org Or null if it's not known.
Definition: Site.php:241
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition: Site.php:311
setLinkPath( $fullUrl)
Set the path used to construct links with.
Definition: Site.php:294
setGroup(string $group)
Sets the group of the site (ie wikipedia).
Definition: Site.php:185
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:529
setExtraData(array $extraData)
Sets the type specific fields.
Definition: Site.php:399
$source