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 * @return string|false
156 public function getProtocol() {
157 $path = $this->getLinkPath();
159 if ( $path === false ) {
163 $protocol = parse_url( $path, PHP_URL_SCHEME
);
165 if ( $protocol === false ) { // malformed URL
166 throw new MWException( "failed to parse URL $path" );
169 if ( $protocol === null ) { // no schema
170 $protocol = ''; // used for protocol relative URLs
177 * Sets the path used to construct links with.
178 * @see Site::setLinkPath
180 * @param string $fullUrl
184 * @throws MWException
186 public function setLinkPath( $fullUrl ) {
187 $type = $this->getLinkPathType();
189 if ( $type === false ) {
190 throw new MWException( "This SiteObject does not support link paths." );
193 $this->setPath( $type, $fullUrl );
197 * Returns the path path used to construct links with or false if there is no such path.
199 * @see Site::getLinkPath
201 * @return string|false
203 public function getLinkPath() {
204 $type = $this->getLinkPathType();
205 return $type === false ?
false : $this->getPath( $type );
209 * @see Site::getLinkPathType
211 * Returns the main path type, that is the type of the path that should generally be used to construct links
212 * to the target site.
214 * This default implementation returns SiteObject::PATH_LINK as the default path type. Subclasses can override this
215 * to define a different default path type, or return false to disable site links.
219 * @return string|false
221 public function getLinkPathType() {
222 return self
::PATH_LINK
;
226 * @see Site::getPageUrl
228 * This implementation returns a URL constructed using the path returned by getLinkPath().
232 * @param bool|String $pageName
234 * @return string|false
236 public function getPageUrl( $pageName = false ) {
237 $url = $this->getLinkPath();
239 if ( $url === false ) {
243 if ( $pageName !== false ) {
244 $url = str_replace( '$1', rawurlencode( $pageName ), $url ) ;
251 * Returns $pageName without changes.
252 * Subclasses may override this to apply some kind of normalization.
254 * @see Site::normalizePageName
258 * @param string $pageName
262 public function normalizePageName( $pageName ) {
267 * Returns the value of a type specific field, or the value
268 * of the $default parameter in case it's not set.
272 * @param string $fieldName
273 * @param mixed $default
277 protected function getExtraData( $fieldName, $default = null ) {
278 $data = $this->getField( 'data', array() );
279 return array_key_exists( $fieldName,$data ) ?
$data[$fieldName] : $default;
283 * Sets the value of a type specific field.
286 * @param string $fieldName
287 * @param mixed $value
289 protected function setExtraData( $fieldName, $value = null ) {
290 $data = $this->getField( 'data', array() );
291 $data[$fieldName] = $value;
292 $this->setField( 'data', $data );
296 * @see Site::getLanguageCode
300 * @return string|false
302 public function getLanguageCode() {
303 return $this->getField( 'language', false );
307 * @see Site::setLanguageCode
311 * @param string $languageCode
313 public function setLanguageCode( $languageCode ) {
314 $this->setField( 'language', $languageCode );
318 * Returns the local identifiers of this site.
322 * @param string $type
326 protected function getLocalIds( $type ) {
327 if ( $this->localIds
=== false ) {
328 $this->loadLocalIds();
331 return array_key_exists( $type, $this->localIds
) ?
$this->localIds
[$type] : array();
335 * Loads the local ids for the site.
339 protected function loadLocalIds() {
340 $dbr = wfGetDB( $this->getTable()->getReadDb() );
349 'si_site' => $this->getId(),
354 $this->localIds
= array();
356 foreach ( $ids as $id ) {
357 $this->addLocalId( $id->si_type
, $id->si_key
);
362 * Adds a local identifier.
366 * @param string $type
367 * @param string $identifier
369 public function addLocalId( $type, $identifier ) {
370 if ( $this->localIds
=== false ) {
371 $this->localIds
= array();
374 if ( !array_key_exists( $type, $this->localIds
) ) {
375 $this->localIds
[$type] = array();
378 if ( !in_array( $identifier, $this->localIds
[$type] ) ) {
379 $this->localIds
[$type][] = $identifier;
384 * @see Site::addInterwikiId
388 * @param string $identifier
390 public function addInterwikiId( $identifier ) {
391 $this->addLocalId( 'interwiki', $identifier );
395 * @see Site::addNavigationId
399 * @param string $identifier
401 public function addNavigationId( $identifier ) {
402 $this->addLocalId( 'equivalent', $identifier );
406 * @see Site::getInterwikiIds
410 * @return array of string
412 public function getInterwikiIds() {
413 return $this->getLocalIds( 'interwiki' );
417 * @see Site::getNavigationIds
421 * @return array of string
423 public function getNavigationIds() {
424 return $this->getLocalIds( 'equivalent' );
428 * @see Site::getInternalId
434 public function getInternalId() {
435 return $this->getId();
444 * @param string|null $functionName
446 * @return boolean Success indicator
448 public function save( $functionName = null ) {
449 $dbw = wfGetDB( DB_MASTER
);
451 $trx = $dbw->trxLevel();
454 $dbw->begin( __METHOD__
);
457 $this->setField( 'protocol', $this->getProtocol() );
458 $this->setField( 'domain', strrev( $this->getDomain() ) . '.' );
460 $existedAlready = $this->hasIdField();
462 $success = parent
::save( $functionName );
464 if ( $success && $existedAlready ) {
467 array( 'si_site' => $this->getId() ),
472 if ( $success && $this->localIds
!== false ) {
473 foreach ( $this->localIds
as $type => $ids ) {
474 foreach ( $ids as $id ) {
478 'si_site' => $this->getId(),
489 $dbw->commit( __METHOD__
);
500 * @param string $pathType
501 * @param string $fullUrl
503 public function setPath( $pathType, $fullUrl ) {
504 $paths = $this->getExtraData( 'paths', array() );
505 $paths[$pathType] = $fullUrl;
506 $this->setExtraData( 'paths', $paths );
510 * @see Sitres::getPath
514 * @param string $pathType
516 * @return string|false
518 public function getPath( $pathType ) {
519 $paths = $this->getExtraData( 'paths', array() );
520 return array_key_exists( $pathType, $paths ) ?
$paths[$pathType] : false;
524 * @see Sitres::getAll
528 * @return array of string
530 public function getAllPaths() {
531 return $this->getExtraData( 'paths', array() );
535 * @see Sitres::removePath
539 * @param string $pathType
541 public function removePath( $pathType ) {
542 $paths = $this->getExtraData( 'paths', array() );
543 unset( $paths[$pathType] );
544 $this->setExtraData( 'paths', $paths );