MediaWiki  1.28.0
DatabaseMysqli.php
Go to the documentation of this file.
1 <?php
38  protected function doQuery( $sql ) {
39  $conn = $this->getBindingHandle();
40 
41  if ( $this->bufferResults() ) {
42  $ret = $conn->query( $sql );
43  } else {
44  $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
45  }
46 
47  return $ret;
48  }
49 
55  protected function mysqlConnect( $realServer ) {
56  # Avoid suppressed fatal error, which is very hard to track down
57  if ( !function_exists( 'mysqli_init' ) ) {
58  throw new DBConnectionError( $this, "MySQLi functions missing,"
59  . " have you compiled PHP with the --with-mysqli option?\n" );
60  }
61 
62  // Other than mysql_connect, mysqli_real_connect expects an explicit port
63  // and socket parameters. So we need to parse the port and socket out of
64  // $realServer
65  $port = null;
66  $socket = null;
67  $hostAndPort = IP::splitHostAndPort( $realServer );
68  if ( $hostAndPort ) {
69  $realServer = $hostAndPort[0];
70  if ( $hostAndPort[1] ) {
71  $port = $hostAndPort[1];
72  }
73  } elseif ( substr_count( $realServer, ':' ) == 1 ) {
74  // If we have a colon and something that's not a port number
75  // inside the hostname, assume it's the socket location
76  $hostAndSocket = explode( ':', $realServer );
77  $realServer = $hostAndSocket[0];
78  $socket = $hostAndSocket[1];
79  }
80 
81  $mysqli = mysqli_init();
82 
83  $connFlags = 0;
84  if ( $this->mFlags & self::DBO_SSL ) {
85  $connFlags |= MYSQLI_CLIENT_SSL;
86  $mysqli->ssl_set(
87  $this->sslKeyPath,
88  $this->sslCertPath,
89  null,
90  $this->sslCAPath,
91  $this->sslCiphers
92  );
93  }
94  if ( $this->mFlags & self::DBO_COMPRESS ) {
95  $connFlags |= MYSQLI_CLIENT_COMPRESS;
96  }
97  if ( $this->mFlags & self::DBO_PERSISTENT ) {
98  $realServer = 'p:' . $realServer;
99  }
100 
101  if ( $this->utf8Mode ) {
102  // Tell the server we're communicating with it in UTF-8.
103  // This may engage various charset conversions.
104  $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
105  } else {
106  $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
107  }
108  $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
109 
110  if ( $mysqli->real_connect( $realServer, $this->mUser,
111  $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
112  ) {
113  return $mysqli;
114  }
115 
116  return false;
117  }
118 
119  protected function connectInitCharset() {
120  // already done in mysqlConnect()
121  return true;
122  }
123 
128  protected function mysqlSetCharset( $charset ) {
129  $conn = $this->getBindingHandle();
130 
131  if ( method_exists( $conn, 'set_charset' ) ) {
132  return $conn->set_charset( $charset );
133  } else {
134  return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
135  }
136  }
137 
141  protected function closeConnection() {
142  $conn = $this->getBindingHandle();
143 
144  return $conn->close();
145  }
146 
150  function insertId() {
151  $conn = $this->getBindingHandle();
152 
153  return (int)$conn->insert_id;
154  }
155 
159  function lastErrno() {
160  if ( $this->mConn ) {
161  return $this->mConn->errno;
162  } else {
163  return mysqli_connect_errno();
164  }
165  }
166 
170  function affectedRows() {
171  $conn = $this->getBindingHandle();
172 
173  return $conn->affected_rows;
174  }
175 
180  function selectDB( $db ) {
181  $conn = $this->getBindingHandle();
182 
183  $this->mDBname = $db;
184 
185  return $conn->select_db( $db );
186  }
187 
192  protected function mysqlFreeResult( $res ) {
193  $res->free_result();
194 
195  return true;
196  }
197 
202  protected function mysqlFetchObject( $res ) {
203  $object = $res->fetch_object();
204  if ( $object === null ) {
205  return false;
206  }
207 
208  return $object;
209  }
210 
215  protected function mysqlFetchArray( $res ) {
216  $array = $res->fetch_array();
217  if ( $array === null ) {
218  return false;
219  }
220 
221  return $array;
222  }
223 
228  protected function mysqlNumRows( $res ) {
229  return $res->num_rows;
230  }
231 
236  protected function mysqlNumFields( $res ) {
237  return $res->field_count;
238  }
239 
245  protected function mysqlFetchField( $res, $n ) {
246  $field = $res->fetch_field_direct( $n );
247 
248  // Add missing properties to result (using flags property)
249  // which will be part of function mysql-fetch-field for backward compatibility
250  $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
251  $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
252  $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
253  $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
254  $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
255  $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
256  $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
257  $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
258  $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
259 
260  return $field;
261  }
262 
268  protected function mysqlFieldName( $res, $n ) {
269  $field = $res->fetch_field_direct( $n );
270 
271  return $field->name;
272  }
273 
279  protected function mysqlFieldType( $res, $n ) {
280  $field = $res->fetch_field_direct( $n );
281 
282  return $field->type;
283  }
284 
290  protected function mysqlDataSeek( $res, $row ) {
291  return $res->data_seek( $row );
292  }
293 
298  protected function mysqlError( $conn = null ) {
299  if ( $conn === null ) {
300  return mysqli_connect_error();
301  } else {
302  return $conn->error;
303  }
304  }
305 
311  protected function mysqlRealEscapeString( $s ) {
312  $conn = $this->getBindingHandle();
313 
314  return $conn->real_escape_string( $s );
315  }
316 
323  public function __toString() {
324  if ( $this->mConn instanceof mysqli ) {
325  return (string)$this->mConn->thread_id;
326  } else {
327  // mConn might be false or something.
328  return (string)$this->mConn;
329  }
330  }
331 }
bufferResults($buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition: Database.php:406
mysqlRealEscapeString($s)
Escapes special characters in a string for use in an SQL statement.
mysqlFieldName($res, $n)
mysqlFieldType($res, $n)
const DBO_PERSISTENT
Definition: defines.php:11
mysqlSetCharset($charset)
mysqlConnect($realServer)
function doQuery $mConn $sql
mysqli
$res
Definition: database.txt:21
__toString()
Give an id for the connection.
static splitHostAndPort($both)
Given a host/port string, like one might find in the host part of a URL per RFC 2732, split the hostname part and the port part and return an array with an element for each.
Definition: IP.php:254
getBindingHandle()
Get the underlying binding handle, mConn.
Definition: Database.php:3405
Database abstraction object for PHP extension mysqli.
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
mysqlDataSeek($res, $row)
Database abstraction object for MySQL.
mysqlFetchField($res, $n)
mysqlError($conn=null)
resource null $mConn
Database connection.
Definition: Database.php:83
const DBO_SSL
Definition: defines.php:14
const DBO_COMPRESS
Definition: defines.php:15
doQuery($sql)
The DBMS-dependent part of query()
query($sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
Definition: Database.php:829