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' );
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37