Move file cache functions into CacheManager class (in CacheManager.php)
[lhc/web/wiklou.git] / includes / DatabaseFunctions.php
1 <?
2 global $IP;
3 include_once( "$IP/FulltextStoplist.php" );
4 include_once( "$IP/CacheManager.php" );
5
6 $wgLastDatabaseQuery = "";
7
8 function wfGetDB( $altuser = "", $altpassword = "" )
9 {
10 global $wgDBserver, $wgDBuser, $wgDBpassword;
11 global $wgDBname, $wgDBconnection, $wgEmergencyContact;
12
13 $noconn = str_replace( "$1", $wgDBserver, wfMsg( "noconnect" ) );
14 $nodb = str_replace( "$1", $wgDBname, wfMsg( "nodb" ) );
15
16 $helpme = "\n<p>If this error persists after reloading and clearing " .
17 "your browser cache, please notify the <a href=\"mailto:" .
18 $wgEmergencyContact . "\">Wikipedia developers</a>.</p>";
19
20 if ( $altuser != "" ) {
21 $wgDBconnection = mysql_connect( $wgDBserver, $altuser, $altpassword )
22 or die( "bad sql user" );
23 mysql_select_db( $wgDBname, $wgDBconnection ) or die(
24 htmlspecialchars(mysql_error()) );
25 }
26
27 if ( ! $wgDBconnection ) {
28 @$wgDBconnection = mysql_pconnect( $wgDBserver, $wgDBuser, $wgDBpassword )
29 or wfEmergencyAbort();
30
31 if( !mysql_select_db( $wgDBname, $wgDBconnection ) ) {
32 /* Persistent connections may become stuck in an unusable state */
33 wfDebug( "Persistent connection is broken?\n", true );
34
35 @$wgDBconnection = mysql_connect( $wgDBserver, $wgDBuser, $wgDBpassword )
36 or wfEmergencyAbort();
37
38 @mysql_select_db( $wgDBname, $wgDBconnection )
39 or wfEmergencyAbort();
40 }
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;
50
51 if($msg == "") $msg = wfMsg( "noconnect" );
52 $text = $msg;
53
54 if($wgUseFileCache) {
55 if($wgTitle) {
56 $t =& $wgTitle;
57 } else {
58 if($title) {
59 $t = Title::newFromURL( $title );
60 } else {
61 $t = Title::newFromText( wfMsg("mainpage") );
62 }
63 }
64
65 $cache = new CacheManager( $t );
66 if( $cache->isFileCached() ) {
67 $msg = "<p style='color: red'><b>$msg<br>\n" .
68 wfMsg( "cachederror" ) . "</b></p>\n";
69
70 $tag = "<div id='article'>";
71 $text = str_replace(
72 $tag,
73 $tag . $msg,
74 $cache->fetchPageText() );
75 }
76 }
77
78 /* Don't cache error pages! They cause no end of trouble... */
79 header( "Cache-control: none" );
80 header( "Pragma: nocache" );
81 echo $text;
82 exit;
83 }
84
85 function wfQuery( $sql, $fname = "" )
86 {
87 global $wgLastDatabaseQuery, $wgOut;
88 ## wfProfileIn( "wfQuery" );
89 $wgLastDatabaseQuery = $sql;
90
91 $conn = wfGetDB();
92 $ret = mysql_query( $sql, $conn );
93
94 if ( "" != $fname ) {
95 # wfDebug( "{$fname}:SQL: {$sql}\n", true );
96 } else {
97 # wfDebug( "SQL: {$sql}\n", true );
98 }
99 if ( false === $ret ) {
100 $wgOut->databaseError( $fname );
101 exit;
102 }
103 ## wfProfileOut();
104 return $ret;
105 }
106
107 function wfFreeResult( $res ) { mysql_free_result( $res ); }
108 function wfFetchObject( $res ) { return mysql_fetch_object( $res ); }
109 function wfNumRows( $res ) { return mysql_num_rows( $res ); }
110 function wfNumFields( $res ) { return mysql_num_fields( $res ); }
111 function wfFieldName( $res, $n ) { return mysql_field_name( $res, $n ); }
112 function wfInsertId() { return mysql_insert_id( wfGetDB() ); }
113 function wfDataSeek( $res, $row ) { return mysql_data_seek( $res, $row ); }
114 function wfLastErrno() { return mysql_errno(); }
115 function wfLastError() { return mysql_error(); }
116
117 function wfLastDBquery()
118 {
119 global $wgLastDatabaseQuery;
120 return $wgLastDatabaseQuery;
121 }
122
123 function wfSetSQL( $table, $var, $value, $cond )
124 {
125 $sql = "UPDATE $table SET $var = '" .
126 wfStrencode( $value ) . "' WHERE ($cond)";
127 wfQuery( $sql, "wfSetSQL" );
128 }
129
130 function wfGetSQL( $table, $var, $cond )
131 {
132 $sql = "SELECT $var FROM $table WHERE ($cond)";
133 $result = wfQuery( $sql, "wfGetSQL" );
134
135 $ret = "";
136 if ( mysql_num_rows( $result ) > 0 ) {
137 $s = mysql_fetch_object( $result );
138 $ret = $s->$var;
139 mysql_free_result( $result );
140 }
141 return $ret;
142 }
143
144 function wfStrencode( $s )
145 {
146 return addslashes( $s );
147 }
148
149 # Ideally we'd be using actual time fields in the db
150 function wfTimestamp2Unix( $ts ) {
151 return gmmktime( ( (int)substr( $ts, 8, 2) ),
152 (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ),
153 (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ),
154 (int)substr( $ts, 0, 4 ) );
155 }
156
157 function wfUnix2Timestamp( $unixtime ) {
158 return gmdate( "YmdHis", $unixtime );
159 }
160
161 function wfTimestampNow() {
162 # return NOW
163 return gmdate( "YmdHis" );
164 }
165
166 # Sorting hack for MySQL 3, which doesn't use index sorts for DESC
167 function wfInvertTimestamp( $ts ) {
168 return strtr(
169 $ts,
170 "0123456789",
171 "9876543210"
172 );
173 }
174
175 ?>