MediaWiki  1.27.2
DatabaseMysqli.php
Go to the documentation of this file.
1 <?php
33  protected $mConn;
34 
39  protected function doQuery( $sql ) {
40  $conn = $this->getBindingHandle();
41 
42  if ( $this->bufferResults() ) {
43  $ret = $conn->query( $sql );
44  } else {
45  $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
46  }
47 
48  return $ret;
49  }
50 
56  protected function mysqlConnect( $realServer ) {
58 
59  # Avoid suppressed fatal error, which is very hard to track down
60  if ( !function_exists( 'mysqli_init' ) ) {
61  throw new DBConnectionError( $this, "MySQLi functions missing,"
62  . " have you compiled PHP with the --with-mysqli option?\n" );
63  }
64 
65  // Other than mysql_connect, mysqli_real_connect expects an explicit port
66  // and socket parameters. So we need to parse the port and socket out of
67  // $realServer
68  $port = null;
69  $socket = null;
70  $hostAndPort = IP::splitHostAndPort( $realServer );
71  if ( $hostAndPort ) {
72  $realServer = $hostAndPort[0];
73  if ( $hostAndPort[1] ) {
74  $port = $hostAndPort[1];
75  }
76  } elseif ( substr_count( $realServer, ':' ) == 1 ) {
77  // If we have a colon and something that's not a port number
78  // inside the hostname, assume it's the socket location
79  $hostAndSocket = explode( ':', $realServer );
80  $realServer = $hostAndSocket[0];
81  $socket = $hostAndSocket[1];
82  }
83 
84  $connFlags = 0;
85  if ( $this->mFlags & DBO_SSL ) {
86  $connFlags |= MYSQLI_CLIENT_SSL;
87  }
88  if ( $this->mFlags & DBO_COMPRESS ) {
89  $connFlags |= MYSQLI_CLIENT_COMPRESS;
90  }
91  if ( $this->mFlags & DBO_PERSISTENT ) {
92  $realServer = 'p:' . $realServer;
93  }
94 
95  $mysqli = mysqli_init();
96  if ( $wgDBmysql5 ) {
97  // Tell the server we're communicating with it in UTF-8.
98  // This may engage various charset conversions.
99  $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
100  } else {
101  $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
102  }
103  $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
104 
105  if ( $mysqli->real_connect( $realServer, $this->mUser,
106  $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
107  ) {
108  return $mysqli;
109  }
110 
111  return false;
112  }
113 
114  protected function connectInitCharset() {
115  // already done in mysqlConnect()
116  return true;
117  }
118 
123  protected function mysqlSetCharset( $charset ) {
124  $conn = $this->getBindingHandle();
125 
126  if ( method_exists( $conn, 'set_charset' ) ) {
127  return $conn->set_charset( $charset );
128  } else {
129  return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
130  }
131  }
132 
136  protected function closeConnection() {
137  $conn = $this->getBindingHandle();
138 
139  return $conn->close();
140  }
141 
145  function insertId() {
146  $conn = $this->getBindingHandle();
147 
148  return (int)$conn->insert_id;
149  }
150 
154  function lastErrno() {
155  if ( $this->mConn ) {
156  return $this->mConn->errno;
157  } else {
158  return mysqli_connect_errno();
159  }
160  }
161 
165  function affectedRows() {
166  $conn = $this->getBindingHandle();
167 
168  return $conn->affected_rows;
169  }
170 
175  function selectDB( $db ) {
176  $conn = $this->getBindingHandle();
177 
178  $this->mDBname = $db;
179 
180  return $conn->select_db( $db );
181  }
182 
187  protected function mysqlFreeResult( $res ) {
188  $res->free_result();
189 
190  return true;
191  }
192 
197  protected function mysqlFetchObject( $res ) {
198  $object = $res->fetch_object();
199  if ( $object === null ) {
200  return false;
201  }
202 
203  return $object;
204  }
205 
210  protected function mysqlFetchArray( $res ) {
211  $array = $res->fetch_array();
212  if ( $array === null ) {
213  return false;
214  }
215 
216  return $array;
217  }
218 
223  protected function mysqlNumRows( $res ) {
224  return $res->num_rows;
225  }
226 
231  protected function mysqlNumFields( $res ) {
232  return $res->field_count;
233  }
234 
240  protected function mysqlFetchField( $res, $n ) {
241  $field = $res->fetch_field_direct( $n );
242 
243  // Add missing properties to result (using flags property)
244  // which will be part of function mysql-fetch-field for backward compatibility
245  $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
246  $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
247  $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
248  $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
249  $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
250  $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
251  $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
252  $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
253  $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
254 
255  return $field;
256  }
257 
263  protected function mysqlFieldName( $res, $n ) {
264  $field = $res->fetch_field_direct( $n );
265 
266  return $field->name;
267  }
268 
274  protected function mysqlFieldType( $res, $n ) {
275  $field = $res->fetch_field_direct( $n );
276 
277  return $field->type;
278  }
279 
285  protected function mysqlDataSeek( $res, $row ) {
286  return $res->data_seek( $row );
287  }
288 
293  protected function mysqlError( $conn = null ) {
294  if ( $conn === null ) {
295  return mysqli_connect_error();
296  } else {
297  return $conn->error;
298  }
299  }
300 
306  protected function mysqlRealEscapeString( $s ) {
307  $conn = $this->getBindingHandle();
308 
309  return $conn->real_escape_string( $s );
310  }
311 
312  protected function mysqlPing() {
313  $conn = $this->getBindingHandle();
314 
315  return $conn->ping();
316  }
317 
324  public function __toString() {
325  if ( $this->mConn instanceof mysqli ) {
326  return (string)$this->mConn->thread_id;
327  } else {
328  // mConn might be false or something.
329  return (string)$this->mConn;
330  }
331  }
332 }
const DBO_PERSISTENT
Definition: Defines.php:35
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:1798
mysqlRealEscapeString($s)
Escapes special characters in a string for use in an SQL statement.
mysqlFieldName($res, $n)
const DBO_COMPRESS
Definition: Defines.php:39
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
mysqlFieldType($res, $n)
mysqlSetCharset($charset)
mysqlConnect($realServer)
bufferResults($buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition: Database.php:203
$res
Definition: database.txt:21
__toString()
Give an id for the connection.
const DBO_SSL
Definition: Defines.php:38
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
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)
query($sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
Definition: Database.php:780
getBindingHandle()
Get the underlying binding handle, mConn.
$wgDBmysql5
Set to true to engage MySQL 4.1/5.0 charset-related features; for now will just cause sending of 'SET...