00001 <?php
00009 class DatabaseMysql extends DatabaseBase {
00010 function getType() {
00011 return 'mysql';
00012 }
00013
00014 function doQuery( $sql ) {
00015 if( $this->bufferResults() ) {
00016 $ret = mysql_query( $sql, $this->mConn );
00017 } else {
00018 $ret = mysql_unbuffered_query( $sql, $this->mConn );
00019 }
00020 return $ret;
00021 }
00022
00023 function open( $server, $user, $password, $dbName ) {
00024 global $wgAllDBsAreLocalhost;
00025 wfProfileIn( __METHOD__ );
00026
00027 # Test for missing mysql.so
00028 # First try to load it
00029 if (!@extension_loaded('mysql')) {
00030 @dl('mysql.so');
00031 }
00032
00033 # Fail now
00034 # Otherwise we get a suppressed fatal error, which is very hard to track down
00035 if ( !function_exists( 'mysql_connect' ) ) {
00036 throw new DBConnectionError( $this, "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n" );
00037 }
00038
00039 # Debugging hack -- fake cluster
00040 if ( $wgAllDBsAreLocalhost ) {
00041 $realServer = 'localhost';
00042 } else {
00043 $realServer = $server;
00044 }
00045 $this->close();
00046 $this->mServer = $server;
00047 $this->mUser = $user;
00048 $this->mPassword = $password;
00049 $this->mDBname = $dbName;
00050
00051 $success = false;
00052
00053 wfProfileIn("dbconnect-$server");
00054
00055 # The kernel's default SYN retransmission period is far too slow for us,
00056 # so we use a short timeout plus a manual retry. Retrying means that a small
00057 # but finite rate of SYN packet loss won't cause user-visible errors.
00058 $this->mConn = false;
00059 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
00060 $numAttempts = 2;
00061 } else {
00062 $numAttempts = 1;
00063 }
00064 $this->installErrorHandler();
00065 for ( $i = 0; $i < $numAttempts && !$this->mConn; $i++ ) {
00066 if ( $i > 1 ) {
00067 usleep( 1000 );
00068 }
00069 if ( $this->mFlags & DBO_PERSISTENT ) {
00070 $this->mConn = mysql_pconnect( $realServer, $user, $password );
00071 } else {
00072 # Create a new connection...
00073 $this->mConn = mysql_connect( $realServer, $user, $password, true );
00074 }
00075 if ($this->mConn === false) {
00076 #$iplus = $i + 1;
00077 #wfLogDBError("Connect loop error $iplus of $max ($server): " . mysql_errno() . " - " . mysql_error()."\n");
00078 }
00079 }
00080 $phpError = $this->restoreErrorHandler();
00081 # Always log connection errors
00082 if ( !$this->mConn ) {
00083 $error = $this->lastError();
00084 if ( !$error ) {
00085 $error = $phpError;
00086 }
00087 wfLogDBError( "Error connecting to {$this->mServer}: $error\n" );
00088 wfDebug( "DB connection error\n" );
00089 wfDebug( "Server: $server, User: $user, Password: " .
00090 substr( $password, 0, 3 ) . "..., error: " . mysql_error() . "\n" );
00091 $success = false;
00092 }
00093
00094 wfProfileOut("dbconnect-$server");
00095
00096 if ( $dbName != '' && $this->mConn !== false ) {
00097 $success = @mysql_select_db( $dbName, $this->mConn );
00098 if ( !$success ) {
00099 $error = "Error selecting database $dbName on server {$this->mServer} " .
00100 "from client host " . wfHostname() . "\n";
00101 wfLogDBError(" Error selecting database $dbName on server {$this->mServer} \n");
00102 wfDebug( $error );
00103 }
00104 } else {
00105 # Delay USE query
00106 $success = (bool)$this->mConn;
00107 }
00108
00109 if ( $success ) {
00110 $version = $this->getServerVersion();
00111 if ( version_compare( $version, '4.1' ) >= 0 ) {
00112
00113
00114 global $wgDBmysql5;
00115 if( $wgDBmysql5 ) {
00116 $this->query( 'SET NAMES utf8', __METHOD__ );
00117 }
00118
00119 $this->query( "SET sql_mode = ''", __METHOD__ );
00120 }
00121
00122
00123 } else {
00124 $this->reportConnectionError( $phpError );
00125 }
00126
00127 $this->mOpened = $success;
00128 wfProfileOut( __METHOD__ );
00129 return $success;
00130 }
00131
00132 function close() {
00133 $this->mOpened = false;
00134 if ( $this->mConn ) {
00135 if ( $this->trxLevel() ) {
00136 $this->commit();
00137 }
00138 return mysql_close( $this->mConn );
00139 } else {
00140 return true;
00141 }
00142 }
00143
00144 function freeResult( $res ) {
00145 if ( $res instanceof ResultWrapper ) {
00146 $res = $res->result;
00147 }
00148 if ( !@mysql_free_result( $res ) ) {
00149 throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
00150 }
00151 }
00152
00153 function fetchObject( $res ) {
00154 if ( $res instanceof ResultWrapper ) {
00155 $res = $res->result;
00156 }
00157 @$row = mysql_fetch_object( $res );
00158 if( $this->lastErrno() ) {
00159 throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) );
00160 }
00161 return $row;
00162 }
00163
00164 function fetchRow( $res ) {
00165 if ( $res instanceof ResultWrapper ) {
00166 $res = $res->result;
00167 }
00168 @$row = mysql_fetch_array( $res );
00169 if ( $this->lastErrno() ) {
00170 throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) );
00171 }
00172 return $row;
00173 }
00174
00175 function numRows( $res ) {
00176 if ( $res instanceof ResultWrapper ) {
00177 $res = $res->result;
00178 }
00179 @$n = mysql_num_rows( $res );
00180 if( $this->lastErrno() ) {
00181 throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( $this->lastError() ) );
00182 }
00183 return $n;
00184 }
00185
00186 function numFields( $res ) {
00187 if ( $res instanceof ResultWrapper ) {
00188 $res = $res->result;
00189 }
00190 return mysql_num_fields( $res );
00191 }
00192
00193 function fieldName( $res, $n ) {
00194 if ( $res instanceof ResultWrapper ) {
00195 $res = $res->result;
00196 }
00197 return mysql_field_name( $res, $n );
00198 }
00199
00200 function insertId() { return mysql_insert_id( $this->mConn ); }
00201
00202 function dataSeek( $res, $row ) {
00203 if ( $res instanceof ResultWrapper ) {
00204 $res = $res->result;
00205 }
00206 return mysql_data_seek( $res, $row );
00207 }
00208
00209 function lastErrno() {
00210 if ( $this->mConn ) {
00211 return mysql_errno( $this->mConn );
00212 } else {
00213 return mysql_errno();
00214 }
00215 }
00216
00217 function lastError() {
00218 if ( $this->mConn ) {
00219 # Even if it's non-zero, it can still be invalid
00220 wfSuppressWarnings();
00221 $error = mysql_error( $this->mConn );
00222 if ( !$error ) {
00223 $error = mysql_error();
00224 }
00225 wfRestoreWarnings();
00226 } else {
00227 $error = mysql_error();
00228 }
00229 if( $error ) {
00230 $error .= ' (' . $this->mServer . ')';
00231 }
00232 return $error;
00233 }
00234
00235 function affectedRows() { return mysql_affected_rows( $this->mConn ); }
00236
00242 public function estimateRowCount( $table, $vars='*', $conds='', $fname = 'Database::estimateRowCount', $options = array() ) {
00243 $options['EXPLAIN'] = true;
00244 $res = $this->select( $table, $vars, $conds, $fname, $options );
00245 if ( $res === false )
00246 return false;
00247 if ( !$this->numRows( $res ) ) {
00248 $this->freeResult($res);
00249 return 0;
00250 }
00251
00252 $rows = 1;
00253 while( $plan = $this->fetchObject( $res ) ) {
00254 $rows *= $plan->rows > 0 ? $plan->rows : 1;
00255 }
00256
00257 $this->freeResult($res);
00258 return $rows;
00259 }
00260
00261 function fieldInfo( $table, $field ) {
00262 $table = $this->tableName( $table );
00263 $res = $this->query( "SELECT * FROM $table LIMIT 1" );
00264 $n = mysql_num_fields( $res->result );
00265 for( $i = 0; $i < $n; $i++ ) {
00266 $meta = mysql_fetch_field( $res->result, $i );
00267 if( $field == $meta->name ) {
00268 return new MySQLField($meta);
00269 }
00270 }
00271 return false;
00272 }
00273
00274 function selectDB( $db ) {
00275 $this->mDBname = $db;
00276 return mysql_select_db( $db, $this->mConn );
00277 }
00278
00279 function strencode( $s ) {
00280 return mysql_real_escape_string( $s, $this->mConn );
00281 }
00282
00283 function ping() {
00284 if( !function_exists( 'mysql_ping' ) ) {
00285 wfDebug( "Tried to call mysql_ping but this is ancient PHP version. Faking it!\n" );
00286 return true;
00287 }
00288 $ping = mysql_ping( $this->mConn );
00289 if ( $ping ) {
00290 return true;
00291 }
00292
00293
00294 if ( version_compare( mysql_get_client_info(), '5.0.13', '>=' ) ) {
00295 mysql_close( $this->mConn );
00296 $this->mOpened = false;
00297 $this->mConn = false;
00298 $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
00299 return true;
00300 }
00301 return false;
00302 }
00303
00304 function getServerVersion() {
00305 return mysql_get_server_info( $this->mConn );
00306 }
00307
00308 function useIndexClause( $index ) {
00309 return "FORCE INDEX (" . $this->indexName( $index ) . ")";
00310 }
00311
00312 function lowPriorityOption() {
00313 return 'LOW_PRIORITY';
00314 }
00315
00316 function getSoftwareLink() {
00317 return '[http://www.mysql.com/ MySQL]';
00318 }
00319
00320 function standardSelectDistinct() {
00321 return false;
00322 }
00323
00324 public function setTimeout( $timeout ) {
00325 $this->query( "SET net_read_timeout=$timeout" );
00326 $this->query( "SET net_write_timeout=$timeout" );
00327 }
00328
00329 public function lock( $lockName, $method, $timeout = 5 ) {
00330 $lockName = $this->addQuotes( $lockName );
00331 $result = $this->query( "SELECT GET_LOCK($lockName, $timeout) AS lockstatus", $method );
00332 $row = $this->fetchObject( $result );
00333 $this->freeResult( $result );
00334
00335 if( $row->lockstatus == 1 ) {
00336 return true;
00337 } else {
00338 wfDebug( __METHOD__." failed to acquire lock\n" );
00339 return false;
00340 }
00341 }
00342
00343 public function unlock( $lockName, $method ) {
00344 $lockName = $this->addQuotes( $lockName );
00345 $result = $this->query( "SELECT RELEASE_LOCK($lockName) as lockstatus", $method );
00346 $row = $this->fetchObject( $result );
00347 return $row->lockstatus;
00348 }
00349
00350 public function lockTables( $read, $write, $method, $lowPriority = true ) {
00351 $items = array();
00352
00353 foreach( $write as $table ) {
00354 $tbl = $this->tableName( $table ) .
00355 ( $lowPriority ? ' LOW_PRIORITY' : '' ) .
00356 ' WRITE';
00357 $items[] = $tbl;
00358 }
00359 foreach( $read as $table ) {
00360 $items[] = $this->tableName( $table ) . ' READ';
00361 }
00362 $sql = "LOCK TABLES " . implode( ',', $items );
00363 $this->query( $sql, $method );
00364 }
00365
00366 public function unlockTables( $method ) {
00367 $this->query( "UNLOCK TABLES", $method );
00368 }
00369
00370 public function setBigSelects( $value = true ) {
00371 if ( $value === 'default' ) {
00372 if ( $this->mDefaultBigSelects === null ) {
00373 # Function hasn't been called before so it must already be set to the default
00374 return;
00375 } else {
00376 $value = $this->mDefaultBigSelects;
00377 }
00378 } elseif ( $this->mDefaultBigSelects === null ) {
00379 $this->mDefaultBigSelects = (bool)$this->selectField( false, '@@sql_big_selects' );
00380 }
00381 $encValue = $value ? '1' : '0';
00382 $this->query( "SET sql_big_selects=$encValue", __METHOD__ );
00383 }
00384
00385
00389 function wasDeadlock() {
00390 return $this->lastErrno() == 1213;
00391 }
00392
00397 function wasErrorReissuable() {
00398 return $this->lastErrno() == 2013 || $this->lastErrno() == 2006;
00399 }
00400
00404 function wasReadOnlyError() {
00405 return $this->lastErrno() == 1223 ||
00406 ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
00407 }
00408
00409 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseMysql::duplicateTableStructure' ) {
00410 $tmp = $temporary ? 'TEMPORARY ' : '';
00411 if ( strcmp( $this->getServerVersion(), '4.1' ) < 0 ) {
00412 # Hack for MySQL versions < 4.1, which don't support
00413 # "CREATE TABLE ... LIKE". Note that
00414 # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
00415 # would not create the indexes we need....
00416 #
00417 # Note that we don't bother changing around the prefixes here be-
00418 # cause we know we're using MySQL anyway.
00419
00420 $res = $this->query( "SHOW CREATE TABLE $oldName" );
00421 $row = $this->fetchRow( $res );
00422 $oldQuery = $row[1];
00423 $query = preg_replace( '/CREATE TABLE `(.*?)`/',
00424 "CREATE $tmp TABLE `$newName`", $oldQuery );
00425 if ($oldQuery === $query) {
00426 # Couldn't do replacement
00427 throw new MWException( "could not create temporary table $newName" );
00428 }
00429 } else {
00430 $query = "CREATE $tmp TABLE $newName (LIKE $oldName)";
00431 }
00432 $this->query( $query, $fname );
00433 }
00434
00435 }
00436
00440 class Database extends DatabaseMysql {}
00441
00442 class MySQLMasterPos {
00443 var $file, $pos;
00444
00445 function __construct( $file, $pos ) {
00446 $this->file = $file;
00447 $this->pos = $pos;
00448 }
00449
00450 function __toString() {
00451 return "{$this->file}/{$this->pos}";
00452 }
00453 }