Updated (unused) recentchangestext, some corrections (bugs and otherwise)
[mediawiki.git] / includes / DatabaseFunctions.php
blobbd767e8ed6ae9636634763d22aa1530fc4c93f46
1 <?
2 global $IP;
3 include_once( "$IP/FulltextStoplist.php" );
4 include_once( "$IP/CacheManager.php" );
6 $wgLastDatabaseQuery = "";
8 function wfGetDB( $altuser = "", $altpassword = "", $altserver = "", $altdb = "" )
10 global $wgDBserver, $wgDBuser, $wgDBpassword;
11 global $wgDBname, $wgDBconnection, $wgEmergencyContact;
13 $noconn = str_replace( "$1", $wgDBserver, wfMsg( "noconnect" ) );
14 $nodb = str_replace( "$1", $wgDBname, wfMsg( "nodb" ) );
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>";
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()) );
29 if ( ! $wgDBconnection ) {
30 @$wgDBconnection = mysql_pconnect( $wgDBserver, $wgDBuser, $wgDBpassword )
31 or wfEmergencyAbort();
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 );
37 @$wgDBconnection = mysql_connect( $wgDBserver, $wgDBuser, $wgDBpassword )
38 or wfEmergencyAbort();
40 @mysql_select_db( $wgDBname, $wgDBconnection )
41 or wfEmergencyAbort();
44 # mysql_ping( $wgDBconnection );
45 return $wgDBconnection;
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;
53 header( "Content-type: text/html; charset=$wgOutputEncoding" );
54 if($msg == "") $msg = wfMsg( "noconnect" );
55 $text = $msg;
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") );
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";
73 $tag = "<div id='article'>";
74 $text = str_replace(
75 $tag,
76 $tag . $msg,
77 $cache->fetchPageText() );
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;
88 function wfQuery( $sql, $fname = "" )
90 global $wgLastDatabaseQuery, $wgOut;
91 ## wfProfileIn( "wfQuery" );
92 $wgLastDatabaseQuery = $sql;
94 $conn = wfGetDB();
95 $ret = mysql_query( $sql, $conn );
97 if ( "" != $fname ) {
98 # wfDebug( "{$fname}:SQL: {$sql}\n", true );
99 } else {
100 # wfDebug( "SQL: {$sql}\n", true );
102 if ( false === $ret ) {
103 $wgOut->databaseError( $fname );
104 exit;
106 ## wfProfileOut();
107 return $ret;
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(); }
120 function wfLastDBquery()
122 global $wgLastDatabaseQuery;
123 return $wgLastDatabaseQuery;
126 function wfSetSQL( $table, $var, $value, $cond )
128 $sql = "UPDATE $table SET $var = '" .
129 wfStrencode( $value ) . "' WHERE ($cond)";
130 wfQuery( $sql, "wfSetSQL" );
133 function wfGetSQL( $table, $var, $cond )
135 $sql = "SELECT $var FROM $table WHERE ($cond)";
136 $result = wfQuery( $sql, "wfGetSQL" );
138 $ret = "";
139 if ( mysql_num_rows( $result ) > 0 ) {
140 $s = mysql_fetch_object( $result );
141 $ret = $s->$var;
142 mysql_free_result( $result );
144 return $ret;
147 function wfStrencode( $s )
149 return addslashes( $s );
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 ) );
160 function wfUnix2Timestamp( $unixtime ) {
161 return gmdate( "YmdHis", $unixtime );
164 function wfTimestampNow() {
165 # return NOW
166 return gmdate( "YmdHis" );
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"