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 >
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';
48 protected $globalId = null;
55 protected $type = self
::TYPE_UNKNOWN
;
62 protected $group = self
::GROUP_NONE
;
69 protected $source = self
::SOURCE_LOCAL
;
76 protected $languageCode = null;
79 * Holds the local ids for this site.
80 * local id type => [ ids for this type (strings) ]
86 protected $localIds = array();
93 protected $extraData = array();
100 protected $extraConfig = array();
107 protected $forward = false;
114 protected $internalId = null;
121 * @param string $type
123 public function __construct( $type = self
::TYPE_UNKNOWN
) {
128 * Returns the global site identifier (ie enwiktionary).
132 * @return string|null
134 public function getGlobalId() {
135 return $this->globalId
;
139 * Sets the global site identifier (ie enwiktionary).
143 * @param string|null $globalId
145 * @throws MWException
147 public function setGlobalId( $globalId ) {
148 if ( $globalId !== null && !is_string( $globalId ) ) {
149 throw new MWException( '$globalId needs to be string or null' );
152 $this->globalId
= $globalId;
156 * Returns the type of the site (ie mediawiki).
162 public function getType() {
167 * Gets the type of the site (ie wikipedia).
173 public function getGroup() {
178 * Sets the type of the site (ie wikipedia).
182 * @param string $group
184 * @throws MWException
186 public function setGroup( $group ) {
187 if ( !is_string( $group ) ) {
188 throw new MWException( '$group needs to be a string' );
191 $this->group
= $group;
195 * Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
201 public function getSource() {
202 return $this->source
;
206 * Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
210 * @param string $source
212 * @throws MWException
214 public function setSource( $source ) {
215 if ( !is_string( $source ) ) {
216 throw new MWException( '$source needs to be a string' );
219 $this->source
= $source;
223 * Gets if site.tld/path/key:pageTitle should forward users to the page on
224 * the actual site, where "key" is the local identifier.
230 public function shouldForward() {
231 return $this->forward
;
235 * Sets if site.tld/path/key:pageTitle should forward users to the page on
236 * the actual site, where "key" is the local identifier.
240 * @param boolean $shouldForward
242 * @throws MWException
244 public function setForward( $shouldForward ) {
245 if ( !is_bool( $shouldForward ) ) {
246 throw new MWException( '$shouldForward needs to be a boolean' );
249 $this->forward
= $shouldForward;
253 * Returns the domain of the site, ie en.wikipedia.org
254 * Or false if it's not known.
258 * @return string|null
260 public function getDomain() {
261 $path = $this->getLinkPath();
263 if ( $path === null ) {
267 return parse_url( $path, PHP_URL_HOST
);
271 * Returns the protocol of the site.
275 * @throws MWException
278 public function getProtocol() {
279 $path = $this->getLinkPath();
281 if ( $path === null ) {
285 $protocol = parse_url( $path, PHP_URL_SCHEME
);
288 if ( $protocol === false ) {
289 throw new MWException( "failed to parse URL '$path'" );
293 if ( $protocol === null ) {
294 // Used for protocol relative URLs
302 * Sets the path used to construct links with.
303 * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
305 * @param string $fullUrl
309 * @throws MWException
311 public function setLinkPath( $fullUrl ) {
312 $type = $this->getLinkPathType();
314 if ( $type === null ) {
315 throw new MWException( "This Site does not support link paths." );
318 $this->setPath( $type, $fullUrl );
322 * Returns the path used to construct links with or false if there is no such path.
324 * Shall be equivalent to getPath( getLinkPathType() ).
326 * @return string|null
328 public function getLinkPath() {
329 $type = $this->getLinkPathType();
330 return $type === null ?
null: $this->getPath( $type );
334 * Returns the main path type, that is the type of the path that should generally be used to construct links
335 * to the target site.
337 * This default implementation returns Site::PATH_LINK as the default path type. Subclasses can override this
338 * to define a different default path type, or return false to disable site links.
342 * @return string|null
344 public function getLinkPathType() {
345 return self
::PATH_LINK
;
349 * Returns the full URL for the given page on the site.
350 * Or false if the needed information is not known.
352 * This generated URL is usually based upon the path returned by getLinkPath(),
353 * but this is not a requirement.
355 * This implementation returns a URL constructed using the path returned by getLinkPath().
359 * @param bool|String $pageName
361 * @return string|boolean false
363 public function getPageUrl( $pageName = false ) {
364 $url = $this->getLinkPath();
366 if ( $url === false ) {
370 if ( $pageName !== false ) {
371 $url = str_replace( '$1', rawurlencode( $pageName ), $url ) ;
378 * Returns $pageName without changes.
379 * Subclasses may override this to apply some kind of normalization.
381 * @see Site::normalizePageName
385 * @param string $pageName
389 public function normalizePageName( $pageName ) {
394 * Returns the type specific fields.
400 public function getExtraData() {
401 return $this->extraData
;
405 * Sets the type specific fields.
409 * @param array $extraData
411 public function setExtraData( array $extraData ) {
412 $this->extraData
= $extraData;
416 * Returns the type specific config.
422 public function getExtraConfig() {
423 return $this->extraConfig
;
427 * Sets the type specific config.
431 * @param array $extraConfig
433 public function setExtraConfig( array $extraConfig ) {
434 $this->extraConfig
= $extraConfig;
438 * Returns language code of the sites primary language.
439 * Or null if it's not known.
443 * @return string|null
445 public function getLanguageCode() {
446 return $this->languageCode
;
450 * Sets language code of the sites primary language.
454 * @param string $languageCode
456 public function setLanguageCode( $languageCode ) {
457 $this->languageCode
= $languageCode;
461 * Returns the set internal identifier for the site.
465 * @return string|null
467 public function getInternalId() {
468 return $this->internalId
;
472 * Sets the internal identifier for the site.
473 * This typically is a primary key in a db table.
477 * @param int|null $internalId
479 public function setInternalId( $internalId = null ) {
480 $this->internalId
= $internalId;
484 * Adds a local identifier.
488 * @param string $type
489 * @param string $identifier
491 public function addLocalId( $type, $identifier ) {
492 if ( $this->localIds
=== false ) {
493 $this->localIds
= array();
496 if ( !array_key_exists( $type, $this->localIds
) ) {
497 $this->localIds
[$type] = array();
500 if ( !in_array( $identifier, $this->localIds
[$type] ) ) {
501 $this->localIds
[$type][] = $identifier;
506 * Adds an interwiki id to the site.
510 * @param string $identifier
512 public function addInterwikiId( $identifier ) {
513 $this->addLocalId( self
::ID_INTERWIKI
, $identifier );
517 * Adds a navigation id to the site.
521 * @param string $identifier
523 public function addNavigationId( $identifier ) {
524 $this->addLocalId( self
::ID_EQUIVALENT
, $identifier );
528 * Returns the interwiki link identifiers that can be used for this site.
534 public function getInterwikiIds() {
535 return array_key_exists( self
::ID_INTERWIKI
, $this->localIds
) ?
$this->localIds
[self
::ID_INTERWIKI
] : array();
539 * Returns the equivalent link identifiers that can be used to make
540 * the site show up in interfaces such as the "language links" section.
546 public function getNavigationIds() {
547 return array_key_exists( self
::ID_EQUIVALENT
, $this->localIds
) ?
$this->localIds
[self
::ID_EQUIVALENT
] : array();
551 * Returns all local ids
557 public function getLocalIds() {
558 return $this->localIds
;
562 * Sets the path used to construct links with.
563 * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
567 * @param string $pathType
568 * @param string $fullUrl
570 * @throws MWException
572 public function setPath( $pathType, $fullUrl ) {
573 if ( !is_string( $fullUrl ) ) {
574 throw new MWException( '$fullUrl needs to be a string' );
577 if ( !array_key_exists( 'paths', $this->extraData
) ) {
578 $this->extraData
['paths'] = array();
581 $this->extraData
['paths'][$pathType] = $fullUrl;
585 * Returns the path of the provided type or false if there is no such path.
589 * @param string $pathType
591 * @return string|null
593 public function getPath( $pathType ) {
594 $paths = $this->getAllPaths();
595 return array_key_exists( $pathType, $paths ) ?
$paths[$pathType] : null;
599 * Returns the paths as associative array.
600 * The keys are path types, the values are the path urls.
606 public function getAllPaths() {
607 return array_key_exists( 'paths', $this->extraData
) ?
$this->extraData
['paths'] : array();
611 * Removes the path of the provided type if it's set.
615 * @param string $pathType
617 public function removePath( $pathType ) {
618 if ( array_key_exists( 'paths', $this->extraData
) ) {
619 unset( $this->extraData
['paths'][$pathType] );
628 * @param string $siteType
632 public static function newForType( $siteType ) {
635 if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
636 return new $wgSiteTypes[$siteType]();
647 class SiteObject
extends Site
{}