MediaWiki  REL1_21
Site.php
Go to the documentation of this file.
00001 <?php
00002 
00029 class Site implements Serializable {
00030 
00031         const TYPE_UNKNOWN = 'unknown';
00032         const TYPE_MEDIAWIKI = 'mediawiki';
00033 
00034         const GROUP_NONE = 'none';
00035 
00036         const ID_INTERWIKI = 'interwiki';
00037         const ID_EQUIVALENT = 'equivalent';
00038 
00039         const SOURCE_LOCAL = 'local';
00040 
00041         const PATH_LINK = 'link';
00042 
00050         const SERIAL_VERSION_ID = '2013-01-23';
00051 
00057         protected $globalId = null;
00058 
00064         protected $type = self::TYPE_UNKNOWN;
00065 
00071         protected $group = self::GROUP_NONE;
00072 
00078         protected $source = self::SOURCE_LOCAL;
00079 
00085         protected $languageCode = null;
00086 
00095         protected $localIds = array();
00096 
00102         protected $extraData = array();
00103 
00109         protected $extraConfig = array();
00110 
00116         protected $forward = false;
00117 
00123         protected $internalId = null;
00124 
00132         public function __construct( $type = self::TYPE_UNKNOWN ) {
00133                 $this->type = $type;
00134         }
00135 
00143         public function getGlobalId() {
00144                 return $this->globalId;
00145         }
00146 
00156         public function setGlobalId( $globalId ) {
00157                 if ( $globalId !== null && !is_string( $globalId ) ) {
00158                         throw new MWException( '$globalId needs to be string or null' );
00159                 }
00160 
00161                 $this->globalId = $globalId;
00162         }
00163 
00171         public function getType() {
00172                 return $this->type;
00173         }
00174 
00182         public function getGroup() {
00183                 return $this->group;
00184         }
00185 
00195         public function setGroup( $group ) {
00196                 if ( !is_string( $group ) ) {
00197                         throw new MWException( '$group needs to be a string' );
00198                 }
00199 
00200                 $this->group = $group;
00201         }
00202 
00210         public function getSource() {
00211                 return $this->source;
00212         }
00213 
00223         public function setSource( $source ) {
00224                 if ( !is_string( $source ) ) {
00225                         throw new MWException( '$source needs to be a string' );
00226                 }
00227 
00228                 $this->source = $source;
00229         }
00230 
00239         public function shouldForward() {
00240                 return $this->forward;
00241         }
00242 
00253         public function setForward( $shouldForward ) {
00254                 if ( !is_bool( $shouldForward ) ) {
00255                         throw new MWException( '$shouldForward needs to be a boolean' );
00256                 }
00257 
00258                 $this->forward = $shouldForward;
00259         }
00260 
00269         public function getDomain() {
00270                 $path = $this->getLinkPath();
00271 
00272                 if ( $path === null ) {
00273                         return null;
00274                 }
00275 
00276                 return parse_url( $path, PHP_URL_HOST );
00277         }
00278 
00287         public function getProtocol() {
00288                 $path = $this->getLinkPath();
00289 
00290                 if ( $path === null ) {
00291                         return '';
00292                 }
00293 
00294                 $protocol = parse_url( $path, PHP_URL_SCHEME );
00295 
00296                 // Malformed URL
00297                 if ( $protocol === false ) {
00298                         throw new MWException( "failed to parse URL '$path'" );
00299                 }
00300 
00301                 // No schema
00302                 if ( $protocol === null ) {
00303                         // Used for protocol relative URLs
00304                         $protocol = '';
00305                 }
00306 
00307                 return $protocol;
00308         }
00309 
00320         public function setLinkPath( $fullUrl ) {
00321                 $type = $this->getLinkPathType();
00322 
00323                 if ( $type === null ) {
00324                         throw new MWException( "This Site does not support link paths." );
00325                 }
00326 
00327                 $this->setPath( $type, $fullUrl );
00328         }
00329 
00337         public function getLinkPath() {
00338                 $type = $this->getLinkPathType();
00339                 return $type === null ? null: $this->getPath( $type );
00340         }
00341 
00353         public function getLinkPathType() {
00354                 return self::PATH_LINK;
00355         }
00356 
00372         public function getPageUrl( $pageName = false ) {
00373                 $url = $this->getLinkPath();
00374 
00375                 if ( $url === false ) {
00376                         return false;
00377                 }
00378 
00379                 if ( $pageName !== false ) {
00380                         $url = str_replace( '$1', rawurlencode( $pageName ), $url );
00381                 }
00382 
00383                 return $url;
00384         }
00385 
00398         public function normalizePageName( $pageName ) {
00399                 return $pageName;
00400         }
00401 
00409         public function getExtraData() {
00410                 return $this->extraData;
00411         }
00412 
00420         public function setExtraData( array $extraData ) {
00421                 $this->extraData = $extraData;
00422         }
00423 
00431         public function getExtraConfig() {
00432                 return $this->extraConfig;
00433         }
00434 
00442         public function setExtraConfig( array $extraConfig ) {
00443                 $this->extraConfig = $extraConfig;
00444         }
00445 
00454         public function getLanguageCode() {
00455                 return $this->languageCode;
00456         }
00457 
00465         public function setLanguageCode( $languageCode ) {
00466                 $this->languageCode = $languageCode;
00467         }
00468 
00476         public function getInternalId() {
00477                 return $this->internalId;
00478         }
00479 
00488         public function setInternalId( $internalId = null ) {
00489                 $this->internalId = $internalId;
00490         }
00491 
00500         public function addLocalId( $type, $identifier ) {
00501                 if ( $this->localIds === false ) {
00502                         $this->localIds = array();
00503                 }
00504 
00505                 if ( !array_key_exists( $type, $this->localIds ) ) {
00506                         $this->localIds[$type] = array();
00507                 }
00508 
00509                 if ( !in_array( $identifier, $this->localIds[$type] ) ) {
00510                         $this->localIds[$type][] = $identifier;
00511                 }
00512         }
00513 
00521         public function addInterwikiId( $identifier ) {
00522                 $this->addLocalId( self::ID_INTERWIKI, $identifier );
00523         }
00524 
00532         public function addNavigationId( $identifier ) {
00533                 $this->addLocalId( self::ID_EQUIVALENT, $identifier );
00534         }
00535 
00543         public function getInterwikiIds() {
00544                 return array_key_exists( self::ID_INTERWIKI, $this->localIds ) ? $this->localIds[self::ID_INTERWIKI] : array();
00545         }
00546 
00555         public function getNavigationIds() {
00556                 return array_key_exists( self::ID_EQUIVALENT, $this->localIds ) ? $this->localIds[self::ID_EQUIVALENT] : array();
00557         }
00558 
00566         public function getLocalIds() {
00567                 return $this->localIds;
00568         }
00569 
00581         public function setPath( $pathType, $fullUrl ) {
00582                 if ( !is_string( $fullUrl ) ) {
00583                         throw new MWException( '$fullUrl needs to be a string' );
00584                 }
00585 
00586                 if ( !array_key_exists( 'paths', $this->extraData ) ) {
00587                         $this->extraData['paths'] = array();
00588                 }
00589 
00590                 $this->extraData['paths'][$pathType] = $fullUrl;
00591         }
00592 
00602         public function getPath( $pathType ) {
00603                 $paths = $this->getAllPaths();
00604                 return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : null;
00605         }
00606 
00615         public function getAllPaths() {
00616                 return array_key_exists( 'paths', $this->extraData ) ? $this->extraData['paths'] : array();
00617         }
00618 
00626         public function removePath( $pathType ) {
00627                 if ( array_key_exists( 'paths', $this->extraData ) ) {
00628                         unset( $this->extraData['paths'][$pathType] );
00629                 }
00630         }
00631 
00639         public static function newForType( $siteType ) {
00640                 global $wgSiteTypes;
00641 
00642                 if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
00643                         return new $wgSiteTypes[$siteType]();
00644                 }
00645 
00646                 return new Site();
00647         }
00648 
00656         public function serialize() {
00657                 $fields = array(
00658                         'globalid' => $this->globalId,
00659                         'type' => $this->type,
00660                         'group' => $this->group,
00661                         'source' => $this->source,
00662                         'language' => $this->languageCode,
00663                         'localids' => $this->localIds,
00664                         'config' => $this->extraConfig,
00665                         'data' => $this->extraData,
00666                         'forward' => $this->forward,
00667                         'internalid' => $this->internalId,
00668 
00669                 );
00670 
00671                 return serialize( $fields );
00672         }
00673 
00681         public function unserialize( $serialized ) {
00682                 $fields = unserialize( $serialized );
00683 
00684                 $this->__construct( $fields['type'] );
00685 
00686                 $this->setGlobalId( $fields['globalid'] );
00687                 $this->setGroup( $fields['group'] );
00688                 $this->setSource( $fields['source'] );
00689                 $this->setLanguageCode( $fields['language'] );
00690                 $this->localIds = $fields['localids'];
00691                 $this->setExtraConfig( $fields['config'] );
00692                 $this->setExtraData( $fields['data'] );
00693                 $this->setForward( $fields['forward'] );
00694                 $this->setInternalId( $fields['internalid'] );
00695         }
00696 
00697 }
00698 
00702 class SiteObject extends Site {}