Merge "Typo fix"
[mediawiki.git] / includes / site / Site.php
blob076dc88cc292cb950452130217ba3d3ccb1731e9
1 <?php
3 /**
4 * Represents a single site.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @since 1.21
23 * @file
24 * @ingroup Site
26 * @license GNU GPL v2+
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 class Site implements Serializable {
31 const TYPE_UNKNOWN = 'unknown';
32 const TYPE_MEDIAWIKI = 'mediawiki';
34 const GROUP_NONE = 'none';
36 const ID_INTERWIKI = 'interwiki';
37 const ID_EQUIVALENT = 'equivalent';
39 const SOURCE_LOCAL = 'local';
41 const PATH_LINK = 'link';
43 /**
44 * A version ID that identifies the serialization structure used by getSerializationData()
45 * and unserialize(). This is useful for constructing cache keys in cases where the cache relies
46 * on serialization for storing the SiteList.
48 * @var string A string uniquely identifying the version of the serialization structure.
50 const SERIAL_VERSION_ID = '2013-01-23';
52 /**
53 * @since 1.21
55 * @var string|null
57 protected $globalId = null;
59 /**
60 * @since 1.21
62 * @var string
64 protected $type = self::TYPE_UNKNOWN;
66 /**
67 * @since 1.21
69 * @var string
71 protected $group = self::GROUP_NONE;
73 /**
74 * @since 1.21
76 * @var string
78 protected $source = self::SOURCE_LOCAL;
80 /**
81 * @since 1.21
83 * @var string|null
85 protected $languageCode = null;
87 /**
88 * Holds the local ids for this site.
89 * local id type => [ ids for this type (strings) ]
91 * @since 1.21
93 * @var array[]
95 protected $localIds = array();
97 /**
98 * @since 1.21
100 * @var array
102 protected $extraData = array();
105 * @since 1.21
107 * @var array
109 protected $extraConfig = array();
112 * @since 1.21
114 * @var bool
116 protected $forward = false;
119 * @since 1.21
121 * @var int|null
123 protected $internalId = null;
126 * Constructor.
128 * @since 1.21
130 * @param string $type
132 public function __construct( $type = self::TYPE_UNKNOWN ) {
133 $this->type = $type;
137 * Returns the global site identifier (ie enwiktionary).
139 * @since 1.21
141 * @return string|null
143 public function getGlobalId() {
144 return $this->globalId;
148 * Sets the global site identifier (ie enwiktionary).
150 * @since 1.21
152 * @param string|null $globalId
154 * @throws MWException
156 public function setGlobalId( $globalId ) {
157 if ( $globalId !== null && !is_string( $globalId ) ) {
158 throw new MWException( '$globalId needs to be string or null' );
161 $this->globalId = $globalId;
165 * Returns the type of the site (ie mediawiki).
167 * @since 1.21
169 * @return string
171 public function getType() {
172 return $this->type;
176 * Gets the type of the site (ie wikipedia).
178 * @since 1.21
180 * @return string
182 public function getGroup() {
183 return $this->group;
187 * Sets the type of the site (ie wikipedia).
189 * @since 1.21
191 * @param string $group
193 * @throws MWException
195 public function setGroup( $group ) {
196 if ( !is_string( $group ) ) {
197 throw new MWException( '$group needs to be a string' );
200 $this->group = $group;
204 * Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
206 * @since 1.21
208 * @return string
210 public function getSource() {
211 return $this->source;
215 * Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
217 * @since 1.21
219 * @param string $source
221 * @throws MWException
223 public function setSource( $source ) {
224 if ( !is_string( $source ) ) {
225 throw new MWException( '$source needs to be a string' );
228 $this->source = $source;
232 * Gets if site.tld/path/key:pageTitle should forward users to the page on
233 * the actual site, where "key" is the local identifier.
235 * @since 1.21
237 * @return boolean
239 public function shouldForward() {
240 return $this->forward;
244 * Sets if site.tld/path/key:pageTitle should forward users to the page on
245 * the actual site, where "key" is the local identifier.
247 * @since 1.21
249 * @param boolean $shouldForward
251 * @throws MWException
253 public function setForward( $shouldForward ) {
254 if ( !is_bool( $shouldForward ) ) {
255 throw new MWException( '$shouldForward needs to be a boolean' );
258 $this->forward = $shouldForward;
262 * Returns the domain of the site, ie en.wikipedia.org
263 * Or false if it's not known.
265 * @since 1.21
267 * @return string|null
269 public function getDomain() {
270 $path = $this->getLinkPath();
272 if ( $path === null ) {
273 return null;
276 return parse_url( $path, PHP_URL_HOST );
280 * Returns the protocol of the site.
282 * @since 1.21
284 * @throws MWException
285 * @return string
287 public function getProtocol() {
288 $path = $this->getLinkPath();
290 if ( $path === null ) {
291 return '';
294 $protocol = parse_url( $path, PHP_URL_SCHEME );
296 // Malformed URL
297 if ( $protocol === false ) {
298 throw new MWException( "failed to parse URL '$path'" );
301 // No schema
302 if ( $protocol === null ) {
303 // Used for protocol relative URLs
304 $protocol = '';
307 return $protocol;
311 * Sets the path used to construct links with.
312 * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
314 * @param string $fullUrl
316 * @since 1.21
318 * @throws MWException
320 public function setLinkPath( $fullUrl ) {
321 $type = $this->getLinkPathType();
323 if ( $type === null ) {
324 throw new MWException( "This Site does not support link paths." );
327 $this->setPath( $type, $fullUrl );
331 * Returns the path used to construct links with or false if there is no such path.
333 * Shall be equivalent to getPath( getLinkPathType() ).
335 * @return string|null
337 public function getLinkPath() {
338 $type = $this->getLinkPathType();
339 return $type === null ? null: $this->getPath( $type );
343 * Returns the main path type, that is the type of the path that should generally be used to construct links
344 * to the target site.
346 * This default implementation returns Site::PATH_LINK as the default path type. Subclasses can override this
347 * to define a different default path type, or return false to disable site links.
349 * @since 1.21
351 * @return string|null
353 public function getLinkPathType() {
354 return self::PATH_LINK;
358 * Returns the full URL for the given page on the site.
359 * Or false if the needed information is not known.
361 * This generated URL is usually based upon the path returned by getLinkPath(),
362 * but this is not a requirement.
364 * This implementation returns a URL constructed using the path returned by getLinkPath().
366 * @since 1.21
368 * @param bool|String $pageName
370 * @return string|boolean false
372 public function getPageUrl( $pageName = false ) {
373 $url = $this->getLinkPath();
375 if ( $url === false ) {
376 return false;
379 if ( $pageName !== false ) {
380 $url = str_replace( '$1', rawurlencode( $pageName ), $url );
383 return $url;
387 * Returns $pageName without changes.
388 * Subclasses may override this to apply some kind of normalization.
390 * @see Site::normalizePageName
392 * @since 1.21
394 * @param string $pageName
396 * @return string
398 public function normalizePageName( $pageName ) {
399 return $pageName;
403 * Returns the type specific fields.
405 * @since 1.21
407 * @return array
409 public function getExtraData() {
410 return $this->extraData;
414 * Sets the type specific fields.
416 * @since 1.21
418 * @param array $extraData
420 public function setExtraData( array $extraData ) {
421 $this->extraData = $extraData;
425 * Returns the type specific config.
427 * @since 1.21
429 * @return array
431 public function getExtraConfig() {
432 return $this->extraConfig;
436 * Sets the type specific config.
438 * @since 1.21
440 * @param array $extraConfig
442 public function setExtraConfig( array $extraConfig ) {
443 $this->extraConfig = $extraConfig;
447 * Returns language code of the sites primary language.
448 * Or null if it's not known.
450 * @since 1.21
452 * @return string|null
454 public function getLanguageCode() {
455 return $this->languageCode;
459 * Sets language code of the sites primary language.
461 * @since 1.21
463 * @param string $languageCode
465 public function setLanguageCode( $languageCode ) {
466 $this->languageCode = $languageCode;
470 * Returns the set internal identifier for the site.
472 * @since 1.21
474 * @return string|null
476 public function getInternalId() {
477 return $this->internalId;
481 * Sets the internal identifier for the site.
482 * This typically is a primary key in a db table.
484 * @since 1.21
486 * @param int|null $internalId
488 public function setInternalId( $internalId = null ) {
489 $this->internalId = $internalId;
493 * Adds a local identifier.
495 * @since 1.21
497 * @param string $type
498 * @param string $identifier
500 public function addLocalId( $type, $identifier ) {
501 if ( $this->localIds === false ) {
502 $this->localIds = array();
505 if ( !array_key_exists( $type, $this->localIds ) ) {
506 $this->localIds[$type] = array();
509 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
510 $this->localIds[$type][] = $identifier;
515 * Adds an interwiki id to the site.
517 * @since 1.21
519 * @param string $identifier
521 public function addInterwikiId( $identifier ) {
522 $this->addLocalId( self::ID_INTERWIKI, $identifier );
526 * Adds a navigation id to the site.
528 * @since 1.21
530 * @param string $identifier
532 public function addNavigationId( $identifier ) {
533 $this->addLocalId( self::ID_EQUIVALENT, $identifier );
537 * Returns the interwiki link identifiers that can be used for this site.
539 * @since 1.21
541 * @return string[]
543 public function getInterwikiIds() {
544 return array_key_exists( self::ID_INTERWIKI, $this->localIds ) ? $this->localIds[self::ID_INTERWIKI] : array();
548 * Returns the equivalent link identifiers that can be used to make
549 * the site show up in interfaces such as the "language links" section.
551 * @since 1.21
553 * @return string[]
555 public function getNavigationIds() {
556 return array_key_exists( self::ID_EQUIVALENT, $this->localIds ) ? $this->localIds[self::ID_EQUIVALENT] : array();
560 * Returns all local ids
562 * @since 1.21
564 * @return array[]
566 public function getLocalIds() {
567 return $this->localIds;
571 * Sets the path used to construct links with.
572 * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
574 * @since 1.21
576 * @param string $pathType
577 * @param string $fullUrl
579 * @throws MWException
581 public function setPath( $pathType, $fullUrl ) {
582 if ( !is_string( $fullUrl ) ) {
583 throw new MWException( '$fullUrl needs to be a string' );
586 if ( !array_key_exists( 'paths', $this->extraData ) ) {
587 $this->extraData['paths'] = array();
590 $this->extraData['paths'][$pathType] = $fullUrl;
594 * Returns the path of the provided type or false if there is no such path.
596 * @since 1.21
598 * @param string $pathType
600 * @return string|null
602 public function getPath( $pathType ) {
603 $paths = $this->getAllPaths();
604 return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : null;
608 * Returns the paths as associative array.
609 * The keys are path types, the values are the path urls.
611 * @since 1.21
613 * @return string[]
615 public function getAllPaths() {
616 return array_key_exists( 'paths', $this->extraData ) ? $this->extraData['paths'] : array();
620 * Removes the path of the provided type if it's set.
622 * @since 1.21
624 * @param string $pathType
626 public function removePath( $pathType ) {
627 if ( array_key_exists( 'paths', $this->extraData ) ) {
628 unset( $this->extraData['paths'][$pathType] );
633 * @since 1.21
635 * @param string $siteType
637 * @return Site
639 public static function newForType( $siteType ) {
640 global $wgSiteTypes;
642 if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
643 return new $wgSiteTypes[$siteType]();
646 return new Site();
650 * @see Serializable::serialize
652 * @since 1.21
654 * @return string
656 public function serialize() {
657 $fields = array(
658 'globalid' => $this->globalId,
659 'type' => $this->type,
660 'group' => $this->group,
661 'source' => $this->source,
662 'language' => $this->languageCode,
663 'localids' => $this->localIds,
664 'config' => $this->extraConfig,
665 'data' => $this->extraData,
666 'forward' => $this->forward,
667 'internalid' => $this->internalId,
671 return serialize( $fields );
675 * @see Serializable::unserialize
677 * @since 1.21
679 * @param string $serialized
681 public function unserialize( $serialized ) {
682 $fields = unserialize( $serialized );
684 $this->__construct( $fields['type'] );
686 $this->setGlobalId( $fields['globalid'] );
687 $this->setGroup( $fields['group'] );
688 $this->setSource( $fields['source'] );
689 $this->setLanguageCode( $fields['language'] );
690 $this->localIds = $fields['localids'];
691 $this->setExtraConfig( $fields['config'] );
692 $this->setExtraData( $fields['data'] );
693 $this->setForward( $fields['forward'] );
694 $this->setInternalId( $fields['internalid'] );
700 * @deprecated
702 class SiteObject extends Site {}