MediaWiki  1.23.6
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  // and socket parameters. So we need to parse the port and socket out of
62  // $realServer
63  $port = null;
64  $socket = null;
65  $hostAndPort = IP::splitHostAndPort( $realServer );
66  if ( $hostAndPort ) {
67  $realServer = $hostAndPort[0];
68  if ( $hostAndPort[1] ) {
69  $port = $hostAndPort[1];
70  }
71  } elseif ( substr_count( $realServer, ':' ) == 1 ) {
72  // If we have a colon and something that's not a port number
73  // inside the hostname, assume it's the socket location
74  $hostAndSocket = explode( ':', $realServer );
75  $realServer = $hostAndSocket[0];
76  $socket = $hostAndSocket[1];
77  }
78 
79  $connFlags = 0;
80  if ( $this->mFlags & DBO_SSL ) {
81  $connFlags |= MYSQLI_CLIENT_SSL;
82  }
83  if ( $this->mFlags & DBO_COMPRESS ) {
84  $connFlags |= MYSQLI_CLIENT_COMPRESS;
85  }
86  if ( $this->mFlags & DBO_PERSISTENT ) {
87  $realServer = 'p:' . $realServer;
88  }
89 
90  $mysqli = mysqli_init();
91  if ( $wgDBmysql5 ) {
92  // Tell the server we're communicating with it in UTF-8.
93  // This may engage various charset conversions.
94  $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
95  } else {
96  $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
97  }
98 
99  $numAttempts = 2;
100  for ( $i = 0; $i < $numAttempts; $i++ ) {
101  if ( $i > 1 ) {
102  usleep( 1000 );
103  }
104  if ( $mysqli->real_connect( $realServer, $this->mUser,
105  $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
106  ) {
107  return $mysqli;
108  }
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  if ( method_exists( $this->mConn, 'set_charset' ) ) {
125  return $this->mConn->set_charset( $charset );
126  } else {
127  return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
128  }
129  }
130 
134  protected function closeConnection() {
135  return $this->mConn->close();
136  }
137 
141  function insertId() {
142  return $this->mConn->insert_id;
143  }
144 
148  function lastErrno() {
149  if ( $this->mConn ) {
150  return $this->mConn->errno;
151  } else {
152  return mysqli_connect_errno();
153  }
154  }
155 
159  function affectedRows() {
160  return $this->mConn->affected_rows;
161  }
162 
167  function selectDB( $db ) {
168  $this->mDBname = $db;
169 
170  return $this->mConn->select_db( $db );
171  }
172 
176  function getServerVersion() {
177  return $this->mConn->server_info;
178  }
179 
184  protected function mysqlFreeResult( $res ) {
185  $res->free_result();
186 
187  return true;
188  }
189 
194  protected function mysqlFetchObject( $res ) {
195  $object = $res->fetch_object();
196  if ( $object === null ) {
197  return false;
198  }
199 
200  return $object;
201  }
202 
207  protected function mysqlFetchArray( $res ) {
208  $array = $res->fetch_array();
209  if ( $array === null ) {
210  return false;
211  }
212 
213  return $array;
214  }
215 
220  protected function mysqlNumRows( $res ) {
221  return $res->num_rows;
222  }
223 
228  protected function mysqlNumFields( $res ) {
229  return $res->field_count;
230  }
231 
237  protected function mysqlFetchField( $res, $n ) {
238  $field = $res->fetch_field_direct( $n );
239  $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
240  $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
241  $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
242  $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
243  $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
244 
245  return $field;
246  }
247 
253  protected function mysqlFieldName( $res, $n ) {
254  $field = $res->fetch_field_direct( $n );
255 
256  return $field->name;
257  }
258 
264  protected function mysqlFieldType( $res, $n ) {
265  $field = $res->fetch_field_direct( $n );
266 
267  return $field->type;
268  }
269 
275  protected function mysqlDataSeek( $res, $row ) {
276  return $res->data_seek( $row );
277  }
278 
283  protected function mysqlError( $conn = null ) {
284  if ( $conn === null ) {
285  return mysqli_connect_error();
286  } else {
287  return $conn->error;
288  }
289  }
290 
296  protected function mysqlRealEscapeString( $s ) {
297  return $this->mConn->real_escape_string( $s );
298  }
299 
300  protected function mysqlPing() {
301  return $this->mConn->ping();
302  }
303 
309  public function __toString() {
310  if ( $this->mConn instanceof Mysqli ) {
311  return (string)$this->mConn->thread_id;
312  } else {
313  // mConn might be false or something.
314  return (string)$this->mConn;
315  }
316  }
317 }
DatabaseMysqlBase
Database abstraction object for MySQL.
Definition: DatabaseMysqlBase.php:32
DatabaseMysqli\__toString
__toString()
Give an id for the connection.
Definition: DatabaseMysqli.php:309
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:194
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:300
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:167
$s
$s
Definition: mergeMessageFileList.php:156
DatabaseMysqli\mysqlError
mysqlError( $conn=null)
Definition: DatabaseMysqli.php:283
DatabaseMysqli\mysqlRealEscapeString
mysqlRealEscapeString( $s)
Escapes special characters in a string for use in an SQL statement.
Definition: DatabaseMysqli.php:296
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:114
DatabaseMysqli\mysqlSetCharset
mysqlSetCharset( $charset)
Definition: DatabaseMysqli.php:123
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:148
DatabaseMysqli\mysqlFieldType
mysqlFieldType( $res, $n)
Definition: DatabaseMysqli.php:264
DatabaseMysqli\mysqlConnect
mysqlConnect( $realServer)
Definition: DatabaseMysqli.php:51
DatabaseMysqli\mysqlFieldName
mysqlFieldName( $res, $n)
Definition: DatabaseMysqli.php:253
DatabaseMysqli\getServerVersion
getServerVersion()
Definition: DatabaseMysqli.php:176
DatabaseMysqli\affectedRows
affectedRows()
Definition: DatabaseMysqli.php:159
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:207
DatabaseMysqli\mysqlDataSeek
mysqlDataSeek( $res, $row)
Definition: DatabaseMysqli.php:275
DatabaseMysqli\mysqlNumRows
mysqlNumRows( $res)
Definition: DatabaseMysqli.php:220
DatabaseMysqli\mysqlFreeResult
mysqlFreeResult( $res)
Definition: DatabaseMysqli.php:184
DatabaseMysqli\closeConnection
closeConnection()
Definition: DatabaseMysqli.php:134
DatabaseMysqli\mysqlFetchField
mysqlFetchField( $res, $n)
Definition: DatabaseMysqli.php:237
DatabaseMysqli\mysqlNumFields
mysqlNumFields( $res)
Definition: DatabaseMysqli.php:228
$res
$res
Definition: database.txt:21
DatabaseMysqli\insertId
insertId()
Definition: DatabaseMysqli.php:141