MediaWiki  REL1_22
WikiMap.php
Go to the documentation of this file.
00001 <?php
00026 class WikiMap {
00027 
00034     public static function getWiki( $wikiID ) {
00035         global $wgConf;
00036 
00037         $wgConf->loadFullData();
00038 
00039         list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
00040         if ( $major === null ) {
00041             return null;
00042         }
00043         $server = $wgConf->get( 'wgServer', $wikiID, $major,
00044             array( 'lang' => $minor, 'site' => $major ) );
00045 
00046         $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
00047             array( 'lang' => $minor, 'site' => $major ) );
00048         if ( $canonicalServer === false || $canonicalServer === null ) {
00049             $canonicalServer = $server;
00050         }
00051 
00052         $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
00053             array( 'lang' => $minor, 'site' => $major ) );
00054         return new WikiReference( $major, $minor, $canonicalServer, $path, $server );
00055     }
00056 
00064     public static function getWikiName( $wikiID ) {
00065         $wiki = WikiMap::getWiki( $wikiID );
00066 
00067         if ( $wiki ) {
00068             return $wiki->getDisplayName();
00069         }
00070         return $wikiID;
00071     }
00072 
00081     public static function foreignUserLink( $wikiID, $user, $text = null ) {
00082         return self::makeForeignLink( $wikiID, "User:$user", $text );
00083     }
00084 
00093     public static function makeForeignLink( $wikiID, $page, $text = null ) {
00094         if ( !$text ) {
00095             $text = $page;
00096         }
00097 
00098         $url = self::getForeignURL( $wikiID, $page );
00099         if ( $url === false ) {
00100             return false;
00101         }
00102 
00103         return Linker::makeExternalLink( $url, $text );
00104     }
00105 
00113     public static function getForeignURL( $wikiID, $page ) {
00114         $wiki = WikiMap::getWiki( $wikiID );
00115 
00116         if ( $wiki ) {
00117             return $wiki->getFullUrl( $page );
00118         }
00119 
00120         return false;
00121     }
00122 }
00123 
00127 class WikiReference {
00128     private $mMinor; 
00129     private $mMajor; 
00130     private $mCanonicalServer; 
00131     private $mServer; 
00132     private $mPath;   
00133 
00141     public function __construct( $major, $minor, $canonicalServer, $path, $server = null ) {
00142         $this->mMajor = $major;
00143         $this->mMinor = $minor;
00144         $this->mCanonicalServer = $canonicalServer;
00145         $this->mPath = $path;
00146         $this->mServer = $server === null ? $canonicalServer : $server;
00147     }
00148 
00153     public function getHostname() {
00154         $prefixes = array( 'http://', 'https://' );
00155         foreach ( $prefixes as $prefix ) {
00156             if ( substr( $this->mCanonicalServer, 0, strlen( $prefix ) ) ) {
00157                 return substr( $this->mCanonicalServer, strlen( $prefix ) );
00158             }
00159         }
00160         throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
00161     }
00162 
00169     public function getDisplayName() {
00170         $url = $this->getUrl( '' );
00171         $parsed = wfParseUrl( $url );
00172         if ( $parsed ) {
00173             return $parsed['host'];
00174         } else {
00175             // Invalid URL. There's no sane thing to do here, so just return it
00176             return $url;
00177         }
00178     }
00179 
00187     private function getLocalUrl( $page ) {
00188         return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
00189     }
00190 
00197     public function getCanonicalUrl( $page ) {
00198         return $this->mCanonicalServer . $this->getLocalUrl( $page );
00199     }
00200 
00205     public function getCanonicalServer() {
00206         return $this->mCanonicalServer;
00207     }
00208 
00214     public function getUrl( $page ) {
00215         return $this->getCanonicalUrl( $page );
00216     }
00217 
00225     public function getFullUrl( $page ) {
00226         return $this->mServer .
00227             $this->getLocalUrl( $page );
00228     }
00229 }