Merge "Add missing wfProfileOut()"
[mediawiki.git] / includes / site / SiteObject.php
blob0c6aeb39b857626ded0be3755c2177ec64d90471
1 <?php
3 /**
4 * Class representing 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 >
28 * @author Daniel Werner
30 class SiteObject extends ORMRow implements Site {
32 const PATH_LINK = 'link';
34 /**
35 * Holds the local ids for this site.
36 * You can obtain them via @see getLocalIds
38 * @since 1.21
40 * @var array|false
42 protected $localIds = false;
44 /**
45 * @see Site::getGlobalId
47 * @since 1.21
49 * @return string
51 public function getGlobalId() {
52 return $this->getField( 'global_key' );
55 /**
56 * @see Site::setGlobalId
58 * @since 1.21
60 * @param string $globalId
62 public function setGlobalId( $globalId ) {
63 $this->setField( 'global_key', $globalId );
66 /**
67 * @see Site::getType
69 * @since 1.21
71 * @return string
73 public function getType() {
74 return $this->getField( 'type' );
77 /**
78 * @see Site::setType
80 * @since 1.21
82 * @param string $type
84 public function setType( $type ) {
85 $this->setField( 'type', $type );
88 /**
89 * @see Site::getGroup
91 * @since 1.21
93 * @return string
95 public function getGroup() {
96 return $this->getField( 'group' );
99 /**
100 * @see Site::setGroup
102 * @since 1.21
104 * @param string $group
106 public function setGroup( $group ) {
107 $this->setField( 'group', $group );
111 * @see Site::getSource
113 * @since 1.21
115 * @return string
117 public function getSource() {
118 return $this->getField( 'source' );
122 * @see Site::setSource
124 * @since 1.21
126 * @param string $source
128 public function setSource( $source ) {
129 $this->setField( 'source', $source );
133 * @see Site::getDomain
135 * @since 1.21
137 * @return string|false
139 public function getDomain() {
140 $path = $this->getLinkPath();
142 if ( $path === false ) {
143 return false;
146 return parse_url( $path, PHP_URL_HOST );
150 * @see Site::getProtocol
152 * @since 1.21
154 * @return string|false
156 public function getProtocol() {
157 $path = $this->getLinkPath();
159 if ( $path === false ) {
160 return '';
163 $protocol = parse_url( $path, PHP_URL_SCHEME );
165 // Malformed URL
166 if ( $protocol === false ) {
167 throw new MWException( "failed to parse URL $path" );
170 // No schema
171 if ( $protocol === null ) {
172 // Used for protocol relative URLs
173 $protocol = '';
176 return $protocol;
180 * Sets the path used to construct links with.
181 * @see Site::setLinkPath
183 * @param string $fullUrl
185 * @since 1.21
187 * @throws MWException
189 public function setLinkPath( $fullUrl ) {
190 $type = $this->getLinkPathType();
192 if ( $type === false ) {
193 throw new MWException( "This SiteObject does not support link paths." );
196 $this->setPath( $type, $fullUrl );
200 * Returns the path path used to construct links with or false if there is no such path.
202 * @see Site::getLinkPath
204 * @return string|false
206 public function getLinkPath() {
207 $type = $this->getLinkPathType();
208 return $type === false ? false : $this->getPath( $type );
212 * @see Site::getLinkPathType
214 * Returns the main path type, that is the type of the path that should generally be used to construct links
215 * to the target site.
217 * This default implementation returns SiteObject::PATH_LINK as the default path type. Subclasses can override this
218 * to define a different default path type, or return false to disable site links.
220 * @since 1.21
222 * @return string|false
224 public function getLinkPathType() {
225 return self::PATH_LINK;
229 * @see Site::getPageUrl
231 * This implementation returns a URL constructed using the path returned by getLinkPath().
233 * @since 1.21
235 * @param bool|String $pageName
237 * @return string|false
239 public function getPageUrl( $pageName = false ) {
240 $url = $this->getLinkPath();
242 if ( $url === false ) {
243 return false;
246 if ( $pageName !== false ) {
247 $url = str_replace( '$1', rawurlencode( $pageName ), $url ) ;
250 return $url;
254 * Returns $pageName without changes.
255 * Subclasses may override this to apply some kind of normalization.
257 * @see Site::normalizePageName
259 * @since 1.21
261 * @param string $pageName
263 * @return string
265 public function normalizePageName( $pageName ) {
266 return $pageName;
270 * Returns the value of a type specific field, or the value
271 * of the $default parameter in case it's not set.
273 * @since 1.21
275 * @param string $fieldName
276 * @param mixed $default
278 * @return array
280 protected function getExtraData( $fieldName, $default = null ) {
281 $data = $this->getField( 'data', array() );
282 return array_key_exists( $fieldName,$data ) ? $data[$fieldName] : $default;
286 * Sets the value of a type specific field.
287 * @since 1.21
289 * @param string $fieldName
290 * @param mixed $value
292 protected function setExtraData( $fieldName, $value = null ) {
293 $data = $this->getField( 'data', array() );
294 $data[$fieldName] = $value;
295 $this->setField( 'data', $data );
299 * @see Site::getLanguageCode
301 * @since 1.21
303 * @return string|false
305 public function getLanguageCode() {
306 return $this->getField( 'language', false );
310 * @see Site::setLanguageCode
312 * @since 1.21
314 * @param string $languageCode
316 public function setLanguageCode( $languageCode ) {
317 $this->setField( 'language', $languageCode );
321 * Returns the local identifiers of this site.
323 * @since 1.21
325 * @param string $type
327 * @return array
329 protected function getLocalIds( $type ) {
330 if ( $this->localIds === false ) {
331 $this->loadLocalIds();
334 return array_key_exists( $type, $this->localIds ) ? $this->localIds[$type] : array();
338 * Loads the local ids for the site.
340 * @since 1.21
342 protected function loadLocalIds() {
343 $dbr = wfGetDB( $this->getTable()->getReadDb() );
345 $ids = $dbr->select(
346 'site_identifiers',
347 array(
348 'si_type',
349 'si_key',
351 array(
352 'si_site' => $this->getId(),
354 __METHOD__
357 $this->localIds = array();
359 foreach ( $ids as $id ) {
360 $this->addLocalId( $id->si_type, $id->si_key );
365 * Adds a local identifier.
367 * @since 1.21
369 * @param string $type
370 * @param string $identifier
372 public function addLocalId( $type, $identifier ) {
373 if ( $this->localIds === false ) {
374 $this->localIds = array();
377 if ( !array_key_exists( $type, $this->localIds ) ) {
378 $this->localIds[$type] = array();
381 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
382 $this->localIds[$type][] = $identifier;
387 * @see Site::addInterwikiId
389 * @since 1.21
391 * @param string $identifier
393 public function addInterwikiId( $identifier ) {
394 $this->addLocalId( 'interwiki', $identifier );
398 * @see Site::addNavigationId
400 * @since 1.21
402 * @param string $identifier
404 public function addNavigationId( $identifier ) {
405 $this->addLocalId( 'equivalent', $identifier );
409 * @see Site::getInterwikiIds
411 * @since 1.21
413 * @return array of string
415 public function getInterwikiIds() {
416 return $this->getLocalIds( 'interwiki' );
420 * @see Site::getNavigationIds
422 * @since 1.21
424 * @return array of string
426 public function getNavigationIds() {
427 return $this->getLocalIds( 'equivalent' );
431 * @see Site::getInternalId
433 * @since 1.21
435 * @return integer
437 public function getInternalId() {
438 return $this->getId();
442 * @see ORMRow::save
443 * @see Site::save
445 * @since 1.21
447 * @param string|null $functionName
449 * @return boolean Success indicator
451 public function save( $functionName = null ) {
452 $dbw = wfGetDB( DB_MASTER );
454 $trx = $dbw->trxLevel();
456 if ( $trx == 0 ) {
457 $dbw->begin( __METHOD__ );
460 $this->setField( 'protocol', $this->getProtocol() );
461 $this->setField( 'domain', strrev( $this->getDomain() ) . '.' );
463 $existedAlready = $this->hasIdField();
465 $success = parent::save( $functionName );
467 if ( $success && $existedAlready ) {
468 $dbw->delete(
469 'site_identifiers',
470 array( 'si_site' => $this->getId() ),
471 __METHOD__
475 if ( $success && $this->localIds !== false ) {
476 foreach ( $this->localIds as $type => $ids ) {
477 foreach ( $ids as $id ) {
478 $dbw->insert(
479 'site_identifiers',
480 array(
481 'si_site' => $this->getId(),
482 'si_type' => $type,
483 'si_key' => $id,
485 __METHOD__
491 if ( $trx == 0 ) {
492 $dbw->commit( __METHOD__ );
495 return $success;
499 * @see Site::setPath
501 * @since 1.21
503 * @param string $pathType
504 * @param string $fullUrl
506 public function setPath( $pathType, $fullUrl ) {
507 $paths = $this->getExtraData( 'paths', array() );
508 $paths[$pathType] = $fullUrl;
509 $this->setExtraData( 'paths', $paths );
513 * @see Sitres::getPath
515 * @since 1.21
517 * @param string $pathType
519 * @return string|false
521 public function getPath( $pathType ) {
522 $paths = $this->getExtraData( 'paths', array() );
523 return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : false;
527 * @see Sitres::getAll
529 * @since 1.21
531 * @return array of string
533 public function getAllPaths() {
534 return $this->getExtraData( 'paths', array() );
538 * @see Sitres::removePath
540 * @since 1.21
542 * @param string $pathType
544 public function removePath( $pathType ) {
545 $paths = $this->getExtraData( 'paths', array() );
546 unset( $paths[$pathType] );
547 $this->setExtraData( 'paths', $paths );