MediaWiki  master
Site.php
Go to the documentation of this file.
1 <?php
2 
5 
32 class Site implements Serializable {
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 
156  public function setGlobalId( $globalId ) {
157  if ( $globalId !== null && !is_string( $globalId ) ) {
158  throw new MWException( '$globalId needs to be string or null' );
159  }
160 
161  $this->globalId = $globalId;
162  }
163 
171  public function getType() {
172  return $this->type;
173  }
174 
182  public function getGroup() {
183  return $this->group;
184  }
185 
195  public function setGroup( $group ) {
196  if ( !is_string( $group ) ) {
197  throw new MWException( '$group needs to be a string' );
198  }
199 
200  $this->group = $group;
201  }
202 
210  public function getSource() {
211  return $this->source;
212  }
213 
223  public function setSource( $source ) {
224  if ( !is_string( $source ) ) {
225  throw new MWException( '$source needs to be a string' );
226  }
227 
228  $this->source = $source;
229  }
230 
239  public function shouldForward() {
240  return $this->forward;
241  }
242 
253  public function setForward( $shouldForward ) {
254  if ( !is_bool( $shouldForward ) ) {
255  throw new MWException( '$shouldForward needs to be a boolean' );
256  }
257 
258  $this->forward = $shouldForward;
259  }
260 
269  public function getDomain() {
270  $path = $this->getLinkPath();
271 
272  if ( $path === null ) {
273  return null;
274  }
275 
276  return parse_url( $path, PHP_URL_HOST );
277  }
278 
287  public function getProtocol() {
288  $path = $this->getLinkPath();
289 
290  if ( $path === null ) {
291  return '';
292  }
293 
294  $protocol = parse_url( $path, PHP_URL_SCHEME );
295 
296  // Malformed URL
297  if ( $protocol === false ) {
298  throw new MWException( "failed to parse URL '$path'" );
299  }
300 
301  // No schema
302  if ( $protocol === null ) {
303  // Used for protocol relative URLs
304  $protocol = '';
305  }
306 
307  return $protocol;
308  }
309 
320  public function setLinkPath( $fullUrl ) {
321  $type = $this->getLinkPathType();
322 
323  if ( $type === null ) {
324  throw new MWException( "This Site does not support link paths." );
325  }
326 
327  $this->setPath( $type, $fullUrl );
328  }
329 
337  public function getLinkPath() {
338  $type = $this->getLinkPathType();
339  return $type === null ? null : $this->getPath( $type );
340  }
341 
354  public function getLinkPathType() {
355  return self::PATH_LINK;
356  }
357 
373  public function getPageUrl( $pageName = false ) {
374  $url = $this->getLinkPath();
375 
376  if ( $url === null ) {
377  return null;
378  }
379 
380  if ( $pageName !== false ) {
381  $url = str_replace( '$1', rawurlencode( $pageName ), $url );
382  }
383 
384  return $url;
385  }
386 
404  public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
405  return $pageName;
406  }
407 
415  public function getExtraData() {
416  return $this->extraData;
417  }
418 
426  public function setExtraData( array $extraData ) {
427  $this->extraData = $extraData;
428  }
429 
437  public function getExtraConfig() {
438  return $this->extraConfig;
439  }
440 
448  public function setExtraConfig( array $extraConfig ) {
449  $this->extraConfig = $extraConfig;
450  }
451 
460  public function getLanguageCode() {
461  return $this->languageCode;
462  }
463 
471  public function setLanguageCode( $languageCode ) {
472  if ( $languageCode !== null
473  && !MediaWikiServices::getInstance()
474  ->getLanguageNameUtils()
475  ->isValidCode( $languageCode )
476  ) {
477  throw new InvalidArgumentException( "$languageCode is not a valid language code." );
478  }
479  $this->languageCode = $languageCode;
480  }
481 
489  public function getInternalId() {
490  return $this->internalId;
491  }
492 
501  public function setInternalId( $internalId = null ) {
502  $this->internalId = $internalId;
503  }
504 
513  public function addLocalId( $type, $identifier ) {
514  if ( $this->localIds === false ) {
515  $this->localIds = [];
516  }
517 
518  if ( !array_key_exists( $type, $this->localIds ) ) {
519  $this->localIds[$type] = [];
520  }
521 
522  if ( !in_array( $identifier, $this->localIds[$type] ) ) {
523  $this->localIds[$type][] = $identifier;
524  }
525  }
526 
534  public function addInterwikiId( $identifier ) {
535  $this->addLocalId( self::ID_INTERWIKI, $identifier );
536  }
537 
545  public function addNavigationId( $identifier ) {
546  $this->addLocalId( self::ID_EQUIVALENT, $identifier );
547  }
548 
556  public function getInterwikiIds() {
557  return array_key_exists( self::ID_INTERWIKI, $this->localIds )
558  ? $this->localIds[self::ID_INTERWIKI]
559  : [];
560  }
561 
570  public function getNavigationIds() {
571  return array_key_exists( self::ID_EQUIVALENT, $this->localIds )
572  ? $this->localIds[self::ID_EQUIVALENT] :
573  [];
574  }
575 
583  public function getLocalIds() {
584  return $this->localIds;
585  }
586 
598  public function setPath( $pathType, $fullUrl ) {
599  if ( !is_string( $fullUrl ) ) {
600  throw new MWException( '$fullUrl needs to be a string' );
601  }
602 
603  if ( !array_key_exists( 'paths', $this->extraData ) ) {
604  $this->extraData['paths'] = [];
605  }
606 
607  $this->extraData['paths'][$pathType] = $fullUrl;
608  }
609 
619  public function getPath( $pathType ) {
620  $paths = $this->getAllPaths();
621  return $paths[$pathType] ?? null;
622  }
623 
632  public function getAllPaths() {
633  return $this->extraData['paths'] ?? [];
634  }
635 
643  public function removePath( $pathType ) {
644  if ( array_key_exists( 'paths', $this->extraData ) ) {
645  unset( $this->extraData['paths'][$pathType] );
646  }
647  }
648 
656  public static function newForType( $siteType ) {
657  global $wgSiteTypes;
658 
659  if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
660  return new $wgSiteTypes[$siteType]();
661  }
662 
663  return new Site();
664  }
665 
673  public function serialize() {
674  $fields = [
675  'globalid' => $this->globalId,
676  'type' => $this->type,
677  'group' => $this->group,
678  'source' => $this->source,
679  'language' => $this->languageCode,
680  'localids' => $this->localIds,
681  'config' => $this->extraConfig,
682  'data' => $this->extraData,
683  'forward' => $this->forward,
684  'internalid' => $this->internalId,
685 
686  ];
687 
688  return serialize( $fields );
689  }
690 
698  public function unserialize( $serialized ) {
699  $fields = unserialize( $serialized );
700 
701  $this->__construct( $fields['type'] );
702 
703  $this->setGlobalId( $fields['globalid'] );
704  $this->setGroup( $fields['group'] );
705  $this->setSource( $fields['source'] );
706  $this->setLanguageCode( $fields['language'] );
707  $this->localIds = $fields['localids'];
708  $this->setExtraConfig( $fields['config'] );
709  $this->setExtraData( $fields['data'] );
710  $this->setForward( $fields['forward'] );
711  $this->setInternalId( $fields['internalid'] );
712  }
713 }
Site\ID_EQUIVALENT
const ID_EQUIVALENT
Definition: Site.php:39
Site\getInterwikiIds
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:556
Site\$extraConfig
array $extraConfig
Definition: Site.php:111
Site\getType
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:171
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:186
Site\getDomain
getDomain()
Returns the domain of the site, ie en.wikipedia.org Or false if it's not known.
Definition: Site.php:269
Site\$languageCode
string null $languageCode
Definition: Site.php:87
Site\setSource
setSource( $source)
Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:223
Site\getPageUrl
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition: Site.php:373
Site\getInternalId
getInternalId()
Returns the set internal identifier for the site.
Definition: Site.php:489
Site\setLinkPath
setLinkPath( $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:320
$serialized
foreach( $res as $row) $serialized
Definition: testCompression.php:88
Site\setGroup
setGroup( $group)
Sets the group of the site (ie wikipedia).
Definition: Site.php:195
Site\setInternalId
setInternalId( $internalId=null)
Sets the internal identifier for the site.
Definition: Site.php:501
Site\getPath
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:619
Site\getLanguageCode
getLanguageCode()
Returns language code of the sites primary language.
Definition: Site.php:460
Site\ID_INTERWIKI
const ID_INTERWIKI
Definition: Site.php:38
Site\newForType
static newForType( $siteType)
Definition: Site.php:656
Site\$type
string $type
Definition: Site.php:66
Site\$internalId
int null $internalId
Definition: Site.php:125
Site\__construct
__construct( $type=self::TYPE_UNKNOWN)
Definition: Site.php:132
Site\serialize
serialize()
Definition: Site.php:673
MWException
MediaWiki exception.
Definition: MWException.php:29
Site\$globalId
string null $globalId
Definition: Site.php:59
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:33
Site\getGlobalId
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:143
Site\getSource
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:210
Site\setForward
setForward( $shouldForward)
Sets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:253
Site\normalizePageName
normalizePageName( $pageName, $followRedirect=MediaWikiPageNameNormalizer::FOLLOW_REDIRECT)
Attempt to normalize the page name in some fashion.
Definition: Site.php:404
Site\setLanguageCode
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition: Site.php:471
Site\getGroup
getGroup()
Gets the group of the site (ie wikipedia).
Definition: Site.php:182
Site\addInterwikiId
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition: Site.php:534
Site\addLocalId
addLocalId( $type, $identifier)
Adds a local identifier.
Definition: Site.php:513
Site\unserialize
unserialize( $serialized)
Definition: Site.php:698
Site
Definition: Site.php:32
Site\getLinkPathType
getLinkPathType()
Returns the main path type, that is the type of the path that should generally be used to construct l...
Definition: Site.php:354
Site\setExtraData
setExtraData(array $extraData)
Sets the type specific fields.
Definition: Site.php:426
MediaWiki\Site\MediaWikiPageNameNormalizer
Service for normalizing a page name using a MediaWiki api.
Definition: MediaWikiPageNameNormalizer.php:36
Site\addNavigationId
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition: Site.php:545
Site\$group
string $group
Definition: Site.php:73
Site\setPath
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:598
Site\$extraData
array $extraData
Definition: Site.php:104
Site\getNavigationIds
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition: Site.php:570
Site\getExtraConfig
getExtraConfig()
Returns the type specific config.
Definition: Site.php:437
Site\getAllPaths
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:632
Site\removePath
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition: Site.php:643
Site\$forward
bool $forward
Definition: Site.php:118
$wgSiteTypes
$wgSiteTypes
Register handlers for specific types of sites.
Definition: DefaultSettings.php:4863
Site\setExtraConfig
setExtraConfig(array $extraConfig)
Sets the type specific config.
Definition: Site.php:448
Site\$localIds
array[] false $localIds
Holds the local ids for this site.
Definition: Site.php:97
Site\setGlobalId
setGlobalId( $globalId)
Sets the global site identifier (ie enwiktionary).
Definition: Site.php:156
$path
$path
Definition: NoLocalSettings.php:25
Site\shouldForward
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:239
Site\GROUP_NONE
const GROUP_NONE
Definition: Site.php:36
Site\SOURCE_LOCAL
const SOURCE_LOCAL
Definition: Site.php:41
Site\PATH_LINK
const PATH_LINK
Definition: Site.php:43
Site\getProtocol
getProtocol()
Returns the protocol of the site.
Definition: Site.php:287
Site\TYPE_MEDIAWIKI
const TYPE_MEDIAWIKI
Definition: Site.php:34
Site\$source
string $source
Definition: Site.php:80
Site\getExtraData
getExtraData()
Returns the type specific fields.
Definition: Site.php:415
Site\getLinkPath
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition: Site.php:337
Site\getLocalIds
getLocalIds()
Returns all local ids.
Definition: Site.php:583