Revert "Use display name in category page subheadings if provided"
[lhc/web/wiklou.git] / includes / db / DatabaseMysqli.php
1 <?php
2 /**
3 * This is the MySQLi database abstraction layer.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * Database abstraction object for PHP extension mysqli.
26 *
27 * @ingroup Database
28 * @since 1.22
29 * @see Database
30 */
31 class DatabaseMysqli extends DatabaseMysqlBase {
32 /** @var mysqli */
33 protected $mConn;
34
35 /**
36 * @param string $sql
37 * @return resource
38 */
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
51 /**
52 * @param string $realServer
53 * @return bool|mysqli
54 * @throws DBConnectionError
55 */
56 protected function mysqlConnect( $realServer ) {
57 global $wgDBmysql5;
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
119 /**
120 * @param string $charset
121 * @return bool
122 */
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
133 /**
134 * @return bool
135 */
136 protected function closeConnection() {
137 $conn = $this->getBindingHandle();
138
139 return $conn->close();
140 }
141
142 /**
143 * @return int
144 */
145 function insertId() {
146 $conn = $this->getBindingHandle();
147
148 return (int)$conn->insert_id;
149 }
150
151 /**
152 * @return int
153 */
154 function lastErrno() {
155 if ( $this->mConn ) {
156 return $this->mConn->errno;
157 } else {
158 return mysqli_connect_errno();
159 }
160 }
161
162 /**
163 * @return int
164 */
165 function affectedRows() {
166 $conn = $this->getBindingHandle();
167
168 return $conn->affected_rows;
169 }
170
171 /**
172 * @param string $db
173 * @return bool
174 */
175 function selectDB( $db ) {
176 $conn = $this->getBindingHandle();
177
178 $this->mDBname = $db;
179
180 return $conn->select_db( $db );
181 }
182
183 /**
184 * @param mysqli $res
185 * @return bool
186 */
187 protected function mysqlFreeResult( $res ) {
188 $res->free_result();
189
190 return true;
191 }
192
193 /**
194 * @param mysqli $res
195 * @return bool
196 */
197 protected function mysqlFetchObject( $res ) {
198 $object = $res->fetch_object();
199 if ( $object === null ) {
200 return false;
201 }
202
203 return $object;
204 }
205
206 /**
207 * @param mysqli $res
208 * @return bool
209 */
210 protected function mysqlFetchArray( $res ) {
211 $array = $res->fetch_array();
212 if ( $array === null ) {
213 return false;
214 }
215
216 return $array;
217 }
218
219 /**
220 * @param mysqli $res
221 * @return mixed
222 */
223 protected function mysqlNumRows( $res ) {
224 return $res->num_rows;
225 }
226
227 /**
228 * @param mysqli $res
229 * @return mixed
230 */
231 protected function mysqlNumFields( $res ) {
232 return $res->field_count;
233 }
234
235 /**
236 * @param mysqli $res
237 * @param int $n
238 * @return mixed
239 */
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
258 /**
259 * @param resource|ResultWrapper $res
260 * @param int $n
261 * @return mixed
262 */
263 protected function mysqlFieldName( $res, $n ) {
264 $field = $res->fetch_field_direct( $n );
265
266 return $field->name;
267 }
268
269 /**
270 * @param resource|ResultWrapper $res
271 * @param int $n
272 * @return mixed
273 */
274 protected function mysqlFieldType( $res, $n ) {
275 $field = $res->fetch_field_direct( $n );
276
277 return $field->type;
278 }
279
280 /**
281 * @param resource|ResultWrapper $res
282 * @param int $row
283 * @return mixed
284 */
285 protected function mysqlDataSeek( $res, $row ) {
286 return $res->data_seek( $row );
287 }
288
289 /**
290 * @param mysqli $conn Optional connection object
291 * @return string
292 */
293 protected function mysqlError( $conn = null ) {
294 if ( $conn === null ) {
295 return mysqli_connect_error();
296 } else {
297 return $conn->error;
298 }
299 }
300
301 /**
302 * Escapes special characters in a string for use in an SQL statement
303 * @param string $s
304 * @return string
305 */
306 protected function mysqlRealEscapeString( $s ) {
307 $conn = $this->getBindingHandle();
308
309 return $conn->real_escape_string( $s );
310 }
311
312 /**
313 * Give an id for the connection
314 *
315 * mysql driver used resource id, but mysqli objects cannot be cast to string.
316 * @return string
317 */
318 public function __toString() {
319 if ( $this->mConn instanceof mysqli ) {
320 return (string)$this->mConn->thread_id;
321 } else {
322 // mConn might be false or something.
323 return (string)$this->mConn;
324 }
325 }
326 }