MediaWiki  master
Site.php
Go to the documentation of this file.
1 <?php
2 
4 
31 class Site implements Serializable {
32  public const TYPE_UNKNOWN = 'unknown';
33  public const TYPE_MEDIAWIKI = 'mediawiki';
34 
35  public const GROUP_NONE = 'none';
36 
37  public const ID_INTERWIKI = 'interwiki';
38  public const ID_EQUIVALENT = 'equivalent';
39 
40  public const SOURCE_LOCAL = 'local';
41 
42  public const PATH_LINK = 'link';
43 
51  public const SERIAL_VERSION_ID = '2013-01-23';
52 
58  protected $globalId = null;
59 
66 
72  protected $group = self::GROUP_NONE;
73 
80 
86  protected $languageCode = null;
87 
96  protected $localIds = [];
97 
103  protected $extraData = [];
104 
110  protected $extraConfig = [];
111 
117  protected $forward = false;
118 
124  protected $internalId = null;
125 
131  public function __construct( $type = self::TYPE_UNKNOWN ) {
132  $this->type = $type;
133  }
134 
142  public function getGlobalId() {
143  return $this->globalId;
144  }
145 
155  public function setGlobalId( $globalId ) {
156  if ( $globalId !== null && !is_string( $globalId ) ) {
157  throw new MWException( '$globalId needs to be string or null' );
158  }
159 
160  $this->globalId = $globalId;
161  }
162 
170  public function getType() {
171  return $this->type;
172  }
173 
181  public function getGroup() {
182  return $this->group;
183  }
184 
194  public function setGroup( $group ) {
195  if ( !is_string( $group ) ) {
196  throw new MWException( '$group needs to be a string' );
197  }
198 
199  $this->group = $group;
200  }
201 
209  public function getSource() {
210  return $this->source;
211  }
212 
222  public function setSource( $source ) {
223  if ( !is_string( $source ) ) {
224  throw new MWException( '$source needs to be a string' );
225  }
226 
227  $this->source = $source;
228  }
229 
238  public function shouldForward() {
239  return $this->forward;
240  }
241 
252  public function setForward( $shouldForward ) {
253  if ( !is_bool( $shouldForward ) ) {
254  throw new MWException( '$shouldForward needs to be a boolean' );
255  }
256 
257  $this->forward = $shouldForward;
258  }
259 
268  public function getDomain() {
269  $path = $this->getLinkPath();
270 
271  if ( $path === null ) {
272  return null;
273  }
274 
275  return parse_url( $path, PHP_URL_HOST );
276  }
277 
286  public function getProtocol() {
287  $path = $this->getLinkPath();
288 
289  if ( $path === null ) {
290  return '';
291  }
292 
293  $protocol = parse_url( $path, PHP_URL_SCHEME );
294 
295  // Malformed URL
296  if ( $protocol === false ) {
297  throw new MWException( "failed to parse URL '$path'" );
298  }
299 
300  // No schema
301  if ( $protocol === null ) {
302  // Used for protocol relative URLs
303  $protocol = '';
304  }
305 
306  return $protocol;
307  }
308 
319  public function setLinkPath( $fullUrl ) {
320  $type = $this->getLinkPathType();
321 
322  if ( $type === null ) {
323  throw new MWException( "This Site does not support link paths." );
324  }
325 
326  $this->setPath( $type, $fullUrl );
327  }
328 
336  public function getLinkPath() {
337  $type = $this->getLinkPathType();
338  return $type === null ? null : $this->getPath( $type );
339  }
340 
353  public function getLinkPathType() {
354  return self::PATH_LINK;
355  }
356 
372  public function getPageUrl( $pageName = false ) {
373  $url = $this->getLinkPath();
374 
375  if ( $url === null ) {
376  return null;
377  }
378 
379  if ( $pageName !== false ) {
380  $url = str_replace( '$1', rawurlencode( $pageName ), $url );
381  }
382 
383  return $url;
384  }
385 
400  public function normalizePageName( $pageName ) {
401  return $pageName;
402  }
403 
411  public function getExtraData() {
412  return $this->extraData;
413  }
414 
422  public function setExtraData( array $extraData ) {
423  $this->extraData = $extraData;
424  }
425 
433  public function getExtraConfig() {
434  return $this->extraConfig;
435  }
436 
444  public function setExtraConfig( array $extraConfig ) {
445  $this->extraConfig = $extraConfig;
446  }
447 
456  public function getLanguageCode() {
457  return $this->languageCode;
458  }
459 
467  public function setLanguageCode( $languageCode ) {
468  if ( $languageCode !== null
469  && !MediaWikiServices::getInstance()
470  ->getLanguageNameUtils()
471  ->isValidCode( $languageCode )
472  ) {
473  throw new InvalidArgumentException( "$languageCode is not a valid language code." );
474  }
475  $this->languageCode = $languageCode;
476  }
477 
485  public function getInternalId() {
486  return $this->internalId;
487  }
488 
497  public function setInternalId( $internalId = null ) {
498  $this->internalId = $internalId;
499  }
500 
509  public function addLocalId( $type, $identifier ) {
510  if ( $this->localIds === false ) {
511  $this->localIds = [];
512  }
513 
514  if ( !array_key_exists( $type, $this->localIds ) ) {
515  $this->localIds[$type] = [];
516  }
517 
518  if ( !in_array( $identifier, $this->localIds[$type] ) ) {
519  $this->localIds[$type][] = $identifier;
520  }
521  }
522 
530  public function addInterwikiId( $identifier ) {
531  $this->addLocalId( self::ID_INTERWIKI, $identifier );
532  }
533 
541  public function addNavigationId( $identifier ) {
542  $this->addLocalId( self::ID_EQUIVALENT, $identifier );
543  }
544 
552  public function getInterwikiIds() {
553  return array_key_exists( self::ID_INTERWIKI, $this->localIds )
554  ? $this->localIds[self::ID_INTERWIKI]
555  : [];
556  }
557 
566  public function getNavigationIds() {
567  return array_key_exists( self::ID_EQUIVALENT, $this->localIds )
568  ? $this->localIds[self::ID_EQUIVALENT] :
569  [];
570  }
571 
579  public function getLocalIds() {
580  return $this->localIds;
581  }
582 
594  public function setPath( $pathType, $fullUrl ) {
595  if ( !is_string( $fullUrl ) ) {
596  throw new MWException( '$fullUrl needs to be a string' );
597  }
598 
599  if ( !array_key_exists( 'paths', $this->extraData ) ) {
600  $this->extraData['paths'] = [];
601  }
602 
603  $this->extraData['paths'][$pathType] = $fullUrl;
604  }
605 
615  public function getPath( $pathType ) {
616  $paths = $this->getAllPaths();
617  return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : null;
618  }
619 
628  public function getAllPaths() {
629  return array_key_exists( 'paths', $this->extraData ) ? $this->extraData['paths'] : [];
630  }
631 
639  public function removePath( $pathType ) {
640  if ( array_key_exists( 'paths', $this->extraData ) ) {
641  unset( $this->extraData['paths'][$pathType] );
642  }
643  }
644 
652  public static function newForType( $siteType ) {
653  global $wgSiteTypes;
654 
655  if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
656  return new $wgSiteTypes[$siteType]();
657  }
658 
659  return new Site();
660  }
661 
669  public function serialize() {
670  $fields = [
671  'globalid' => $this->globalId,
672  'type' => $this->type,
673  'group' => $this->group,
674  'source' => $this->source,
675  'language' => $this->languageCode,
676  'localids' => $this->localIds,
677  'config' => $this->extraConfig,
678  'data' => $this->extraData,
679  'forward' => $this->forward,
680  'internalid' => $this->internalId,
681 
682  ];
683 
684  return serialize( $fields );
685  }
686 
694  public function unserialize( $serialized ) {
695  $fields = unserialize( $serialized );
696 
697  $this->__construct( $fields['type'] );
698 
699  $this->setGlobalId( $fields['globalid'] );
700  $this->setGroup( $fields['group'] );
701  $this->setSource( $fields['source'] );
702  $this->setLanguageCode( $fields['language'] );
703  $this->localIds = $fields['localids'];
704  $this->setExtraConfig( $fields['config'] );
705  $this->setExtraData( $fields['data'] );
706  $this->setForward( $fields['forward'] );
707  $this->setInternalId( $fields['internalid'] );
708  }
709 }
Site\ID_EQUIVALENT
const ID_EQUIVALENT
Definition: Site.php:38
Site\getInterwikiIds
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:552
Site\$extraConfig
array $extraConfig
Definition: Site.php:110
Site\getType
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:170
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
Site\getDomain
getDomain()
Returns the domain of the site, ie en.wikipedia.org Or false if it's not known.
Definition: Site.php:268
Site\$languageCode
string null $languageCode
Definition: Site.php:86
Site\setSource
setSource( $source)
Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:222
Site\getPageUrl
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition: Site.php:372
Site\getInternalId
getInternalId()
Returns the set internal identifier for the site.
Definition: Site.php:485
Site\setLinkPath
setLinkPath( $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:319
$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:194
Site\setInternalId
setInternalId( $internalId=null)
Sets the internal identifier for the site.
Definition: Site.php:497
Site\getPath
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:615
Site\getLanguageCode
getLanguageCode()
Returns language code of the sites primary language.
Definition: Site.php:456
Site\ID_INTERWIKI
const ID_INTERWIKI
Definition: Site.php:37
Site\newForType
static newForType( $siteType)
Definition: Site.php:652
Site\$type
string $type
Definition: Site.php:65
Site\$internalId
int null $internalId
Definition: Site.php:124
Site\__construct
__construct( $type=self::TYPE_UNKNOWN)
Definition: Site.php:131
Site\serialize
serialize()
Definition: Site.php:669
MWException
MediaWiki exception.
Definition: MWException.php:29
Site\$globalId
string null $globalId
Definition: Site.php:58
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:32
Site\getGlobalId
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:142
Site\getSource
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:209
Site\setForward
setForward( $shouldForward)
Sets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:252
Site\setLanguageCode
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition: Site.php:467
Site\getGroup
getGroup()
Gets the group of the site (ie wikipedia).
Definition: Site.php:181
Site\addInterwikiId
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition: Site.php:530
Site\addLocalId
addLocalId( $type, $identifier)
Adds a local identifier.
Definition: Site.php:509
Site\unserialize
unserialize( $serialized)
Definition: Site.php:694
Site
Definition: Site.php:31
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:353
Site\setExtraData
setExtraData(array $extraData)
Sets the type specific fields.
Definition: Site.php:422
Site\addNavigationId
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition: Site.php:541
Site\$group
string $group
Definition: Site.php:72
Site\setPath
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:594
Site\$extraData
array $extraData
Definition: Site.php:103
Site\getNavigationIds
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition: Site.php:566
Site\getExtraConfig
getExtraConfig()
Returns the type specific config.
Definition: Site.php:433
Site\getAllPaths
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:628
Site\removePath
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition: Site.php:639
Site\$forward
bool $forward
Definition: Site.php:117
$wgSiteTypes
$wgSiteTypes
Register handlers for specific types of sites.
Definition: DefaultSettings.php:9116
Site\setExtraConfig
setExtraConfig(array $extraConfig)
Sets the type specific config.
Definition: Site.php:444
Site\$localIds
array[] false $localIds
Holds the local ids for this site.
Definition: Site.php:96
Site\setGlobalId
setGlobalId( $globalId)
Sets the global site identifier (ie enwiktionary).
Definition: Site.php:155
$path
$path
Definition: NoLocalSettings.php:25
Site\normalizePageName
normalizePageName( $pageName)
Attempt to normalize the page name in some fashion.
Definition: Site.php:400
Site\shouldForward
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:238
Site\GROUP_NONE
const GROUP_NONE
Definition: Site.php:35
Site\SOURCE_LOCAL
const SOURCE_LOCAL
Definition: Site.php:40
Site\PATH_LINK
const PATH_LINK
Definition: Site.php:42
Site\getProtocol
getProtocol()
Returns the protocol of the site.
Definition: Site.php:286
Site\TYPE_MEDIAWIKI
const TYPE_MEDIAWIKI
Definition: Site.php:33
Site\$source
string $source
Definition: Site.php:79
Site\getExtraData
getExtraData()
Returns the type specific fields.
Definition: Site.php:411
Site\getLinkPath
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition: Site.php:336
Site\getLocalIds
getLocalIds()
Returns all local ids.
Definition: Site.php:579