MediaWiki REL1_33
DatabaseMysqli.php
Go to the documentation of this file.
1<?php
23namespace Wikimedia\Rdbms;
24
25use mysqli;
26use mysqli_result;
27use IP;
28use stdClass;
29
42 protected function doQuery( $sql ) {
43 $conn = $this->getBindingHandle();
44
45 if ( $this->bufferResults() ) {
46 $ret = $conn->query( $sql );
47 } else {
48 $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
49 }
50
51 return $ret;
52 }
53
60 protected function mysqlConnect( $realServer, $dbName ) {
61 # Avoid suppressed fatal error, which is very hard to track down
62 if ( !function_exists( 'mysqli_init' ) ) {
63 throw new DBConnectionError( $this, "MySQLi functions missing,"
64 . " have you compiled PHP with the --with-mysqli option?\n" );
65 }
66
67 // Other than mysql_connect, mysqli_real_connect expects an explicit port
68 // and socket parameters. So we need to parse the port and socket out of
69 // $realServer
70 $port = null;
71 $socket = null;
72 $hostAndPort = IP::splitHostAndPort( $realServer );
73 if ( $hostAndPort ) {
74 $realServer = $hostAndPort[0];
75 if ( $hostAndPort[1] ) {
76 $port = $hostAndPort[1];
77 }
78 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
79 // If we have a colon and something that's not a port number
80 // inside the hostname, assume it's the socket location
81 list( $realServer, $socket ) = explode( ':', $realServer, 2 );
82 }
83
84 $mysqli = mysqli_init();
85
86 $connFlags = 0;
87 if ( $this->flags & self::DBO_SSL ) {
88 $connFlags |= MYSQLI_CLIENT_SSL;
89 $mysqli->ssl_set(
90 $this->sslKeyPath,
91 $this->sslCertPath,
92 $this->sslCAFile,
93 $this->sslCAPath,
94 $this->sslCiphers
95 );
96 }
97 if ( $this->flags & self::DBO_COMPRESS ) {
98 $connFlags |= MYSQLI_CLIENT_COMPRESS;
99 }
100 if ( $this->flags & self::DBO_PERSISTENT ) {
101 $realServer = 'p:' . $realServer;
102 }
103
104 if ( $this->utf8Mode ) {
105 // Tell the server we're communicating with it in UTF-8.
106 // This may engage various charset conversions.
107 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
108 } else {
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
110 }
111 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
112
113 if ( $mysqli->real_connect(
114 $realServer,
115 $this->user,
116 $this->password,
117 $dbName,
118 $port,
119 $socket,
120 $connFlags
121 ) ) {
122 return $mysqli;
123 }
124
125 return false;
126 }
127
128 protected function connectInitCharset() {
129 // already done in mysqlConnect()
130 return true;
131 }
132
137 protected function mysqlSetCharset( $charset ) {
138 $conn = $this->getBindingHandle();
139
140 return $conn->set_charset( $charset );
141 }
142
146 protected function closeConnection() {
147 $conn = $this->getBindingHandle();
148
149 return $conn->close();
150 }
151
155 function insertId() {
156 $conn = $this->getBindingHandle();
157
158 return (int)$conn->insert_id;
159 }
160
164 function lastErrno() {
165 if ( $this->conn instanceof mysqli ) {
166 return $this->conn->errno;
167 } else {
168 return mysqli_connect_errno();
169 }
170 }
171
175 protected function fetchAffectedRowCount() {
176 $conn = $this->getBindingHandle();
177
178 return $conn->affected_rows;
179 }
180
185 protected function mysqlFreeResult( $res ) {
186 $res->free_result();
187
188 return true;
189 }
190
195 protected function mysqlFetchObject( $res ) {
196 $object = $res->fetch_object();
197 if ( $object === null ) {
198 return false;
199 }
200
201 return $object;
202 }
203
208 protected function mysqlFetchArray( $res ) {
209 $array = $res->fetch_array();
210 if ( $array === null ) {
211 return false;
212 }
213
214 return $array;
215 }
216
221 protected function mysqlNumRows( $res ) {
222 return $res->num_rows;
223 }
224
229 protected function mysqlNumFields( $res ) {
230 return $res->field_count;
231 }
232
238 protected function mysqlFetchField( $res, $n ) {
239 $field = $res->fetch_field_direct( $n );
240
241 // Add missing properties to result (using flags property)
242 // which will be part of function mysql-fetch-field for backward compatibility
243 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
244 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
245 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
246 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
247 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
248 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
249 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
250 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
251 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
252
253 return $field;
254 }
255
261 protected function mysqlFieldName( $res, $n ) {
262 $field = $res->fetch_field_direct( $n );
263
264 return $field->name;
265 }
266
272 protected function mysqlFieldType( $res, $n ) {
273 $field = $res->fetch_field_direct( $n );
274
275 return $field->type;
276 }
277
283 protected function mysqlDataSeek( $res, $row ) {
284 return $res->data_seek( $row );
285 }
286
291 protected function mysqlError( $conn = null ) {
292 if ( $conn === null ) {
293 return mysqli_connect_error();
294 } else {
295 return $conn->error;
296 }
297 }
298
304 protected function mysqlRealEscapeString( $s ) {
305 $conn = $this->getBindingHandle();
306
307 return $conn->real_escape_string( (string)$s );
308 }
309
316 public function __toString() {
317 if ( $this->conn instanceof mysqli ) {
318 return (string)$this->conn->thread_id;
319 } else {
320 // mConn might be false or something.
321 return (string)$this->conn;
322 }
323 }
324
328 protected function getBindingHandle() {
329 return parent::getBindingHandle();
330 }
331}
332
336class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
A collection of public static functions to play with IP address and IP ranges.
Definition IP.php:67
static splitHostAndPort( $both)
Given a host/port string, like one might find in the host part of a URL per RFC 2732,...
Definition IP.php:266
Database abstraction object for MySQL.
Database abstraction object for PHP extension mysqli.
connectInitCharset()
Set the character set information right after connection.
mysqlConnect( $realServer, $dbName)
mysqlRealEscapeString( $s)
Escapes special characters in a string for use in an SQL statement.
__toString()
Give an id for the connection.
object resource null $conn
Database connection.
Definition Database.php:109
bufferResults( $buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition Database.php:580
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2003