r89802 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89801‎ | r89802 | r89803 >
Date:21:49, 9 June 2011
Author:pdhanda
Status:deferred
Tags:
Comment:
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 (modified) (history)
  • /trunk/tools/mwmultiversion/wmf-config/CommonSettings.php (modified) (history)
  • /trunk/tools/mwmultiversion/wmf-config/MWMultiVersion.php (modified) (history)

Diff [purge]

Index: trunk/tools/mwmultiversion/live-1.5/MWVersion.php
@@ -1,20 +1,13 @@
22 <?php
33
44 function getMediaWiki( $file ) {
5 -$dbname = $multiVersion->getDatabase( $siteInfo['site'], $siteInfo['lang']);
65 $secure = getenv( 'MW_SECURE_HOST' );
76 $host = $secure ? $secure : $_SERVER['HTTP_HOST'];
87
98 require( dirname( __FILE__ ) . '/../wmf-config/MWMultiVersion.php' );
10 - $multiVersion = new MWMultiVersion;
11 - $siteInfo = array();
12 - if ( (@$_SERVER['SCRIPT_NAME']) == '/w/thumb.php' && (@$_SERVER['SERVER_NAME']) == 'upload.wikimedia.org' ) {
13 - $siteInfo = $multiVersion->getUploadSiteInfo( $_SERVER['PATH_INFO'] );
14 - } else {
15 - $siteInfo = $multiVersion->getSiteInfo( $_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'] );
16 - }
 9+ $multiVersion = MWMultiVersion::getInstanceForWiki( $_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'] );
1710
18 - $version = $multiVersion->getVersion( $siteInfo['site'], $siteInfo['lang']);
 11+ $version = $multiVersion->getVersion();
1912
2013 if ( $host == 'test.wikipedia.org' && !$secure &&
2114 !preg_match( '!thumb\.php!', $_SERVER['REQUEST_URI'] ) ) {
Index: trunk/tools/mwmultiversion/wmf-config/CommonSettings.php
@@ -97,18 +97,17 @@
9898 wfProfileOut( "$fname-init" );
9999 wfProfileIn( "$fname-host" );
100100
101 -# Determine domain and language
 101+# Determine domain and language and the directories for this instance
102102 require_once( $IP . '/../wmf-config/MWMultiVersion.php' );
103 -$multiVersion = new MWMultiVersion;
104 -$siteInfo = array();
105103 if ( (@$_SERVER['SCRIPT_NAME']) == '/w/thumb.php' && (@$_SERVER['SERVER_NAME']) == 'upload.wikimedia.org' ) {
106 - $siteInfo = $multiVersion->getUploadSiteInfo( $_SERVER['PATH_INFO'] );
 104+ $multiVersion = MWMultiVersion::getInstanceForUploadWiki( $_SERVER['PATH_INFO'] );
107105 } else {
108 - $siteInfo = $multiVersion->getSiteInfo( $_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'] );
 106+ $multiVersion = MWMultiVersion::getInstanceForWiki( $_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'] );
109107 }
110 -$site = $siteInfo['site'];
111 -$lang = $siteInfo['lang'];
112 -$wgDBname = $multiVersion->getDatabase( $site, $lang);
 108+$site = $multiVersion->getSite();
 109+$lang = $multiVersion->getLang();
 110+$wgDBname = $multiVersion->getDatabase();
 111+$wgVersionDirectory = $multiVersion->getVersion();
113112
114113 # Disabled, no IPv6 support, waste of a regex -- TS 20051207
115114 /*
@@ -117,15 +116,6 @@
118117 $ipv6 = true;
119118 }*/
120119
121 -
122 -//changed for hetdeploy testing --pdhanda
123 -$match = array();
124 -if ( preg_match("/^[0-9.]*/", $wgVersion, $match) ) {
125 - $wgVersionDirectory = $match[0];
126 -} else {
127 - $wgVersionDirectory = "1.17";
128 -}
129 -
130120 # Shutting eswiki down
131121 #if ( $wgDBname == 'eswiki' && php_sapi_name() != 'cli' ) { die(); }
132122
Index: trunk/tools/mwmultiversion/wmf-config/MWMultiVersion.php
@@ -1,88 +1,130 @@
22 <?php
33
44 class MWMultiVersion {
 5+ private static $mwversion;
 6+ private $site;
 7+ private $lang;
58
6 - function getSiteInfo( $serverName, $docRoot ) {
 9+ /**
 10+ * To get an inststance of this class, use the statuc helper methods.
 11+ * @see getInstanceForWiki
 12+ * @see getInstanceForUploadWiki
 13+ */
 14+ private function __construct() {
 15+ }
 16+
 17+ /**
 18+ * Derives site and lang from the parameters and sets $site and $lang on the instance
 19+ * @param $serverName the ServerName for this wiki -- $_SERVER['SERVER_NAME']
 20+ * @docroot the DocumentRoot for this wiki -- $_SERVER['DOCUMENT_ROOT']
 21+ */
 22+ private function setSiteInfoForWiki( $serverName, $docRoot) {
 23+ //print "serverName " . $serverName . " docRoot " . $docRoot; die();
724 $secure = getenv( 'MW_SECURE_HOST' );
825 $matches = array();
926 if (php_sapi_name() == 'cgi-fcgi') {
1027 if (!preg_match('/^([^.]+).([^.]+).*$/', $serverName, $matches))
1128 die("invalid hostname");
1229
13 - $lang = $matches[1];
14 - $site = $$matches[2];
 30+ $this->lang = $matches[1];
 31+ $this->site = $$matches[2];
1532
16 - if (in_array($lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
17 - $site = "wikipedia";
 33+ if (in_array($this->lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
 34+ $this->site = "wikipedia";
1835 } elseif( $secure ) {
1936 if (!preg_match('/^([^.]+).([^.]+).*$/', $secure, $$matches))
2037 die("invalid hostname");
2138
22 - $lang = $$matches[1];
23 - $site = $$matches[2];
 39+ $this->lang = $$matches[1];
 40+ $this->site = $$matches[2];
2441
25 - if (in_array($lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
26 - $site = "wikipedia";
 42+ if (in_array($this->lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
 43+ $this->site = "wikipedia";
2744 } else {
28 - if ( !isset( $site ) ) {
29 - $site = "wikipedia";
30 - if ( !isset( $lang ) ) {
 45+ if ( !isset( $this->site ) ) {
 46+ $this->site = "wikipedia";
 47+ if ( !isset( $this->lang ) ) {
3148 if ( preg_match( '/^(?:\/usr\/local\/apache\/|\/home\/wikipedia\/)(?:htdocs|common\/docroot)\/([a-z]+)\.org/', $docRoot, $matches ) ) {
32 - $site = $matches[1];
33 - if ( preg_match( '/^(.*)\.' . preg_quote( $site ) . '\.org$/', $serverName, $matches ) ) {
34 - $lang = $matches[1];
 49+ $this->site = $matches[1];
 50+ if ( preg_match( '/^(.*)\.' . preg_quote( $this->site ) . '\.org$/', $serverName, $matches ) ) {
 51+ $this->lang = $matches[1];
3552 // For some special subdomains, like pa.us
36 - $lang = str_replace( '.', '-', $lang );
 53+ $this->lang = str_replace( '.', '-', $this->lang );
3754 } else if ( preg_match( '/^(.*)\.prototype\.wikimedia\.org$/', $serverName, $matches ) ) {
38 - $lang = $matches[1];
 55+ $this->lang = $matches[1];
3956 } else {
4057 die( "Invalid host name ($serverName), can't determine language" );
4158 }
4259 } elseif ( preg_match( "/^\/usr\/local\/apache\/(?:htdocs|common\/docroot)\/([a-z0-9\-_]*)$/", $docRoot, $matches ) ) {
43 - $site = "wikipedia";
44 - $lang = $matches[1];
45 - } elseif ( $siteName == 'localhost' ) {
46 - $lang = getenv( 'MW_LANG' );
 60+ $this->site = "wikipedia";
 61+ $this->lang = $matches[1];
 62+ } elseif ( $this->siteName == 'localhost' ) {
 63+ $this->lang = getenv( 'MW_LANG' );
4764 } else {
4865 die( "Invalid host name (docroot=" . $_SERVER['DOCUMENT_ROOT'] . "), can't determine language" );
4966 }
5067 }
5168 }
5269 }
53 - return array(
54 - 'site' => $site,
55 - 'lang' => $lang,
56 - );
5770
5871 }
5972
60 - function getUploadSiteInfo( $pathInfo) {
 73+ /**
 74+ * Derives site and lang from the parameter and sets $site and $lang on the instance
 75+ * @param pathInfo the PathInfo -- $_SERVER['PATH_INFO']
 76+ */
 77+ private function setSiteInfoForUploadWiki( $pathInfo) {
 78+ if ( !empty( $this->siteInfo ) ) {
 79+ return $this->siteInfo;
 80+ }
6181 $pathBits = explode( '/', $pathInfo );
62 - $site = $pathBits[1];
63 - $lang = $pathBits[2];
64 - return array(
65 - 'site' => $site,
66 - 'lang' => $lang,
67 - );
68 - }
 82+ $this->site = $pathBits[1];
 83+ $this->lang = $pathBits[2];
 84+ }
6985
70 - function getDatabase( $site, $lang ) {
 86+ /**
 87+ * Get the site for this wiki
 88+ * @return String site. Eg: wikipedia, wikinews, wikiversity
 89+ */
 90+ public function getSite() {
 91+ return $this->site;
 92+ }
 93+
 94+ /**
 95+ * Get the lang for this wiki
 96+ * @return String lang Eg: en, de, ar, hi
 97+ */
 98+ public function getLang() {
 99+ return $this->lang;
 100+ }
 101+
 102+ /**
 103+ * If in env variable MW_DBNAME is found then use that,
 104+ * otherwise derive dbname from lang and site
 105+ * @return String the database name
 106+ */
 107+ public function getDatabase( ) {
71108 $dbname = getenv( 'MW_DBNAME' );
72109 if ( strlen( $dbname ) == 0 ) {
73 - if ( $site == "wikipedia" ) {
 110+ if ( $this->site == "wikipedia" ) {
74111 $dbSuffix = "wiki";
75112 } else {
76 - $dbSuffix = $site;
 113+ $dbSuffix = $this->site;
77114 }
78 - $dbname = str_replace( "-", "_", $lang . $dbSuffix );
 115+ $dbname = str_replace( "-", "_", $this->lang . $dbSuffix );
79116 putenv( 'MW_DBNAME=' . $dbname );
80117 }
81118 return $dbname;
82119
83120 }
84121
85 - function getVersion( $site, $lang ) {
86 - $dbname = $this->getDatabase( $site, $lang );
 122+ /**
 123+ * Get the version as specified in a cdb file located in /usr/local/apache/common/wikiversions.db
 124+ * The key should be the dbname and the version should be the version directory for this wiki
 125+ * @return String the version wirectory for this wiki
 126+ */
 127+ public function getVersion( ) {
 128+ $dbname = $this->getDatabase( $this->site, $this->lang );
87129 $db = dba_open( '/usr/local/apache/common/wikiversions.db', 'r', 'cdb' );
88130 if ( $db ) {
89131 $version = dba_fetch( $dbname, $db );
@@ -93,6 +135,34 @@
94136 }
95137 return $version;
96138 }
 139+
 140+ /**
 141+ * Factory method to get an instance of MWMultiVersion.
 142+ * Use this for all wikis except calls to /w/thumb.php on upload.wikmedia.org.
 143+ * @return An MWMultiVersion object for this wiki
 144+ */
 145+ public static function getInstanceForWiki( $serverName, $docRoot ) {
 146+ if (!isset(self::$mwversion)) {
 147+ $c = __CLASS__;
 148+ self::$mwversion = new $c;
 149+ }
 150+ self::$mwversion->setSiteInfoForWiki( $serverName, $docRoot);
 151+ return self::$mwversion;
 152+ }
 153+
 154+
 155+ /**
 156+ * Factory method to get an instance of MWMultiVersion used for calls to /w/thumb.php on upload.wikmedia.org.
 157+ * @return An MWMultiVersion object for the wiki derived from the pathinfo
 158+ */
 159+ public static function getInstanceForUploadWiki( $pathInfo ) {
 160+ if (!isset(self::$mwversion)) {
 161+ $c = __CLASS__;
 162+ self::$mwversion = new $c;
 163+ }
 164+ self::$mwversion->setSiteInfoForUploadWiki( $PathInfo);
 165+ return self::$mwversion;
 166+ }
97167
98168 }
99169

Status & tagging log