r89816 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89815‎ | r89816 | r89817 >
Date:07:36, 10 June 2011
Author:tstarling
Status:ok
Tags:
Comment:
Reverted r89393. A single Database object certainly should not be attempting to manage multiple database connections, that is the job of LBFactory/LoadBalancer. I would like to see $wgSharedDB managed by LBFactory instead, for all DBMSes. Then the cruft in Database::tableName() can be removed.

r89393 caused tableExists() etc. to be completely broken, so PostgreSQL upgrade was broken too, see CR.
Modified paths:
  • /trunk/phase3/CREDITS (modified) (history)
  • /trunk/phase3/includes/db/DatabasePostgres.php (modified) (history)

Diff [purge]

Index: trunk/phase3/CREDITS
@@ -108,7 +108,6 @@
109109 * Lejonel
110110 * liangent
111111 * Louperivois
112 -* Luca Fulchir
113112 * Lucas Garczewski
114113 * Luigi Corsaro
115114 * Manuel Menal
Index: trunk/phase3/includes/db/DatabasePostgres.php
@@ -7,7 +7,7 @@
88 */
99
1010 class PostgresField implements Field {
11 - private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname, $sharedmConn = null;
 11+ private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname;
1212
1313 /**
1414 * @param $db DatabaseBase
@@ -152,7 +152,7 @@
153153 throw new DBConnectionError( $this, "Postgres functions missing, have you compiled PHP with the --with-pgsql option?\n (Note: if you recently installed PHP, you may need to restart your webserver and database)\n" );
154154 }
155155
156 - global $wgDBport, $wgSharedDB;
 156+ global $wgDBport;
157157
158158 if ( !strlen( $user ) ) { # e.g. the class is being loaded
159159 return;
@@ -189,30 +189,6 @@
190190 throw new DBConnectionError( $this, $phpError );
191191 }
192192
193 - if( $wgSharedDB ) {
194 - $connectVars = array(
195 - 'dbname' => $wgSharedDB,
196 - 'user' => $user,
197 - 'password' => $password );
198 - if ($server!=false && $server!="") {
199 - $connectVars['host'] = $server;
200 - }
201 - if ($port!=false && $port!="") {
202 - $connectVars['port'] = $port;
203 - }
204 - $connectString = $this->makeConnectionString( $connectVars, PGSQL_CONNECT_FORCE_NEW );
205 -
206 - $this->installErrorHandler();
207 - $this->sharedmConn = pg_connect( $connectString );
208 - $phpError = $this->restoreErrorHandler();
209 - if ( $this->sharedmConn == false ) {
210 - wfDebug( "SharedDB connection error\n" );
211 - wfDebug( "Server: $server, Database: $wgSharedDB, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
212 - wfDebug( $this->lastError()."\n" );
213 - $this->close();
214 - throw new DBConnectionError( $this, $phpError );
215 - }
216 - }
217193 $this->mOpened = true;
218194
219195 global $wgCommandLineMode;
@@ -258,56 +234,19 @@
259235 * Returns success, true if already closed
260236 */
261237 function close() {
262 - $mainConn = true;
263 - $sharedConn = true;
264 - if ( $this->mConn ) {
265 - $mainConn = pg_close ( $this->mConn );
 238+ $this->mOpened = false;
 239+ if ( $this->mConn ) {
 240+ return pg_close( $this->mConn );
 241+ } else {
 242+ return true;
266243 }
267 - if ( isset( $this->sharedmConn ) && $this->sharedmConn ) {
268 - $sharedConn = pg_close ( $this->sharedmConn );
269 - }
270 - $this->mOpened = !($mainConn && $sharedConn);
271 - return !$this->mOpened;
272244 }
273245
274246 function doQuery( $sql ) {
275 - global $wgSharedDB;
276247 if ( function_exists( 'mb_convert_encoding' ) ) {
277248 $sql = mb_convert_encoding( $sql, 'UTF-8' );
278249 }
279 - # we can find the shared tables after FROM or UPDATE statements.
280 - $FROMpos = strpos( $sql, 'FROM' );
281 - if ( $FROMpos === false ) {
282 - $FROMpos = strpos( $sql, 'UPDATE' );
283 - if ( $FROMpos === false ) {
284 - # does this query even exists?
285 - $this->mLastResult = pg_query( $this->mConn, $sql );
286 - $this->mAffectedRows = null; // use pg_affected_rows(mLastResult)
287 - return $this->mLastResult;
288 - }
289 - }
290 - # check if we should connect to the shared database
291 - # skip FROM/UPDATE statement
292 - $FROMquery = substr( $sql, $FROMpos + ($sql[$FROMpos] == 'F' ? 4 : 6) );
293 - $trimmed = trim( $FROMquery );
294 - $FROMquery = $trimmed;
295 - # skip eventual comment and spaces
296 - if ( substr( $FROMquery, 0, 2 ) == '/*' ) {
297 - $FROMquery = substr( $FROMquery, strpos( $FROMquery, '*/') + 2 );
298 - $trimmed = trim( $FROMquery );
299 - $FROMquery = $trimmed;
300 - }
301 - # select only dbname
302 - $FROMquery = substr( $FROMquery, 0, strpos($FROMquery, ' ') );
303 - $FROMvalue = explode( '.', $FROMquery );
304 - # db is always quoted, compare it to wgSharedDB
305 - if ( $FROMvalue[0] == "\"$wgSharedDB\"" ) {
306 - # shared db requested. switch connection
307 - $this->mLastResult = pg_query( $this->sharedmConn, $sql );
308 - } else {
309 - # main db: basic query
310 - $this->mLastResult = pg_query( $this->mConn, $sql );
311 - }
 250+ $this->mLastResult = pg_query( $this->mConn, $sql );
312251 $this->mAffectedRows = null; // use pg_affected_rows(mLastResult)
313252 return $this->mLastResult;
314253 }
@@ -671,81 +610,15 @@
672611 }
673612
674613 function tableName( $name, $quoted = true ) {
675 - global $wgSharedDB, $wgSharedPrefix, $wgSharedTables, $wgDBmwschema;
676614 # Replace reserved words with better ones
677615 switch( $name ) {
678616 case 'user':
679 - $name = 'mwuser';
680 - break;
681 - case '"user"':
682 - $name = '"mwuser"';
683 - break;
684 - case 'text':
685 - $name = 'pagecontent';
686 - break;
687 - case 'text':
688 - $name = 'pagecontent';
689 - break;
 617+ return 'mwuser';
 618+ case 'text':
 619+ return 'pagecontent';
 620+ default:
 621+ return parent::tableName( $name, $quoted );
690622 }
691 - # Skip the entire process when we have a string quoted on both ends.
692 - # Note that we check the end so that we will still quote any use of
693 - # use of `database`.table. But won't break things if someone wants
694 - # to query a database table with a dot in the name.
695 - if ( $name[0] == '"' && substr( $name, -1, 1 ) == '"' ) {
696 - return $name;
697 - }
698 -
699 - # Lets test for any bits of text that should never show up in a table
700 - # name. Basically anything like JOIN or ON which are actually part of
701 - # SQL queries, but may end up inside of the table value to combine
702 - # sql. Such as how the API is doing.
703 - # Note that we use a whitespace test rather than a \b test to avoid
704 - # any remote case where a word like on may be inside of a table name
705 - # surrounded by symbols which may be considered word breaks.
706 - if( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
707 - return $name;
708 - }
709 -
710 - # Split database and table into proper variables.
711 - # We reverse the explode so that database.table and table both output
712 - # the correct table.
713 - $dbDetails = array_reverse( explode( '.', $name, 2 ) );
714 - if ( isset( $dbDetails[1] ) ) {
715 - @list( $table, $database ) = $dbDetails;
716 - } else {
717 - @list( $table ) = $dbDetails;
718 - }
719 - $prefix = $this->mTablePrefix; # Default prefix
720 -
721 - # A database name has been specified in input. Quote the table name
722 - # because we don't want any prefixes added.
723 - if( isset($database) ) {
724 - $table = ( $table[0] == '"' ? $table : "\"{$table}\"" );
725 - }
726 -
727 - # Note that we use the long format because php will complain in in_array if
728 - # the input is not an array, and will complain in is_array if it is not set.
729 - if( !isset( $database ) # Don't use shared database if pre selected.
730 - && isset( $wgSharedDB ) # We have a shared database
731 - && $table[0] != '"' # Paranoia check to prevent shared tables listing '`table`'
732 - && isset( $wgSharedTables )
733 - && is_array( $wgSharedTables )
734 - && in_array( $table, $wgSharedTables ) ) { # A shared table is selected
735 - $database = $wgSharedDB;
736 - $prefix = isset( $wgSharedPrefix ) ? $wgSharedPrefix : $prefix;
737 - }
738 -
739 - # Quote the $database and $table and apply the prefix if not quoted.
740 - if( isset($database) ) {
741 - $database = ( $database[0] == '"' ? $database : "\"{$database}\"" );
742 - }
743 - $table = ( $table[0] == '"' ? $table : "\"{$prefix}{$table}\"" );
744 -
745 - # Merge our database and table into our final table name.
746 - $tableName = ( isset($database) ? "{$database}.\"{$wgDBmwschema}\".{$table}" : "{$table}" );
747 -
748 - # We're finished, return.
749 - return $tableName;
750623 }
751624
752625 /**

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r89393Apply a patch adapted from the one on Bug #16794...mah04:10, 3 June 2011

Status & tagging log