MediaWiki  REL1_19
WikiMap.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class WikiMap {
00007 
00014         public static function getWiki( $wikiID ) {
00015                 global $wgConf;
00016 
00017                 $wgConf->loadFullData();
00018 
00019                 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
00020                 if( $major === null ) {
00021                         return null;
00022                 }
00023                 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
00024                         array( 'lang' => $minor, 'site' => $major ) );
00025                 $server = $wgConf->get( 'wgServer', $wikiID, $major,
00026                         array( 'lang' => $minor, 'site' => $major ) );
00027                 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
00028                         array( 'lang' => $minor, 'site' => $major ) );
00029                 return new WikiReference( $major, $minor, $canonicalServer, $path, $server );
00030         }
00031 
00039         public static function getWikiName( $wikiID ) {
00040                 $wiki = WikiMap::getWiki( $wikiID );
00041 
00042                 if ( $wiki ) {
00043                         return $wiki->getDisplayName();
00044                 }
00045                 return $wikiID;
00046         }
00047 
00056         public static function foreignUserLink( $wikiID, $user, $text=null ) {
00057                 return self::makeForeignLink( $wikiID, "User:$user", $text );
00058         }
00059 
00068         public static function makeForeignLink( $wikiID, $page, $text=null ) {
00069                 if ( !$text ) {
00070                         $text = $page;
00071                 }
00072 
00073                 $url = self::getForeignURL( $wikiID, $page );
00074                 if ( $url === false ) {
00075                         return false;
00076                 }
00077 
00078                 return Linker::makeExternalLink( $url, $text );
00079         }
00080 
00088         public static function getForeignURL( $wikiID, $page ) {
00089                 $wiki = WikiMap::getWiki( $wikiID );
00090 
00091                 if ( $wiki ) {
00092                         return $wiki->getUrl( $page );
00093                 }
00094 
00095                 return false;
00096         }
00097 }
00098 
00102 class WikiReference {
00103         private $mMinor; 
00104         private $mMajor; 
00105         private $mCanonicalServer; 
00106         private $mServer; 
00107         private $mPath;   
00108 
00109         public function __construct( $major, $minor, $canonicalServer, $path, $server = null ) {
00110                 $this->mMajor = $major;
00111                 $this->mMinor = $minor;
00112                 $this->mCanonicalServer = $canonicalServer;
00113                 $this->mPath = $path;
00114                 $this->mServer = $server === null ? $canonicalServer : $server;
00115         }
00116 
00121         public function getHostname() {
00122                 $prefixes = array( 'http://', 'https://' );
00123                 foreach ( $prefixes as $prefix ) {
00124                         if ( substr( $this->mCanonicalServer, 0, strlen( $prefix ) ) ) {
00125                                 return substr( $this->mCanonicalServer, strlen( $prefix ) );
00126                         }
00127                 }
00128                 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
00129         }
00130 
00137         public function getDisplayName() {
00138                 $url = $this->getUrl( '' );
00139                 $parsed = wfParseUrl( $url );
00140                 if ( $parsed ) {
00141                         return $parsed['host'];
00142                 } else {
00143                         // Invalid URL. There's no sane thing to do here, so just return it
00144                         return $url;
00145                 }
00146         }
00147 
00155         private function getLocalUrl( $page ) {
00156                 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
00157         }
00158 
00165         public function getCanonicalUrl( $page ) {
00166                 return $this->mCanonicalServer . $this->getLocalUrl( $page );
00167         }
00168 
00173         public function getUrl( $page ) {
00174                 return $this->getCanonicalUrl( $page );
00175         }
00176 
00184         public function getFullUrl( $page ) {
00185                 return
00186                         $this->mServer .
00187                         $this->getLocalUrl( $page );
00188         }
00189 }