Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / includes / site / Site.php
bloba6e63391da33418ef0777a8addce537b2ff2e0bb
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 {
30 const TYPE_UNKNOWN = 'unknown';
31 const TYPE_MEDIAWIKI = 'mediawiki';
33 const GROUP_NONE = 'none';
35 const ID_INTERWIKI = 'interwiki';
36 const ID_EQUIVALENT = 'equivalent';
38 const SOURCE_LOCAL = 'local';
40 const PATH_LINK = 'link';
42 /**
43 * A version ID that identifies the serialization structure used by getSerializationData()
44 * and unserialize(). This is useful for constructing cache keys in cases where the cache relies
45 * on serialization for storing the SiteList.
47 * @var string A string uniquely identifying the version of the serialization structure.
49 const SERIAL_VERSION_ID = '2013-01-23';
51 /**
52 * @since 1.21
54 * @var string|null
56 protected $globalId = null;
58 /**
59 * @since 1.21
61 * @var string
63 protected $type = self::TYPE_UNKNOWN;
65 /**
66 * @since 1.21
68 * @var string
70 protected $group = self::GROUP_NONE;
72 /**
73 * @since 1.21
75 * @var string
77 protected $source = self::SOURCE_LOCAL;
79 /**
80 * @since 1.21
82 * @var string|null
84 protected $languageCode = null;
86 /**
87 * Holds the local ids for this site.
88 * local id type => [ ids for this type (strings) ]
90 * @since 1.21
92 * @var array[]
94 protected $localIds = [];
96 /**
97 * @since 1.21
99 * @var array
101 protected $extraData = [];
104 * @since 1.21
106 * @var array
108 protected $extraConfig = [];
111 * @since 1.21
113 * @var bool
115 protected $forward = false;
118 * @since 1.21
120 * @var int|null
122 protected $internalId = null;
125 * @since 1.21
127 * @param string $type
129 public function __construct( $type = self::TYPE_UNKNOWN ) {
130 $this->type = $type;
134 * Returns the global site identifier (ie enwiktionary).
136 * @since 1.21
138 * @return string|null
140 public function getGlobalId() {
141 return $this->globalId;
145 * Sets the global site identifier (ie enwiktionary).
147 * @since 1.21
149 * @param string|null $globalId
151 * @throws MWException
153 public function setGlobalId( $globalId ) {
154 if ( $globalId !== null && !is_string( $globalId ) ) {
155 throw new MWException( '$globalId needs to be string or null' );
158 $this->globalId = $globalId;
162 * Returns the type of the site (ie mediawiki).
164 * @since 1.21
166 * @return string
168 public function getType() {
169 return $this->type;
173 * Gets the group of the site (ie wikipedia).
175 * @since 1.21
177 * @return string
179 public function getGroup() {
180 return $this->group;
184 * Sets the group of the site (ie wikipedia).
186 * @since 1.21
188 * @param string $group
190 * @throws MWException
192 public function setGroup( $group ) {
193 if ( !is_string( $group ) ) {
194 throw new MWException( '$group needs to be a string' );
197 $this->group = $group;
201 * Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
203 * @since 1.21
205 * @return string
207 public function getSource() {
208 return $this->source;
212 * Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
214 * @since 1.21
216 * @param string $source
218 * @throws MWException
220 public function setSource( $source ) {
221 if ( !is_string( $source ) ) {
222 throw new MWException( '$source needs to be a string' );
225 $this->source = $source;
229 * Gets if site.tld/path/key:pageTitle should forward users to the page on
230 * the actual site, where "key" is the local identifier.
232 * @since 1.21
234 * @return bool
236 public function shouldForward() {
237 return $this->forward;
241 * Sets if site.tld/path/key:pageTitle should forward users to the page on
242 * the actual site, where "key" is the local identifier.
244 * @since 1.21
246 * @param bool $shouldForward
248 * @throws MWException
250 public function setForward( $shouldForward ) {
251 if ( !is_bool( $shouldForward ) ) {
252 throw new MWException( '$shouldForward needs to be a boolean' );
255 $this->forward = $shouldForward;
259 * Returns the domain of the site, ie en.wikipedia.org
260 * Or false if it's not known.
262 * @since 1.21
264 * @return string|null
266 public function getDomain() {
267 $path = $this->getLinkPath();
269 if ( $path === null ) {
270 return null;
273 return parse_url( $path, PHP_URL_HOST );
277 * Returns the protocol of the site.
279 * @since 1.21
281 * @throws MWException
282 * @return string
284 public function getProtocol() {
285 $path = $this->getLinkPath();
287 if ( $path === null ) {
288 return '';
291 $protocol = parse_url( $path, PHP_URL_SCHEME );
293 // Malformed URL
294 if ( $protocol === false ) {
295 throw new MWException( "failed to parse URL '$path'" );
298 // No schema
299 if ( $protocol === null ) {
300 // Used for protocol relative URLs
301 $protocol = '';
304 return $protocol;
308 * Sets the path used to construct links with.
309 * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
311 * @param string $fullUrl
313 * @since 1.21
315 * @throws MWException
317 public function setLinkPath( $fullUrl ) {
318 $type = $this->getLinkPathType();
320 if ( $type === null ) {
321 throw new MWException( "This Site does not support link paths." );
324 $this->setPath( $type, $fullUrl );
328 * Returns the path used to construct links with or false if there is no such path.
330 * Shall be equivalent to getPath( getLinkPathType() ).
332 * @return string|null
334 public function getLinkPath() {
335 $type = $this->getLinkPathType();
336 return $type === null ? null : $this->getPath( $type );
340 * Returns the main path type, that is the type of the path that should
341 * generally be used to construct links to the target site.
343 * This default implementation returns Site::PATH_LINK as the default path
344 * type. Subclasses can override this to define a different default path
345 * type, or return false to disable site links.
347 * @since 1.21
349 * @return string|null
351 public function getLinkPathType() {
352 return self::PATH_LINK;
356 * Returns the full URL for the given page on the site.
357 * Or false if the needed information is not known.
359 * This generated URL is usually based upon the path returned by getLinkPath(),
360 * but this is not a requirement.
362 * This implementation returns a URL constructed using the path returned by getLinkPath().
364 * @since 1.21
366 * @param bool|string $pageName
368 * @return string|bool
370 public function getPageUrl( $pageName = false ) {
371 $url = $this->getLinkPath();
373 if ( $url === false ) {
374 return false;
377 if ( $pageName !== false ) {
378 $url = str_replace( '$1', rawurlencode( $pageName ), $url );
381 return $url;
385 * Returns $pageName without changes.
386 * Subclasses may override this to apply some kind of normalization.
388 * @see Site::normalizePageName
390 * @since 1.21
392 * @param string $pageName
394 * @return string
396 public function normalizePageName( $pageName ) {
397 return $pageName;
401 * Returns the type specific fields.
403 * @since 1.21
405 * @return array
407 public function getExtraData() {
408 return $this->extraData;
412 * Sets the type specific fields.
414 * @since 1.21
416 * @param array $extraData
418 public function setExtraData( array $extraData ) {
419 $this->extraData = $extraData;
423 * Returns the type specific config.
425 * @since 1.21
427 * @return array
429 public function getExtraConfig() {
430 return $this->extraConfig;
434 * Sets the type specific config.
436 * @since 1.21
438 * @param array $extraConfig
440 public function setExtraConfig( array $extraConfig ) {
441 $this->extraConfig = $extraConfig;
445 * Returns language code of the sites primary language.
446 * Or null if it's not known.
448 * @since 1.21
450 * @return string|null
452 public function getLanguageCode() {
453 return $this->languageCode;
457 * Sets language code of the sites primary language.
459 * @since 1.21
461 * @param string $languageCode
463 public function setLanguageCode( $languageCode ) {
464 if ( !Language::isValidCode( $languageCode ) ) {
465 throw new InvalidArgumentException( "$languageCode is not a valid language code." );
467 $this->languageCode = $languageCode;
471 * Returns the set internal identifier for the site.
473 * @since 1.21
475 * @return string|null
477 public function getInternalId() {
478 return $this->internalId;
482 * Sets the internal identifier for the site.
483 * This typically is a primary key in a db table.
485 * @since 1.21
487 * @param int|null $internalId
489 public function setInternalId( $internalId = null ) {
490 $this->internalId = $internalId;
494 * Adds a local identifier.
496 * @since 1.21
498 * @param string $type
499 * @param string $identifier
501 public function addLocalId( $type, $identifier ) {
502 if ( $this->localIds === false ) {
503 $this->localIds = [];
506 if ( !array_key_exists( $type, $this->localIds ) ) {
507 $this->localIds[$type] = [];
510 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
511 $this->localIds[$type][] = $identifier;
516 * Adds an interwiki id to the site.
518 * @since 1.21
520 * @param string $identifier
522 public function addInterwikiId( $identifier ) {
523 $this->addLocalId( self::ID_INTERWIKI, $identifier );
527 * Adds a navigation id to the site.
529 * @since 1.21
531 * @param string $identifier
533 public function addNavigationId( $identifier ) {
534 $this->addLocalId( self::ID_EQUIVALENT, $identifier );
538 * Returns the interwiki link identifiers that can be used for this site.
540 * @since 1.21
542 * @return string[]
544 public function getInterwikiIds() {
545 return array_key_exists( self::ID_INTERWIKI, $this->localIds )
546 ? $this->localIds[self::ID_INTERWIKI]
547 : [];
551 * Returns the equivalent link identifiers that can be used to make
552 * the site show up in interfaces such as the "language links" section.
554 * @since 1.21
556 * @return string[]
558 public function getNavigationIds() {
559 return array_key_exists( self::ID_EQUIVALENT, $this->localIds )
560 ? $this->localIds[self::ID_EQUIVALENT] :
565 * Returns all local ids
567 * @since 1.21
569 * @return array[]
571 public function getLocalIds() {
572 return $this->localIds;
576 * Sets the path used to construct links with.
577 * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
579 * @since 1.21
581 * @param string $pathType
582 * @param string $fullUrl
584 * @throws MWException
586 public function setPath( $pathType, $fullUrl ) {
587 if ( !is_string( $fullUrl ) ) {
588 throw new MWException( '$fullUrl needs to be a string' );
591 if ( !array_key_exists( 'paths', $this->extraData ) ) {
592 $this->extraData['paths'] = [];
595 $this->extraData['paths'][$pathType] = $fullUrl;
599 * Returns the path of the provided type or false if there is no such path.
601 * @since 1.21
603 * @param string $pathType
605 * @return string|null
607 public function getPath( $pathType ) {
608 $paths = $this->getAllPaths();
609 return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : null;
613 * Returns the paths as associative array.
614 * The keys are path types, the values are the path urls.
616 * @since 1.21
618 * @return string[]
620 public function getAllPaths() {
621 return array_key_exists( 'paths', $this->extraData ) ? $this->extraData['paths'] : [];
625 * Removes the path of the provided type if it's set.
627 * @since 1.21
629 * @param string $pathType
631 public function removePath( $pathType ) {
632 if ( array_key_exists( 'paths', $this->extraData ) ) {
633 unset( $this->extraData['paths'][$pathType] );
638 * @since 1.21
640 * @param string $siteType
642 * @return Site
644 public static function newForType( $siteType ) {
645 global $wgSiteTypes;
647 if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
648 return new $wgSiteTypes[$siteType]();
651 return new Site();
655 * @see Serializable::serialize
657 * @since 1.21
659 * @return string
661 public function serialize() {
662 $fields = [
663 'globalid' => $this->globalId,
664 'type' => $this->type,
665 'group' => $this->group,
666 'source' => $this->source,
667 'language' => $this->languageCode,
668 'localids' => $this->localIds,
669 'config' => $this->extraConfig,
670 'data' => $this->extraData,
671 'forward' => $this->forward,
672 'internalid' => $this->internalId,
676 return serialize( $fields );
680 * @see Serializable::unserialize
682 * @since 1.21
684 * @param string $serialized
686 public function unserialize( $serialized ) {
687 $fields = unserialize( $serialized );
689 $this->__construct( $fields['type'] );
691 $this->setGlobalId( $fields['globalid'] );
692 $this->setGroup( $fields['group'] );
693 $this->setSource( $fields['source'] );
694 $this->setLanguageCode( $fields['language'] );
695 $this->localIds = $fields['localids'];
696 $this->setExtraConfig( $fields['config'] );
697 $this->setExtraData( $fields['data'] );
698 $this->setForward( $fields['forward'] );
699 $this->setInternalId( $fields['internalid'] );