]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/wp-db.php
Wordpress 2.0.4
[autoinstalls/wordpress.git] / wp-includes / wp-db.php
1 <?php
2 //  WordPress DB Class
3
4 //  ORIGINAL CODE FROM:
5 //  Justin Vincent (justin@visunet.ie)
6 //      http://php.justinvincent.com
7
8 define('EZSQL_VERSION', 'WP1.25');
9 define('OBJECT', 'OBJECT', true);
10 define('ARRAY_A', 'ARRAY_A', false);
11 define('ARRAY_N', 'ARRAY_N', false);
12
13 if (!defined('SAVEQUERIES'))
14         define('SAVEQUERIES', false);
15
16 class wpdb {
17
18         var $show_errors = true;
19         var $num_queries = 0;   
20         var $last_query;
21         var $col_info;
22         var $queries;
23
24         // Our tables
25         var $posts;
26         var $users;
27         var $categories;
28         var $post2cat;
29         var $comments;
30         var $links;
31         var $linkcategories;
32         var $options;
33         var $optiontypes;
34         var $optionvalues;
35         var $optiongroups;
36         var $optiongroup_options;
37         var $postmeta;
38
39         // ==================================================================
40         //      DB Constructor - connects to the server and selects a database
41
42         function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
43                 $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword);
44                 if (!$this->dbh) {
45                         $this->bail("
46 <h1>Error establishing a database connection</h1>
47 <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
48 <ul>
49         <li>Are you sure you have the correct username and password?</li>
50         <li>Are you sure that you have typed the correct hostname?</li>
51         <li>Are you sure that the database server is running?</li>
52 </ul>
53 <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
54 ");
55                 }
56
57                 $this->select($dbname);
58         }
59
60         // ==================================================================
61         //      Select a DB (if another one needs to be selected)
62
63         function select($db) {
64                 if (!@mysql_select_db($db, $this->dbh)) {
65                         $this->bail("
66 <h1>Can&#8217;t select database</h1>
67 <p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>$db</code> database.</p>
68 <ul>
69 <li>Are you sure it exists?</li>
70 <li>On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?</li>
71 </ul>
72 <p>If you don't know how to setup a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>");
73                 }
74         }
75
76         // ====================================================================
77         //      Format a string correctly for safe insert under all PHP conditions
78         
79         function escape($string) {
80                 return addslashes( $string ); // Disable rest for now, causing problems
81                 if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )
82                         return mysql_escape_string( $string );
83                 else
84                         return mysql_real_escape_string( $string, $this->dbh );
85         }
86
87         // ==================================================================
88         //      Print SQL/DB error.
89
90         function print_error($str = '') {
91                 global $EZSQL_ERROR;
92                 if (!$str) $str = mysql_error();
93                 $EZSQL_ERROR[] = 
94                 array ('query' => $this->last_query, 'error_str' => $str);
95
96                 $str = htmlspecialchars($str, ENT_QUOTES);
97                 $query = htmlspecialchars($this->last_query, ENT_QUOTES);
98                 // Is error output turned on or not..
99                 if ( $this->show_errors ) {
100                         // If there is an error then take note of it
101                         print "<div id='error'>
102                         <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
103                         <code>$query</code></p>
104                         </div>";
105                 } else {
106                         return false;   
107                 }
108         }
109
110         // ==================================================================
111         //      Turn error handling on or off..
112
113         function show_errors() {
114                 $this->show_errors = true;
115         }
116         
117         function hide_errors() {
118                 $this->show_errors = false;
119         }
120
121         // ==================================================================
122         //      Kill cached query results
123
124         function flush() {
125                 $this->last_result = null;
126                 $this->col_info = null;
127                 $this->last_query = null;
128         }
129
130         // ==================================================================
131         //      Basic Query     - see docs for more detail
132
133         function query($query) {
134                 // initialise return
135                 $return_val = 0;
136                 $this->flush();
137
138                 // Log how the function was called
139                 $this->func_call = "\$db->query(\"$query\")";
140
141                 // Keep track of the last query for debug..
142                 $this->last_query = $query;
143
144                 // Perform the query via std mysql_query function..
145                 if (SAVEQUERIES)
146                         $this->timer_start();
147                 
148                 $this->result = @mysql_query($query, $this->dbh);
149                 ++$this->num_queries;
150
151                 if (SAVEQUERIES)
152                         $this->queries[] = array( $query, $this->timer_stop() );
153
154                 // If there is an error then take note of it..
155                 if ( mysql_error() ) {
156                         $this->print_error();
157                         return false;
158                 }
159
160                 if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
161                         $this->rows_affected = mysql_affected_rows();
162                         // Take note of the insert_id
163                         if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
164                                 $this->insert_id = mysql_insert_id($this->dbh); 
165                         }
166                         // Return number of rows affected
167                         $return_val = $this->rows_affected;
168                 } else {
169                         $i = 0;
170                         while ($i < @mysql_num_fields($this->result)) {
171                                 $this->col_info[$i] = @mysql_fetch_field($this->result);
172                                 $i++;
173                         }
174                         $num_rows = 0;
175                         while ( $row = @mysql_fetch_object($this->result) ) {
176                                 $this->last_result[$num_rows] = $row;
177                                 $num_rows++;
178                         }
179
180                         @mysql_free_result($this->result);
181
182                         // Log number of rows the query returned
183                         $this->num_rows = $num_rows;
184                         
185                         // Return number of rows selected
186                         $return_val = $this->num_rows;
187                 }
188
189                 return $return_val;
190         }
191
192         // ==================================================================
193         //      Get one variable from the DB - see docs for more detail
194
195         function get_var($query=null, $x = 0, $y = 0) {
196                 $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
197                 if ( $query )
198                         $this->query($query);
199
200                 // Extract var out of cached results based x,y vals
201                 if ( $this->last_result[$y] ) {
202                         $values = array_values(get_object_vars($this->last_result[$y]));
203                 }
204
205                 // If there is a value return it else return null
206                 return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
207         }
208
209         // ==================================================================
210         //      Get one row from the DB - see docs for more detail
211
212         function get_row($query = null, $output = OBJECT, $y = 0) {
213                 $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
214                 if ( $query )
215                         $this->query($query);
216
217                 if ( $output == OBJECT ) {
218                         return $this->last_result[$y] ? $this->last_result[$y] : null;
219                 } elseif ( $output == ARRAY_A ) {
220                         return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
221                 } elseif ( $output == ARRAY_N ) {
222                         return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
223                 } else {
224                         $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
225                 }
226         }
227
228         // ==================================================================
229         //      Function to get 1 column from the cached result set based in X index
230         // se docs for usage and info
231
232         function get_col($query = null , $x = 0) {
233                 if ( $query )
234                         $this->query($query);
235
236                 // Extract the column values
237                 for ( $i=0; $i < count($this->last_result); $i++ ) {
238                         $new_array[$i] = $this->get_var(null, $x, $i);
239                 }
240                 return $new_array;
241         }
242
243         // ==================================================================
244         // Return the the query as a result set - see docs for more details
245
246         function get_results($query = null, $output = OBJECT) {
247                 $this->func_call = "\$db->get_results(\"$query\", $output)";
248
249                 if ( $query )
250                         $this->query($query);
251
252                 // Send back array of objects. Each row is an object
253                 if ( $output == OBJECT ) {
254                         return $this->last_result;
255                 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
256                         if ( $this->last_result ) {
257                                 $i = 0;
258                                 foreach( $this->last_result as $row ) {
259                                         $new_array[$i] = (array) $row;
260                                         if ( $output == ARRAY_N ) {
261                                                 $new_array[$i] = array_values($new_array[$i]);
262                                         }
263                                         $i++;
264                                 }
265                                 return $new_array;
266                         } else {
267                                 return null;
268                         }
269                 }
270         }
271
272
273         // ==================================================================
274         // Function to get column meta data info pertaining to the last query
275         // see docs for more info and usage
276
277         function get_col_info($info_type = 'name', $col_offset = -1) {
278                 if ( $this->col_info ) {
279                         if ( $col_offset == -1 ) {
280                                 $i = 0;
281                                 foreach($this->col_info as $col ) {
282                                         $new_array[$i] = $col->{$info_type};
283                                         $i++;
284                                 }
285                                 return $new_array;
286                         } else {
287                                 return $this->col_info[$col_offset]->{$info_type};
288                         }
289                 }
290         }
291
292         function timer_start() {
293                 $mtime = microtime();
294                 $mtime = explode(' ', $mtime);
295                 $this->time_start = $mtime[1] + $mtime[0];
296                 return true;
297         }
298         
299         function timer_stop($precision = 3) {
300                 $mtime = microtime();
301                 $mtime = explode(' ', $mtime);
302                 $time_end = $mtime[1] + $mtime[0];
303                 $time_total = $time_end - $this->time_start;
304                 return $time_total;
305         }
306
307         function bail($message) { // Just wraps errors in a nice header and footer
308         if ( !$this->show_errors )
309                 return false;
310         header( 'Content-Type: text/html; charset=utf-8');              
311         echo <<<HEAD
312         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
313         <html xmlns="http://www.w3.org/1999/xhtml">
314         <head>
315                 <title>WordPress &rsaquo; Error</title>
316                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
317                 <style media="screen" type="text/css">
318                 <!--
319                 html {
320                         background: #eee;
321                 }
322                 body {
323                         background: #fff;
324                         color: #000;
325                         font-family: Georgia, "Times New Roman", Times, serif;
326                         margin-left: 25%;
327                         margin-right: 25%;
328                         padding: .2em 2em;
329                 }
330                 
331                 h1 {
332                         color: #006;
333                         font-size: 18px;
334                         font-weight: lighter;
335                 }
336                 
337                 h2 {
338                         font-size: 16px;
339                 }
340                 
341                 p, li, dt {
342                         line-height: 140%;
343                         padding-bottom: 2px;
344                 }
345         
346                 ul, ol {
347                         padding: 5px 5px 5px 20px;
348                 }
349                 #logo {
350                         margin-bottom: 2em;
351                 }
352                 -->
353                 </style>
354         </head>
355         <body>
356         <h1 id="logo"><img alt="WordPress" src="http://static.wordpress.org/logo.png" /></h1>
357 HEAD;
358         echo $message;
359         echo "</body></html>";
360         die();
361         }
362 }
363
364 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
365 ?>