r89737 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89736‎ | r89737 | r89738 >
Date:19:48, 8 June 2011
Author:pdhanda
Status:deferred
Tags:
Comment:
Separated some utility functions. Setting MW_DBNAME as an env variable
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 (added) (history)

Diff [purge]

Index: trunk/tools/mwmultiversion/live-1.5/MWVersion.php
@@ -1,84 +1,21 @@
22 <?php
33
4 -function host2db() {
5 - if ( $_SERVER['SERVER_NAME'] == 'localhost') {
6 - $dbname = getenv( 'MW_DBNAME' );
7 - if ( !isset( $dbname ) ) {
8 - die( 'Unable to determine MW_DBNAME on localhost' );
9 - }
10 - return $dbname;
11 - }
 4+function getMediaWiki( $file ) {
 5+$dbname = $multiVersion->getDatabase( $siteInfo['site'], $siteInfo['lang']);
126 $secure = getenv( 'MW_SECURE_HOST' );
 7+ $host = $secure ? $secure : $_SERVER['HTTP_HOST'];
 8+
 9+ require( dirname( __FILE__ ) . '/../wmf-config/MWMultiVersion.php' );
 10+ $multiVersion = new MWMultiVersion;
 11+ $siteInfo = array();
1312 if ( (@$_SERVER['SCRIPT_NAME']) == '/w/thumb.php' && (@$_SERVER['SERVER_NAME']) == 'upload.wikimedia.org' ) {
14 - $pathBits = explode( '/', $_SERVER['PATH_INFO'] );
15 - $site = $pathBits[1];
16 - $lang = $pathBits[2];
17 - } elseif (php_sapi_name() == 'cgi-fcgi') {
18 - if (!preg_match('/^([^.]+).([^.]+).*$/', $_SERVER['SERVER_NAME'], $m))
19 - die("invalid hostname");
20 -
21 - $lang = $m[1];
22 - $site = $m[2];
23 -
24 - if (in_array($lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
25 - $site = "wikipedia";
26 - } elseif( $secure ) {
27 - if (!preg_match('/^([^.]+).([^.]+).*$/', $secure, $m))
28 - die("invalid hostname");
29 -
30 - $lang = $m[1];
31 - $site = $m[2];
32 -
33 - if (in_array($lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
34 - $site = "wikipedia";
 13+ $siteInfo = $multiVersion->getUploadSiteInfo( $_SERVER['PATH_INFO'] );
3514 } else {
36 - if ( !isset( $site ) ) {
37 - $site = "wikipedia";
38 - if ( !isset( $lang ) ) {
39 - $server = $_SERVER['SERVER_NAME'];
40 - $docRoot = $_SERVER['DOCUMENT_ROOT'];
41 -
42 - if ( preg_match( '/^(?:\/usr\/local\/apache\/|\/home\/wikipedia\/)(?:htdocs|common\/docroot)\/([a-z]+)\.org/', $docRoot, $matches ) ) {
43 - $site = $matches[1];
44 - if ( preg_match( '/^(.*)\.' . preg_quote( $site ) . '\.org$/', $server, $matches ) ) {
45 - $lang = $matches[1];
46 - // For some special subdomains, like pa.us
47 - $lang = str_replace( '.', '-', $lang );
48 - } else if ( preg_match( '/^(.*)\.prototype\.wikimedia\.org$/', $server, $matches ) ) {
49 - $lang = $matches[1];
50 - } else {
51 - die( "Invalid host name ($server), can't determine language" );
52 - }
53 - } elseif ( preg_match( "/^\/usr\/local\/apache\/(?:htdocs|common\/docroot)\/([a-z0-9\-_]*)$/", $docRoot, $matches ) ) {
54 - $site = "wikipedia";
55 - $lang = $matches[1];
56 - } else {
57 - die( "Invalid host name (docroot=" . $_SERVER['DOCUMENT_ROOT'] . "), can't determine language" );
58 - }
59 - }
60 - }
 15+ $siteInfo = $multiVersion->getSiteInfo( $_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'] );
6116 }
62 - if ( $site == "wikipedia" ) {
63 - $dbSuffix = "wiki";
64 - } else {
65 - $dbSuffix = $site;
66 - }
67 - $dbname = str_replace( "-", "_", $lang . $dbSuffix );
68 - return $dbname;
69 -}
 17+
 18+ $version = $multiVersion->getVersion( $siteInfo['site'], $siteInfo['lang']);
7019
71 -function getMediaWiki( $file ) {
72 - $dbname = host2db( );
73 -
74 - $db = dba_open( '/usr/local/apache/common/wikiversions.db', 'r', 'cdb' );
75 - if ( $db ) {
76 - $version = dba_fetch( $dbname, $db );
77 - } else {
78 - //don't error for now
79 - //trigger_error( "Unable to open /usr/local/apache/common/wikiversions.db. Assuming php-1.17", E_USER_ERROR );
80 - $version = 'php-1.17';
81 - }
82 -
8320 if ( $host == 'test.wikipedia.org' && !$secure &&
8421 !preg_match( '!thumb\.php!', $_SERVER['REQUEST_URI'] ) ) {
8522 define( 'TESTWIKI', 1 );
@@ -94,5 +31,4 @@
9532 return "$IP/$file";
9633 }
9734
98 -?>
9935
Index: trunk/tools/mwmultiversion/wmf-config/CommonSettings.php
@@ -98,56 +98,18 @@
9999 wfProfileIn( "$fname-host" );
100100
101101 # Determine domain and language
102 -$secure = getenv( 'MW_SECURE_HOST' );
 102+require_once( $IP . '/../wmf-config/MWMultiVersion.php' );
 103+$multiVersion = new MWMultiVersion;
 104+$siteInfo = array();
103105 if ( (@$_SERVER['SCRIPT_NAME']) == '/w/thumb.php' && (@$_SERVER['SERVER_NAME']) == 'upload.wikimedia.org' ) {
104 - $pathBits = explode( '/', $_SERVER['PATH_INFO'] );
105 - $site = $pathBits[1];
106 - $lang = $pathBits[2];
107 -} elseif (php_sapi_name() == 'cgi-fcgi') {
108 - if (!preg_match('/^([^.]+).([^.]+).*$/', $_SERVER['SERVER_NAME'], $m))
109 - die("invalid hostname");
110 -
111 - $lang = $m[1];
112 - $site = $m[2];
113 -
114 - if (in_array($lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
115 - $site = "wikipedia";
116 -} elseif( $secure ) {
117 - if (!preg_match('/^([^.]+).([^.]+).*$/', $secure, $m))
118 - die("invalid hostname");
119 -
120 - $lang = $m[1];
121 - $site = $m[2];
122 -
123 - if (in_array($lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
124 - $site = "wikipedia";
 106+ $siteInfo = $multiVersion->getUploadSiteInfo( $_SERVER['PATH_INFO'] );
125107 } else {
126 - if ( !isset( $site ) ) {
127 - $site = "wikipedia";
128 - if ( !isset( $lang ) ) {
129 -
130 - $server = $_SERVER['SERVER_NAME'];
131 - $docRoot = $_SERVER['DOCUMENT_ROOT'];
132 - if ( preg_match( '/^(?:\/usr\/local\/apache\/|\/home\/wikipedia\/)(?:htdocs|common\/docroot)\/([a-z]+)\.org/', $docRoot, $matches ) ) {
133 - $site = $matches[1];
134 - if ( preg_match( '/^(.*)\.' . preg_quote( $site ) . '\.org$/', $server, $matches ) ) {
135 - $lang = $matches[1];
136 - // For some special subdomains, like pa.us
137 - $lang = str_replace( '.', '-', $lang );
138 - } else {
139 - die( "Invalid host name, can't determine language" );
140 - }
141 - } elseif ( preg_match( "/^\/usr\/local\/apache\/(?:htdocs|common\/docroot)\/([a-z0-9\-_]*)$/", $docRoot, $matches ) ) {
142 - $site = "wikipedia";
143 - $lang = $matches[1];
144 - } else {
145 - die( "Invalid host name (docroot=" . $_SERVER['DOCUMENT_ROOT'] . "), can't determine language" );
146 - }
147 - }
148 - }
 108+ $siteInfo = $multiVersion->getSiteInfo( $_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'] );
149109 }
 110+$site = $siteInfo['site'];
 111+$lang = $siteInfo['lang'];
 112+$wgDBname = $multiVersion->getDatabase( $site, $lang);
150113
151 -
152114 # Disabled, no IPv6 support, waste of a regex -- TS 20051207
153115 /*
154116 $ipv6 = false;
@@ -156,13 +118,6 @@
157119 }*/
158120
159121
160 -if ( $site == "wikipedia" ) {
161 - $dbSuffix = "wiki";
162 -} else {
163 - $dbSuffix = $site;
164 -}
165 -$wgDBname = str_replace( "-", "_", $lang . $dbSuffix );
166 -
167122 //changed for hetdeploy testing --pdhanda
168123 $match = array();
169124 if ( preg_match("/^[0-9.]*/", $wgVersion, $match) ) {
@@ -190,7 +145,7 @@
191146 wfProfileIn( "$fname-confcache" );
192147
193148 # Is this database listed in $cluster.dblist?
194 -if ( array_search( $wgDBname, $wgLocalDatabases ) === false ){
 149+if ( array_search( $wgDBname, $wgLocalDatabases ) === false ){
195150 # No? Load missing.php
196151 if ( $wgCommandLineMode) {
197152 print "Database name $wgDBname is not listed in $cluster.dblist\n";
Index: trunk/tools/mwmultiversion/wmf-config/MWMultiVersion.php
@@ -0,0 +1,99 @@
 2+<?php
 3+
 4+class MWMultiVersion {
 5+
 6+ function getSiteInfo( $serverName, $docRoot ) {
 7+ $secure = getenv( 'MW_SECURE_HOST' );
 8+ $matches = array();
 9+ if (php_sapi_name() == 'cgi-fcgi') {
 10+ if (!preg_match('/^([^.]+).([^.]+).*$/', $serverName, $matches))
 11+ die("invalid hostname");
 12+
 13+ $lang = $matches[1];
 14+ $site = $$matches[2];
 15+
 16+ if (in_array($lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
 17+ $site = "wikipedia";
 18+ } elseif( $secure ) {
 19+ if (!preg_match('/^([^.]+).([^.]+).*$/', $secure, $$matches))
 20+ die("invalid hostname");
 21+
 22+ $lang = $$matches[1];
 23+ $site = $$matches[2];
 24+
 25+ if (in_array($lang, array("commons", "grants", "sources", "wikimania", "wikimania2006", "foundation", "meta")))
 26+ $site = "wikipedia";
 27+ } else {
 28+ if ( !isset( $site ) ) {
 29+ $site = "wikipedia";
 30+ if ( !isset( $lang ) ) {
 31+ 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];
 35+ // For some special subdomains, like pa.us
 36+ $lang = str_replace( '.', '-', $lang );
 37+ } else if ( preg_match( '/^(.*)\.prototype\.wikimedia\.org$/', $serverName, $matches ) ) {
 38+ $lang = $matches[1];
 39+ } else {
 40+ die( "Invalid host name ($serverName), can't determine language" );
 41+ }
 42+ } 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' );
 47+ } else {
 48+ die( "Invalid host name (docroot=" . $_SERVER['DOCUMENT_ROOT'] . "), can't determine language" );
 49+ }
 50+ }
 51+ }
 52+ }
 53+ return array(
 54+ 'site' => $site,
 55+ 'lang' => $lang,
 56+ );
 57+
 58+ }
 59+
 60+ function getUploadSiteInfo( $pathInfo) {
 61+ $pathBits = explode( '/', $pathInfo );
 62+ $site = $pathBits[1];
 63+ $lang = $pathBits[2];
 64+ return array(
 65+ 'site' => $site,
 66+ 'lang' => $lang,
 67+ );
 68+ }
 69+
 70+ function getDatabase( $site, $lang ) {
 71+ $dbname = getenv( 'MW_DBNAME' );
 72+ if ( strlen( $dbname ) == 0 ) {
 73+ if ( $site == "wikipedia" ) {
 74+ $dbSuffix = "wiki";
 75+ } else {
 76+ $dbSuffix = $site;
 77+ }
 78+ $dbname = str_replace( "-", "_", $lang . $dbSuffix );
 79+ putenv( 'MW_DBNAME=' . $dbname );
 80+ }
 81+ return $dbname;
 82+
 83+ }
 84+
 85+ function getVersion( $site, $lang ) {
 86+ $dbname = $this->getDatabase( $site, $lang );
 87+ $db = dba_open( '/usr/local/apache/common/wikiversions.db', 'r', 'cdb' );
 88+ if ( $db ) {
 89+ $version = dba_fetch( $dbname, $db );
 90+ } else {
 91+ //don't error for now
 92+ //trigger_error( "Unable to open /usr/local/apache/common/wikiversions.db. Assuming php-1.17", E_USER_ERROR );
 93+ $version = 'php-1.17';
 94+ }
 95+ return $version;
 96+ }
 97+
 98+}
 99+
 100+?>
\ No newline at end of file
Property changes on: trunk/tools/mwmultiversion/wmf-config/MWMultiVersion.php
___________________________________________________________________
Added: svn:eol-style
1101 + native

Status & tagging log