Move WikiMap class from CentralAuth to core, since it's used in 2 extensions at least...
authorAndrew Garrett <werdna@users.mediawiki.org>
Thu, 26 Mar 2009 13:31:30 +0000 (13:31 +0000)
committerAndrew Garrett <werdna@users.mediawiki.org>
Thu, 26 Mar 2009 13:31:30 +0000 (13:31 +0000)
includes/AutoLoader.php
includes/WikiMap.php [new file with mode: 0644]

index d830c03..8edefe5 100644 (file)
@@ -209,6 +209,8 @@ $wgAutoloadLocalClasses = array(
        'WikiError' => 'includes/WikiError.php',
        'WikiErrorMsg' => 'includes/WikiError.php',
        'WikiExporter' => 'includes/Export.php',
+       'WikiMap' => 'includes/WikiMap.php',
+       'WikiReference' => 'includes/WikiReference.php',
        'WikiXmlError' => 'includes/WikiError.php',
        'XCacheBagOStuff' => 'includes/BagOStuff.php',
        'XmlDumpWriter' => 'includes/Export.php',
diff --git a/includes/WikiMap.php b/includes/WikiMap.php
new file mode 100644 (file)
index 0000000..db9d17d
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+
+/**
+ * Helper tools for dealing with other locally-hosted wikis
+ */
+
+class WikiMap {
+       static function getWiki( $wikiID ) {
+               global $wgConf, $IP;
+               static $initialiseSettingsDone = false;
+
+               // This is a damn dirty hack
+               if ( !$initialiseSettingsDone ) {
+                       $initialiseSettingsDone = true;
+                       if( file_exists( "$IP/InitialiseSettings.php" ) ) {
+                               require_once "$IP/InitialiseSettings.php";
+                       }
+               }
+
+               list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
+               if( isset( $major ) ) {
+                       $server = $wgConf->get( 'wgServer', $wikiID, $major,
+                               array( 'lang' => $minor, 'site' => $major ) );
+                       $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
+                               array( 'lang' => $minor, 'site' => $major ) );
+                       return new WikiReference( $major, $minor, $server, $path );
+               } else {
+                       return null;
+               }
+       }
+       
+       // Convenience functions from GlobalBlocking
+       static function getWikiName( $wiki_id ) {
+               // We can give more info than just the wiki id!
+               $wiki = WikiMap::getWiki( $wiki_id );
+                       
+               if ($wiki) {
+                       return $wiki->getDisplayName();
+               }
+               return $wiki_id;
+       }
+       
+       static function foreignUserLink( $wiki_id, $user ) {
+               return self::makeForeignLink( $wiki_id, "User:$user" );
+       }
+       
+       static function makeForeignLink( $wiki_id, $page, $text=null ) {
+               global $wgUser;
+               $sk = $wgUser->getSkin();
+               
+               if (!$text)
+                       $text=$page;
+               
+               return $sk->makeExternalLink( self::getForeignURL( $wiki_id, $page ) , $text );
+       }
+       
+       static function getForeignURL( $wiki_id, $page ) {
+               $wiki = WikiMap::getWiki( $wiki_id );
+               
+               if ($wiki)
+                       return $wiki->getUrl( $page );
+                       
+               return false;
+       }
+}
+
+class WikiReference {
+       private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
+       private $mMajor; ///< 'wiki', 'wiktionary', etc
+       private $mServer; ///< server override, 'www.mediawiki.org'
+       private $mPath;   ///< path override, '/wiki/$1'
+
+       function __construct( $major, $minor, $server, $path ) {
+               $this->mMajor = $major;
+               $this->mMinor = $minor;
+               $this->mServer = $server;
+               $this->mPath = $path;
+       }
+
+       function getHostname() {
+               $prefixes = array( 'http://', 'https://' );
+               foreach ( $prefixes as $prefix ) {
+                       if ( substr( $this->mServer, 0, strlen( $prefix ) ) ) {
+                               return substr( $this->mServer, strlen( $prefix ) );
+                       }
+               }
+               throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
+       }
+
+       /**
+        * pretty it up
+        */
+       function getDisplayName() {
+               $url = $this->getUrl( '' );
+               $url = preg_replace( '!^https?://!', '', $url );
+               $url = preg_replace( '!/index\.php(\?title=|/)$!', '/', $url );
+               $url = preg_replace( '!/wiki/$!', '/', $url );
+               $url = preg_replace( '!/$!', '', $url );
+               return $url;
+       }
+
+       private function getLocalUrl( $page ) {
+               // FIXME: this may be generalized...
+               return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
+       }
+
+       function getUrl( $page ) {
+               return
+                       $this->mServer . 
+                       $this->getLocalUrl( $page );
+       }
+}