Date/time fixes: try to ensure that timestamps are always kept in GMT, with conversio...
[lhc/web/wiklou.git] / includes / DatabaseFunctions.php
1 <?
2 global $IP;
3 include_once( "$IP/FulltextStoplist.php" );
4
5 $wgLastDatabaseQuery = "";
6
7 function wfGetDB( $altuser = "", $altpassword = "" )
8 {
9 global $wgDBserver, $wgDBuser, $wgDBpassword;
10 global $wgDBname, $wgDBconnection, $wgEmergencyContact;
11
12 $noconn = str_replace( "$1", $wgDBserver, wfMsg( "noconnect" ) );
13 $nodb = str_replace( "$1", $wgDBname, wfMsg( "nodb" ) );
14
15 $helpme = "\n<p>If this error persists after reloading and clearing " .
16 "your browser cache, please notify the <a href=\"mailto:" .
17 $wgEmergencyContact . "\">Wikipedia developers</a>.</p>";
18
19 if ( $altuser != "" ) {
20 $wgDBconnection = mysql_connect( $wgDBserver, $altuser, $altpassword )
21 or die( "bad sql user" );
22 mysql_select_db( $wgDBname, $wgDBconnection ) or die(
23 htmlspecialchars(mysql_error()) );
24 }
25
26 if ( ! $wgDBconnection ) {
27 $wgDBconnection = mysql_pconnect( $wgDBserver, $wgDBuser,
28 $wgDBpassword ) or die( $noconn .
29 "\n<p><b>" . htmlspecialchars(mysql_error()) . "</b></p>\n" . $helpme );
30 if( !mysql_select_db( $wgDBname, $wgDBconnection ) ) {
31 wfDebug( "Persistent connection is broken?\n", true );
32
33 $wgDBconnection = mysql_connect( $wgDBserver, $wgDBuser,
34 $wgDBpassword ) or die( $noconn .
35 "\n<p><b>" . htmlspecialchars(mysql_error()) . "</b> (tried non-p connect)</p>\n" . $helpme );
36 mysql_select_db( $wgDBname, $wgDBconnection ) or die( $nodb .
37 "\n<p><b>" . htmlspecialchars(mysql_error()) . "</b> (tried non-p connect)</p>\n" . $helpme );
38 }
39 }
40 # mysql_ping( $wgDBconnection );
41 return $wgDBconnection;
42 }
43
44 function wfQuery( $sql, $fname = "" )
45 {
46 global $wgLastDatabaseQuery, $wgOut;
47 ## wfProfileIn( "wfQuery" );
48 $wgLastDatabaseQuery = $sql;
49
50 $conn = wfGetDB();
51 $ret = mysql_query( $sql, $conn );
52
53 if ( "" != $fname ) {
54 # wfDebug( "{$fname}:SQL: {$sql}\n", true );
55 } else {
56 # wfDebug( "SQL: {$sql}\n", true );
57 }
58 if ( false === $ret ) {
59 $wgOut->databaseError( $fname );
60 exit;
61 }
62 ## wfProfileOut();
63 return $ret;
64 }
65
66 function wfFreeResult( $res ) { mysql_free_result( $res ); }
67 function wfFetchObject( $res ) { return mysql_fetch_object( $res ); }
68 function wfNumRows( $res ) { return mysql_num_rows( $res ); }
69 function wfNumFields( $res ) { return mysql_num_fields( $res ); }
70 function wfFieldName( $res, $n ) { return mysql_field_name( $res, $n ); }
71 function wfInsertId() { return mysql_insert_id( wfGetDB() ); }
72 function wfDataSeek( $res, $row ) { return mysql_data_seek( $res, $row ); }
73 function wfLastErrno() { return mysql_errno(); }
74 function wfLastError() { return mysql_error(); }
75
76 function wfLastDBquery()
77 {
78 global $wgLastDatabaseQuery;
79 return $wgLastDatabaseQuery;
80 }
81
82 function wfSetSQL( $table, $var, $value, $cond )
83 {
84 $sql = "UPDATE $table SET $var = '" .
85 wfStrencode( $value ) . "' WHERE ($cond)";
86 wfQuery( $sql, "wfSetSQL" );
87 }
88
89 function wfGetSQL( $table, $var, $cond )
90 {
91 $sql = "SELECT $var FROM $table WHERE ($cond)";
92 $result = wfQuery( $sql, "wfGetSQL" );
93
94 $ret = "";
95 if ( mysql_num_rows( $result ) > 0 ) {
96 $s = mysql_fetch_object( $result );
97 $ret = $s->$var;
98 mysql_free_result( $result );
99 }
100 return $ret;
101 }
102
103 function wfStrencode( $s )
104 {
105 return addslashes( $s );
106 }
107
108 # Ideally we'd be using actual time fields in the db
109 function wfTimestamp2Unix( $ts ) {
110 return gmmktime( ( (int)substr( $ts, 8, 2) ),
111 (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ),
112 (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ),
113 (int)substr( $ts, 0, 4 ) );
114 }
115
116 function wfUnix2Timestamp( $unixtime ) {
117 return gmdate( "YmdHis", $unixtime );
118 }
119
120 function wfTimestampNow() {
121 # return NOW
122 return gmdate( "YmdHis" );
123 }
124
125 # Sorting hack for MySQL 3, which doesn't use index sorts for DESC
126 function wfInvertTimestamp( $ts ) {
127 return strtr(
128 $ts,
129 "0123456789",
130 "9876543210"
131 );
132 }
133
134 ?>