Use wfMsg()'s parameters
[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 define( "DB_READ", -1 );
7 define( "DB_WRITE", -2 );
8 define( "DB_LAST", -3 );
9
10 $wgLastDatabaseQuery = "";
11
12 function wfGetDB( $altuser = "", $altpassword = "", $altserver = "", $altdb = "" )
13 {
14 global $wgDBserver, $wgDBuser, $wgDBpassword;
15 global $wgDBname, $wgDBconnection, $wgEmergencyContact;
16
17 $noconn = wfMsgNoDB( "noconnect", $wgDBserver );
18 $nodb = wfMsgNoDB( "nodb", $wgDBname );
19
20 $helpme = "\n<p>If this error persists after reloading and clearing " .
21 "your browser cache, please notify the <a href=\"mailto:" .
22 $wgEmergencyContact . "\">Wikipedia developers</a>.</p>";
23
24 if ( $altuser != "" ) {
25 $serve = ($altserver ? $altserver : $wgDBserver );
26 $db = ($altdb ? $altdb : $wgDBname );
27 $wgDBconnection = mysql_connect( $serve, $altuser, $altpassword )
28 or die( "bad sql user" );
29 mysql_select_db( $db, $wgDBconnection ) or die(
30 htmlspecialchars(mysql_error()) );
31 }
32
33 if ( ! $wgDBconnection ) {
34 @$wgDBconnection = mysql_pconnect( $wgDBserver, $wgDBuser, $wgDBpassword )
35 or wfEmergencyAbort();
36
37 if( !mysql_select_db( $wgDBname, $wgDBconnection ) ) {
38 /* Persistent connections may become stuck in an unusable state */
39 wfDebug( "Persistent connection is broken?\n", true );
40
41 @$wgDBconnection = mysql_connect( $wgDBserver, $wgDBuser, $wgDBpassword )
42 or wfEmergencyAbort();
43
44 @mysql_select_db( $wgDBname, $wgDBconnection )
45 or wfEmergencyAbort();
46 }
47 }
48 # mysql_ping( $wgDBconnection );
49 return $wgDBconnection;
50 }
51
52 /* Call this function if we couldn't contact the database...
53 We'll try to use the cache to display something in the meantime */
54 function wfEmergencyAbort( $msg = "" ) {
55 global $wgTitle, $wgUseFileCache, $title, $wgOutputEncoding;
56
57 header( "Content-type: text/html; charset=$wgOutputEncoding" );
58 if($msg == "") $msg = wfMsgNoDB( "noconnect" );
59 $text = $msg;
60
61 if($wgUseFileCache) {
62 if($wgTitle) {
63 $t =& $wgTitle;
64 } else {
65 if($title) {
66 $t = Title::newFromURL( $title );
67 } else {
68 $t = Title::newFromText( wfMsgNoDB( "mainpage" ) );
69 }
70 }
71
72 $cache = new CacheManager( $t );
73 if( $cache->isFileCached() ) {
74 $msg = "<p style='color: red'><b>$msg<br>\n" .
75 wfMsgNoDB( "cachederror" ) . "</b></p>\n";
76
77 $tag = "<div id='article'>";
78 $text = str_replace(
79 $tag,
80 $tag . $msg,
81 $cache->fetchPageText() );
82 }
83 }
84
85 /* Don't cache error pages! They cause no end of trouble... */
86 header( "Cache-control: none" );
87 header( "Pragma: nocache" );
88 echo $text;
89 exit;
90 }
91
92 # $db: DB_READ = -1 read from slave (or only server)
93 # DB_WRITE = -2 write to master (or only server)
94 # 0,1,2,... query a database with a specific index
95 # Replication is not actually implemented just yet
96 function wfQuery( $sql, $db, $fname = "" )
97 {
98 global $wgLastDatabaseQuery, $wgOut, $wgDebugDumpSql;
99 $profName = "wfQuery(\"" . strtr( substr($sql, 0, 80 ), "\t\n\r", " " ) . "...\")";
100 wfProfileIn( $profName );
101
102 if ( !is_numeric( $db ) ) {
103 # Someone has tried to call this the old way
104 $wgOut->fatalError( wfMsgNoDB( "wrong_wfQuery_params", $db, $sql ) );
105 }
106
107 $wgLastDatabaseQuery = $sql;
108
109 if( $wgDebugDumpSql ) {
110 $sqlx = substr( $sql, 0, 500 );
111 $sqlx = wordwrap(strtr($sqlx,"\t\n"," "));
112 wfDebug( "SQL: $sqlx\n" );
113 }
114
115 $conn = wfGetDB();
116 $ret = mysql_query( $sql, $conn );
117
118 if ( false === $ret ) {
119 $wgOut->databaseError( $fname );
120 exit;
121 }
122 wfProfileOut( $profName );
123 return $ret;
124 }
125
126 function wfFreeResult( $res ) { mysql_free_result( $res ); }
127 function wfFetchObject( $res ) { return mysql_fetch_object( $res ); }
128 function wfNumRows( $res ) { return mysql_num_rows( $res ); }
129 function wfNumFields( $res ) { return mysql_num_fields( $res ); }
130 function wfFieldName( $res, $n ) { return mysql_field_name( $res, $n ); }
131 function wfInsertId() { return mysql_insert_id( wfGetDB() ); }
132 function wfDataSeek( $res, $row ) { return mysql_data_seek( $res, $row ); }
133 function wfLastErrno() { return mysql_errno(); }
134 function wfLastError() { return mysql_error(); }
135 function wfAffectedRows() { return mysql_affected_rows( wfGetDB() ); }
136
137 function wfLastDBquery()
138 {
139 global $wgLastDatabaseQuery;
140 return $wgLastDatabaseQuery;
141 }
142
143 function wfSetSQL( $table, $var, $value, $cond )
144 {
145 $sql = "UPDATE $table SET $var = '" .
146 wfStrencode( $value ) . "' WHERE ($cond)";
147 wfQuery( $sql, DB_WRITE, "wfSetSQL" );
148 }
149
150 function wfGetSQL( $table, $var, $cond )
151 {
152 $sql = "SELECT $var FROM $table WHERE ($cond)";
153 $result = wfQuery( $sql, DB_READ, "wfGetSQL" );
154
155 $ret = "";
156 if ( mysql_num_rows( $result ) > 0 ) {
157 $s = mysql_fetch_object( $result );
158 $ret = $s->$var;
159 mysql_free_result( $result );
160 }
161 return $ret;
162 }
163
164 function wfStrencode( $s )
165 {
166 return addslashes( $s );
167 }
168
169 # Ideally we'd be using actual time fields in the db
170 function wfTimestamp2Unix( $ts ) {
171 return gmmktime( ( (int)substr( $ts, 8, 2) ),
172 (int)substr( $ts, 10, 2 ), (int)substr( $ts, 12, 2 ),
173 (int)substr( $ts, 4, 2 ), (int)substr( $ts, 6, 2 ),
174 (int)substr( $ts, 0, 4 ) );
175 }
176
177 function wfUnix2Timestamp( $unixtime ) {
178 return gmdate( "YmdHis", $unixtime );
179 }
180
181 function wfTimestampNow() {
182 # return NOW
183 return gmdate( "YmdHis" );
184 }
185
186 # Sorting hack for MySQL 3, which doesn't use index sorts for DESC
187 function wfInvertTimestamp( $ts ) {
188 return strtr(
189 $ts,
190 "0123456789",
191 "9876543210"
192 );
193 }
194
195 function wfFieldExists( $table, $field )
196 {
197 $fname = "wfFieldExists";
198 $res = wfQuery( "DESCRIBE $table", DB_READ, $fname );
199 $found = false;
200
201 while ( $row = wfFetchObject( $res ) ) {
202 if ( $row->Field == $field ) {
203 $found = true;
204 break;
205 }
206 }
207 return $found;
208 }
209
210 function wfIndexExists( $table, $index )
211 {
212 global $wgDBname;
213 $fname = "wfIndexExists";
214 $sql = "SHOW INDEXES FROM $table";
215 $res = wfQuery( $sql, DB_READ, $fname );
216 $found = false;
217 while ( $row = wfFetchObject( $res ) ) {
218 if ( $row->Key_name == $index ) {
219 $found = true;
220 break;
221 }
222 }
223 return $found;
224 }
225 ?>