2 include_once( "FulltextStoplist.php" );
3 include_once( "CacheManager.php" );
5 define( "DB_READ", -1 );
6 define( "DB_WRITE", -2 );
7 define( "DB_LAST", -3 );
9 $wgLastDatabaseQuery = "";
11 function wfGetDB( $altuser = "", $altpassword = "", $altserver = "", $altdb = "" )
13 global $wgDBserver, $wgDBuser, $wgDBpassword;
14 global $wgDBname, $wgDBconnection, $wgEmergencyContact;
16 $noconn = wfMsgNoDB( "noconnect", $wgDBserver );
17 $nodb = wfMsgNoDB( "nodb", $wgDBname );
19 $helpme = "\n<p>If this error persists after reloading and clearing " .
20 "your browser cache, please notify the <a href=\"mailto:" .
21 $wgEmergencyContact . "\">Wikipedia developers</a>.</p>";
23 if ( $altuser != "" ) {
24 $serve = ($altserver ?
$altserver : $wgDBserver );
25 $db = ($altdb ?
$altdb : $wgDBname );
26 $wgDBconnection = mysql_connect( $serve, $altuser, $altpassword )
27 or die( "bad sql user" );
28 mysql_select_db( $db, $wgDBconnection ) or die(
29 htmlspecialchars(mysql_error()) );
32 if ( ! $wgDBconnection ) {
33 @$wgDBconnection = mysql_pconnect( $wgDBserver, $wgDBuser, $wgDBpassword )
34 or wfEmergencyAbort();
36 if( !mysql_select_db( $wgDBname, $wgDBconnection ) ) {
37 /* Persistent connections may become stuck in an unusable state */
38 wfDebug( "Persistent connection is broken?\n", true );
40 @$wgDBconnection = mysql_connect( $wgDBserver, $wgDBuser, $wgDBpassword )
41 or wfEmergencyAbort();
43 @mysql_select_db
( $wgDBname, $wgDBconnection )
44 or wfEmergencyAbort();
47 # mysql_ping( $wgDBconnection );
48 return $wgDBconnection;
51 /* Call this function if we couldn't contact the database...
52 We'll try to use the cache to display something in the meantime */
53 function wfEmergencyAbort( $msg = "" ) {
54 global $wgTitle, $wgUseFileCache, $title, $wgOutputEncoding;
56 header( "Content-type: text/html; charset=$wgOutputEncoding" );
57 if($msg == "") $msg = wfMsgNoDB( "noconnect" );
65 $t = Title
::newFromURL( $title );
67 $t = Title
::newFromText( wfMsgNoDB( "mainpage" ) );
71 $cache = new CacheManager( $t );
72 if( $cache->isFileCached() ) {
73 $msg = "<p style='color: red'><b>$msg<br>\n" .
74 wfMsgNoDB( "cachederror" ) . "</b></p>\n";
76 $tag = "<div id='article'>";
80 $cache->fetchPageText() );
84 /* Don't cache error pages! They cause no end of trouble... */
85 header( "Cache-control: none" );
86 header( "Pragma: nocache" );
91 # $db: DB_READ = -1 read from slave (or only server)
92 # DB_WRITE = -2 write to master (or only server)
93 # 0,1,2,... query a database with a specific index
94 # Replication is not actually implemented just yet
95 function wfQuery( $sql, $db, $fname = "" )
97 global $wgLastDatabaseQuery, $wgOut, $wgDebugDumpSql;
98 $profName = "wfQuery(\"" . strtr( substr($sql, 0, 80 ), "\t\n\r", " " ) . "...\")";
99 wfProfileIn( $profName );
101 if ( !is_numeric( $db ) ) {
102 # Someone has tried to call this the old way
103 $wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
106 $wgLastDatabaseQuery = $sql;
108 if( $wgDebugDumpSql ) {
109 $sqlx = substr( $sql, 0, 500 );
110 $sqlx = wordwrap(strtr($sqlx,"\t\n"," "));
111 wfDebug( "SQL: $sqlx\n" );
115 $ret = mysql_query( $sql, $conn );
117 if ( false === $ret ) {
118 $wgOut->databaseError( $fname );
121 wfProfileOut( $profName );
125 function wfFreeResult( $res ) { mysql_free_result( $res ); }
126 function wfFetchObject( $res ) { return mysql_fetch_object( $res ); }
127 function wfNumRows( $res ) { return mysql_num_rows( $res ); }
128 function wfNumFields( $res ) { return mysql_num_fields( $res ); }
129 function wfFieldName( $res, $n ) { return mysql_field_name( $res, $n ); }
130 function wfInsertId() { return mysql_insert_id( wfGetDB() ); }
131 function wfDataSeek( $res, $row ) { return mysql_data_seek( $res, $row ); }
132 function wfLastErrno() { return mysql_errno(); }
133 function wfLastError() { return mysql_error(); }
134 function wfAffectedRows() { return mysql_affected_rows( wfGetDB() ); }
136 function wfLastDBquery()
138 global $wgLastDatabaseQuery;
139 return $wgLastDatabaseQuery;
142 function wfSetSQL( $table, $var, $value, $cond )
144 $sql = "UPDATE $table SET $var = '" .
145 wfStrencode( $value ) . "' WHERE ($cond)";
146 wfQuery( $sql, DB_WRITE
, "wfSetSQL" );
149 function wfGetSQL( $table, $var, $cond )
151 $sql = "SELECT $var FROM $table WHERE ($cond)";
152 $result = wfQuery( $sql, DB_READ
, "wfGetSQL" );
155 if ( mysql_num_rows( $result ) > 0 ) {
156 $s = mysql_fetch_object( $result );
158 mysql_free_result( $result );
163 function wfStrencode( $s )
165 return addslashes( $s );
168 # Ideally we'd be using actual time fields in the db
169 function wfTimestamp2Unix( $ts ) {
170 return gmmktime( ( (int)substr( $ts, 8, 2) ),
171 (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ),
172 (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ),
173 (int)substr( $ts, 0, 4 ) );
176 function wfUnix2Timestamp( $unixtime ) {
177 return gmdate( "YmdHis", $unixtime );
180 function wfTimestampNow() {
182 return gmdate( "YmdHis" );
185 # Sorting hack for MySQL 3, which doesn't use index sorts for DESC
186 function wfInvertTimestamp( $ts ) {
194 function wfFieldExists( $table, $field )
196 $fname = "wfFieldExists";
197 $res = wfQuery( "DESCRIBE $table", DB_READ
, $fname );
200 while ( $row = wfFetchObject( $res ) ) {
201 if ( $row->Field
== $field ) {
209 function wfIndexExists( $table, $index )
212 $fname = "wfIndexExists";
213 $sql = "SHOW INDEXES FROM $table";
214 $res = wfQuery( $sql, DB_READ
, $fname );
216 while ( $row = wfFetchObject( $res ) ) {
217 if ( $row->Key_name
== $index ) {