MediaWiki REL1_31
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
59 protected function mysqlConnect( $realServer ) {
60 # Avoid suppressed fatal error, which is very hard to track down
61 if ( !function_exists( 'mysqli_init' ) ) {
62 throw new DBConnectionError( $this, "MySQLi functions missing,"
63 . " have you compiled PHP with the --with-mysqli option?\n" );
64 }
65
66 // Other than mysql_connect, mysqli_real_connect expects an explicit port
67 // and socket parameters. So we need to parse the port and socket out of
68 // $realServer
69 $port = null;
70 $socket = null;
71 $hostAndPort = IP::splitHostAndPort( $realServer );
72 if ( $hostAndPort ) {
73 $realServer = $hostAndPort[0];
74 if ( $hostAndPort[1] ) {
75 $port = $hostAndPort[1];
76 }
77 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
78 // If we have a colon and something that's not a port number
79 // inside the hostname, assume it's the socket location
80 $hostAndSocket = explode( ':', $realServer );
81 $realServer = $hostAndSocket[0];
82 $socket = $hostAndSocket[1];
83 }
84
85 $mysqli = mysqli_init();
86
87 $connFlags = 0;
88 if ( $this->flags & self::DBO_SSL ) {
89 $connFlags |= MYSQLI_CLIENT_SSL;
90 $mysqli->ssl_set(
91 $this->sslKeyPath,
92 $this->sslCertPath,
93 $this->sslCAFile,
94 $this->sslCAPath,
95 $this->sslCiphers
96 );
97 }
98 if ( $this->flags & self::DBO_COMPRESS ) {
99 $connFlags |= MYSQLI_CLIENT_COMPRESS;
100 }
101 if ( $this->flags & self::DBO_PERSISTENT ) {
102 $realServer = 'p:' . $realServer;
103 }
104
105 if ( $this->utf8Mode ) {
106 // Tell the server we're communicating with it in UTF-8.
107 // This may engage various charset conversions.
108 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
109 } else {
110 $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
111 }
112 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
113
114 if ( $mysqli->real_connect( $realServer, $this->user,
115 $this->password, $this->dbName, $port, $socket, $connFlags )
116 ) {
117 return $mysqli;
118 }
119
120 return false;
121 }
122
123 protected function connectInitCharset() {
124 // already done in mysqlConnect()
125 return true;
126 }
127
132 protected function mysqlSetCharset( $charset ) {
133 $conn = $this->getBindingHandle();
134
135 if ( method_exists( $conn, 'set_charset' ) ) {
136 return $conn->set_charset( $charset );
137 } else {
138 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
139 }
140 }
141
145 protected function closeConnection() {
146 $conn = $this->getBindingHandle();
147
148 return $conn->close();
149 }
150
154 function insertId() {
155 $conn = $this->getBindingHandle();
156
157 return (int)$conn->insert_id;
158 }
159
163 function lastErrno() {
164 if ( $this->conn instanceof mysqli ) {
165 return $this->conn->errno;
166 } else {
167 return mysqli_connect_errno();
168 }
169 }
170
174 protected function fetchAffectedRowCount() {
175 $conn = $this->getBindingHandle();
176
177 return $conn->affected_rows;
178 }
179
184 function selectDB( $db ) {
185 $conn = $this->getBindingHandle();
186
187 $this->dbName = $db;
188
189 return $conn->select_db( $db );
190 }
191
196 protected function mysqlFreeResult( $res ) {
197 $res->free_result();
198
199 return true;
200 }
201
206 protected function mysqlFetchObject( $res ) {
207 $object = $res->fetch_object();
208 if ( $object === null ) {
209 return false;
210 }
211
212 return $object;
213 }
214
219 protected function mysqlFetchArray( $res ) {
220 $array = $res->fetch_array();
221 if ( $array === null ) {
222 return false;
223 }
224
225 return $array;
226 }
227
232 protected function mysqlNumRows( $res ) {
233 return $res->num_rows;
234 }
235
240 protected function mysqlNumFields( $res ) {
241 return $res->field_count;
242 }
243
249 protected function mysqlFetchField( $res, $n ) {
250 $field = $res->fetch_field_direct( $n );
251
252 // Add missing properties to result (using flags property)
253 // which will be part of function mysql-fetch-field for backward compatibility
254 $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
255 $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
256 $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
257 $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
258 $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
259 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
260 $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
261 $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
262 $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
263
264 return $field;
265 }
266
272 protected function mysqlFieldName( $res, $n ) {
273 $field = $res->fetch_field_direct( $n );
274
275 return $field->name;
276 }
277
283 protected function mysqlFieldType( $res, $n ) {
284 $field = $res->fetch_field_direct( $n );
285
286 return $field->type;
287 }
288
294 protected function mysqlDataSeek( $res, $row ) {
295 return $res->data_seek( $row );
296 }
297
302 protected function mysqlError( $conn = null ) {
303 if ( $conn === null ) {
304 return mysqli_connect_error();
305 } else {
306 return $conn->error;
307 }
308 }
309
315 protected function mysqlRealEscapeString( $s ) {
316 $conn = $this->getBindingHandle();
317
318 return $conn->real_escape_string( (string)$s );
319 }
320
327 public function __toString() {
328 if ( $this->conn instanceof mysqli ) {
329 return (string)$this->conn->thread_id;
330 } else {
331 // mConn might be false or something.
332 return (string)$this->conn;
333 }
334 }
335
339 protected function getBindingHandle() {
340 return parent::getBindingHandle();
341 }
342}
343
344class_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.
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:108
bufferResults( $buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition Database.php:566
query( $sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
$res
Definition database.txt:21
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database etc For and for historical it also represents a few features of articles that don t involve their such as access rights See also title txt Article Encapsulates access to the page table of the database The object represents a an and maintains state such as flags
Definition design.txt:34
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:2005
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