MediaWiki  1.30.0
DatabaseMysqli.php
Go to the documentation of this file.
1 <?php
23 namespace Wikimedia\Rdbms;
24 
25 use mysqli;
26 use mysqli_result;
27 use IP;
28 
43  protected function doQuery( $sql ) {
44  $conn = $this->getBindingHandle();
45 
46  if ( $this->bufferResults() ) {
47  $ret = $conn->query( $sql );
48  } else {
49  $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
50  }
51 
52  return $ret;
53  }
54 
60  protected function mysqlConnect( $realServer ) {
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 );
82  $realServer = $hostAndSocket[0];
83  $socket = $hostAndSocket[1];
84  }
85 
86  $mysqli = mysqli_init();
87 
88  $connFlags = 0;
89  if ( $this->mFlags & 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->mFlags & self::DBO_COMPRESS ) {
100  $connFlags |= MYSQLI_CLIENT_COMPRESS;
101  }
102  if ( $this->mFlags & 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( $realServer, $this->mUser,
116  $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
117  ) {
118  return $mysqli;
119  }
120 
121  return false;
122  }
123 
124  protected function connectInitCharset() {
125  // already done in mysqlConnect()
126  return true;
127  }
128 
133  protected function mysqlSetCharset( $charset ) {
134  $conn = $this->getBindingHandle();
135 
136  if ( method_exists( $conn, 'set_charset' ) ) {
137  return $conn->set_charset( $charset );
138  } else {
139  return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
140  }
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->mConn ) {
166  return $this->mConn->errno;
167  } else {
168  return mysqli_connect_errno();
169  }
170  }
171 
175  function affectedRows() {
176  $conn = $this->getBindingHandle();
177 
178  return $conn->affected_rows;
179  }
180 
185  function selectDB( $db ) {
186  $conn = $this->getBindingHandle();
187 
188  $this->mDBname = $db;
189 
190  return $conn->select_db( $db );
191  }
192 
197  protected function mysqlFreeResult( $res ) {
198  $res->free_result();
199 
200  return true;
201  }
202 
207  protected function mysqlFetchObject( $res ) {
208  $object = $res->fetch_object();
209  if ( $object === null ) {
210  return false;
211  }
212 
213  return $object;
214  }
215 
220  protected function mysqlFetchArray( $res ) {
221  $array = $res->fetch_array();
222  if ( $array === null ) {
223  return false;
224  }
225 
226  return $array;
227  }
228 
233  protected function mysqlNumRows( $res ) {
234  return $res->num_rows;
235  }
236 
241  protected function mysqlNumFields( $res ) {
242  return $res->field_count;
243  }
244 
250  protected function mysqlFetchField( $res, $n ) {
251  $field = $res->fetch_field_direct( $n );
252 
253  // Add missing properties to result (using flags property)
254  // which will be part of function mysql-fetch-field for backward compatibility
255  $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
256  $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
257  $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
258  $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
259  $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
260  $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
261  $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
262  $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
263  $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
264 
265  return $field;
266  }
267 
273  protected function mysqlFieldName( $res, $n ) {
274  $field = $res->fetch_field_direct( $n );
275 
276  return $field->name;
277  }
278 
284  protected function mysqlFieldType( $res, $n ) {
285  $field = $res->fetch_field_direct( $n );
286 
287  return $field->type;
288  }
289 
295  protected function mysqlDataSeek( $res, $row ) {
296  return $res->data_seek( $row );
297  }
298 
303  protected function mysqlError( $conn = null ) {
304  if ( $conn === null ) {
305  return mysqli_connect_error();
306  } else {
307  return $conn->error;
308  }
309  }
310 
316  protected function mysqlRealEscapeString( $s ) {
317  $conn = $this->getBindingHandle();
318 
319  return $conn->real_escape_string( (string)$s );
320  }
321 
328  public function __toString() {
329  if ( $this->mConn instanceof mysqli ) {
330  return (string)$this->mConn->thread_id;
331  } else {
332  // mConn might be false or something.
333  return (string)$this->mConn;
334  }
335  }
336 }
337 
338 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );
DBO_PERSISTENT
const DBO_PERSISTENT
Definition: defines.php:14
Wikimedia\Rdbms\Database\getBindingHandle
getBindingHandle()
Get the underlying binding handle, mConn.
Definition: Database.php:3619
Wikimedia\Rdbms\Database\$mConn
resource null $mConn
Database connection.
Definition: Database.php:92
Wikimedia\Rdbms\DatabaseMysqli\mysqlDataSeek
mysqlDataSeek( $res, $row)
Definition: DatabaseMysqli.php:295
Wikimedia\Rdbms\DatabaseMysqli\mysqlFieldType
mysqlFieldType( $res, $n)
Definition: DatabaseMysqli.php:284
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Wikimedia\Rdbms\DatabaseMysqli\lastErrno
lastErrno()
Definition: DatabaseMysqli.php:164
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
IP
A collection of public static functions to play with IP address and IP ranges.
Definition: IP.php:67
$s
$s
Definition: mergeMessageFileList.php:188
DBO_SSL
const DBO_SSL
Definition: defines.php:17
$res
$res
Definition: database.txt:21
Wikimedia\Rdbms\DatabaseMysqli
Database abstraction object for PHP extension mysqli.
Definition: DatabaseMysqli.php:36
Wikimedia\Rdbms\DatabaseMysqli\doQuery
doQuery( $sql)
Definition: DatabaseMysqli.php:43
Wikimedia\Rdbms\DatabaseMysqli\mysqlFetchObject
mysqlFetchObject( $res)
Definition: DatabaseMysqli.php:207
php
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:35
Wikimedia\Rdbms\DatabaseMysqli\closeConnection
closeConnection()
Definition: DatabaseMysqli.php:146
Wikimedia\Rdbms\DatabaseMysqli\connectInitCharset
connectInitCharset()
Set the character set information right after connection.
Definition: DatabaseMysqli.php:124
Wikimedia\Rdbms\DatabaseMysqli\mysqlFreeResult
mysqlFreeResult( $res)
Definition: DatabaseMysqli.php:197
Wikimedia\Rdbms\DatabaseMysqli\mysqlNumRows
mysqlNumRows( $res)
Definition: DatabaseMysqli.php:233
Wikimedia\Rdbms\DatabaseMysqli\mysqlSetCharset
mysqlSetCharset( $charset)
Definition: DatabaseMysqli.php:133
DBO_COMPRESS
const DBO_COMPRESS
Definition: defines.php:18
IP\splitHostAndPort
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
Wikimedia\Rdbms\DatabaseMysqli\insertId
insertId()
Definition: DatabaseMysqli.php:155
Wikimedia\Rdbms\DatabaseMysqli\mysqlNumFields
mysqlNumFields( $res)
Definition: DatabaseMysqli.php:241
Wikimedia\Rdbms\DatabaseMysqli\mysqlError
mysqlError( $conn=null)
Definition: DatabaseMysqli.php:303
Wikimedia\Rdbms\DatabaseMysqli\selectDB
selectDB( $db)
Definition: DatabaseMysqli.php:185
Wikimedia\Rdbms\DatabaseMysqli\mysqlFetchArray
mysqlFetchArray( $res)
Definition: DatabaseMysqli.php:220
Wikimedia\Rdbms\DatabaseMysqli\mysqlFieldName
mysqlFieldName( $res, $n)
Definition: DatabaseMysqli.php:273
$ret
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:1965
Wikimedia\Rdbms\Database\query
query( $sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
Definition: Database.php:888
Wikimedia\Rdbms\Database\bufferResults
bufferResults( $buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition: Database.php:438
Wikimedia\Rdbms\DatabaseMysqli\mysqlFetchField
mysqlFetchField( $res, $n)
Definition: DatabaseMysqli.php:250
Wikimedia\Rdbms\DBConnectionError
Definition: DBConnectionError.php:26
Wikimedia\Rdbms\DatabaseMysqli\affectedRows
affectedRows()
Definition: DatabaseMysqli.php:175
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Wikimedia\Rdbms\DatabaseMysqli\__toString
__toString()
Give an id for the connection.
Definition: DatabaseMysqli.php:328
Wikimedia\Rdbms\DatabaseMysqli\mysqlConnect
mysqlConnect( $realServer)
Definition: DatabaseMysqli.php:60
Wikimedia\Rdbms\DatabaseMysqlBase
Database abstraction object for MySQL.
Definition: DatabaseMysqlBase.php:40
Wikimedia\Rdbms\DatabaseMysqli\mysqlRealEscapeString
mysqlRealEscapeString( $s)
Escapes special characters in a string for use in an SQL statement.
Definition: DatabaseMysqli.php:316