URL-escape the main page link on the logo
[mediawiki.git] / includes / DatabaseFunctions.php
bloba1c438818ebc29f78a7e6fac75f066f3d57c9db3
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(); }
119 function wfAffectedRows() { return mysql_affected_rows( wfGetDB() ); }
121 function wfLastDBquery()
123 global $wgLastDatabaseQuery;
124 return $wgLastDatabaseQuery;
127 function wfSetSQL( $table, $var, $value, $cond )
129 $sql = "UPDATE $table SET $var = '" .
130 wfStrencode( $value ) . "' WHERE ($cond)";
131 wfQuery( $sql, "wfSetSQL" );
134 function wfGetSQL( $table, $var, $cond )
136 $sql = "SELECT $var FROM $table WHERE ($cond)";
137 $result = wfQuery( $sql, "wfGetSQL" );
139 $ret = "";
140 if ( mysql_num_rows( $result ) > 0 ) {
141 $s = mysql_fetch_object( $result );
142 $ret = $s->$var;
143 mysql_free_result( $result );
145 return $ret;
148 function wfStrencode( $s )
150 return addslashes( $s );
153 # Ideally we'd be using actual time fields in the db
154 function wfTimestamp2Unix( $ts ) {
155 return gmmktime( ( (int)substr( $ts, 8, 2) ),
156 (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ),
157 (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ),
158 (int)substr( $ts, 0, 4 ) );
161 function wfUnix2Timestamp( $unixtime ) {
162 return gmdate( "YmdHis", $unixtime );
165 function wfTimestampNow() {
166 # return NOW
167 return gmdate( "YmdHis" );
170 # Sorting hack for MySQL 3, which doesn't use index sorts for DESC
171 function wfInvertTimestamp( $ts ) {
172 return strtr(
173 $ts,
174 "0123456789",
175 "9876543210"