MediaWiki  master
Site.php
Go to the documentation of this file.
1 <?php
2 
6 
33 class Site implements Serializable {
34  public const TYPE_UNKNOWN = 'unknown';
35  public const TYPE_MEDIAWIKI = 'mediawiki';
36 
37  public const GROUP_NONE = 'none';
38 
39  public const ID_INTERWIKI = 'interwiki';
40  public const ID_EQUIVALENT = 'equivalent';
41 
42  public const SOURCE_LOCAL = 'local';
43 
44  public const PATH_LINK = 'link';
45 
53  public const SERIAL_VERSION_ID = '2013-01-23';
54 
60  protected $globalId = null;
61 
68 
74  protected $group = self::GROUP_NONE;
75 
82 
88  protected $languageCode = null;
89 
98  protected $localIds = [];
99 
105  protected $extraData = [];
106 
112  protected $extraConfig = [];
113 
119  protected $forward = false;
120 
126  protected $internalId = null;
127 
133  public function __construct( $type = self::TYPE_UNKNOWN ) {
134  $this->type = $type;
135  }
136 
144  public function getGlobalId() {
145  return $this->globalId;
146  }
147 
157  public function setGlobalId( $globalId ) {
158  if ( $globalId !== null && !is_string( $globalId ) ) {
159  throw new MWException( '$globalId needs to be string or null' );
160  }
161 
162  $this->globalId = $globalId;
163  }
164 
172  public function getType() {
173  return $this->type;
174  }
175 
183  public function getGroup() {
184  return $this->group;
185  }
186 
196  public function setGroup( $group ) {
197  if ( !is_string( $group ) ) {
198  throw new MWException( '$group needs to be a string' );
199  }
200 
201  $this->group = $group;
202  }
203 
211  public function getSource() {
212  return $this->source;
213  }
214 
224  public function setSource( $source ) {
225  if ( !is_string( $source ) ) {
226  throw new MWException( '$source needs to be a string' );
227  }
228 
229  $this->source = $source;
230  }
231 
240  public function shouldForward() {
241  return $this->forward;
242  }
243 
254  public function setForward( $shouldForward ) {
255  if ( !is_bool( $shouldForward ) ) {
256  throw new MWException( '$shouldForward needs to be a boolean' );
257  }
258 
259  $this->forward = $shouldForward;
260  }
261 
270  public function getDomain() {
271  $path = $this->getLinkPath();
272 
273  if ( $path === null ) {
274  return null;
275  }
276 
277  return parse_url( $path, PHP_URL_HOST );
278  }
279 
288  public function getProtocol() {
289  $path = $this->getLinkPath();
290 
291  if ( $path === null ) {
292  return '';
293  }
294 
295  $protocol = parse_url( $path, PHP_URL_SCHEME );
296 
297  // Malformed URL
298  if ( $protocol === false ) {
299  throw new MWException( "failed to parse URL '$path'" );
300  }
301 
302  // No schema
303  if ( $protocol === null ) {
304  // Used for protocol relative URLs
305  $protocol = '';
306  }
307 
308  return $protocol;
309  }
310 
321  public function setLinkPath( $fullUrl ) {
322  $type = $this->getLinkPathType();
323 
324  if ( $type === null ) {
325  throw new MWException( "This Site does not support link paths." );
326  }
327 
328  $this->setPath( $type, $fullUrl );
329  }
330 
338  public function getLinkPath() {
339  $type = $this->getLinkPathType();
340  return $type === null ? null : $this->getPath( $type );
341  }
342 
355  public function getLinkPathType() {
356  return self::PATH_LINK;
357  }
358 
374  public function getPageUrl( $pageName = false ) {
375  $url = $this->getLinkPath();
376 
377  if ( $url === null ) {
378  return null;
379  }
380 
381  if ( $pageName !== false ) {
382  $url = str_replace( '$1', rawurlencode( $pageName ), $url );
383  }
384 
385  return $url;
386  }
387 
405  public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
406  return $pageName;
407  }
408 
416  public function getExtraData() {
417  return $this->extraData;
418  }
419 
427  public function setExtraData( array $extraData ) {
428  $this->extraData = $extraData;
429  }
430 
438  public function getExtraConfig() {
439  return $this->extraConfig;
440  }
441 
449  public function setExtraConfig( array $extraConfig ) {
450  $this->extraConfig = $extraConfig;
451  }
452 
461  public function getLanguageCode() {
462  return $this->languageCode;
463  }
464 
472  public function setLanguageCode( $languageCode ) {
473  if ( $languageCode !== null
474  && !MediaWikiServices::getInstance()
475  ->getLanguageNameUtils()
476  ->isValidCode( $languageCode )
477  ) {
478  throw new InvalidArgumentException( "$languageCode is not a valid language code." );
479  }
480  $this->languageCode = $languageCode;
481  }
482 
490  public function getInternalId() {
491  return $this->internalId;
492  }
493 
502  public function setInternalId( $internalId = null ) {
503  $this->internalId = $internalId;
504  }
505 
514  public function addLocalId( $type, $identifier ) {
515  if ( $this->localIds === false ) {
516  $this->localIds = [];
517  }
518 
519  if ( !array_key_exists( $type, $this->localIds ) ) {
520  $this->localIds[$type] = [];
521  }
522 
523  if ( !in_array( $identifier, $this->localIds[$type] ) ) {
524  $this->localIds[$type][] = $identifier;
525  }
526  }
527 
535  public function addInterwikiId( $identifier ) {
536  $this->addLocalId( self::ID_INTERWIKI, $identifier );
537  }
538 
546  public function addNavigationId( $identifier ) {
547  $this->addLocalId( self::ID_EQUIVALENT, $identifier );
548  }
549 
557  public function getInterwikiIds() {
558  return array_key_exists( self::ID_INTERWIKI, $this->localIds )
559  ? $this->localIds[self::ID_INTERWIKI]
560  : [];
561  }
562 
571  public function getNavigationIds() {
572  return array_key_exists( self::ID_EQUIVALENT, $this->localIds )
573  ? $this->localIds[self::ID_EQUIVALENT] :
574  [];
575  }
576 
584  public function getLocalIds() {
585  return $this->localIds;
586  }
587 
599  public function setPath( $pathType, $fullUrl ) {
600  if ( !is_string( $fullUrl ) ) {
601  throw new MWException( '$fullUrl needs to be a string' );
602  }
603 
604  if ( !array_key_exists( 'paths', $this->extraData ) ) {
605  $this->extraData['paths'] = [];
606  }
607 
608  $this->extraData['paths'][$pathType] = $fullUrl;
609  }
610 
620  public function getPath( $pathType ) {
621  $paths = $this->getAllPaths();
622  return $paths[$pathType] ?? null;
623  }
624 
633  public function getAllPaths() {
634  return $this->extraData['paths'] ?? [];
635  }
636 
644  public function removePath( $pathType ) {
645  if ( array_key_exists( 'paths', $this->extraData ) ) {
646  unset( $this->extraData['paths'][$pathType] );
647  }
648  }
649 
657  public static function newForType( $siteType ) {
658  $siteTypes = MediaWikiServices::getInstance()->getMainConfig()->get(
659  MainConfigNames::SiteTypes );
660 
661  if ( array_key_exists( $siteType, $siteTypes ) ) {
662  return new $siteTypes[$siteType]();
663  }
664 
665  return new Site();
666  }
667 
675  public function serialize() {
676  return serialize( $this->__serialize() );
677  }
678 
686  public function __serialize() {
687  return [
688  'globalid' => $this->globalId,
689  'type' => $this->type,
690  'group' => $this->group,
691  'source' => $this->source,
692  'language' => $this->languageCode,
693  'localids' => $this->localIds,
694  'config' => $this->extraConfig,
695  'data' => $this->extraData,
696  'forward' => $this->forward,
697  'internalid' => $this->internalId,
698  ];
699  }
700 
708  public function unserialize( $serialized ) {
710  }
711 
719  public function __unserialize( $fields ) {
720  $this->__construct( $fields['type'] );
721 
722  $this->setGlobalId( $fields['globalid'] );
723  $this->setGroup( $fields['group'] );
724  $this->setSource( $fields['source'] );
725  $this->setLanguageCode( $fields['language'] );
726  $this->localIds = $fields['localids'];
727  $this->setExtraConfig( $fields['config'] );
728  $this->setExtraData( $fields['data'] );
729  $this->setForward( $fields['forward'] );
730  $this->setInternalId( $fields['internalid'] );
731  }
732 }
MediaWiki exception.
Definition: MWException.php:29
A class containing constants representing the names of configuration variables.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Service for normalizing a page name using a MediaWiki api.
Definition: Site.php:33
normalizePageName( $pageName, $followRedirect=MediaWikiPageNameNormalizer::FOLLOW_REDIRECT)
Attempt to normalize the page name in some fashion.
Definition: Site.php:405
__unserialize( $fields)
Definition: Site.php:719
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:240
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:599
const TYPE_MEDIAWIKI
Definition: Site.php:35
getExtraData()
Returns the type specific fields.
Definition: Site.php:416
setGroup( $group)
Sets the group of the site (ie wikipedia).
Definition: Site.php:196
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition: Site.php:535
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:620
const GROUP_NONE
Definition: Site.php:37
setForward( $shouldForward)
Sets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:254
setSource( $source)
Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:224
string[][] false $localIds
Holds the local ids for this site.
Definition: Site.php:98
string $type
Definition: Site.php:67
array $extraConfig
Definition: Site.php:112
array $extraData
Definition: Site.php:105
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:211
__serialize()
Definition: Site.php:686
static newForType( $siteType)
Definition: Site.php:657
getProtocol()
Returns the protocol of the site.
Definition: Site.php:288
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:172
int null $internalId
Definition: Site.php:126
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition: Site.php:644
addLocalId( $type, $identifier)
Adds a local identifier.
Definition: Site.php:514
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition: Site.php:472
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:144
setGlobalId( $globalId)
Sets the global site identifier (ie enwiktionary).
Definition: Site.php:157
getLinkPathType()
Returns the main path type, that is the type of the path that should generally be used to construct l...
Definition: Site.php:355
const PATH_LINK
Definition: Site.php:44
const TYPE_UNKNOWN
Definition: Site.php:34
const ID_EQUIVALENT
Definition: Site.php:40
getExtraConfig()
Returns the type specific config.
Definition: Site.php:438
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition: Site.php:546
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition: Site.php:374
string null $languageCode
Definition: Site.php:88
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:633
getLocalIds()
Returns all local ids.
Definition: Site.php:584
unserialize( $serialized)
Definition: Site.php:708
const ID_INTERWIKI
Definition: Site.php:39
getGroup()
Gets the group of the site (ie wikipedia).
Definition: Site.php:183
setInternalId( $internalId=null)
Sets the internal identifier for the site.
Definition: Site.php:502
string $source
Definition: Site.php:81
serialize()
Definition: Site.php:675
getLanguageCode()
Returns language code of the sites primary language.
Definition: Site.php:461
getInternalId()
Returns the set internal identifier for the site.
Definition: Site.php:490
bool $forward
Definition: Site.php:119
const SOURCE_LOCAL
Definition: Site.php:42
setExtraConfig(array $extraConfig)
Sets the type specific config.
Definition: Site.php:449
__construct( $type=self::TYPE_UNKNOWN)
Definition: Site.php:133
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition: Site.php:571
string null $globalId
Definition: Site.php:60
string $group
Definition: Site.php:74
getDomain()
Returns the domain of the site, ie en.wikipedia.org Or false if it's not known.
Definition: Site.php:270
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition: Site.php:338
setLinkPath( $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:321
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:557
setExtraData(array $extraData)
Sets the type specific fields.
Definition: Site.php:427
foreach( $res as $row) $serialized