[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * This is the MySQLi database abstraction layer. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 * http://www.gnu.org/copyleft/gpl.html 19 * 20 * @file 21 * @ingroup Database 22 */ 23 24 /** 25 * Database abstraction object for PHP extension mysqli. 26 * 27 * @ingroup Database 28 * @since 1.22 29 * @see Database 30 */ 31 class DatabaseMysqli extends DatabaseMysqlBase { 32 /** 33 * @param string $sql 34 * @return resource 35 */ 36 protected function doQuery( $sql ) { 37 if ( $this->bufferResults() ) { 38 $ret = $this->mConn->query( $sql ); 39 } else { 40 $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT ); 41 } 42 43 return $ret; 44 } 45 46 /** 47 * @param string $realServer 48 * @return bool|mysqli 49 * @throws DBConnectionError 50 */ 51 protected function mysqlConnect( $realServer ) { 52 global $wgDBmysql5; 53 # Fail now 54 # Otherwise we get a suppressed fatal error, which is very hard to track down 55 if ( !function_exists( 'mysqli_init' ) ) { 56 throw new DBConnectionError( $this, "MySQLi functions missing," 57 . " have you compiled PHP with the --with-mysqli option?\n" ); 58 } 59 60 // Other than mysql_connect, mysqli_real_connect expects an explicit port 61 // and socket parameters. So we need to parse the port and socket out of 62 // $realServer 63 $port = null; 64 $socket = null; 65 $hostAndPort = IP::splitHostAndPort( $realServer ); 66 if ( $hostAndPort ) { 67 $realServer = $hostAndPort[0]; 68 if ( $hostAndPort[1] ) { 69 $port = $hostAndPort[1]; 70 } 71 } elseif ( substr_count( $realServer, ':' ) == 1 ) { 72 // If we have a colon and something that's not a port number 73 // inside the hostname, assume it's the socket location 74 $hostAndSocket = explode( ':', $realServer ); 75 $realServer = $hostAndSocket[0]; 76 $socket = $hostAndSocket[1]; 77 } 78 79 $connFlags = 0; 80 if ( $this->mFlags & DBO_SSL ) { 81 $connFlags |= MYSQLI_CLIENT_SSL; 82 } 83 if ( $this->mFlags & DBO_COMPRESS ) { 84 $connFlags |= MYSQLI_CLIENT_COMPRESS; 85 } 86 if ( $this->mFlags & DBO_PERSISTENT ) { 87 $realServer = 'p:' . $realServer; 88 } 89 90 $mysqli = mysqli_init(); 91 if ( $wgDBmysql5 ) { 92 // Tell the server we're communicating with it in UTF-8. 93 // This may engage various charset conversions. 94 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' ); 95 } else { 96 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' ); 97 } 98 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 ); 99 100 if ( $mysqli->real_connect( $realServer, $this->mUser, 101 $this->mPassword, $this->mDBname, $port, $socket, $connFlags ) 102 ) { 103 return $mysqli; 104 } 105 106 return false; 107 } 108 109 protected function connectInitCharset() { 110 // already done in mysqlConnect() 111 return true; 112 } 113 114 /** 115 * @param string $charset 116 * @return bool 117 */ 118 protected function mysqlSetCharset( $charset ) { 119 if ( method_exists( $this->mConn, 'set_charset' ) ) { 120 return $this->mConn->set_charset( $charset ); 121 } else { 122 return $this->query( 'SET NAMES ' . $charset, __METHOD__ ); 123 } 124 } 125 126 /** 127 * @return bool 128 */ 129 protected function closeConnection() { 130 return $this->mConn->close(); 131 } 132 133 /** 134 * @return int 135 */ 136 function insertId() { 137 return $this->mConn->insert_id; 138 } 139 140 /** 141 * @return int 142 */ 143 function lastErrno() { 144 if ( $this->mConn ) { 145 return $this->mConn->errno; 146 } else { 147 return mysqli_connect_errno(); 148 } 149 } 150 151 /** 152 * @return int 153 */ 154 function affectedRows() { 155 return $this->mConn->affected_rows; 156 } 157 158 /** 159 * @param string $db 160 * @return bool 161 */ 162 function selectDB( $db ) { 163 $this->mDBname = $db; 164 165 return $this->mConn->select_db( $db ); 166 } 167 168 /** 169 * @return string 170 */ 171 function getServerVersion() { 172 return $this->mConn->server_info; 173 } 174 175 /** 176 * @param mysqli $res 177 * @return bool 178 */ 179 protected function mysqlFreeResult( $res ) { 180 $res->free_result(); 181 182 return true; 183 } 184 185 /** 186 * @param mysqli $res 187 * @return bool 188 */ 189 protected function mysqlFetchObject( $res ) { 190 $object = $res->fetch_object(); 191 if ( $object === null ) { 192 return false; 193 } 194 195 return $object; 196 } 197 198 /** 199 * @param mysqli $res 200 * @return bool 201 */ 202 protected function mysqlFetchArray( $res ) { 203 $array = $res->fetch_array(); 204 if ( $array === null ) { 205 return false; 206 } 207 208 return $array; 209 } 210 211 /** 212 * @param mysqli $res 213 * @return mixed 214 */ 215 protected function mysqlNumRows( $res ) { 216 return $res->num_rows; 217 } 218 219 /** 220 * @param mysqli $res 221 * @return mixed 222 */ 223 protected function mysqlNumFields( $res ) { 224 return $res->field_count; 225 } 226 227 /** 228 * @param mysqli $res 229 * @param int $n 230 * @return mixed 231 */ 232 protected function mysqlFetchField( $res, $n ) { 233 $field = $res->fetch_field_direct( $n ); 234 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG; 235 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG; 236 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG; 237 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG; 238 $field->binary = $field->flags & MYSQLI_BINARY_FLAG; 239 240 return $field; 241 } 242 243 /** 244 * @param resource|ResultWrapper $res 245 * @param int $n 246 * @return mixed 247 */ 248 protected function mysqlFieldName( $res, $n ) { 249 $field = $res->fetch_field_direct( $n ); 250 251 return $field->name; 252 } 253 254 /** 255 * @param resource|ResultWrapper $res 256 * @param int $n 257 * @return mixed 258 */ 259 protected function mysqlFieldType( $res, $n ) { 260 $field = $res->fetch_field_direct( $n ); 261 262 return $field->type; 263 } 264 265 /** 266 * @param resource|ResultWrapper $res 267 * @param int $row 268 * @return mixed 269 */ 270 protected function mysqlDataSeek( $res, $row ) { 271 return $res->data_seek( $row ); 272 } 273 274 /** 275 * @param mysqli $conn Optional connection object 276 * @return string 277 */ 278 protected function mysqlError( $conn = null ) { 279 if ( $conn === null ) { 280 return mysqli_connect_error(); 281 } else { 282 return $conn->error; 283 } 284 } 285 286 /** 287 * Escapes special characters in a string for use in an SQL statement 288 * @param string $s 289 * @return string 290 */ 291 protected function mysqlRealEscapeString( $s ) { 292 return $this->mConn->real_escape_string( $s ); 293 } 294 295 protected function mysqlPing() { 296 return $this->mConn->ping(); 297 } 298 299 /** 300 * Give an id for the connection 301 * 302 * mysql driver used resource id, but mysqli objects cannot be cast to string. 303 * @return string 304 */ 305 public function __toString() { 306 if ( $this->mConn instanceof Mysqli ) { 307 return (string)$this->mConn->thread_id; 308 } else { 309 // mConn might be false or something. 310 return (string)$this->mConn; 311 } 312 } 313 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |