Merge "Enable module storage for 0.05% of visitors w/storage-capable browsers"
[lhc/web/wiklou.git] / includes / db / 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
24 /**
25 * Database abstraction object for PHP extension mysql.
26 *
27 * @ingroup Database
28 * @see Database
29 */
30 class DatabaseMysql extends DatabaseMysqlBase {
31
32 /**
33 * @param $sql string
34 * @return resource
35 */
36 protected function doQuery( $sql ) {
37 if ( $this->bufferResults() ) {
38 $ret = mysql_query( $sql, $this->mConn );
39 } else {
40 $ret = mysql_unbuffered_query( $sql, $this->mConn );
41 }
42 return $ret;
43 }
44
45 protected function mysqlConnect( $realServer ) {
46 # Fail now
47 # Otherwise we get a suppressed fatal error, which is very hard to track down
48 if ( !extension_loaded( 'mysql' ) ) {
49 throw new DBConnectionError( $this, "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n" );
50 }
51
52 $connFlags = 0;
53 if ( $this->mFlags & DBO_SSL ) {
54 $connFlags |= MYSQL_CLIENT_SSL;
55 }
56 if ( $this->mFlags & DBO_COMPRESS ) {
57 $connFlags |= MYSQL_CLIENT_COMPRESS;
58 }
59
60 if ( ini_get( 'mysql.connect_timeout' ) <= 3 ) {
61 $numAttempts = 2;
62 } else {
63 $numAttempts = 1;
64 }
65
66 $conn = false;
67
68 for ( $i = 0; $i < $numAttempts && !$conn; $i++ ) {
69 if ( $i > 1 ) {
70 usleep( 1000 );
71 }
72 if ( $this->mFlags & DBO_PERSISTENT ) {
73 $conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags );
74 } else {
75 # Create a new connection...
76 $conn = mysql_connect( $realServer, $this->mUser, $this->mPassword, true, $connFlags );
77 }
78 }
79
80 return $conn;
81 }
82
83 /**
84 * @return bool
85 */
86 protected function mysqlSetCharset( $charset ) {
87 if ( function_exists( 'mysql_set_charset' ) ) {
88 return mysql_set_charset( $charset, $this->mConn );
89 } else {
90 return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
91 }
92 }
93
94 /**
95 * @return bool
96 */
97 protected function closeConnection() {
98 return mysql_close( $this->mConn );
99 }
100
101 /**
102 * @return int
103 */
104 function insertId() {
105 return mysql_insert_id( $this->mConn );
106 }
107
108 /**
109 * @return int
110 */
111 function lastErrno() {
112 if ( $this->mConn ) {
113 return mysql_errno( $this->mConn );
114 } else {
115 return mysql_errno();
116 }
117 }
118
119 /**
120 * @return int
121 */
122 function affectedRows() {
123 return mysql_affected_rows( $this->mConn );
124 }
125
126 /**
127 * @param $db
128 * @return bool
129 */
130 function selectDB( $db ) {
131 $this->mDBname = $db;
132 return mysql_select_db( $db, $this->mConn );
133 }
134
135 /**
136 * @return string
137 */
138 function getServerVersion() {
139 return mysql_get_server_info( $this->mConn );
140 }
141
142 protected function mysqlFreeResult( $res ) {
143 return mysql_free_result( $res );
144 }
145
146 protected function mysqlFetchObject( $res ) {
147 return mysql_fetch_object( $res );
148 }
149
150 protected function mysqlFetchArray( $res ) {
151 return mysql_fetch_array( $res );
152 }
153
154 protected function mysqlNumRows( $res ) {
155 return mysql_num_rows( $res );
156 }
157
158 protected function mysqlNumFields( $res ) {
159 return mysql_num_fields( $res );
160 }
161
162 protected function mysqlFetchField( $res, $n ) {
163 return mysql_fetch_field( $res, $n );
164 }
165
166 protected function mysqlFieldName( $res, $n ) {
167 return mysql_field_name( $res, $n );
168 }
169
170 protected function mysqlDataSeek( $res, $row ) {
171 return mysql_data_seek( $res, $row );
172 }
173
174 protected function mysqlError( $conn = null ) {
175 return ( $conn !== null ) ? mysql_error( $conn ) : mysql_error(); // avoid warning
176 }
177
178 protected function mysqlRealEscapeString( $s ) {
179 return mysql_real_escape_string( $s, $this->mConn );
180 }
181
182 protected function mysqlPing() {
183 return mysql_ping( $this->mConn );
184 }
185 }