Rewrite pref cleanup script
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseMysql.php
1 <?php
2 /**
3 * This is the MySQL 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 namespace Wikimedia\Rdbms;
24
25 /**
26 * Database abstraction object for PHP extension mysql.
27 *
28 * @ingroup Database
29 * @see Database
30 */
31 class DatabaseMysql extends DatabaseMysqlBase {
32 /**
33 * @param string $sql
34 * @return resource False on error
35 */
36 protected function doQuery( $sql ) {
37 $conn = $this->getBindingHandle();
38
39 if ( $this->bufferResults() ) {
40 $ret = mysql_query( $sql, $conn );
41 } else {
42 $ret = mysql_unbuffered_query( $sql, $conn );
43 }
44
45 return $ret;
46 }
47
48 /**
49 * @param string $realServer
50 * @return bool|resource MySQL Database connection or false on failure to connect
51 * @throws DBConnectionError
52 */
53 protected function mysqlConnect( $realServer ) {
54 # Avoid a suppressed fatal error, which is very hard to track down
55 if ( !extension_loaded( 'mysql' ) ) {
56 throw new DBConnectionError(
57 $this,
58 "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n"
59 );
60 }
61
62 $connFlags = 0;
63 if ( $this->mFlags & self::DBO_SSL ) {
64 $connFlags |= MYSQL_CLIENT_SSL;
65 }
66 if ( $this->mFlags & self::DBO_COMPRESS ) {
67 $connFlags |= MYSQL_CLIENT_COMPRESS;
68 }
69
70 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
71 $numAttempts = 2;
72 } else {
73 $numAttempts = 1;
74 }
75
76 $conn = false;
77
78 # The kernel's default SYN retransmission period is far too slow for us,
79 # so we use a short timeout plus a manual retry. Retrying means that a small
80 # but finite rate of SYN packet loss won't cause user-visible errors.
81 for ( $i = 0; $i < $numAttempts && !$conn; $i++ ) {
82 if ( $i > 1 ) {
83 usleep( 1000 );
84 }
85 if ( $this->mFlags & self::DBO_PERSISTENT ) {
86 $conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags );
87 } else {
88 # Create a new connection...
89 $conn = mysql_connect( $realServer, $this->mUser, $this->mPassword, true, $connFlags );
90 }
91 }
92
93 return $conn;
94 }
95
96 /**
97 * @param string $charset
98 * @return bool
99 */
100 protected function mysqlSetCharset( $charset ) {
101 $conn = $this->getBindingHandle();
102
103 if ( function_exists( 'mysql_set_charset' ) ) {
104 return mysql_set_charset( $charset, $conn );
105 } else {
106 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
107 }
108 }
109
110 /**
111 * @return bool
112 */
113 protected function closeConnection() {
114 $conn = $this->getBindingHandle();
115
116 return mysql_close( $conn );
117 }
118
119 /**
120 * @return int
121 */
122 function insertId() {
123 $conn = $this->getBindingHandle();
124
125 return mysql_insert_id( $conn );
126 }
127
128 /**
129 * @return int
130 */
131 function lastErrno() {
132 if ( $this->mConn ) {
133 return mysql_errno( $this->mConn );
134 } else {
135 return mysql_errno();
136 }
137 }
138
139 /**
140 * @return int
141 */
142 function affectedRows() {
143 $conn = $this->getBindingHandle();
144
145 return mysql_affected_rows( $conn );
146 }
147
148 /**
149 * @param string $db
150 * @return bool
151 */
152 function selectDB( $db ) {
153 $conn = $this->getBindingHandle();
154
155 $this->mDBname = $db;
156
157 return mysql_select_db( $db, $conn );
158 }
159
160 protected function mysqlFreeResult( $res ) {
161 return mysql_free_result( $res );
162 }
163
164 protected function mysqlFetchObject( $res ) {
165 return mysql_fetch_object( $res );
166 }
167
168 protected function mysqlFetchArray( $res ) {
169 return mysql_fetch_array( $res );
170 }
171
172 protected function mysqlNumRows( $res ) {
173 return mysql_num_rows( $res );
174 }
175
176 protected function mysqlNumFields( $res ) {
177 return mysql_num_fields( $res );
178 }
179
180 protected function mysqlFetchField( $res, $n ) {
181 return mysql_fetch_field( $res, $n );
182 }
183
184 protected function mysqlFieldName( $res, $n ) {
185 return mysql_field_name( $res, $n );
186 }
187
188 protected function mysqlFieldType( $res, $n ) {
189 return mysql_field_type( $res, $n );
190 }
191
192 protected function mysqlDataSeek( $res, $row ) {
193 return mysql_data_seek( $res, $row );
194 }
195
196 protected function mysqlError( $conn = null ) {
197 return ( $conn !== null ) ? mysql_error( $conn ) : mysql_error(); // avoid warning
198 }
199
200 protected function mysqlRealEscapeString( $s ) {
201 $conn = $this->getBindingHandle();
202
203 return mysql_real_escape_string( $s, $conn );
204 }
205 }
206
207 class_alias( DatabaseMysql::class, 'DatabaseMysql' );