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