MediaWiki  1.23.2
DatabaseMysqli.php
Go to the documentation of this file.
1 <?php
36  protected function doQuery( $sql ) {
37  if ( $this->bufferResults() ) {
38  $ret = $this->mConn->query( $sql );
39  } else {
40  $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
41  }
42 
43  return $ret;
44  }
45 
51  protected function mysqlConnect( $realServer ) {
52  global $wgDBmysql5;
53  # Fail now
54  # Otherwise we get a suppressed fatal error, which is very hard to track down
55  if ( !function_exists( 'mysqli_init' ) ) {
56  throw new DBConnectionError( $this, "MySQLi functions missing,"
57  . " have you compiled PHP with the --with-mysqli option?\n" );
58  }
59 
60  // Other than mysql_connect, mysqli_real_connect expects an explicit port
61  // parameter. So we need to parse the port out of $realServer
62  $port = null;
63  $hostAndPort = IP::splitHostAndPort( $realServer );
64  if ( $hostAndPort ) {
65  $realServer = $hostAndPort[0];
66  if ( $hostAndPort[1] ) {
67  $port = $hostAndPort[1];
68  }
69  }
70 
71  $connFlags = 0;
72  if ( $this->mFlags & DBO_SSL ) {
73  $connFlags |= MYSQLI_CLIENT_SSL;
74  }
75  if ( $this->mFlags & DBO_COMPRESS ) {
76  $connFlags |= MYSQLI_CLIENT_COMPRESS;
77  }
78  if ( $this->mFlags & DBO_PERSISTENT ) {
79  $realServer = 'p:' . $realServer;
80  }
81 
82  $mysqli = mysqli_init();
83  if ( $wgDBmysql5 ) {
84  // Tell the server we're communicating with it in UTF-8.
85  // This may engage various charset conversions.
86  $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
87  } else {
88  $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
89  }
90 
91  $numAttempts = 2;
92  for ( $i = 0; $i < $numAttempts; $i++ ) {
93  if ( $i > 1 ) {
94  usleep( 1000 );
95  }
96  if ( $mysqli->real_connect( $realServer, $this->mUser,
97  $this->mPassword, $this->mDBname, $port, null, $connFlags )
98  ) {
99  return $mysqli;
100  }
101  }
102 
103  return false;
104  }
105 
106  protected function connectInitCharset() {
107  // already done in mysqlConnect()
108  return true;
109  }
110 
115  protected function mysqlSetCharset( $charset ) {
116  if ( method_exists( $this->mConn, 'set_charset' ) ) {
117  return $this->mConn->set_charset( $charset );
118  } else {
119  return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
120  }
121  }
122 
126  protected function closeConnection() {
127  return $this->mConn->close();
128  }
129 
133  function insertId() {
134  return $this->mConn->insert_id;
135  }
136 
140  function lastErrno() {
141  if ( $this->mConn ) {
142  return $this->mConn->errno;
143  } else {
144  return mysqli_connect_errno();
145  }
146  }
147 
151  function affectedRows() {
152  return $this->mConn->affected_rows;
153  }
154 
159  function selectDB( $db ) {
160  $this->mDBname = $db;
161 
162  return $this->mConn->select_db( $db );
163  }
164 
168  function getServerVersion() {
169  return $this->mConn->server_info;
170  }
171 
176  protected function mysqlFreeResult( $res ) {
177  $res->free_result();
178 
179  return true;
180  }
181 
186  protected function mysqlFetchObject( $res ) {
187  $object = $res->fetch_object();
188  if ( $object === null ) {
189  return false;
190  }
191 
192  return $object;
193  }
194 
199  protected function mysqlFetchArray( $res ) {
200  $array = $res->fetch_array();
201  if ( $array === null ) {
202  return false;
203  }
204 
205  return $array;
206  }
207 
212  protected function mysqlNumRows( $res ) {
213  return $res->num_rows;
214  }
215 
220  protected function mysqlNumFields( $res ) {
221  return $res->field_count;
222  }
223 
229  protected function mysqlFetchField( $res, $n ) {
230  $field = $res->fetch_field_direct( $n );
231  $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
232  $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
233  $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
234  $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
235  $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
236 
237  return $field;
238  }
239 
245  protected function mysqlFieldName( $res, $n ) {
246  $field = $res->fetch_field_direct( $n );
247 
248  return $field->name;
249  }
250 
256  protected function mysqlFieldType( $res, $n ) {
257  $field = $res->fetch_field_direct( $n );
258 
259  return $field->type;
260  }
261 
267  protected function mysqlDataSeek( $res, $row ) {
268  return $res->data_seek( $row );
269  }
270 
275  protected function mysqlError( $conn = null ) {
276  if ( $conn === null ) {
277  return mysqli_connect_error();
278  } else {
279  return $conn->error;
280  }
281  }
282 
288  protected function mysqlRealEscapeString( $s ) {
289  return $this->mConn->real_escape_string( $s );
290  }
291 
292  protected function mysqlPing() {
293  return $this->mConn->ping();
294  }
295 
301  public function __toString() {
302  if ( $this->mConn instanceof Mysqli ) {
303  return (string)$this->mConn->thread_id;
304  } else {
305  // mConn might be false or something.
306  return (string)$this->mConn;
307  }
308  }
309 }
DatabaseMysqlBase
Database abstraction object for MySQL.
Definition: DatabaseMysqlBase.php:32
DatabaseMysqli\__toString
__toString()
Give an id for the connection.
Definition: DatabaseMysqli.php:301
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
DatabaseMysqli\doQuery
doQuery( $sql)
Definition: DatabaseMysqli.php:36
DatabaseMysqli\mysqlFetchObject
mysqlFetchObject( $res)
Definition: DatabaseMysqli.php:186
DatabaseMysqli
Database abstraction object for PHP extension mysqli.
Definition: DatabaseMysqli.php:31
DBO_PERSISTENT
const DBO_PERSISTENT
Definition: Defines.php:44
DatabaseMysqli\mysqlPing
mysqlPing()
Ping a server connection or reconnect if there is no connection.
Definition: DatabaseMysqli.php:292
DatabaseBase\query
query( $sql, $fname=__METHOD__, $tempIgnore=false)
Run an SQL query and return the result.
Definition: Database.php:1001
$n
$n
Definition: RandomTest.php:76
$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:1530
DatabaseMysqli\selectDB
selectDB( $db)
Definition: DatabaseMysqli.php:159
$s
$s
Definition: mergeMessageFileList.php:156
DatabaseMysqli\mysqlError
mysqlError( $conn=null)
Definition: DatabaseMysqli.php:275
DatabaseMysqli\mysqlRealEscapeString
mysqlRealEscapeString( $s)
Escapes special characters in a string for use in an SQL statement.
Definition: DatabaseMysqli.php:288
DBO_COMPRESS
const DBO_COMPRESS
Definition: Defines.php:48
DatabaseBase\$mConn
resource $mConn
Database connection *.
Definition: Database.php:239
DatabaseMysqli\connectInitCharset
connectInitCharset()
Set the character set information right after connection.
Definition: DatabaseMysqli.php:106
DatabaseMysqli\mysqlSetCharset
mysqlSetCharset( $charset)
Definition: DatabaseMysqli.php:115
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DBO_SSL
const DBO_SSL
Definition: Defines.php:47
DBConnectionError
Definition: DatabaseError.php:98
DatabaseMysqli\lastErrno
lastErrno()
Definition: DatabaseMysqli.php:140
DatabaseMysqli\mysqlFieldType
mysqlFieldType( $res, $n)
Definition: DatabaseMysqli.php:256
DatabaseMysqli\mysqlConnect
mysqlConnect( $realServer)
Definition: DatabaseMysqli.php:51
DatabaseMysqli\mysqlFieldName
mysqlFieldName( $res, $n)
Definition: DatabaseMysqli.php:245
DatabaseMysqli\getServerVersion
getServerVersion()
Definition: DatabaseMysqli.php:168
DatabaseMysqli\affectedRows
affectedRows()
Definition: DatabaseMysqli.php:151
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:240
DatabaseBase\bufferResults
bufferResults( $buffer=null)
Turns buffering of SQL result sets on (true) or off (false).
Definition: Database.php:368
DatabaseMysqli\mysqlFetchArray
mysqlFetchArray( $res)
Definition: DatabaseMysqli.php:199
DatabaseMysqli\mysqlDataSeek
mysqlDataSeek( $res, $row)
Definition: DatabaseMysqli.php:267
DatabaseMysqli\mysqlNumRows
mysqlNumRows( $res)
Definition: DatabaseMysqli.php:212
DatabaseMysqli\mysqlFreeResult
mysqlFreeResult( $res)
Definition: DatabaseMysqli.php:176
DatabaseMysqli\closeConnection
closeConnection()
Definition: DatabaseMysqli.php:126
DatabaseMysqli\mysqlFetchField
mysqlFetchField( $res, $n)
Definition: DatabaseMysqli.php:229
DatabaseMysqli\mysqlNumFields
mysqlNumFields( $res)
Definition: DatabaseMysqli.php:220
$res
$res
Definition: database.txt:21
DatabaseMysqli\insertId
insertId()
Definition: DatabaseMysqli.php:133