Projects : mp-wp : mp-wp_genesis
1 | <?php |
2 | |
3 | |
4 | /** |
5 | * WordPress DB Class |
6 | * |
7 | * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)} |
8 | * |
9 | * @package WordPress |
10 | * @subpackage Database |
11 | * @since 0.71 |
12 | */ |
13 | |
14 | /** |
15 | * @since 0.71 |
16 | */ |
17 | define('EZSQL_VERSION', 'WP1.25'); |
18 | |
19 | /** |
20 | * @since 0.71 |
21 | */ |
22 | define('OBJECT', 'OBJECT', true); |
23 | |
24 | /** |
25 | * @since {@internal Version Unknown}} |
26 | */ |
27 | define('OBJECT_K', 'OBJECT_K', false); |
28 | |
29 | /** |
30 | * @since 0.71 |
31 | */ |
32 | define('ARRAY_A', 'ARRAY_A', false); |
33 | |
34 | /** |
35 | * @since 0.71 |
36 | */ |
37 | define('ARRAY_N', 'ARRAY_N', false); |
38 | |
39 | /** |
40 | * WordPress Database Access Abstraction Object |
41 | * |
42 | * It is possible to replace this class with your own |
43 | * by setting the $wpdb global variable in wp-content/db.php |
44 | * file with your class. You can name it wpdb also, since |
45 | * this file will not be included, if the other file is |
46 | * available. |
47 | * |
48 | * @link http://codex.wordpress.org/Function_Reference/wpdb_Class |
49 | * |
50 | * @package WordPress |
51 | * @subpackage Database |
52 | * @since 0.71 |
53 | * @final |
54 | */ |
55 | class wpdb { |
56 | |
57 | /** |
58 | * Whether to show SQL/DB errors |
59 | * |
60 | * @since 0.71 |
61 | * @access private |
62 | * @var bool |
63 | */ |
64 | var $show_errors = false; |
65 | |
66 | /** |
67 | * Whether to suppress errors during the DB bootstrapping. |
68 | * |
69 | * @access private |
70 | * @since {@internal Version Unknown}} |
71 | * @var bool |
72 | */ |
73 | var $suppress_errors = false; |
74 | |
75 | /** |
76 | * The last error during query. |
77 | * |
78 | * @since {@internal Version Unknown}} |
79 | * @var string |
80 | */ |
81 | var $last_error = ''; |
82 | |
83 | /** |
84 | * Amount of queries made |
85 | * |
86 | * @since 1.2.0 |
87 | * @access private |
88 | * @var int |
89 | */ |
90 | var $num_queries = 0; |
91 | |
92 | /** |
93 | * Saved result of the last query made |
94 | * |
95 | * @since 1.2.0 |
96 | * @access private |
97 | * @var array |
98 | */ |
99 | var $last_query; |
100 | |
101 | /** |
102 | * Saved info on the table column |
103 | * |
104 | * @since 1.2.0 |
105 | * @access private |
106 | * @var array |
107 | */ |
108 | var $col_info; |
109 | |
110 | /** |
111 | * Saved queries that were executed |
112 | * |
113 | * @since 1.5.0 |
114 | * @access private |
115 | * @var array |
116 | */ |
117 | var $queries; |
118 | |
119 | /** |
120 | * WordPress table prefix |
121 | * |
122 | * You can set this to have multiple WordPress installations |
123 | * in a single database. The second reason is for possible |
124 | * security precautions. |
125 | * |
126 | * @since 0.71 |
127 | * @access private |
128 | * @var string |
129 | */ |
130 | var $prefix = ''; |
131 | |
132 | /** |
133 | * Whether the database queries are ready to start executing. |
134 | * |
135 | * @since 2.5.0 |
136 | * @access private |
137 | * @var bool |
138 | */ |
139 | var $ready = false; |
140 | |
141 | /** |
142 | * WordPress Posts table |
143 | * |
144 | * @since 1.5.0 |
145 | * @access public |
146 | * @var string |
147 | */ |
148 | var $posts; |
149 | |
150 | /** |
151 | * WordPress Users table |
152 | * |
153 | * @since 1.5.0 |
154 | * @access public |
155 | * @var string |
156 | */ |
157 | var $users; |
158 | |
159 | /** |
160 | * WordPress Categories table |
161 | * |
162 | * @since 1.5.0 |
163 | * @access public |
164 | * @var string |
165 | */ |
166 | var $categories; |
167 | |
168 | /** |
169 | * WordPress Post to Category table |
170 | * |
171 | * @since 1.5.0 |
172 | * @access public |
173 | * @var string |
174 | */ |
175 | var $post2cat; |
176 | |
177 | /** |
178 | * WordPress Comments table |
179 | * |
180 | * @since 1.5.0 |
181 | * @access public |
182 | * @var string |
183 | */ |
184 | var $comments; |
185 | |
186 | /** |
187 | * WordPress Links table |
188 | * |
189 | * @since 1.5.0 |
190 | * @access public |
191 | * @var string |
192 | */ |
193 | var $links; |
194 | |
195 | /** |
196 | * WordPress Options table |
197 | * |
198 | * @since 1.5.0 |
199 | * @access public |
200 | * @var string |
201 | */ |
202 | var $options; |
203 | |
204 | /** |
205 | * WordPress Post Metadata table |
206 | * |
207 | * @since {@internal Version Unknown}} |
208 | * @access public |
209 | * @var string |
210 | */ |
211 | var $postmeta; |
212 | |
213 | /** |
214 | * WordPress User Metadata table |
215 | * |
216 | * @since 2.3.0 |
217 | * @access public |
218 | * @var string |
219 | */ |
220 | var $usermeta; |
221 | |
222 | /** |
223 | * WordPress Terms table |
224 | * |
225 | * @since 2.3.0 |
226 | * @access public |
227 | * @var string |
228 | */ |
229 | var $terms; |
230 | |
231 | /** |
232 | * WordPress Term Taxonomy table |
233 | * |
234 | * @since 2.3.0 |
235 | * @access public |
236 | * @var string |
237 | */ |
238 | var $term_taxonomy; |
239 | |
240 | /** |
241 | * WordPress Term Relationships table |
242 | * |
243 | * @since 2.3.0 |
244 | * @access public |
245 | * @var string |
246 | */ |
247 | var $term_relationships; |
248 | |
249 | /** |
250 | * List of WordPress tables |
251 | * |
252 | * @since {@internal Version Unknown}} |
253 | * @access private |
254 | * @var array |
255 | */ |
256 | var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options', |
257 | 'postmeta', 'terms', 'term_taxonomy', 'term_relationships'); |
258 | |
259 | /** |
260 | * Database table columns charset |
261 | * |
262 | * @since 2.2.0 |
263 | * @access public |
264 | * @var string |
265 | */ |
266 | var $charset; |
267 | |
268 | /** |
269 | * Database table columns collate |
270 | * |
271 | * @since 2.2.0 |
272 | * @access public |
273 | * @var string |
274 | */ |
275 | var $collate; |
276 | |
277 | /** |
278 | * Connects to the database server and selects a database |
279 | * |
280 | * PHP4 compatibility layer for calling the PHP5 constructor. |
281 | * |
282 | * @uses wpdb::__construct() Passes parameters and returns result |
283 | * @since 0.71 |
284 | * |
285 | * @param string $dbuser MySQL database user |
286 | * @param string $dbpassword MySQL database password |
287 | * @param string $dbname MySQL database name |
288 | * @param string $dbhost MySQL database host |
289 | */ |
290 | function wpdb($dbuser, $dbpassword, $dbname, $dbhost) { |
291 | return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost); |
292 | } |
293 | |
294 | /** |
295 | * Connects to the database server and selects a database |
296 | * |
297 | * PHP5 style constructor for compatibility with PHP5. Does |
298 | * the actual setting up of the class properties and connection |
299 | * to the database. |
300 | * |
301 | * @since 2.0.8 |
302 | * |
303 | * @param string $dbuser MySQL database user |
304 | * @param string $dbpassword MySQL database password |
305 | * @param string $dbname MySQL database name |
306 | * @param string $dbhost MySQL database host |
307 | */ |
308 | function __construct($dbuser, $dbpassword, $dbname, $dbhost) { |
309 | register_shutdown_function(array(&$this, "__destruct")); |
310 | |
311 | if ( defined('WP_DEBUG') and WP_DEBUG == true ) |
312 | $this->show_errors(); |
313 | |
314 | if ( defined('DB_CHARSET') ) |
315 | $this->charset = DB_CHARSET; |
316 | |
317 | if ( defined('DB_COLLATE') ) |
318 | $this->collate = DB_COLLATE; |
319 | |
320 | $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true); |
321 | if (!$this->dbh) { |
322 | $this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/" |
323 | <h1>Error establishing a database connection</h1> |
324 | <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>%s</code>. This could mean your host's database server is down.</p> |
325 | <ul> |
326 | <li>Are you sure you have the correct username and password?</li> |
327 | <li>Are you sure that you have typed the correct hostname?</li> |
328 | <li>Are you sure that the database server is running?</li> |
329 | </ul> |
330 | <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> |
331 | "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost)); |
332 | return; |
333 | } |
334 | |
335 | $this->ready = true; |
336 | |
337 | if ( $this->has_cap( 'collation' ) ) { |
338 | $collation_query = ''; |
339 | if ( !empty($this->charset) ) { |
340 | $collation_query = "SET NAMES '{$this->charset}'"; |
341 | if (!empty($this->collate) ) |
342 | $collation_query .= " COLLATE '{$this->collate}'"; |
343 | } |
344 | |
345 | if ( !empty($collation_query) ) |
346 | $this->query($collation_query); |
347 | |
348 | } |
349 | |
350 | $this->select($dbname); |
351 | } |
352 | |
353 | /** |
354 | * PHP5 style destructor and will run when database object is destroyed. |
355 | * |
356 | * @since 2.0.8 |
357 | * |
358 | * @return bool Always true |
359 | */ |
360 | function __destruct() { |
361 | return true; |
362 | } |
363 | |
364 | /** |
365 | * Sets the table prefix for the WordPress tables. |
366 | * |
367 | * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to |
368 | * override the WordPress users and usersmeta tables. |
369 | * |
370 | * @since 2.5.0 |
371 | * |
372 | * @param string $prefix Alphanumeric name for the new prefix. |
373 | * @return string Old prefix |
374 | */ |
375 | function set_prefix($prefix) { |
376 | |
377 | if ( preg_match('|[^a-z0-9_]|i', $prefix) ) |
378 | return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/); |
379 | |
380 | $old_prefix = $this->prefix; |
381 | $this->prefix = $prefix; |
382 | |
383 | foreach ( (array) $this->tables as $table ) |
384 | $this->$table = $this->prefix . $table; |
385 | |
386 | if ( defined('CUSTOM_USER_TABLE') ) |
387 | $this->users = CUSTOM_USER_TABLE; |
388 | |
389 | if ( defined('CUSTOM_USER_META_TABLE') ) |
390 | $this->usermeta = CUSTOM_USER_META_TABLE; |
391 | |
392 | return $old_prefix; |
393 | } |
394 | |
395 | /** |
396 | * Selects a database using the current database connection. |
397 | * |
398 | * The database name will be changed based on the current database |
399 | * connection. On failure, the execution will bail and display an DB error. |
400 | * |
401 | * @since 0.71 |
402 | * |
403 | * @param string $db MySQL database name |
404 | * @return null Always null. |
405 | */ |
406 | function select($db) { |
407 | if (!@mysql_select_db($db, $this->dbh)) { |
408 | $this->ready = false; |
409 | $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/' |
410 | <h1>Can’t select database</h1> |
411 | <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>%1$s</code> database.</p> |
412 | <ul> |
413 | <li>Are you sure it exists?</li> |
414 | <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> |
415 | <li>On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?</li> |
416 | </ul> |
417 | <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>'/*/WP_I18N_DB_SELECT_DB*/, $db, DB_USER)); |
418 | return; |
419 | } |
420 | } |
421 | |
422 | /** |
423 | * Escapes content for insertion into the database, for security |
424 | * |
425 | * @since 0.71 |
426 | * |
427 | * @param string $string |
428 | * @return string query safe string |
429 | */ |
430 | function escape($string) { |
431 | return addslashes( $string ); |
432 | // Disable rest for now, causing problems |
433 | /* |
434 | if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' ) |
435 | return mysql_escape_string( $string ); |
436 | else |
437 | return mysql_real_escape_string( $string, $this->dbh ); |
438 | */ |
439 | } |
440 | |
441 | /** |
442 | * Escapes content by reference for insertion into the database, for security |
443 | * |
444 | * @since 2.3.0 |
445 | * |
446 | * @param string $s |
447 | */ |
448 | function escape_by_ref(&$s) { |
449 | $s = $this->escape($s); |
450 | } |
451 | |
452 | /** |
453 | * Prepares a SQL query for safe use, using sprintf() syntax. |
454 | * |
455 | * @link http://php.net/sprintf See for syntax to use for query string. |
456 | * @since 2.3.0 |
457 | * |
458 | * @param null|string $args If string, first parameter must be query statement |
459 | * @param mixed $args,... If additional parameters, they will be set inserted into the query. |
460 | * @return null|string Sanitized query string |
461 | */ |
462 | function prepare($args=null) { |
463 | if ( is_null( $args ) ) |
464 | return; |
465 | $args = func_get_args(); |
466 | $query = array_shift($args); |
467 | $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it |
468 | $query = str_replace('"%s"', '%s', $query); // doublequote unquoting |
469 | $query = str_replace('%s', "'%s'", $query); // quote the strings |
470 | array_walk($args, array(&$this, 'escape_by_ref')); |
471 | return @vsprintf($query, $args); |
472 | } |
473 | |
474 | /** |
475 | * Print SQL/DB error. |
476 | * |
477 | * @since 0.71 |
478 | * @global array $EZSQL_ERROR Stores error information of query and error string |
479 | * |
480 | * @param string $str The error to display |
481 | * @return bool False if the showing of errors is disabled. |
482 | */ |
483 | function print_error($str = '') { |
484 | global $EZSQL_ERROR; |
485 | |
486 | if (!$str) $str = mysql_error($this->dbh); |
487 | $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str); |
488 | |
489 | if ( $this->suppress_errors ) |
490 | return false; |
491 | |
492 | if ( $caller = $this->get_caller() ) |
493 | $error_str = sprintf(/*WP_I18N_DB_QUERY_ERROR_FULL*/'WordPress database error %1$s for query %2$s made by %3$s'/*/WP_I18N_DB_QUERY_ERROR_FULL*/, $str, $this->last_query, $caller); |
494 | else |
495 | $error_str = sprintf(/*WP_I18N_DB_QUERY_ERROR*/'WordPress database error %1$s for query %2$s'/*/WP_I18N_DB_QUERY_ERROR*/, $str, $this->last_query); |
496 | |
497 | $log_error = true; |
498 | if ( ! function_exists('error_log') ) |
499 | $log_error = false; |
500 | |
501 | $log_file = @ini_get('error_log'); |
502 | if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) ) |
503 | $log_error = false; |
504 | |
505 | if ( $log_error ) |
506 | @error_log($error_str, 0); |
507 | |
508 | // Is error output turned on or not.. |
509 | if ( !$this->show_errors ) |
510 | return false; |
511 | |
512 | $str = htmlspecialchars($str, ENT_QUOTES); |
513 | $query = htmlspecialchars($this->last_query, ENT_QUOTES); |
514 | |
515 | // If there is an error then take note of it |
516 | print "<div id='error'> |
517 | <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> |
518 | <code>$query</code></p> |
519 | </div>"; |
520 | } |
521 | |
522 | /** |
523 | * Enables showing of database errors. |
524 | * |
525 | * This function should be used only to enable showing of errors. |
526 | * wpdb::hide_errors() should be used instead for hiding of errors. However, |
527 | * this function can be used to enable and disable showing of database |
528 | * errors. |
529 | * |
530 | * @since 0.71 |
531 | * |
532 | * @param bool $show Whether to show or hide errors |
533 | * @return bool Old value for showing errors. |
534 | */ |
535 | function show_errors( $show = true ) { |
536 | $errors = $this->show_errors; |
537 | $this->show_errors = $show; |
538 | return $errors; |
539 | } |
540 | |
541 | /** |
542 | * Disables showing of database errors. |
543 | * |
544 | * @since 0.71 |
545 | * |
546 | * @return bool Whether showing of errors was active or not |
547 | */ |
548 | function hide_errors() { |
549 | $show = $this->show_errors; |
550 | $this->show_errors = false; |
551 | return $show; |
552 | } |
553 | |
554 | /** |
555 | * Whether to suppress database errors. |
556 | * |
557 | * @param unknown_type $suppress |
558 | * @return unknown |
559 | */ |
560 | function suppress_errors( $suppress = true ) { |
561 | $errors = $this->suppress_errors; |
562 | $this->suppress_errors = $suppress; |
563 | return $errors; |
564 | } |
565 | |
566 | /** |
567 | * Kill cached query results. |
568 | * |
569 | * @since 0.71 |
570 | */ |
571 | function flush() { |
572 | $this->last_result = array(); |
573 | $this->col_info = null; |
574 | $this->last_query = null; |
575 | } |
576 | |
577 | /** |
578 | * Perform a MySQL database query, using current database connection. |
579 | * |
580 | * More information can be found on the codex page. |
581 | * |
582 | * @since 0.71 |
583 | * |
584 | * @param string $query |
585 | * @return unknown |
586 | */ |
587 | function query($query) { |
588 | if ( ! $this->ready ) |
589 | return false; |
590 | |
591 | // filter the query, if filters are available |
592 | // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method |
593 | if ( function_exists('apply_filters') ) |
594 | $query = apply_filters('query', $query); |
595 | |
596 | // initialise return |
597 | $return_val = 0; |
598 | $this->flush(); |
599 | |
600 | // Log how the function was called |
601 | $this->func_call = "\$db->query(\"$query\")"; |
602 | |
603 | // Keep track of the last query for debug.. |
604 | $this->last_query = $query; |
605 | |
606 | // Perform the query via std mysql_query function.. |
607 | if ( defined('SAVEQUERIES') && SAVEQUERIES ) |
608 | $this->timer_start(); |
609 | |
610 | $this->result = @mysql_query($query, $this->dbh); |
611 | ++$this->num_queries; |
612 | |
613 | if ( defined('SAVEQUERIES') && SAVEQUERIES ) |
614 | $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); |
615 | |
616 | // If there is an error then take note of it.. |
617 | if ( $this->last_error = mysql_error($this->dbh) ) { |
618 | $this->print_error(); |
619 | return false; |
620 | } |
621 | |
622 | if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) { |
623 | $this->rows_affected = mysql_affected_rows($this->dbh); |
624 | // Take note of the insert_id |
625 | if ( preg_match("/^\\s*(insert|replace) /i",$query) ) { |
626 | $this->insert_id = mysql_insert_id($this->dbh); |
627 | } |
628 | // Return number of rows affected |
629 | $return_val = $this->rows_affected; |
630 | } else { |
631 | $i = 0; |
632 | while ($i < @mysql_num_fields($this->result)) { |
633 | $this->col_info[$i] = @mysql_fetch_field($this->result); |
634 | $i++; |
635 | } |
636 | $num_rows = 0; |
637 | while ( $row = @mysql_fetch_object($this->result) ) { |
638 | $this->last_result[$num_rows] = $row; |
639 | $num_rows++; |
640 | } |
641 | |
642 | @mysql_free_result($this->result); |
643 | |
644 | // Log number of rows the query returned |
645 | $this->num_rows = $num_rows; |
646 | |
647 | // Return number of rows selected |
648 | $return_val = $this->num_rows; |
649 | } |
650 | |
651 | return $return_val; |
652 | } |
653 | |
654 | /** |
655 | * Insert an array of data into a table. |
656 | * |
657 | * @since 2.5.0 |
658 | * |
659 | * @param string $table WARNING: not sanitized! |
660 | * @param array $data Should not already be SQL-escaped |
661 | * @return mixed Results of $this->query() |
662 | */ |
663 | function insert($table, $data) { |
664 | $data = add_magic_quotes($data); |
665 | $fields = array_keys($data); |
666 | return $this->query("INSERT INTO $table (`" . implode('`,`',$fields) . "`) VALUES ('".implode("','",$data)."')"); |
667 | } |
668 | |
669 | /** |
670 | * Update a row in the table with an array of data. |
671 | * |
672 | * @since 2.5.0 |
673 | * |
674 | * @param string $table WARNING: not sanitized! |
675 | * @param array $data Should not already be SQL-escaped |
676 | * @param array $where A named array of WHERE column => value relationships. Multiple member pairs will be joined with ANDs. WARNING: the column names are not currently sanitized! |
677 | * @return mixed Results of $this->query() |
678 | */ |
679 | function update($table, $data, $where){ |
680 | $data = add_magic_quotes($data); |
681 | $bits = $wheres = array(); |
682 | foreach ( (array) array_keys($data) as $k ) |
683 | $bits[] = "`$k` = '$data[$k]'"; |
684 | |
685 | if ( is_array( $where ) ) |
686 | foreach ( $where as $c => $v ) |
687 | $wheres[] = "$c = '" . $this->escape( $v ) . "'"; |
688 | else |
689 | return false; |
690 | |
691 | return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) ); |
692 | } |
693 | |
694 | /** |
695 | * Retrieve one variable from the database. |
696 | * |
697 | * This combines the functionality of wpdb::get_row() and wpdb::get_col(), |
698 | * so both the column and row can be picked. |
699 | * |
700 | * It is possible to use this function without executing more queries. If |
701 | * you already made a query, you can set the $query to 'null' value and just |
702 | * retrieve either the column and row of the last query result. |
703 | * |
704 | * @since 0.71 |
705 | * |
706 | * @param string $query Can be null as well, for caching |
707 | * @param int $x Column num to return |
708 | * @param int $y Row num to return |
709 | * @return mixed Database query results |
710 | */ |
711 | function get_var($query=null, $x = 0, $y = 0) { |
712 | $this->func_call = "\$db->get_var(\"$query\",$x,$y)"; |
713 | if ( $query ) |
714 | $this->query($query); |
715 | |
716 | // Extract var out of cached results based x,y vals |
717 | if ( !empty( $this->last_result[$y] ) ) { |
718 | $values = array_values(get_object_vars($this->last_result[$y])); |
719 | } |
720 | |
721 | // If there is a value return it else return null |
722 | return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null; |
723 | } |
724 | |
725 | /** |
726 | * Retrieve one row from the database. |
727 | * |
728 | * @since 0.71 |
729 | * |
730 | * @param string $query SQL query |
731 | * @param string $output ARRAY_A | ARRAY_N | OBJECT |
732 | * @param int $y Row num to return |
733 | * @return mixed Database query results |
734 | */ |
735 | function get_row($query = null, $output = OBJECT, $y = 0) { |
736 | $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; |
737 | if ( $query ) |
738 | $this->query($query); |
739 | else |
740 | return null; |
741 | |
742 | if ( !isset($this->last_result[$y]) ) |
743 | return null; |
744 | |
745 | if ( $output == OBJECT ) { |
746 | return $this->last_result[$y] ? $this->last_result[$y] : null; |
747 | } elseif ( $output == ARRAY_A ) { |
748 | return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null; |
749 | } elseif ( $output == ARRAY_N ) { |
750 | return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null; |
751 | } else { |
752 | $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"/*/WP_I18N_DB_GETROW_ERROR*/); |
753 | } |
754 | } |
755 | |
756 | /** |
757 | * Retrieve one column from the database. |
758 | * |
759 | * @since 0.71 |
760 | * |
761 | * @param string $query Can be null as well, for caching |
762 | * @param int $x Col num to return. Starts from 0. |
763 | * @return array Column results |
764 | */ |
765 | function get_col($query = null , $x = 0) { |
766 | if ( $query ) |
767 | $this->query($query); |
768 | |
769 | $new_array = array(); |
770 | // Extract the column values |
771 | for ( $i=0; $i < count($this->last_result); $i++ ) { |
772 | $new_array[$i] = $this->get_var(null, $x, $i); |
773 | } |
774 | return $new_array; |
775 | } |
776 | |
777 | /** |
778 | * Retrieve an entire result set from the database. |
779 | * |
780 | * @since 0.71 |
781 | * |
782 | * @param string|null $query Can also be null to pull from the cache |
783 | * @param string $output ARRAY_A | ARRAY_N | OBJECT_K | OBJECT |
784 | * @return mixed Database query results |
785 | */ |
786 | function get_results($query = null, $output = OBJECT) { |
787 | $this->func_call = "\$db->get_results(\"$query\", $output)"; |
788 | |
789 | if ( $query ) |
790 | $this->query($query); |
791 | else |
792 | return null; |
793 | |
794 | if ( $output == OBJECT ) { |
795 | // Return an integer-keyed array of row objects |
796 | return $this->last_result; |
797 | } elseif ( $output == OBJECT_K ) { |
798 | // Return an array of row objects with keys from column 1 |
799 | // (Duplicates are discarded) |
800 | foreach ( $this->last_result as $row ) { |
801 | $key = array_shift( get_object_vars( $row ) ); |
802 | if ( !isset( $new_array[ $key ] ) ) |
803 | $new_array[ $key ] = $row; |
804 | } |
805 | return $new_array; |
806 | } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { |
807 | // Return an integer-keyed array of... |
808 | if ( $this->last_result ) { |
809 | $i = 0; |
810 | foreach( (array) $this->last_result as $row ) { |
811 | if ( $output == ARRAY_N ) { |
812 | // ...integer-keyed row arrays |
813 | $new_array[$i] = array_values( get_object_vars( $row ) ); |
814 | } else { |
815 | // ...column name-keyed row arrays |
816 | $new_array[$i] = get_object_vars( $row ); |
817 | } |
818 | ++$i; |
819 | } |
820 | return $new_array; |
821 | } |
822 | } |
823 | } |
824 | |
825 | /** |
826 | * Retrieve column metadata from the last query. |
827 | * |
828 | * @since 0.71 |
829 | * |
830 | * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill |
831 | * @param int $col_offset 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type |
832 | * @return mixed Column Results |
833 | */ |
834 | function get_col_info($info_type = 'name', $col_offset = -1) { |
835 | if ( $this->col_info ) { |
836 | if ( $col_offset == -1 ) { |
837 | $i = 0; |
838 | foreach( (array) $this->col_info as $col ) { |
839 | $new_array[$i] = $col->{$info_type}; |
840 | $i++; |
841 | } |
842 | return $new_array; |
843 | } else { |
844 | return $this->col_info[$col_offset]->{$info_type}; |
845 | } |
846 | } |
847 | } |
848 | |
849 | /** |
850 | * Starts the timer, for debugging purposes. |
851 | * |
852 | * @since 1.5.0 |
853 | * |
854 | * @return bool Always returns true |
855 | */ |
856 | function timer_start() { |
857 | $mtime = microtime(); |
858 | $mtime = explode(' ', $mtime); |
859 | $this->time_start = $mtime[1] + $mtime[0]; |
860 | return true; |
861 | } |
862 | |
863 | /** |
864 | * Stops the debugging timer. |
865 | * |
866 | * @since 1.5.0 |
867 | * |
868 | * @return int Total time spent on the query, in milliseconds |
869 | */ |
870 | function timer_stop() { |
871 | $mtime = microtime(); |
872 | $mtime = explode(' ', $mtime); |
873 | $time_end = $mtime[1] + $mtime[0]; |
874 | $time_total = $time_end - $this->time_start; |
875 | return $time_total; |
876 | } |
877 | |
878 | /** |
879 | * Wraps fatal errors in a nice header and footer and dies. |
880 | * |
881 | * @since 1.5.0 |
882 | * |
883 | * @param string $message |
884 | * @return unknown |
885 | */ |
886 | function bail($message) { |
887 | if ( !$this->show_errors ) { |
888 | if ( class_exists('WP_Error') ) |
889 | $this->error = new WP_Error('500', $message); |
890 | else |
891 | $this->error = $message; |
892 | return false; |
893 | } |
894 | wp_die($message); |
895 | } |
896 | |
897 | /** |
898 | * Whether or not MySQL database is minimal required version. |
899 | * |
900 | * @since 2.5.0 |
901 | * @uses $wp_version |
902 | * |
903 | * @return WP_Error |
904 | */ |
905 | function check_database_version() |
906 | { |
907 | global $wp_version; |
908 | // Make sure the server has MySQL 4.0 |
909 | if ( version_compare($this->db_version(), '4.0.0', '<') ) |
910 | return new WP_Error('database_version',sprintf(__('<strong>ERROR</strong>: WordPress %s requires MySQL 4.0.0 or higher'), $wp_version)); |
911 | } |
912 | |
913 | /** |
914 | * Whether of not the database version supports collation. |
915 | * |
916 | * Called when WordPress is generating the table scheme. |
917 | * |
918 | * @since 2.5.0 |
919 | * |
920 | * @return bool True if collation is supported, false if version does not |
921 | */ |
922 | function supports_collation() |
923 | { |
924 | return $this->has_cap( 'collation' ); |
925 | } |
926 | |
927 | /** |
928 | * Generic function to determine if a database supports a particular feature |
929 | * @param string $db_cap the feature |
930 | * @param false|string|resource $dbh_or_table the databaese (the current database, the database housing the specified table, or the database of the mysql resource) |
931 | * @return bool |
932 | */ |
933 | function has_cap( $db_cap ) { |
934 | $version = $this->db_version(); |
935 | |
936 | switch ( strtolower( $db_cap ) ) : |
937 | case 'collation' : // @since 2.5.0 |
938 | case 'group_concat' : // @since 2.7 |
939 | case 'subqueries' : // @since 2.7 |
940 | return version_compare($version, '4.1', '>='); |
941 | break; |
942 | endswitch; |
943 | |
944 | return false; |
945 | } |
946 | |
947 | /** |
948 | * Retrieve the name of the function that called wpdb. |
949 | * |
950 | * Requires PHP 4.3 and searches up the list of functions until it reaches |
951 | * the one that would most logically had called this method. |
952 | * |
953 | * @since 2.5.0 |
954 | * |
955 | * @return string The name of the calling function |
956 | */ |
957 | function get_caller() { |
958 | // requires PHP 4.3+ |
959 | if ( !is_callable('debug_backtrace') ) |
960 | return ''; |
961 | |
962 | $bt = debug_backtrace(); |
963 | $caller = array(); |
964 | |
965 | $bt = array_reverse( $bt ); |
966 | foreach ( (array) $bt as $call ) { |
967 | if ( @$call['class'] == __CLASS__ ) |
968 | continue; |
969 | $function = $call['function']; |
970 | if ( isset( $call['class'] ) ) |
971 | $function = $call['class'] . "->$function"; |
972 | $caller[] = $function; |
973 | } |
974 | $caller = join( ', ', $caller ); |
975 | |
976 | return $caller; |
977 | } |
978 | |
979 | /** |
980 | * The database version number |
981 | * @return false|string false on failure, version number on success |
982 | */ |
983 | function db_version() { |
984 | return preg_replace('/[^0-9.].*/', '', mysql_get_server_info( $this->dbh )); |
985 | } |
986 | } |
987 | |
988 | if ( ! isset($wpdb) ) { |
989 | /** |
990 | * WordPress Database Object, if it isn't set already in wp-content/db.php |
991 | * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database |
992 | * @since 0.71 |
993 | */ |
994 | $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); |
995 | } |
996 | ?> |