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
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';
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';
57 protected $globalId = null;
64 protected $type = self
::TYPE_UNKNOWN
;
71 protected $group = self
::GROUP_NONE
;
78 protected $source = self
::SOURCE_LOCAL
;
85 protected $languageCode = null;
88 * Holds the local ids for this site.
89 * local id type => [ ids for this type (strings) ]
95 protected $localIds = array();
102 protected $extraData = array();
109 protected $extraConfig = array();
116 protected $forward = false;
123 protected $internalId = null;
130 * @param string $type
132 public function __construct( $type = self
::TYPE_UNKNOWN
) {
137 * Returns the global site identifier (ie enwiktionary).
141 * @return string|null
143 public function getGlobalId() {
144 return $this->globalId
;
148 * Sets the global site identifier (ie enwiktionary).
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).
171 public function getType() {
176 * Gets the type of the site (ie wikipedia).
182 public function getGroup() {
187 * Sets the type of the site (ie wikipedia).
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').
210 public function getSource() {
211 return $this->source
;
215 * Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
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.
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.
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.
267 * @return string|null
269 public function getDomain() {
270 $path = $this->getLinkPath();
272 if ( $path === null ) {
276 return parse_url( $path, PHP_URL_HOST
);
280 * Returns the protocol of the site.
284 * @throws MWException
287 public function getProtocol() {
288 $path = $this->getLinkPath();
290 if ( $path === null ) {
294 $protocol = parse_url( $path, PHP_URL_SCHEME
);
297 if ( $protocol === false ) {
298 throw new MWException( "failed to parse URL '$path'" );
302 if ( $protocol === null ) {
303 // Used for protocol relative URLs
311 * Sets the path used to construct links with.
312 * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
314 * @param string $fullUrl
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.
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().
368 * @param bool|String $pageName
370 * @return string|boolean false
372 public function getPageUrl( $pageName = false ) {
373 $url = $this->getLinkPath();
375 if ( $url === false ) {
379 if ( $pageName !== false ) {
380 $url = str_replace( '$1', rawurlencode( $pageName ), $url );
387 * Returns $pageName without changes.
388 * Subclasses may override this to apply some kind of normalization.
390 * @see Site::normalizePageName
394 * @param string $pageName
398 public function normalizePageName( $pageName ) {
403 * Returns the type specific fields.
409 public function getExtraData() {
410 return $this->extraData
;
414 * Sets the type specific fields.
418 * @param array $extraData
420 public function setExtraData( array $extraData ) {
421 $this->extraData
= $extraData;
425 * Returns the type specific config.
431 public function getExtraConfig() {
432 return $this->extraConfig
;
436 * Sets the type specific config.
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.
452 * @return string|null
454 public function getLanguageCode() {
455 return $this->languageCode
;
459 * Sets language code of the sites primary language.
463 * @param string $languageCode
465 public function setLanguageCode( $languageCode ) {
466 $this->languageCode
= $languageCode;
470 * Returns the set internal identifier for the site.
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.
486 * @param int|null $internalId
488 public function setInternalId( $internalId = null ) {
489 $this->internalId
= $internalId;
493 * Adds a local identifier.
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.
519 * @param string $identifier
521 public function addInterwikiId( $identifier ) {
522 $this->addLocalId( self
::ID_INTERWIKI
, $identifier );
526 * Adds a navigation id to the site.
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.
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.
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
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 ).
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.
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.
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.
624 * @param string $pathType
626 public function removePath( $pathType ) {
627 if ( array_key_exists( 'paths', $this->extraData
) ) {
628 unset( $this->extraData
['paths'][$pathType] );
635 * @param string $siteType
639 public static function newForType( $siteType ) {
642 if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
643 return new $wgSiteTypes[$siteType]();
650 * @see Serializable::serialize
656 public function serialize() {
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
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'] );
702 class SiteObject
extends Site
{}