MediaWiki
REL1_24
|
00001 <?php 00002 00029 class Site implements Serializable { 00030 const TYPE_UNKNOWN = 'unknown'; 00031 const TYPE_MEDIAWIKI = 'mediawiki'; 00032 00033 const GROUP_NONE = 'none'; 00034 00035 const ID_INTERWIKI = 'interwiki'; 00036 const ID_EQUIVALENT = 'equivalent'; 00037 00038 const SOURCE_LOCAL = 'local'; 00039 00040 const PATH_LINK = 'link'; 00041 00049 const SERIAL_VERSION_ID = '2013-01-23'; 00050 00056 protected $globalId = null; 00057 00063 protected $type = self::TYPE_UNKNOWN; 00064 00070 protected $group = self::GROUP_NONE; 00071 00077 protected $source = self::SOURCE_LOCAL; 00078 00084 protected $languageCode = null; 00085 00094 protected $localIds = array(); 00095 00101 protected $extraData = array(); 00102 00108 protected $extraConfig = array(); 00109 00115 protected $forward = false; 00116 00122 protected $internalId = null; 00123 00131 public function __construct( $type = self::TYPE_UNKNOWN ) { 00132 $this->type = $type; 00133 } 00134 00142 public function getGlobalId() { 00143 return $this->globalId; 00144 } 00145 00155 public function setGlobalId( $globalId ) { 00156 if ( $globalId !== null && !is_string( $globalId ) ) { 00157 throw new MWException( '$globalId needs to be string or null' ); 00158 } 00159 00160 $this->globalId = $globalId; 00161 } 00162 00170 public function getType() { 00171 return $this->type; 00172 } 00173 00181 public function getGroup() { 00182 return $this->group; 00183 } 00184 00194 public function setGroup( $group ) { 00195 if ( !is_string( $group ) ) { 00196 throw new MWException( '$group needs to be a string' ); 00197 } 00198 00199 $this->group = $group; 00200 } 00201 00209 public function getSource() { 00210 return $this->source; 00211 } 00212 00222 public function setSource( $source ) { 00223 if ( !is_string( $source ) ) { 00224 throw new MWException( '$source needs to be a string' ); 00225 } 00226 00227 $this->source = $source; 00228 } 00229 00238 public function shouldForward() { 00239 return $this->forward; 00240 } 00241 00252 public function setForward( $shouldForward ) { 00253 if ( !is_bool( $shouldForward ) ) { 00254 throw new MWException( '$shouldForward needs to be a boolean' ); 00255 } 00256 00257 $this->forward = $shouldForward; 00258 } 00259 00268 public function getDomain() { 00269 $path = $this->getLinkPath(); 00270 00271 if ( $path === null ) { 00272 return null; 00273 } 00274 00275 return parse_url( $path, PHP_URL_HOST ); 00276 } 00277 00286 public function getProtocol() { 00287 $path = $this->getLinkPath(); 00288 00289 if ( $path === null ) { 00290 return ''; 00291 } 00292 00293 $protocol = parse_url( $path, PHP_URL_SCHEME ); 00294 00295 // Malformed URL 00296 if ( $protocol === false ) { 00297 throw new MWException( "failed to parse URL '$path'" ); 00298 } 00299 00300 // No schema 00301 if ( $protocol === null ) { 00302 // Used for protocol relative URLs 00303 $protocol = ''; 00304 } 00305 00306 return $protocol; 00307 } 00308 00319 public function setLinkPath( $fullUrl ) { 00320 $type = $this->getLinkPathType(); 00321 00322 if ( $type === null ) { 00323 throw new MWException( "This Site does not support link paths." ); 00324 } 00325 00326 $this->setPath( $type, $fullUrl ); 00327 } 00328 00336 public function getLinkPath() { 00337 $type = $this->getLinkPathType(); 00338 return $type === null ? null: $this->getPath( $type ); 00339 } 00340 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 ) 00545 ? $this->localIds[self::ID_INTERWIKI] 00546 : array(); 00547 } 00548 00557 public function getNavigationIds() { 00558 return array_key_exists( self::ID_EQUIVALENT, $this->localIds ) 00559 ? $this->localIds[self::ID_EQUIVALENT] : 00560 array(); 00561 } 00562 00570 public function getLocalIds() { 00571 return $this->localIds; 00572 } 00573 00585 public function setPath( $pathType, $fullUrl ) { 00586 if ( !is_string( $fullUrl ) ) { 00587 throw new MWException( '$fullUrl needs to be a string' ); 00588 } 00589 00590 if ( !array_key_exists( 'paths', $this->extraData ) ) { 00591 $this->extraData['paths'] = array(); 00592 } 00593 00594 $this->extraData['paths'][$pathType] = $fullUrl; 00595 } 00596 00606 public function getPath( $pathType ) { 00607 $paths = $this->getAllPaths(); 00608 return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : null; 00609 } 00610 00619 public function getAllPaths() { 00620 return array_key_exists( 'paths', $this->extraData ) ? $this->extraData['paths'] : array(); 00621 } 00622 00630 public function removePath( $pathType ) { 00631 if ( array_key_exists( 'paths', $this->extraData ) ) { 00632 unset( $this->extraData['paths'][$pathType] ); 00633 } 00634 } 00635 00643 public static function newForType( $siteType ) { 00644 global $wgSiteTypes; 00645 00646 if ( array_key_exists( $siteType, $wgSiteTypes ) ) { 00647 return new $wgSiteTypes[$siteType](); 00648 } 00649 00650 return new Site(); 00651 } 00652 00660 public function serialize() { 00661 $fields = array( 00662 'globalid' => $this->globalId, 00663 'type' => $this->type, 00664 'group' => $this->group, 00665 'source' => $this->source, 00666 'language' => $this->languageCode, 00667 'localids' => $this->localIds, 00668 'config' => $this->extraConfig, 00669 'data' => $this->extraData, 00670 'forward' => $this->forward, 00671 'internalid' => $this->internalId, 00672 00673 ); 00674 00675 return serialize( $fields ); 00676 } 00677 00685 public function unserialize( $serialized ) { 00686 $fields = unserialize( $serialized ); 00687 00688 $this->__construct( $fields['type'] ); 00689 00690 $this->setGlobalId( $fields['globalid'] ); 00691 $this->setGroup( $fields['group'] ); 00692 $this->setSource( $fields['source'] ); 00693 $this->setLanguageCode( $fields['language'] ); 00694 $this->localIds = $fields['localids']; 00695 $this->setExtraConfig( $fields['config'] ); 00696 $this->setExtraData( $fields['data'] ); 00697 $this->setForward( $fields['forward'] ); 00698 $this->setInternalId( $fields['internalid'] ); 00699 } 00700 } 00701 00705 class SiteObject extends Site { 00706 }