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
26 * @licence 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';
35 * Holds the local ids for this site.
36 * You can obtain them via @see getLocalIds
42 protected $localIds = false;
45 * @see Site::getGlobalId
51 public function getGlobalId() {
52 return $this->getField( 'global_key' );
56 * @see Site::setGlobalId
60 * @param string $globalId
62 public function setGlobalId( $globalId ) {
63 $this->setField( 'global_key', $globalId );
73 public function getType() {
74 return $this->getField( 'type' );
84 public function setType( $type ) {
85 $this->setField( 'type', $type );
95 public function getGroup() {
96 return $this->getField( 'group' );
100 * @see Site::setGroup
104 * @param string $group
106 public function setGroup( $group ) {
107 $this->setField( 'group', $group );
111 * @see Site::getSource
117 public function getSource() {
118 return $this->getField( 'source' );
122 * @see Site::setSource
126 * @param string $source
128 public function setSource( $source ) {
129 $this->setField( 'source', $source );
133 * @see Site::getDomain
137 * @return string|false
139 public function getDomain() {
140 $path = $this->getLinkPath();
142 if ( $path === false ) {
146 return parse_url( $path, PHP_URL_HOST
);
150 * @see Site::getProtocol
154 * @return string|false
156 public function getProtocol() {
157 $path = $this->getLinkPath();
159 if ( $path === false ) {
163 return parse_url( $path, PHP_URL_SCHEME
);
167 * Sets the path used to construct links with.
168 * @see Site::setLinkPath
170 * @param string $fullUrl
174 * @throws MWException
176 public function setLinkPath( $fullUrl ) {
177 $type = $this->getLinkPathType();
179 if ( $type === false ) {
180 throw new MWException( "This SiteObject does not support link paths." );
183 $this->setPath( $type, $fullUrl );
187 * Returns the path path used to construct links with or false if there is no such path.
189 * @see Site::getLinkPath
191 * @return string|false
193 public function getLinkPath() {
194 $type = $this->getLinkPathType();
195 return $type === false ?
false : $this->getPath( $type );
199 * @see Site::getLinkPathType
201 * Returns the main path type, that is the type of the path that should generally be used to construct links
202 * to the target site.
204 * This default implementation returns SiteObject::PATH_LINK as the default path type. Subclasses can override this
205 * to define a different default path type, or return false to disable site links.
209 * @return string|false
211 public function getLinkPathType() {
212 return self
::PATH_LINK
;
216 * @see Site::getPageUrl
218 * This implementation returns a URL constructed using the path returned by getLinkPath().
222 * @param bool|String $pageName
224 * @return string|false
226 public function getPageUrl( $pageName = false ) {
227 $url = $this->getLinkPath();
229 if ( $url === false ) {
233 if ( $pageName !== false ) {
234 $url = str_replace( '$1', rawurlencode( $pageName ), $url ) ;
241 * Returns $pageName without changes.
242 * Subclasses may override this to apply some kind of normalization.
244 * @see Site::normalizePageName
248 * @param string $pageName
252 public function normalizePageName( $pageName ) {
257 * Returns the value of a type specific field, or the value
258 * of the $default parameter in case it's not set.
262 * @param string $fieldName
263 * @param mixed $default
267 protected function getExtraData( $fieldName, $default = null ) {
268 $data = $this->getField( 'data', array() );
269 return array_key_exists( $fieldName,$data ) ?
$data[$fieldName] : $default;
273 * Sets the value of a type specific field.
276 * @param string $fieldName
277 * @param mixed $value
279 protected function setExtraData( $fieldName, $value = null ) {
280 $data = $this->getField( 'data', array() );
281 $data[$fieldName] = $value;
282 $this->setField( 'data', $data );
286 * @see Site::getLanguageCode
290 * @return string|false
292 public function getLanguageCode() {
293 return $this->getField( 'language', false );
297 * @see Site::setLanguageCode
301 * @param string $languageCode
303 public function setLanguageCode( $languageCode ) {
304 $this->setField( 'language', $languageCode );
308 * Returns the local identifiers of this site.
312 * @param string $type
316 protected function getLocalIds( $type ) {
317 if ( $this->localIds
=== false ) {
318 $this->loadLocalIds();
321 return array_key_exists( $type, $this->localIds
) ?
$this->localIds
[$type] : array();
325 * Loads the local ids for the site.
329 protected function loadLocalIds() {
330 $dbr = wfGetDB( $this->getTable()->getReadDb() );
339 'si_site' => $this->getId(),
344 $this->localIds
= array();
346 foreach ( $ids as $id ) {
347 $this->addLocalId( $id->si_type
, $id->si_key
);
352 * Adds a local identifier.
356 * @param string $type
357 * @param string $identifier
359 public function addLocalId( $type, $identifier ) {
360 if ( $this->localIds
=== false ) {
361 $this->localIds
= array();
364 if ( !array_key_exists( $type, $this->localIds
) ) {
365 $this->localIds
[$type] = array();
368 if ( !in_array( $identifier, $this->localIds
[$type] ) ) {
369 $this->localIds
[$type][] = $identifier;
374 * @see Site::addInterwikiId
378 * @param string $identifier
380 public function addInterwikiId( $identifier ) {
381 $this->addLocalId( 'interwiki', $identifier );
385 * @see Site::addNavigationId
389 * @param string $identifier
391 public function addNavigationId( $identifier ) {
392 $this->addLocalId( 'equivalent', $identifier );
396 * @see Site::getInterwikiIds
400 * @return array of string
402 public function getInterwikiIds() {
403 return $this->getLocalIds( 'interwiki' );
407 * @see Site::getNavigationIds
411 * @return array of string
413 public function getNavigationIds() {
414 return $this->getLocalIds( 'equivalent' );
418 * @see Site::getInternalId
424 public function getInternalId() {
425 return $this->getId();
434 * @param string|null $functionName
436 * @return boolean Success indicator
438 public function save( $functionName = null ) {
439 $dbw = wfGetDB( DB_MASTER
);
441 $trx = $dbw->trxLevel();
444 $dbw->begin( __METHOD__
);
447 $this->setField( 'protocol', $this->getProtocol() );
448 $this->setField( 'domain', strrev( $this->getDomain() ) . '.' );
450 $existedAlready = $this->hasIdField();
452 $success = parent
::save( $functionName );
454 if ( $success && $existedAlready ) {
457 array( 'si_site' => $this->getId() ),
462 if ( $success && $this->localIds
!== false ) {
463 foreach ( $this->localIds
as $type => $ids ) {
464 foreach ( $ids as $id ) {
468 'si_site' => $this->getId(),
479 $dbw->commit( __METHOD__
);
490 * @param string $pathType
491 * @param string $fullUrl
493 public function setPath( $pathType, $fullUrl ) {
494 $paths = $this->getExtraData( 'paths', array() );
495 $paths[$pathType] = $fullUrl;
496 $this->setExtraData( 'paths', $paths );
500 * @see Sitres::getPath
504 * @param string $pathType
506 * @return string|false
508 public function getPath( $pathType ) {
509 $paths = $this->getExtraData( 'paths', array() );
510 return array_key_exists( $pathType, $paths ) ?
$paths[$pathType] : false;
514 * @see Sitres::getAll
518 * @return array of string
520 public function getAllPaths() {
521 return $this->getExtraData( 'paths', array() );
525 * @see Sitres::removePath
529 * @param string $pathType
531 public function removePath( $pathType ) {
532 $paths = $this->getExtraData( 'paths', array() );
533 unset( $paths[$pathType] );
534 $this->setExtraData( 'paths', $paths );