MediaWiki
REL1_23
|
00001 <?php 00031 class DatabaseMysqli extends DatabaseMysqlBase { 00036 protected function doQuery( $sql ) { 00037 if ( $this->bufferResults() ) { 00038 $ret = $this->mConn->query( $sql ); 00039 } else { 00040 $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT ); 00041 } 00042 00043 return $ret; 00044 } 00045 00051 protected function mysqlConnect( $realServer ) { 00052 global $wgDBmysql5; 00053 # Fail now 00054 # Otherwise we get a suppressed fatal error, which is very hard to track down 00055 if ( !function_exists( 'mysqli_init' ) ) { 00056 throw new DBConnectionError( $this, "MySQLi functions missing," 00057 . " have you compiled PHP with the --with-mysqli option?\n" ); 00058 } 00059 00060 // Other than mysql_connect, mysqli_real_connect expects an explicit port 00061 // and socket parameters. So we need to parse the port and socket out of 00062 // $realServer 00063 $port = null; 00064 $socket = null; 00065 $hostAndPort = IP::splitHostAndPort( $realServer ); 00066 if ( $hostAndPort ) { 00067 $realServer = $hostAndPort[0]; 00068 if ( $hostAndPort[1] ) { 00069 $port = $hostAndPort[1]; 00070 } 00071 } elseif ( substr_count( $realServer, ':' ) == 1 ) { 00072 // If we have a colon and something that's not a port number 00073 // inside the hostname, assume it's the socket location 00074 $hostAndSocket = explode( ':', $realServer ); 00075 $realServer = $hostAndSocket[0]; 00076 $socket = $hostAndSocket[1]; 00077 } 00078 00079 $connFlags = 0; 00080 if ( $this->mFlags & DBO_SSL ) { 00081 $connFlags |= MYSQLI_CLIENT_SSL; 00082 } 00083 if ( $this->mFlags & DBO_COMPRESS ) { 00084 $connFlags |= MYSQLI_CLIENT_COMPRESS; 00085 } 00086 if ( $this->mFlags & DBO_PERSISTENT ) { 00087 $realServer = 'p:' . $realServer; 00088 } 00089 00090 $mysqli = mysqli_init(); 00091 if ( $wgDBmysql5 ) { 00092 // Tell the server we're communicating with it in UTF-8. 00093 // This may engage various charset conversions. 00094 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' ); 00095 } else { 00096 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' ); 00097 } 00098 00099 $numAttempts = 2; 00100 for ( $i = 0; $i < $numAttempts; $i++ ) { 00101 if ( $i > 1 ) { 00102 usleep( 1000 ); 00103 } 00104 if ( $mysqli->real_connect( $realServer, $this->mUser, 00105 $this->mPassword, $this->mDBname, $port, $socket, $connFlags ) 00106 ) { 00107 return $mysqli; 00108 } 00109 } 00110 00111 return false; 00112 } 00113 00114 protected function connectInitCharset() { 00115 // already done in mysqlConnect() 00116 return true; 00117 } 00118 00123 protected function mysqlSetCharset( $charset ) { 00124 if ( method_exists( $this->mConn, 'set_charset' ) ) { 00125 return $this->mConn->set_charset( $charset ); 00126 } else { 00127 return $this->query( 'SET NAMES ' . $charset, __METHOD__ ); 00128 } 00129 } 00130 00134 protected function closeConnection() { 00135 return $this->mConn->close(); 00136 } 00137 00141 function insertId() { 00142 return $this->mConn->insert_id; 00143 } 00144 00148 function lastErrno() { 00149 if ( $this->mConn ) { 00150 return $this->mConn->errno; 00151 } else { 00152 return mysqli_connect_errno(); 00153 } 00154 } 00155 00159 function affectedRows() { 00160 return $this->mConn->affected_rows; 00161 } 00162 00167 function selectDB( $db ) { 00168 $this->mDBname = $db; 00169 00170 return $this->mConn->select_db( $db ); 00171 } 00172 00176 function getServerVersion() { 00177 return $this->mConn->server_info; 00178 } 00179 00184 protected function mysqlFreeResult( $res ) { 00185 $res->free_result(); 00186 00187 return true; 00188 } 00189 00194 protected function mysqlFetchObject( $res ) { 00195 $object = $res->fetch_object(); 00196 if ( $object === null ) { 00197 return false; 00198 } 00199 00200 return $object; 00201 } 00202 00207 protected function mysqlFetchArray( $res ) { 00208 $array = $res->fetch_array(); 00209 if ( $array === null ) { 00210 return false; 00211 } 00212 00213 return $array; 00214 } 00215 00220 protected function mysqlNumRows( $res ) { 00221 return $res->num_rows; 00222 } 00223 00228 protected function mysqlNumFields( $res ) { 00229 return $res->field_count; 00230 } 00231 00237 protected function mysqlFetchField( $res, $n ) { 00238 $field = $res->fetch_field_direct( $n ); 00239 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG; 00240 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG; 00241 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG; 00242 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG; 00243 $field->binary = $field->flags & MYSQLI_BINARY_FLAG; 00244 00245 return $field; 00246 } 00247 00253 protected function mysqlFieldName( $res, $n ) { 00254 $field = $res->fetch_field_direct( $n ); 00255 00256 return $field->name; 00257 } 00258 00264 protected function mysqlFieldType( $res, $n ) { 00265 $field = $res->fetch_field_direct( $n ); 00266 00267 return $field->type; 00268 } 00269 00275 protected function mysqlDataSeek( $res, $row ) { 00276 return $res->data_seek( $row ); 00277 } 00278 00283 protected function mysqlError( $conn = null ) { 00284 if ( $conn === null ) { 00285 return mysqli_connect_error(); 00286 } else { 00287 return $conn->error; 00288 } 00289 } 00290 00296 protected function mysqlRealEscapeString( $s ) { 00297 return $this->mConn->real_escape_string( $s ); 00298 } 00299 00300 protected function mysqlPing() { 00301 return $this->mConn->ping(); 00302 } 00303 00309 public function __toString() { 00310 if ( $this->mConn instanceof Mysqli ) { 00311 return (string)$this->mConn->thread_id; 00312 } else { 00313 // mConn might be false or something. 00314 return (string)$this->mConn; 00315 } 00316 } 00317 }