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