MediaWiki REL1_32
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 $hostAndSocket = explode( ':', $realServer, 2 );
82 $realServer = $hostAndSocket[0];
83 $socket = $hostAndSocket[1];
84 }
85
86 $mysqli = mysqli_init();
87
88 $connFlags = 0;
89 if ( $this->flags & self::DBO_SSL ) {
90 $connFlags |= MYSQLI_CLIENT_SSL;
91 $mysqli->ssl_set(
92 $this->sslKeyPath,
93 $this->sslCertPath,
94 $this->sslCAFile,
95 $this->sslCAPath,
96 $this->sslCiphers
97 );
98 }
99 if ( $this->flags & self::DBO_COMPRESS ) {
100 $connFlags |= MYSQLI_CLIENT_COMPRESS;
101 }
102 if ( $this->flags & self::DBO_PERSISTENT ) {
103 $realServer = 'p:' . $realServer;
104 }
105
106 if ( $this->utf8Mode ) {
107 // Tell the server we're communicating with it in UTF-8.
108 // This may engage various charset conversions.
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
110 } else {
111 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
112 }
113 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
114
115 if ( $mysqli->real_connect(
116 $realServer,
117 $this->user,
118 $this->password,
119 $dbName,
120 $port,
121 $socket,
122 $connFlags
123 ) ) {
124 return $mysqli;
125 }
126
127 return false;
128 }
129
130 protected function connectInitCharset() {
131 // already done in mysqlConnect()
132 return true;
133 }
134
139 protected function mysqlSetCharset( $charset ) {
140 $conn = $this->getBindingHandle();
141
142 if ( method_exists( $conn, 'set_charset' ) ) {
143 return $conn->set_charset( $charset );
144 } else {
145 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
146 }
147 }
148
152 protected function closeConnection() {
153 $conn = $this->getBindingHandle();
154
155 return $conn->close();
156 }
157
161 function insertId() {
162 $conn = $this->getBindingHandle();
163
164 return (int)$conn->insert_id;
165 }
166
170 function lastErrno() {
171 if ( $this->conn instanceof mysqli ) {
172 return $this->conn->errno;
173 } else {
174 return mysqli_connect_errno();
175 }
176 }
177
181 protected function fetchAffectedRowCount() {
182 $conn = $this->getBindingHandle();
183
184 return $conn->affected_rows;
185 }
186
187 function doSelectDomain( DatabaseDomain $domain ) {
188 $conn = $this->getBindingHandle();
189
190 if ( $domain->getSchema() !== null ) {
191 throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
192 }
193
194 $database = $domain->getDatabase();
195 if ( !$conn->select_db( $database ) ) {
196 throw new DBExpectedError( $this, "Could not select database '$database'." );
197 }
198
199 // Update that domain fields on success (no exception thrown)
200 $this->currentDomain = $domain;
201
202 return true;
203 }
204
209 protected function mysqlFreeResult( $res ) {
210 $res->free_result();
211
212 return true;
213 }
214
219 protected function mysqlFetchObject( $res ) {
220 $object = $res->fetch_object();
221 if ( $object === null ) {
222 return false;
223 }
224
225 return $object;
226 }
227
232 protected function mysqlFetchArray( $res ) {
233 $array = $res->fetch_array();
234 if ( $array === null ) {
235 return false;
236 }
237
238 return $array;
239 }
240
245 protected function mysqlNumRows( $res ) {
246 return $res->num_rows;
247 }
248
253 protected function mysqlNumFields( $res ) {
254 return $res->field_count;
255 }
256
262 protected function mysqlFetchField( $res, $n ) {
263 $field = $res->fetch_field_direct( $n );
264
265 // Add missing properties to result (using flags property)
266 // which will be part of function mysql-fetch-field for backward compatibility
267 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
268 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
269 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
270 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
271 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
272 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
273 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
274 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
275 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
276
277 return $field;
278 }
279
285 protected function mysqlFieldName( $res, $n ) {
286 $field = $res->fetch_field_direct( $n );
287
288 return $field->name;
289 }
290
296 protected function mysqlFieldType( $res, $n ) {
297 $field = $res->fetch_field_direct( $n );
298
299 return $field->type;
300 }
301
307 protected function mysqlDataSeek( $res, $row ) {
308 return $res->data_seek( $row );
309 }
310
315 protected function mysqlError( $conn = null ) {
316 if ( $conn === null ) {
317 return mysqli_connect_error();
318 } else {
319 return $conn->error;
320 }
321 }
322
328 protected function mysqlRealEscapeString( $s ) {
329 $conn = $this->getBindingHandle();
330
331 return $conn->real_escape_string( (string)$s );
332 }
333
340 public function __toString() {
341 if ( $this->conn instanceof mysqli ) {
342 return (string)$this->conn->thread_id;
343 } else {
344 // mConn might be false or something.
345 return (string)$this->conn;
346 }
347 }
348
352 protected function getBindingHandle() {
353 return parent::getBindingHandle();
354 }
355}
356
360class_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
Base class for the more common types of database errors.
Class to handle database/prefix specification for IDatabase domains.
Database abstraction object for MySQL.
Database abstraction object for PHP extension mysqli.
doSelectDomain(DatabaseDomain $domain)
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.
resource null $conn
Database connection.
Definition Database.php:106
bufferResults( $buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition Database.php:568
query( $sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
$res
Definition database.txt:21
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:2054
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