Removed use of PHP persistent connections. Se URL below for details.
[lhc/web/wiklou.git] / includes / DatabaseFunctions.php
1 <?
2 include_once( "FulltextStoplist.php" );
3 include_once( "CacheManager.php" );
4
5 define( "DB_READ", -1 );
6 define( "DB_WRITE", -2 );
7 define( "DB_LAST", -3 );
8
9 $wgLastDatabaseQuery = "";
10
11 /* private */ $wgBufferSQLResults = true;
12 /* private */ $wgIgnoreSQLErrors = false;
13
14 function wfGetDB( $altuser = "", $altpassword = "", $altserver = "", $altdb = "" )
15 {
16 global $wgDBserver, $wgDBuser, $wgDBpassword;
17 global $wgDBname, $wgDBconnection, $wgEmergencyContact;
18
19 $noconn = wfMsgNoDB( "noconnect", $wgDBserver );
20 $nodb = wfMsgNoDB( "nodb", $wgDBname );
21
22 $helpme = "\n<p>If this error persists after reloading and clearing " .
23 "your browser cache, please notify the <a href=\"mailto:" .
24 $wgEmergencyContact . "\">Wikipedia developers</a>.</p>";
25
26 if ( $altuser != "" ) {
27 $serve = ($altserver ? $altserver : $wgDBserver );
28 $db = ($altdb ? $altdb : $wgDBname );
29 $wgDBconnection = mysql_connect( $serve, $altuser, $altpassword )
30 or die( "bad sql user" );
31 mysql_select_db( $db, $wgDBconnection ) or die(
32 htmlspecialchars(mysql_error()) );
33 }
34
35 if ( ! $wgDBconnection ) {
36 @$wgDBconnection = mysql_connect( $wgDBserver, $wgDBuser, $wgDBpassword )
37 or wfEmergencyAbort();
38
39 @mysql_select_db( $wgDBname, $wgDBconnection )
40 or wfEmergencyAbort();
41 }
42 # mysql_ping( $wgDBconnection );
43 return $wgDBconnection;
44 }
45
46 /* Call this function if we couldn't contact the database...
47 We'll try to use the cache to display something in the meantime */
48 function wfEmergencyAbort( $msg = "" ) {
49 global $wgTitle, $wgUseFileCache, $title, $wgOutputEncoding;
50
51 header( "Content-type: text/html; charset=$wgOutputEncoding" );
52 if($msg == "") $msg = wfMsgNoDB( "noconnect" );
53 $text = $msg;
54
55 if($wgUseFileCache) {
56 if($wgTitle) {
57 $t =& $wgTitle;
58 } else {
59 if($title) {
60 $t = Title::newFromURL( $title );
61 } else {
62 $t = Title::newFromText( wfMsgNoDB( "mainpage" ) );
63 }
64 }
65
66 $cache = new CacheManager( $t );
67 if( $cache->isFileCached() ) {
68 $msg = "<p style='color: red'><b>$msg<br>\n" .
69 wfMsgNoDB( "cachederror" ) . "</b></p>\n";
70
71 $tag = "<div id='article'>";
72 $text = str_replace(
73 $tag,
74 $tag . $msg,
75 $cache->fetchPageText() );
76 }
77 }
78
79 /* Don't cache error pages! They cause no end of trouble... */
80 header( "Cache-control: none" );
81 header( "Pragma: nocache" );
82 echo $text;
83 wfAbruptExit();
84 }
85
86 # $db: DB_READ = -1 read from slave (or only server)
87 # DB_WRITE = -2 write to master (or only server)
88 # 0,1,2,... query a database with a specific index
89 # Replication is not actually implemented just yet
90 function wfQuery( $sql, $db, $fname = "" )
91 {
92 global $wgLastDatabaseQuery, $wgOut, $wgDebugDumpSql, $wgBufferSQLResults,
93 $wgIgnoreSQLErrors, $wgProfiling;
94
95 if ( $wgProfiling ) {
96 # wfGeneralizeSQL will probably cut down the query to reasonable
97 # logging size most of the time. The substr is really just a sanity check.
98 $profName = "wfQuery: " . substr( wfGeneralizeSQL( $sql ), 0, 255 );
99 wfProfileIn( $profName );
100 }
101
102 if ( !is_numeric( $db ) ) {
103 # Someone has tried to call this the old way
104 $wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
105 }
106
107 $wgLastDatabaseQuery = $sql;
108
109 if( $wgDebugDumpSql ) {
110 $sqlx = substr( $sql, 0, 500 );
111 $sqlx = wordwrap(strtr($sqlx,"\t\n"," "));
112 wfDebug( "SQL: $sqlx\n" );
113 }
114
115 $conn = wfGetDB();
116 if( $wgBufferSQLResults ) {
117 $ret = mysql_query( $sql, $conn );
118 } else {
119 $ret = mysql_unbuffered_query( $sql, $conn );
120 }
121
122 if ( false === $ret ) {
123 if( $wgIgnoreSQLErrors ) {
124 wfDebug("SQL ERROR (ignored): " . mysql_error( $conn ) . "\n");
125 } else {
126 wfDebug("SQL ERROR: " . mysql_error( $conn ) . "\n");
127 $wgOut->databaseError( $fname ); // calls wfAbruptExit()
128 }
129 }
130
131 if ( $wgProfiling ) {
132 wfProfileOut( $profName );
133 }
134 return $ret;
135 }
136
137 # Turns buffering of SQL result sets on (true) or off (false). Default is
138 # "on" and it should not be changed without good reasons.
139 # Returns the previous state.
140
141 function wfBufferSQLResults( $newstate ){
142 global $wgBufferSQLResults;
143 $oldstate = $wgBufferSQLResults;
144 $wgBufferSQLResults = $newstate;
145 return $oldstate;
146 }
147
148 # Turns on (false) or off (true) the automatic generation and sending
149 # of a "we're sorry, but there has been a database error" page on
150 # database errors. Default is on (false). When turned off, the
151 # code should use wfLastErrno() and wfLastError() to handle the
152 # situation as appropriate.
153 # Returns the previous state.
154
155 function wfIgnoreSQLErrors( $newstate ){
156 global $wgIgnoreSQLErrors;
157 $oldstate = $wgIgnoreSQLErrors;
158 $wgIgnoreSQLErrors = $newstate;
159 return $oldstate;
160 }
161
162 function wfFreeResult( $res ) { mysql_free_result( $res ); }
163 function wfFetchObject( $res ) { return mysql_fetch_object( $res ); }
164 function wfNumRows( $res ) { return mysql_num_rows( $res ); }
165 function wfNumFields( $res ) { return mysql_num_fields( $res ); }
166 function wfFieldName( $res, $n ) { return mysql_field_name( $res, $n ); }
167 function wfInsertId() { return mysql_insert_id( wfGetDB() ); }
168 function wfDataSeek( $res, $row ) { return mysql_data_seek( $res, $row ); }
169 function wfLastErrno() { return mysql_errno(); }
170 function wfLastError() { return mysql_error(); }
171 function wfAffectedRows() { return mysql_affected_rows( wfGetDB() ); }
172
173 function wfLastDBquery()
174 {
175 global $wgLastDatabaseQuery;
176 return $wgLastDatabaseQuery;
177 }
178
179 function wfSetSQL( $table, $var, $value, $cond )
180 {
181 $sql = "UPDATE $table SET $var = '" .
182 wfStrencode( $value ) . "' WHERE ($cond)";
183 wfQuery( $sql, DB_WRITE, "wfSetSQL" );
184 }
185
186 function wfGetSQL( $table, $var, $cond )
187 {
188 $sql = "SELECT $var FROM $table WHERE ($cond)";
189 $result = wfQuery( $sql, DB_READ, "wfGetSQL" );
190
191 $ret = "";
192 if ( mysql_num_rows( $result ) > 0 ) {
193 $s = mysql_fetch_object( $result );
194 $ret = $s->$var;
195 mysql_free_result( $result );
196 }
197 return $ret;
198 }
199
200 function wfStrencode( $s )
201 {
202 return addslashes( $s );
203 }
204
205 # Ideally we'd be using actual time fields in the db
206 function wfTimestamp2Unix( $ts ) {
207 return gmmktime( ( (int)substr( $ts, 8, 2) ),
208 (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ),
209 (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ),
210 (int)substr( $ts, 0, 4 ) );
211 }
212
213 function wfUnix2Timestamp( $unixtime ) {
214 return gmdate( "YmdHis", $unixtime );
215 }
216
217 function wfTimestampNow() {
218 # return NOW
219 return gmdate( "YmdHis" );
220 }
221
222 # Sorting hack for MySQL 3, which doesn't use index sorts for DESC
223 function wfInvertTimestamp( $ts ) {
224 return strtr(
225 $ts,
226 "0123456789",
227 "9876543210"
228 );
229 }
230
231 # Removes most variables from an SQL query and replaces them with X or N for numbers.
232 # It's only slightly flawed. Don't use for anything important.
233 function wfGeneralizeSQL( $sql )
234 {
235 # This does the same as the regexp below would do, but in such a way
236 # as to avoid crashing php on some large strings.
237 # $sql = preg_replace ( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql);
238
239 $sql = str_replace ( "\\\\", "", $sql);
240 $sql = str_replace ( "\\'", "", $sql);
241 $sql = str_replace ( "\\\"", "", $sql);
242 $sql = preg_replace ("/'.*'/s", "'X'", $sql);
243 $sql = preg_replace ('/".*"/s', "'X'", $sql);
244
245 # All newlines, tabs, etc replaced by single space
246 $sql = preg_replace ( "/\s+/", " ", $sql);
247
248 # All numbers => N
249 $sql = preg_replace ('/-?[0-9]+/s', "N", $sql);
250
251 return $sql;
252 }
253
254 function wfFieldExists( $table, $field )
255 {
256 $fname = "wfFieldExists";
257 $res = wfQuery( "DESCRIBE $table", DB_READ, $fname );
258 $found = false;
259
260 while ( $row = wfFetchObject( $res ) ) {
261 if ( $row->Field == $field ) {
262 $found = true;
263 break;
264 }
265 }
266 return $found;
267 }
268
269 function wfIndexExists( $table, $index )
270 {
271 global $wgDBname;
272 $fname = "wfIndexExists";
273 $sql = "SHOW INDEXES FROM $table";
274 $res = wfQuery( $sql, DB_READ, $fname );
275 $found = false;
276 while ( $row = wfFetchObject( $res ) ) {
277 if ( $row->Key_name == $index ) {
278 $found = true;
279 break;
280 }
281 }
282 return $found;
283 }
284 ?>