http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89802

Revision: 89802
Author:   pdhanda
Date:     2011-06-09 21:49:08 +0000 (Thu, 09 Jun 2011)
Log Message:
-----------
Cleaned up MWMultiVersion so that it can be used as an object instead of a 
collection of utility functions. Also some cleanup in MWVersion. 
wgVersionDirectory is now the same as MWMultiVersion::getVersion()

Modified Paths:
--------------
    trunk/tools/mwmultiversion/live-1.5/MWVersion.php
    trunk/tools/mwmultiversion/wmf-config/CommonSettings.php
    trunk/tools/mwmultiversion/wmf-config/MWMultiVersion.php

Modified: trunk/tools/mwmultiversion/live-1.5/MWVersion.php
===================================================================
--- trunk/tools/mwmultiversion/live-1.5/MWVersion.php   2011-06-09 21:47:07 UTC 
(rev 89801)
+++ trunk/tools/mwmultiversion/live-1.5/MWVersion.php   2011-06-09 21:49:08 UTC 
(rev 89802)
@@ -1,20 +1,13 @@
 <?php
 
 function getMediaWiki( $file ) {
-$dbname = $multiVersion->getDatabase( $siteInfo['site'], $siteInfo['lang']);
        $secure = getenv( 'MW_SECURE_HOST' );
     $host = $secure ? $secure : $_SERVER['HTTP_HOST'];
     
        require( dirname( __FILE__ ) . '/../wmf-config/MWMultiVersion.php' );
-       $multiVersion = new MWMultiVersion;
-       $siteInfo = array();
-       if ( (@$_SERVER['SCRIPT_NAME']) == '/w/thumb.php' && 
(@$_SERVER['SERVER_NAME']) == 'upload.wikimedia.org' ) {
-               $siteInfo = $multiVersion->getUploadSiteInfo( 
$_SERVER['PATH_INFO'] );
-       } else {
-               $siteInfo = $multiVersion->getSiteInfo( 
$_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'] );
-       }
+       $multiVersion = MWMultiVersion::getInstanceForWiki( 
$_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'] );
        
-       $version = $multiVersion->getVersion( $siteInfo['site'], 
$siteInfo['lang']);
+       $version = $multiVersion->getVersion();
 
        if ( $host == 'test.wikipedia.org' && !$secure &&
        !preg_match( '!thumb\.php!', $_SERVER['REQUEST_URI'] ) ) {

Modified: trunk/tools/mwmultiversion/wmf-config/CommonSettings.php
===================================================================
--- trunk/tools/mwmultiversion/wmf-config/CommonSettings.php    2011-06-09 
21:47:07 UTC (rev 89801)
+++ trunk/tools/mwmultiversion/wmf-config/CommonSettings.php    2011-06-09 
21:49:08 UTC (rev 89802)
@@ -97,18 +97,17 @@
 wfProfileOut( "$fname-init" );
 wfProfileIn( "$fname-host" );
 
-# Determine domain and language
+# Determine domain and language and the directories for this instance
 require_once( $IP . '/../wmf-config/MWMultiVersion.php' );
-$multiVersion = new MWMultiVersion;
-$siteInfo = array();
 if ( (@$_SERVER['SCRIPT_NAME']) == '/w/thumb.php' && 
(@$_SERVER['SERVER_NAME']) == 'upload.wikimedia.org' ) {
-       $siteInfo = $multiVersion->getUploadSiteInfo( $_SERVER['PATH_INFO'] );
+       $multiVersion = MWMultiVersion::getInstanceForUploadWiki( 
$_SERVER['PATH_INFO'] );
 } else {
-       $siteInfo = $multiVersion->getSiteInfo( $_SERVER['SERVER_NAME'], 
$_SERVER['DOCUMENT_ROOT'] );
+       $multiVersion = MWMultiVersion::getInstanceForWiki( 
$_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'] );
 }
-$site = $siteInfo['site'];
-$lang = $siteInfo['lang'];
-$wgDBname = $multiVersion->getDatabase( $site, $lang);
+$site = $multiVersion->getSite();
+$lang = $multiVersion->getLang();
+$wgDBname = $multiVersion->getDatabase();
+$wgVersionDirectory = $multiVersion->getVersion();
 
 # Disabled, no IPv6 support, waste of a regex -- TS 20051207
 /*
@@ -117,15 +116,6 @@
        $ipv6 = true;
 }*/
 
-
-//changed for hetdeploy testing --pdhanda
-$match = array();
-if ( preg_match("/^[0-9.]*/", $wgVersion, $match) ) {
-       $wgVersionDirectory = $match[0];
-} else {
-       $wgVersionDirectory = "1.17";
-}
-
 # Shutting eswiki down
 #if ( $wgDBname == 'eswiki' && php_sapi_name() != 'cli' ) { die(); }
 

Modified: trunk/tools/mwmultiversion/wmf-config/MWMultiVersion.php
===================================================================
--- trunk/tools/mwmultiversion/wmf-config/MWMultiVersion.php    2011-06-09 
21:47:07 UTC (rev 89801)
+++ trunk/tools/mwmultiversion/wmf-config/MWMultiVersion.php    2011-06-09 
21:49:08 UTC (rev 89802)
@@ -1,88 +1,130 @@
 <?php
 
 class MWMultiVersion {
+       private static $mwversion;
+       private $site;
+       private $lang;
 
-       function getSiteInfo( $serverName, $docRoot ) {
+       /**
+        * To get an inststance of this class, use the statuc helper methods.
+        * @see getInstanceForWiki
+        * @see getInstanceForUploadWiki
+        */
+       private function __construct() {
+       }
+
+       /**
+        * Derives site and lang from the parameters and sets $site and $lang 
on the instance
+        *  @param $serverName the ServerName for this wiki -- 
$_SERVER['SERVER_NAME']
+        *  @docroot the DocumentRoot for this wiki -- $_SERVER['DOCUMENT_ROOT']
+        */
+       private function setSiteInfoForWiki( $serverName, $docRoot) {
+               //print "serverName " . $serverName . " docRoot " . $docRoot; 
die();
                $secure = getenv( 'MW_SECURE_HOST' );
                $matches = array();
                if (php_sapi_name() == 'cgi-fcgi') {
                        if (!preg_match('/^([^.]+).([^.]+).*$/', $serverName, 
$matches))
                        die("invalid hostname");
        
-                       $lang = $matches[1];
-                       $site = $$matches[2];
+                       $this->lang = $matches[1];
+                       $this->site = $$matches[2];
        
-                       if (in_array($lang, array("commons", "grants", 
"sources", "wikimania", "wikimania2006", "foundation", "meta")))
-                       $site = "wikipedia";
+                       if (in_array($this->lang, array("commons", "grants", 
"sources", "wikimania", "wikimania2006", "foundation", "meta")))
+                       $this->site = "wikipedia";
                } elseif( $secure ) {
                        if (!preg_match('/^([^.]+).([^.]+).*$/', $secure, 
$$matches))
                        die("invalid hostname");
        
-                       $lang = $$matches[1];
-                       $site = $$matches[2];
+                       $this->lang = $$matches[1];
+                       $this->site = $$matches[2];
        
-                       if (in_array($lang, array("commons", "grants", 
"sources", "wikimania", "wikimania2006", "foundation", "meta")))
-                       $site = "wikipedia";
+                       if (in_array($this->lang, array("commons", "grants", 
"sources", "wikimania", "wikimania2006", "foundation", "meta")))
+                       $this->site = "wikipedia";
                } else {
-                       if ( !isset( $site ) ) {
-                               $site = "wikipedia";
-                               if ( !isset( $lang ) ) {        
+                       if ( !isset( $this->site ) ) {
+                               $this->site = "wikipedia";
+                               if ( !isset( $this->lang ) ) {  
                                        if ( preg_match( 
'/^(?:\/usr\/local\/apache\/|\/home\/wikipedia\/)(?:htdocs|common\/docroot)\/([a-z]+)\.org/',
 $docRoot, $matches ) ) {
-                                               $site = $matches[1];
-                                               if ( preg_match( '/^(.*)\.' . 
preg_quote( $site ) . '\.org$/', $serverName, $matches ) ) {
-                                                       $lang = $matches[1];
+                                               $this->site = $matches[1];
+                                               if ( preg_match( '/^(.*)\.' . 
preg_quote( $this->site ) . '\.org$/', $serverName, $matches ) ) {
+                                                       $this->lang = 
$matches[1];
                                                        // For some special 
subdomains, like pa.us
-                                                       $lang = str_replace( 
'.', '-', $lang );
+                                                       $this->lang = 
str_replace( '.', '-', $this->lang );
                                                } else if ( preg_match( 
'/^(.*)\.prototype\.wikimedia\.org$/', $serverName, $matches ) ) {
-                                                       $lang = $matches[1];
+                                                       $this->lang = 
$matches[1];
                                                } else {
                                                        die( "Invalid host name 
($serverName), can't determine language" );
                                                }
                                        } elseif ( preg_match( 
"/^\/usr\/local\/apache\/(?:htdocs|common\/docroot)\/([a-z0-9\-_]*)$/", 
$docRoot, $matches ) ) {
-                                               $site = "wikipedia";
-                                               $lang = $matches[1];
-                                       } elseif ( $siteName == 'localhost' ) {
-                                               $lang = getenv( 'MW_LANG' );
+                                               $this->site = "wikipedia";
+                                               $this->lang = $matches[1];
+                                       } elseif ( $this->siteName == 
'localhost' ) {
+                                               $this->lang = getenv( 'MW_LANG' 
);
                                        } else {
                                                die( "Invalid host name 
(docroot=" . $_SERVER['DOCUMENT_ROOT'] . "), can't determine language" );
                                        }
                                }
                        }
                }
-               return array(
-                       'site' => $site,
-                       'lang' => $lang,
-               );
                
        }
        
-       function getUploadSiteInfo( $pathInfo) {
+       /**
+        * Derives site and lang from the parameter and sets $site and $lang on 
the instance
+        *  @param pathInfo the PathInfo -- $_SERVER['PATH_INFO']
+        */
+       private function setSiteInfoForUploadWiki( $pathInfo) {
+               if ( !empty( $this->siteInfo ) ) {
+                       return $this->siteInfo;
+               }
                $pathBits = explode( '/', $pathInfo );
-               $site = $pathBits[1];
-               $lang = $pathBits[2];
-               return array(
-                       'site' => $site,
-                       'lang' => $lang,
-               );
-       }       
+               $this->site = $pathBits[1];
+               $this->lang = $pathBits[2];
+       }
        
-       function getDatabase( $site, $lang ) {
+       /**
+        * Get the site for this wiki
+        * @return String site. Eg: wikipedia, wikinews, wikiversity
+        */
+       public function getSite() {
+               return $this->site;
+       }
+       
+       /**
+        * Get the lang for this wiki
+        * @return String lang Eg: en, de, ar, hi
+        */
+       public function getLang() {
+               return $this->lang;
+       }
+
+       /**
+        * If in env variable MW_DBNAME is found then use that,
+        * otherwise derive dbname from lang and site
+        * @return String the database name
+        */
+       public function getDatabase( ) {
                $dbname = getenv( 'MW_DBNAME' );
                if ( strlen( $dbname ) == 0 ) {
-                       if ( $site == "wikipedia" ) {
+                       if ( $this->site == "wikipedia" ) {
                                $dbSuffix = "wiki";
                        } else {
-                               $dbSuffix = $site;
+                               $dbSuffix = $this->site;
                        }
-                       $dbname = str_replace( "-", "_", $lang . $dbSuffix );
+                       $dbname = str_replace( "-", "_", $this->lang . 
$dbSuffix );
                        putenv( 'MW_DBNAME=' . $dbname );
                }
                return $dbname;
        
        }
        
-       function getVersion( $site, $lang ) {
-               $dbname = $this->getDatabase( $site, $lang );
+       /**
+        * Get the version as specified in a cdb file located in 
/usr/local/apache/common/wikiversions.db
+        * The key should be the dbname and the version should be the version 
directory for this wiki
+        * @return String the version wirectory for this wiki
+        */
+       public function getVersion( ) {
+               $dbname = $this->getDatabase( $this->site, $this->lang );
                $db = dba_open( '/usr/local/apache/common/wikiversions.db', 
'r', 'cdb' );
                if ( $db ) {
                        $version = dba_fetch( $dbname, $db );
@@ -93,6 +135,34 @@
                }
                return $version;
        }
+       
+       /**
+        * Factory method to get an instance of MWMultiVersion. 
+        * Use this for all wikis except calls to /w/thumb.php on 
upload.wikmedia.org.
+        * @return An MWMultiVersion object for this wiki
+        */
+       public static function getInstanceForWiki( $serverName, $docRoot ) {
+               if (!isset(self::$mwversion)) {
+                     $c = __CLASS__;
+                     self::$mwversion = new $c;
+               }
+               self::$mwversion->setSiteInfoForWiki( $serverName, $docRoot);
+               return self::$mwversion; 
+       }
+               
+       
+       /**
+        * Factory method to get an instance of MWMultiVersion used for calls 
to /w/thumb.php on upload.wikmedia.org.
+        * @return An MWMultiVersion object for the wiki derived from the 
pathinfo
+        */
+       public static function getInstanceForUploadWiki( $pathInfo ) {
+               if (!isset(self::$mwversion)) {
+                     $c = __CLASS__;
+                     self::$mwversion = new $c;
+               }
+               self::$mwversion->setSiteInfoForUploadWiki( $PathInfo);
+               return self::$mwversion;
+       }
 
 }
 


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to