Some more tweaking of prelim memcached support; shortened keys, added newtalk for...
[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 = "", $altserver = "", $altdb = "" )
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 $serve = ($altserver ? $altserver : $wgDBserver );
22 $db = ($altdb ? $altdb : $wgDBname );
23 $wgDBconnection = mysql_connect( $serve, $altuser, $altpassword )
24 or die( "bad sql user" );
25 mysql_select_db( $db, $wgDBconnection ) or die(
26 htmlspecialchars(mysql_error()) );
27 }
28
29 if ( ! $wgDBconnection ) {
30 @$wgDBconnection = mysql_pconnect( $wgDBserver, $wgDBuser, $wgDBpassword )
31 or wfEmergencyAbort();
32
33 if( !mysql_select_db( $wgDBname, $wgDBconnection ) ) {
34 /* Persistent connections may become stuck in an unusable state */
35 wfDebug( "Persistent connection is broken?\n", true );
36
37 @$wgDBconnection = mysql_connect( $wgDBserver, $wgDBuser, $wgDBpassword )
38 or wfEmergencyAbort();
39
40 @mysql_select_db( $wgDBname, $wgDBconnection )
41 or wfEmergencyAbort();
42 }
43 }
44 # mysql_ping( $wgDBconnection );
45 return $wgDBconnection;
46 }
47
48 /* Call this function if we couldn't contact the database...
49 We'll try to use the cache to display something in the meantime */
50 function wfEmergencyAbort( $msg = "" ) {
51 global $wgTitle, $wgUseFileCache, $title, $wgOutputEncoding;
52
53 header( "Content-type: text/html; charset=$wgOutputEncoding" );
54 if($msg == "") $msg = wfMsg( "noconnect" );
55 $text = $msg;
56
57 if($wgUseFileCache) {
58 if($wgTitle) {
59 $t =& $wgTitle;
60 } else {
61 if($title) {
62 $t = Title::newFromURL( $title );
63 } else {
64 $t = Title::newFromText( wfMsg("mainpage") );
65 }
66 }
67
68 $cache = new CacheManager( $t );
69 if( $cache->isFileCached() ) {
70 $msg = "<p style='color: red'><b>$msg<br>\n" .
71 wfMsg( "cachederror" ) . "</b></p>\n";
72
73 $tag = "<div id='article'>";
74 $text = str_replace(
75 $tag,
76 $tag . $msg,
77 $cache->fetchPageText() );
78 }
79 }
80
81 /* Don't cache error pages! They cause no end of trouble... */
82 header( "Cache-control: none" );
83 header( "Pragma: nocache" );
84 echo $text;
85 exit;
86 }
87
88 function wfQuery( $sql, $fname = "" )
89 {
90 global $wgLastDatabaseQuery, $wgOut;
91 ## wfProfileIn( "wfQuery" );
92 $wgLastDatabaseQuery = $sql;
93
94 $conn = wfGetDB();
95 $ret = mysql_query( $sql, $conn );
96
97 if ( "" != $fname ) {
98 # wfDebug( "{$fname}:SQL: {$sql}\n", true );
99 } else {
100 # wfDebug( "SQL: {$sql}\n", true );
101 }
102 if ( false === $ret ) {
103 $wgOut->databaseError( $fname );
104 exit;
105 }
106 ## wfProfileOut();
107 return $ret;
108 }
109
110 function wfFreeResult( $res ) { mysql_free_result( $res ); }
111 function wfFetchObject( $res ) { return mysql_fetch_object( $res ); }
112 function wfNumRows( $res ) { return mysql_num_rows( $res ); }
113 function wfNumFields( $res ) { return mysql_num_fields( $res ); }
114 function wfFieldName( $res, $n ) { return mysql_field_name( $res, $n ); }
115 function wfInsertId() { return mysql_insert_id( wfGetDB() ); }
116 function wfDataSeek( $res, $row ) { return mysql_data_seek( $res, $row ); }
117 function wfLastErrno() { return mysql_errno(); }
118 function wfLastError() { return mysql_error(); }
119
120 function wfLastDBquery()
121 {
122 global $wgLastDatabaseQuery;
123 return $wgLastDatabaseQuery;
124 }
125
126 function wfSetSQL( $table, $var, $value, $cond )
127 {
128 $sql = "UPDATE $table SET $var = '" .
129 wfStrencode( $value ) . "' WHERE ($cond)";
130 wfQuery( $sql, "wfSetSQL" );
131 }
132
133 function wfGetSQL( $table, $var, $cond )
134 {
135 $sql = "SELECT $var FROM $table WHERE ($cond)";
136 $result = wfQuery( $sql, "wfGetSQL" );
137
138 $ret = "";
139 if ( mysql_num_rows( $result ) > 0 ) {
140 $s = mysql_fetch_object( $result );
141 $ret = $s->$var;
142 mysql_free_result( $result );
143 }
144 return $ret;
145 }
146
147 function wfStrencode( $s )
148 {
149 return addslashes( $s );
150 }
151
152 # Ideally we'd be using actual time fields in the db
153 function wfTimestamp2Unix( $ts ) {
154 return gmmktime( ( (int)substr( $ts, 8, 2) ),
155 (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ),
156 (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ),
157 (int)substr( $ts, 0, 4 ) );
158 }
159
160 function wfUnix2Timestamp( $unixtime ) {
161 return gmdate( "YmdHis", $unixtime );
162 }
163
164 function wfTimestampNow() {
165 # return NOW
166 return gmdate( "YmdHis" );
167 }
168
169 # Sorting hack for MySQL 3, which doesn't use index sorts for DESC
170 function wfInvertTimestamp( $ts ) {
171 return strtr(
172 $ts,
173 "0123456789",
174 "9876543210"
175 );
176 }
177
178 ?>