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