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 * @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';
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 * @throws MWException
155 * @return string|false
157 public function getProtocol() {
158 $path = $this->getLinkPath();
160 if ( $path === false ) {
164 $protocol = parse_url( $path, PHP_URL_SCHEME
);
167 if ( $protocol === false ) {
168 throw new MWException( "failed to parse URL $path" );
172 if ( $protocol === null ) {
173 // Used for protocol relative URLs
181 * Sets the path used to construct links with.
182 * @see Site::setLinkPath
184 * @param string $fullUrl
188 * @throws MWException
190 public function setLinkPath( $fullUrl ) {
191 $type = $this->getLinkPathType();
193 if ( $type === false ) {
194 throw new MWException( "This SiteObject does not support link paths." );
197 $this->setPath( $type, $fullUrl );
201 * Returns the path path used to construct links with or false if there is no such path.
203 * @see Site::getLinkPath
205 * @return string|false
207 public function getLinkPath() {
208 $type = $this->getLinkPathType();
209 return $type === false ?
false : $this->getPath( $type );
213 * @see Site::getLinkPathType
215 * Returns the main path type, that is the type of the path that should generally be used to construct links
216 * to the target site.
218 * This default implementation returns SiteObject::PATH_LINK as the default path type. Subclasses can override this
219 * to define a different default path type, or return false to disable site links.
223 * @return string|false
225 public function getLinkPathType() {
226 return self
::PATH_LINK
;
230 * @see Site::getPageUrl
232 * This implementation returns a URL constructed using the path returned by getLinkPath().
236 * @param bool|String $pageName
238 * @return string|false
240 public function getPageUrl( $pageName = false ) {
241 $url = $this->getLinkPath();
243 if ( $url === false ) {
247 if ( $pageName !== false ) {
248 $url = str_replace( '$1', rawurlencode( $pageName ), $url ) ;
255 * Returns $pageName without changes.
256 * Subclasses may override this to apply some kind of normalization.
258 * @see Site::normalizePageName
262 * @param string $pageName
266 public function normalizePageName( $pageName ) {
271 * Returns the value of a type specific field, or the value
272 * of the $default parameter in case it's not set.
276 * @param string $fieldName
277 * @param mixed $default
281 protected function getExtraData( $fieldName, $default = null ) {
282 $data = $this->getField( 'data', array() );
283 return array_key_exists( $fieldName,$data ) ?
$data[$fieldName] : $default;
287 * Sets the value of a type specific field.
290 * @param string $fieldName
291 * @param mixed $value
293 protected function setExtraData( $fieldName, $value = null ) {
294 $data = $this->getField( 'data', array() );
295 $data[$fieldName] = $value;
296 $this->setField( 'data', $data );
300 * @see Site::getLanguageCode
304 * @return string|false
306 public function getLanguageCode() {
307 return $this->getField( 'language', false );
311 * @see Site::setLanguageCode
315 * @param string $languageCode
317 public function setLanguageCode( $languageCode ) {
318 $this->setField( 'language', $languageCode );
322 * Returns the local identifiers of this site.
326 * @param string $type
330 protected function getLocalIds( $type ) {
331 if ( $this->localIds
=== false ) {
332 $this->loadLocalIds();
335 return array_key_exists( $type, $this->localIds
) ?
$this->localIds
[$type] : array();
339 * Loads the local ids for the site.
343 protected function loadLocalIds() {
344 $dbr = wfGetDB( $this->getTable()->getReadDb() );
353 'si_site' => $this->getId(),
358 $this->localIds
= array();
360 foreach ( $ids as $id ) {
361 $this->addLocalId( $id->si_type
, $id->si_key
);
366 * Adds a local identifier.
370 * @param string $type
371 * @param string $identifier
373 public function addLocalId( $type, $identifier ) {
374 if ( $this->localIds
=== false ) {
375 $this->localIds
= array();
378 if ( !array_key_exists( $type, $this->localIds
) ) {
379 $this->localIds
[$type] = array();
382 if ( !in_array( $identifier, $this->localIds
[$type] ) ) {
383 $this->localIds
[$type][] = $identifier;
388 * @see Site::addInterwikiId
392 * @param string $identifier
394 public function addInterwikiId( $identifier ) {
395 $this->addLocalId( 'interwiki', $identifier );
399 * @see Site::addNavigationId
403 * @param string $identifier
405 public function addNavigationId( $identifier ) {
406 $this->addLocalId( 'equivalent', $identifier );
410 * @see Site::getInterwikiIds
414 * @return array of string
416 public function getInterwikiIds() {
417 return $this->getLocalIds( 'interwiki' );
421 * @see Site::getNavigationIds
425 * @return array of string
427 public function getNavigationIds() {
428 return $this->getLocalIds( 'equivalent' );
432 * @see Site::getInternalId
438 public function getInternalId() {
439 return $this->getId();
448 * @param string|null $functionName
450 * @return boolean Success indicator
452 public function save( $functionName = null ) {
453 $dbw = $this->table
->getWriteDbConnection();
455 $trx = $dbw->trxLevel();
458 $dbw->begin( __METHOD__
);
461 $this->setField( 'protocol', $this->getProtocol() );
462 $this->setField( 'domain', strrev( $this->getDomain() ) . '.' );
464 $existedAlready = $this->hasIdField();
466 $success = parent
::save( $functionName );
468 if ( $success && $existedAlready ) {
471 array( 'si_site' => $this->getId() ),
476 if ( $success && $this->localIds
!== false ) {
477 foreach ( $this->localIds
as $type => $ids ) {
478 foreach ( $ids as $id ) {
482 'si_site' => $this->getId(),
493 $dbw->commit( __METHOD__
);
502 * @see ORMRow::onRemoved
504 protected function onRemoved() {
505 $dbw = $this->table
->getWriteDbConnection();
510 'si_site' => $this->getId()
523 * @param string $pathType
524 * @param string $fullUrl
526 public function setPath( $pathType, $fullUrl ) {
527 $paths = $this->getExtraData( 'paths', array() );
528 $paths[$pathType] = $fullUrl;
529 $this->setExtraData( 'paths', $paths );
533 * @see Sitres::getPath
537 * @param string $pathType
539 * @return string|false
541 public function getPath( $pathType ) {
542 $paths = $this->getExtraData( 'paths', array() );
543 return array_key_exists( $pathType, $paths ) ?
$paths[$pathType] : false;
547 * @see Sitres::getAll
551 * @return array of string
553 public function getAllPaths() {
554 return $this->getExtraData( 'paths', array() );
558 * @see Sitres::removePath
562 * @param string $pathType
564 public function removePath( $pathType ) {
565 $paths = $this->getExtraData( 'paths', array() );
566 unset( $paths[$pathType] );
567 $this->setExtraData( 'paths', $paths );