MediaWiki  1.32.5
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 use 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 
360 class_alias( DatabaseMysqli::class, 'DatabaseMysqli' );
DBO_PERSISTENT
const DBO_PERSISTENT
Definition: defines.php:14
Wikimedia\Rdbms\DatabaseMysqli\getBindingHandle
getBindingHandle()
Definition: DatabaseMysqli.php:352
Wikimedia\Rdbms\DatabaseMysqli\mysqlDataSeek
mysqlDataSeek( $res, $row)
Definition: DatabaseMysqli.php:307
Wikimedia\Rdbms\DatabaseMysqli\mysqlFieldType
mysqlFieldType( $res, $n)
Definition: DatabaseMysqli.php:296
Wikimedia\Rdbms\DatabaseMysqli\lastErrno
lastErrno()
Definition: DatabaseMysqli.php:170
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:187
DBO_SSL
const DBO_SSL
Definition: defines.php:17
Wikimedia\Rdbms\DatabaseDomain\getDatabase
getDatabase()
Definition: DatabaseDomain.php:158
$res
$res
Definition: database.txt:21
Wikimedia\Rdbms\DatabaseMysqli
Database abstraction object for PHP extension mysqli.
Definition: DatabaseMysqli.php:37
Wikimedia\Rdbms\DatabaseMysqli\doQuery
doQuery( $sql)
Definition: DatabaseMysqli.php:42
Wikimedia\Rdbms\DatabaseMysqli\doSelectDomain
doSelectDomain(DatabaseDomain $domain)
Definition: DatabaseMysqli.php:187
Wikimedia\Rdbms\DatabaseMysqli\mysqlFetchObject
mysqlFetchObject( $res)
Definition: DatabaseMysqli.php:219
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:152
Wikimedia\Rdbms\DatabaseMysqli\connectInitCharset
connectInitCharset()
Set the character set information right after connection.
Definition: DatabaseMysqli.php:130
Wikimedia\Rdbms\DatabaseMysqli\fetchAffectedRowCount
fetchAffectedRowCount()
Definition: DatabaseMysqli.php:181
Wikimedia\Rdbms\DatabaseMysqli\mysqlFreeResult
mysqlFreeResult( $res)
Definition: DatabaseMysqli.php:209
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\mysqlNumRows
mysqlNumRows( $res)
Definition: DatabaseMysqli.php:245
Wikimedia\Rdbms\DatabaseMysqli\mysqlSetCharset
mysqlSetCharset( $charset)
Definition: DatabaseMysqli.php:139
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:161
Wikimedia\Rdbms\DatabaseMysqli\mysqlNumFields
mysqlNumFields( $res)
Definition: DatabaseMysqli.php:253
Wikimedia\Rdbms\DatabaseMysqli\mysqlError
mysqlError( $conn=null)
Definition: DatabaseMysqli.php:315
Wikimedia\Rdbms\DatabaseMysqli\mysqlFetchArray
mysqlFetchArray( $res)
Definition: DatabaseMysqli.php:232
Wikimedia\Rdbms\DatabaseMysqli\mysqlFieldName
mysqlFieldName( $res, $n)
Definition: DatabaseMysqli.php:285
$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:2044
Wikimedia\Rdbms\Database\$conn
resource null $conn
Database connection.
Definition: Database.php:106
Wikimedia\Rdbms\Database\query
query( $sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
Definition: Database.php:1136
Wikimedia\Rdbms\Database\bufferResults
bufferResults( $buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition: Database.php:568
Wikimedia\Rdbms\DBExpectedError
Base class for the more common types of database errors.
Definition: DBExpectedError.php:32
Wikimedia\Rdbms\DatabaseMysqli\mysqlFetchField
mysqlFetchField( $res, $n)
Definition: DatabaseMysqli.php:262
Wikimedia\Rdbms\DatabaseDomain\getSchema
getSchema()
Definition: DatabaseDomain.php:165
Wikimedia\Rdbms\DBConnectionError
Definition: DBConnectionError.php:26
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\mysqlConnect
mysqlConnect( $realServer, $dbName)
Definition: DatabaseMysqli.php:60
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
Wikimedia\Rdbms\DatabaseMysqli\__toString
__toString()
Give an id for the connection.
Definition: DatabaseMysqli.php:340
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:328