/Web/wp-includes/wp-db.php
PHP | 1605 lines | 572 code | 163 blank | 870 comment | 119 complexity | 8b9983438550184586b2923f34671e2f MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1
1<?php 2/** 3 * WordPress DB Class 4 * 5 * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)} 6 * 7 * @package WordPress 8 * @subpackage Database 9 * @since 0.71 10 */ 11 12/** 13 * @since 0.71 14 */ 15define( 'EZSQL_VERSION', 'WP1.25' ); 16 17/** 18 * @since 0.71 19 */ 20define( 'OBJECT', 'OBJECT', true ); 21 22/** 23 * @since 2.5.0 24 */ 25define( 'OBJECT_K', 'OBJECT_K' ); 26 27/** 28 * @since 0.71 29 */ 30define( 'ARRAY_A', 'ARRAY_A' ); 31 32/** 33 * @since 0.71 34 */ 35define( 'ARRAY_N', 'ARRAY_N' ); 36 37/** 38 * WordPress Database Access Abstraction Object 39 * 40 * It is possible to replace this class with your own 41 * by setting the $wpdb global variable in wp-content/db.php 42 * file to your class. The wpdb class will still be included, 43 * so you can extend it or simply use your own. 44 * 45 * @link http://codex.wordpress.org/Function_Reference/wpdb_Class 46 * 47 * @package WordPress 48 * @subpackage Database 49 * @since 0.71 50 */ 51class wpdb { 52 53 /** 54 * Whether to show SQL/DB errors 55 * 56 * @since 0.71 57 * @access private 58 * @var bool 59 */ 60 var $show_errors = false; 61 62 /** 63 * Whether to suppress errors during the DB bootstrapping. 64 * 65 * @access private 66 * @since 2.5.0 67 * @var bool 68 */ 69 var $suppress_errors = false; 70 71 /** 72 * The last error during query. 73 * 74 * @since 2.5.0 75 * @var string 76 */ 77 var $last_error = ''; 78 79 /** 80 * Amount of queries made 81 * 82 * @since 1.2.0 83 * @access private 84 * @var int 85 */ 86 var $num_queries = 0; 87 88 /** 89 * Count of rows returned by previous query 90 * 91 * @since 1.2.0 92 * @access private 93 * @var int 94 */ 95 var $num_rows = 0; 96 97 /** 98 * Count of affected rows by previous query 99 * 100 * @since 0.71 101 * @access private 102 * @var int 103 */ 104 var $rows_affected = 0; 105 106 /** 107 * The ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT). 108 * 109 * @since 0.71 110 * @access public 111 * @var int 112 */ 113 var $insert_id = 0; 114 115 /** 116 * Saved result of the last query made 117 * 118 * @since 1.2.0 119 * @access private 120 * @var array 121 */ 122 var $last_query; 123 124 /** 125 * Results of the last query made 126 * 127 * @since 1.0.0 128 * @access private 129 * @var array|null 130 */ 131 var $last_result; 132 133 /** 134 * Saved info on the table column 135 * 136 * @since 1.2.0 137 * @access private 138 * @var array 139 */ 140 var $col_info; 141 142 /** 143 * Saved queries that were executed 144 * 145 * @since 1.5.0 146 * @access private 147 * @var array 148 */ 149 var $queries; 150 151 /** 152 * WordPress table prefix 153 * 154 * You can set this to have multiple WordPress installations 155 * in a single database. The second reason is for possible 156 * security precautions. 157 * 158 * @since 0.71 159 * @access private 160 * @var string 161 */ 162 var $prefix = ''; 163 164 /** 165 * Whether the database queries are ready to start executing. 166 * 167 * @since 2.5.0 168 * @access private 169 * @var bool 170 */ 171 var $ready = false; 172 173 /** 174 * {@internal Missing Description}} 175 * 176 * @since 3.0.0 177 * @access public 178 * @var int 179 */ 180 var $blogid = 0; 181 182 /** 183 * {@internal Missing Description}} 184 * 185 * @since 3.0.0 186 * @access public 187 * @var int 188 */ 189 var $siteid = 0; 190 191 /** 192 * List of WordPress per-blog tables 193 * 194 * @since 2.5.0 195 * @access private 196 * @see wpdb::tables() 197 * @var array 198 */ 199 var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta', 200 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' ); 201 202 /** 203 * List of deprecated WordPress tables 204 * 205 * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539 206 * 207 * @since 2.9.0 208 * @access private 209 * @see wpdb::tables() 210 * @var array 211 */ 212 var $old_tables = array( 'categories', 'post2cat', 'link2cat' ); 213 214 /** 215 * List of WordPress global tables 216 * 217 * @since 3.0.0 218 * @access private 219 * @see wpdb::tables() 220 * @var array 221 */ 222 var $global_tables = array( 'users', 'usermeta' ); 223 224 /** 225 * List of Multisite global tables 226 * 227 * @since 3.0.0 228 * @access private 229 * @see wpdb::tables() 230 * @var array 231 */ 232 var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta', 233 'sitecategories', 'registration_log', 'blog_versions' ); 234 235 /** 236 * WordPress Comments table 237 * 238 * @since 1.5.0 239 * @access public 240 * @var string 241 */ 242 var $comments; 243 244 /** 245 * WordPress Comment Metadata table 246 * 247 * @since 2.9.0 248 * @access public 249 * @var string 250 */ 251 var $commentmeta; 252 253 /** 254 * WordPress Links table 255 * 256 * @since 1.5.0 257 * @access public 258 * @var string 259 */ 260 var $links; 261 262 /** 263 * WordPress Options table 264 * 265 * @since 1.5.0 266 * @access public 267 * @var string 268 */ 269 var $options; 270 271 /** 272 * WordPress Post Metadata table 273 * 274 * @since 1.5.0 275 * @access public 276 * @var string 277 */ 278 var $postmeta; 279 280 /** 281 * WordPress Posts table 282 * 283 * @since 1.5.0 284 * @access public 285 * @var string 286 */ 287 var $posts; 288 289 /** 290 * WordPress Terms table 291 * 292 * @since 2.3.0 293 * @access public 294 * @var string 295 */ 296 var $terms; 297 298 /** 299 * WordPress Term Relationships table 300 * 301 * @since 2.3.0 302 * @access public 303 * @var string 304 */ 305 var $term_relationships; 306 307 /** 308 * WordPress Term Taxonomy table 309 * 310 * @since 2.3.0 311 * @access public 312 * @var string 313 */ 314 var $term_taxonomy; 315 316 /* 317 * Global and Multisite tables 318 */ 319 320 /** 321 * WordPress User Metadata table 322 * 323 * @since 2.3.0 324 * @access public 325 * @var string 326 */ 327 var $usermeta; 328 329 /** 330 * WordPress Users table 331 * 332 * @since 1.5.0 333 * @access public 334 * @var string 335 */ 336 var $users; 337 338 /** 339 * Multisite Blogs table 340 * 341 * @since 3.0.0 342 * @access public 343 * @var string 344 */ 345 var $blogs; 346 347 /** 348 * Multisite Blog Versions table 349 * 350 * @since 3.0.0 351 * @access public 352 * @var string 353 */ 354 var $blog_versions; 355 356 /** 357 * Multisite Registration Log table 358 * 359 * @since 3.0.0 360 * @access public 361 * @var string 362 */ 363 var $registration_log; 364 365 /** 366 * Multisite Signups table 367 * 368 * @since 3.0.0 369 * @access public 370 * @var string 371 */ 372 var $signups; 373 374 /** 375 * Multisite Sites table 376 * 377 * @since 3.0.0 378 * @access public 379 * @var string 380 */ 381 var $site; 382 383 /** 384 * Multisite Sitewide Terms table 385 * 386 * @since 3.0.0 387 * @access public 388 * @var string 389 */ 390 var $sitecategories; 391 392 /** 393 * Multisite Site Metadata table 394 * 395 * @since 3.0.0 396 * @access public 397 * @var string 398 */ 399 var $sitemeta; 400 401 /** 402 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load. 403 * 404 * Keys are column names, values are format types: 'ID' => '%d' 405 * 406 * @since 2.8.0 407 * @see wpdb::prepare() 408 * @see wpdb::insert() 409 * @see wpdb::update() 410 * @see wpdb::delete() 411 * @see wp_set_wpdb_vars() 412 * @access public 413 * @var array 414 */ 415 var $field_types = array(); 416 417 /** 418 * Database table columns charset 419 * 420 * @since 2.2.0 421 * @access public 422 * @var string 423 */ 424 var $charset; 425 426 /** 427 * Database table columns collate 428 * 429 * @since 2.2.0 430 * @access public 431 * @var string 432 */ 433 var $collate; 434 435 /** 436 * Whether to use mysql_real_escape_string 437 * 438 * @since 2.8.0 439 * @access public 440 * @var bool 441 */ 442 var $real_escape = false; 443 444 /** 445 * Database Username 446 * 447 * @since 2.9.0 448 * @access private 449 * @var string 450 */ 451 var $dbuser; 452 453 /** 454 * A textual description of the last query/get_row/get_var call 455 * 456 * @since 3.0.0 457 * @access public 458 * @var string 459 */ 460 var $func_call; 461 462 /** 463 * Whether MySQL is used as the database engine. 464 * 465 * Set in WPDB::db_connect() to true, by default. This is used when checking 466 * against the required MySQL version for WordPress. Normally, a replacement 467 * database drop-in (db.php) will skip these checks, but setting this to true 468 * will force the checks to occur. 469 * 470 * @since 3.3.0 471 * @access public 472 * @var bool 473 */ 474 public $is_mysql = null; 475 476 /** 477 * Connects to the database server and selects a database 478 * 479 * PHP5 style constructor for compatibility with PHP5. Does 480 * the actual setting up of the class properties and connection 481 * to the database. 482 * 483 * @link http://core.trac.wordpress.org/ticket/3354 484 * @since 2.0.8 485 * 486 * @param string $dbuser MySQL database user 487 * @param string $dbpassword MySQL database password 488 * @param string $dbname MySQL database name 489 * @param string $dbhost MySQL database host 490 */ 491 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { 492 register_shutdown_function( array( &$this, '__destruct' ) ); 493 494 if ( WP_DEBUG ) 495 $this->show_errors(); 496 497 $this->init_charset(); 498 499 $this->dbuser = $dbuser; 500 $this->dbpassword = $dbpassword; 501 $this->dbname = $dbname; 502 $this->dbhost = $dbhost; 503 504 $this->db_connect(); 505 } 506 507 /** 508 * PHP5 style destructor and will run when database object is destroyed. 509 * 510 * @see wpdb::__construct() 511 * @since 2.0.8 512 * @return bool true 513 */ 514 function __destruct() { 515 return true; 516 } 517 518 /** 519 * Set $this->charset and $this->collate 520 * 521 * @since 3.1.0 522 */ 523 function init_charset() { 524 if ( function_exists('is_multisite') && is_multisite() ) { 525 $this->charset = 'utf8'; 526 if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) 527 $this->collate = DB_COLLATE; 528 else 529 $this->collate = 'utf8_general_ci'; 530 } elseif ( defined( 'DB_COLLATE' ) ) { 531 $this->collate = DB_COLLATE; 532 } 533 534 if ( defined( 'DB_CHARSET' ) ) 535 $this->charset = DB_CHARSET; 536 } 537 538 /** 539 * Sets the connection's character set. 540 * 541 * @since 3.1.0 542 * 543 * @param resource $dbh The resource given by mysql_connect 544 * @param string $charset The character set (optional) 545 * @param string $collate The collation (optional) 546 */ 547 function set_charset($dbh, $charset = null, $collate = null) { 548 if ( !isset($charset) ) 549 $charset = $this->charset; 550 if ( !isset($collate) ) 551 $collate = $this->collate; 552 if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) { 553 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) { 554 mysql_set_charset( $charset, $dbh ); 555 $this->real_escape = true; 556 } else { 557 $query = $this->prepare( 'SET NAMES %s', $charset ); 558 if ( ! empty( $collate ) ) 559 $query .= $this->prepare( ' COLLATE %s', $collate ); 560 mysql_query( $query, $dbh ); 561 } 562 } 563 } 564 565 /** 566 * Sets the table prefix for the WordPress tables. 567 * 568 * @since 2.5.0 569 * 570 * @param string $prefix Alphanumeric name for the new prefix. 571 * @param bool $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not. 572 * @return string|WP_Error Old prefix or WP_Error on error 573 */ 574 function set_prefix( $prefix, $set_table_names = true ) { 575 576 if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) 577 return new WP_Error('invalid_db_prefix', 'Invalid database prefix' ); 578 579 $old_prefix = is_multisite() ? '' : $prefix; 580 581 if ( isset( $this->base_prefix ) ) 582 $old_prefix = $this->base_prefix; 583 584 $this->base_prefix = $prefix; 585 586 if ( $set_table_names ) { 587 foreach ( $this->tables( 'global' ) as $table => $prefixed_table ) 588 $this->$table = $prefixed_table; 589 590 if ( is_multisite() && empty( $this->blogid ) ) 591 return $old_prefix; 592 593 $this->prefix = $this->get_blog_prefix(); 594 595 foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) 596 $this->$table = $prefixed_table; 597 598 foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) 599 $this->$table = $prefixed_table; 600 } 601 return $old_prefix; 602 } 603 604 /** 605 * Sets blog id. 606 * 607 * @since 3.0.0 608 * @access public 609 * @param int $blog_id 610 * @param int $site_id Optional. 611 * @return string previous blog id 612 */ 613 function set_blog_id( $blog_id, $site_id = 0 ) { 614 if ( ! empty( $site_id ) ) 615 $this->siteid = $site_id; 616 617 $old_blog_id = $this->blogid; 618 $this->blogid = $blog_id; 619 620 $this->prefix = $this->get_blog_prefix(); 621 622 foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) 623 $this->$table = $prefixed_table; 624 625 foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) 626 $this->$table = $prefixed_table; 627 628 return $old_blog_id; 629 } 630 631 /** 632 * Gets blog prefix. 633 * 634 * @uses is_multisite() 635 * @since 3.0.0 636 * @param int $blog_id Optional. 637 * @return string Blog prefix. 638 */ 639 function get_blog_prefix( $blog_id = null ) { 640 if ( is_multisite() ) { 641 if ( null === $blog_id ) 642 $blog_id = $this->blogid; 643 $blog_id = (int) $blog_id; 644 if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) ) 645 return $this->base_prefix; 646 else 647 return $this->base_prefix . $blog_id . '_'; 648 } else { 649 return $this->base_prefix; 650 } 651 } 652 653 /** 654 * Returns an array of WordPress tables. 655 * 656 * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to 657 * override the WordPress users and usermeta tables that would otherwise 658 * be determined by the prefix. 659 * 660 * The scope argument can take one of the following: 661 * 662 * 'all' - returns 'all' and 'global' tables. No old tables are returned. 663 * 'blog' - returns the blog-level tables for the queried blog. 664 * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite. 665 * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite. 666 * 'old' - returns tables which are deprecated. 667 * 668 * @since 3.0.0 669 * @uses wpdb::$tables 670 * @uses wpdb::$old_tables 671 * @uses wpdb::$global_tables 672 * @uses wpdb::$ms_global_tables 673 * @uses is_multisite() 674 * 675 * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all. 676 * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog 677 * prefix is requested, then the custom users and usermeta tables will be mapped. 678 * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested. 679 * @return array Table names. When a prefix is requested, the key is the unprefixed table name. 680 */ 681 function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) { 682 switch ( $scope ) { 683 case 'all' : 684 $tables = array_merge( $this->global_tables, $this->tables ); 685 if ( is_multisite() ) 686 $tables = array_merge( $tables, $this->ms_global_tables ); 687 break; 688 case 'blog' : 689 $tables = $this->tables; 690 break; 691 case 'global' : 692 $tables = $this->global_tables; 693 if ( is_multisite() ) 694 $tables = array_merge( $tables, $this->ms_global_tables ); 695 break; 696 case 'ms_global' : 697 $tables = $this->ms_global_tables; 698 break; 699 case 'old' : 700 $tables = $this->old_tables; 701 break; 702 default : 703 return array(); 704 break; 705 } 706 707 if ( $prefix ) { 708 if ( ! $blog_id ) 709 $blog_id = $this->blogid; 710 $blog_prefix = $this->get_blog_prefix( $blog_id ); 711 $base_prefix = $this->base_prefix; 712 $global_tables = array_merge( $this->global_tables, $this->ms_global_tables ); 713 foreach ( $tables as $k => $table ) { 714 if ( in_array( $table, $global_tables ) ) 715 $tables[ $table ] = $base_prefix . $table; 716 else 717 $tables[ $table ] = $blog_prefix . $table; 718 unset( $tables[ $k ] ); 719 } 720 721 if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) 722 $tables['users'] = CUSTOM_USER_TABLE; 723 724 if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) ) 725 $tables['usermeta'] = CUSTOM_USER_META_TABLE; 726 } 727 728 return $tables; 729 } 730 731 /** 732 * Selects a database using the current database connection. 733 * 734 * The database name will be changed based on the current database 735 * connection. On failure, the execution will bail and display an DB error. 736 * 737 * @since 0.71 738 * 739 * @param string $db MySQL database name 740 * @param resource $dbh Optional link identifier. 741 * @return null Always null. 742 */ 743 function select( $db, $dbh = null ) { 744 if ( is_null($dbh) ) 745 $dbh = $this->dbh; 746 747 if ( !@mysql_select_db( $db, $dbh ) ) { 748 $this->ready = false; 749 wp_load_translations_early(); 750 $this->bail( sprintf( __( '<h1>Can’t select database</h1> 751<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> 752<ul> 753<li>Are you sure it exists?</li> 754<li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> 755<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> 756</ul> 757<p>If you don\'t know how to set up 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>' ), htmlspecialchars( $db, ENT_QUOTES ), htmlspecialchars( $this->dbuser, ENT_QUOTES ) ), 'db_select_fail' ); 758 return; 759 } 760 } 761 762 /** 763 * Weak escape, using addslashes() 764 * 765 * @see addslashes() 766 * @since 2.8.0 767 * @access private 768 * 769 * @param string $string 770 * @return string 771 */ 772 function _weak_escape( $string ) { 773 return addslashes( $string ); 774 } 775 776 /** 777 * Real escape, using mysql_real_escape_string() or addslashes() 778 * 779 * @see mysql_real_escape_string() 780 * @see addslashes() 781 * @since 2.8.0 782 * @access private 783 * 784 * @param string $string to escape 785 * @return string escaped 786 */ 787 function _real_escape( $string ) { 788 if ( $this->dbh && $this->real_escape ) 789 return mysql_real_escape_string( $string, $this->dbh ); 790 else 791 return addslashes( $string ); 792 } 793 794 /** 795 * Escape data. Works on arrays. 796 * 797 * @uses wpdb::_escape() 798 * @uses wpdb::_real_escape() 799 * @since 2.8.0 800 * @access private 801 * 802 * @param string|array $data 803 * @return string|array escaped 804 */ 805 function _escape( $data ) { 806 if ( is_array( $data ) ) { 807 foreach ( (array) $data as $k => $v ) { 808 if ( is_array($v) ) 809 $data[$k] = $this->_escape( $v ); 810 else 811 $data[$k] = $this->_real_escape( $v ); 812 } 813 } else { 814 $data = $this->_real_escape( $data ); 815 } 816 817 return $data; 818 } 819 820 /** 821 * Escapes content for insertion into the database using addslashes(), for security. 822 * 823 * Works on arrays. 824 * 825 * @since 0.71 826 * @param string|array $data to escape 827 * @return string|array escaped as query safe string 828 */ 829 function escape( $data ) { 830 if ( is_array( $data ) ) { 831 foreach ( (array) $data as $k => $v ) { 832 if ( is_array( $v ) ) 833 $data[$k] = $this->escape( $v ); 834 else 835 $data[$k] = $this->_weak_escape( $v ); 836 } 837 } else { 838 $data = $this->_weak_escape( $data ); 839 } 840 841 return $data; 842 } 843 844 /** 845 * Escapes content by reference for insertion into the database, for security 846 * 847 * @uses wpdb::_real_escape() 848 * @since 2.3.0 849 * @param string $string to escape 850 * @return void 851 */ 852 function escape_by_ref( &$string ) { 853 $string = $this->_real_escape( $string ); 854 } 855 856 /** 857 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. 858 * 859 * The following directives can be used in the query format string: 860 * %d (integer) 861 * %f (float) 862 * %s (string) 863 * %% (literal percentage sign - no argument needed) 864 * 865 * All of %d, %f, and %s are to be left unquoted in the query string and they need an argument passed for them. 866 * Literals (%) as parts of the query must be properly written as %%. 867 * 868 * This function only supports a small subset of the sprintf syntax; it only supports %d (integer), %f (float), and %s (string). 869 * Does not support sign, padding, alignment, width or precision specifiers. 870 * Does not support argument numbering/swapping. 871 * 872 * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}. 873 * 874 * Both %d and %s should be left unquoted in the query string. 875 * 876 * <code> 877 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 ) 878 * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); 879 * </code> 880 * 881 * @link http://php.net/sprintf Description of syntax. 882 * @since 2.3.0 883 * 884 * @param string $query Query statement with sprintf()-like placeholders 885 * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like 886 * {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if 887 * being called like {@link http://php.net/sprintf sprintf()}. 888 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like 889 * {@link http://php.net/sprintf sprintf()}. 890 * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string 891 * if there was something to prepare 892 */ 893 function prepare( $query = null ) { // ( $query, *$args ) 894 if ( is_null( $query ) ) 895 return; 896 897 $args = func_get_args(); 898 array_shift( $args ); 899 // If args were passed as an array (as in vsprintf), move them up 900 if ( isset( $args[0] ) && is_array($args[0]) ) 901 $args = $args[0]; 902 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it 903 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting 904 $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s 905 array_walk( $args, array( &$this, 'escape_by_ref' ) ); 906 return @vsprintf( $query, $args ); 907 } 908 909 /** 910 * Print SQL/DB error. 911 * 912 * @since 0.71 913 * @global array $EZSQL_ERROR Stores error information of query and error string 914 * 915 * @param string $str The error to display 916 * @return bool False if the showing of errors is disabled. 917 */ 918 function print_error( $str = '' ) { 919 global $EZSQL_ERROR; 920 921 if ( !$str ) 922 $str = mysql_error( $this->dbh ); 923 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); 924 925 if ( $this->suppress_errors ) 926 return false; 927 928 wp_load_translations_early(); 929 930 if ( $caller = $this->get_caller() ) 931 $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller ); 932 else 933 $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query ); 934 935 if ( function_exists( 'error_log' ) 936 && ( $log_file = @ini_get( 'error_log' ) ) 937 && ( 'syslog' == $log_file || @is_writable( $log_file ) ) 938 ) 939 @error_log( $error_str ); 940 941 // Are we showing errors? 942 if ( ! $this->show_errors ) 943 return false; 944 945 // If there is an error then take note of it 946 if ( is_multisite() ) { 947 $msg = "WordPress database error: [$str]\n{$this->last_query}\n"; 948 if ( defined( 'ERRORLOGFILE' ) ) 949 error_log( $msg, 3, ERRORLOGFILE ); 950 if ( defined( 'DIEONDBERROR' ) ) 951 wp_die( $msg ); 952 } else { 953 $str = htmlspecialchars( $str, ENT_QUOTES ); 954 $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); 955 956 print "<div id='error'> 957 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> 958 <code>$query</code></p> 959 </div>"; 960 } 961 } 962 963 /** 964 * Enables showing of database errors. 965 * 966 * This function should be used only to enable showing of errors. 967 * wpdb::hide_errors() should be used instead for hiding of errors. However, 968 * this function can be used to enable and disable showing of database 969 * errors. 970 * 971 * @since 0.71 972 * @see wpdb::hide_errors() 973 * 974 * @param bool $show Whether to show or hide errors 975 * @return bool Old value for showing errors. 976 */ 977 function show_errors( $show = true ) { 978 $errors = $this->show_errors; 979 $this->show_errors = $show; 980 return $errors; 981 } 982 983 /** 984 * Disables showing of database errors. 985 * 986 * By default database errors are not shown. 987 * 988 * @since 0.71 989 * @see wpdb::show_errors() 990 * 991 * @return bool Whether showing of errors was active 992 */ 993 function hide_errors() { 994 $show = $this->show_errors; 995 $this->show_errors = false; 996 return $show; 997 } 998 999 /** 1000 * Whether to suppress database errors. 1001 * 1002 * By default database errors are suppressed, with a simple 1003 * call to this function they can be enabled. 1004 * 1005 * @since 2.5.0 1006 * @see wpdb::hide_errors() 1007 * @param bool $suppress Optional. New value. Defaults to true. 1008 * @return bool Old value 1009 */ 1010 function suppress_errors( $suppress = true ) { 1011 $errors = $this->suppress_errors; 1012 $this->suppress_errors = (bool) $suppress; 1013 return $errors; 1014 } 1015 1016 /** 1017 * Kill cached query results. 1018 * 1019 * @since 0.71 1020 * @return void 1021 */ 1022 function flush() { 1023 $this->last_result = array(); 1024 $this->col_info = null; 1025 $this->last_query = null; 1026 } 1027 1028 /** 1029 * Connect to and select database 1030 * 1031 * @since 3.0.0 1032 */ 1033 function db_connect() { 1034 1035 $this->is_mysql = true; 1036 1037 if ( WP_DEBUG ) { 1038 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); 1039 } else { 1040 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); 1041 } 1042 1043 if ( !$this->dbh ) { 1044 wp_load_translations_early(); 1045 $this->bail( sprintf( __( " 1046<h1>Error establishing a database connection</h1> 1047<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> 1048<ul> 1049 <li>Are you sure you have the correct username and password?</li> 1050 <li>Are you sure that you have typed the correct hostname?</li> 1051 <li>Are you sure that the database server is running?</li> 1052</ul> 1053<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> 1054" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); 1055 1056 return; 1057 } 1058 1059 $this->set_charset( $this->dbh ); 1060 1061 $this->ready = true; 1062 1063 $this->select( $this->dbname, $this->dbh ); 1064 } 1065 1066 /** 1067 * Perform a MySQL database query, using current database connection. 1068 * 1069 * More information can be found on the codex page. 1070 * 1071 * @since 0.71 1072 * 1073 * @param string $query Database query 1074 * @return int|false Number of rows affected/selected or false on error 1075 */ 1076 function query( $query ) { 1077 if ( ! $this->ready ) 1078 return false; 1079 1080 // some queries are made before the plugins have been loaded, and thus cannot be filtered with this method 1081 $query = apply_filters( 'query', $query ); 1082 1083 $return_val = 0; 1084 $this->flush(); 1085 1086 // Log how the function was called 1087 $this->func_call = "\$db->query(\"$query\")"; 1088 1089 // Keep track of the last query for debug.. 1090 $this->last_query = $query; 1091 1092 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1093 $this->timer_start(); 1094 1095 $this->result = @mysql_query( $query, $this->dbh ); 1096 $this->num_queries++; 1097 1098 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) 1099 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1100 1101 // If there is an error then take note of it.. 1102 if ( $this->last_error = mysql_error( $this->dbh ) ) { 1103 $this->print_error(); 1104 return false; 1105 } 1106 1107 if ( preg_match( '/^\s*(create|alter|truncate|drop) /i', $query ) ) { 1108 $return_val = $this->result; 1109 } elseif ( preg_match( '/^\s*(insert|delete|update|replace) /i', $query ) ) { 1110 $this->rows_affected = mysql_affected_rows( $this->dbh ); 1111 // Take note of the insert_id 1112 if ( preg_match( '/^\s*(insert|replace) /i', $query ) ) { 1113 $this->insert_id = mysql_insert_id($this->dbh); 1114 } 1115 // Return number of rows affected 1116 $return_val = $this->rows_affected; 1117 } else { 1118 $i = 0; 1119 while ( $i < @mysql_num_fields( $this->result ) ) { 1120 $this->col_info[$i] = @mysql_fetch_field( $this->result ); 1121 $i++; 1122 } 1123 $num_rows = 0; 1124 while ( $row = @mysql_fetch_object( $this->result ) ) { 1125 $this->last_result[$num_rows] = $row; 1126 $num_rows++; 1127 } 1128 1129 @mysql_free_result( $this->result ); 1130 1131 // Log number of rows the query returned 1132 // and return number of rows selected 1133 $this->num_rows = $num_rows; 1134 $return_val = $num_rows; 1135 } 1136 1137 return $return_val; 1138 } 1139 1140 /** 1141 * Insert a row into a table. 1142 * 1143 * <code> 1144 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) 1145 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) 1146 * </code> 1147 * 1148 * @since 2.5.0 1149 * @see wpdb::prepare() 1150 * @see wpdb::$field_types 1151 * @see wp_set_wpdb_vars() 1152 * 1153 * @param string $table table name 1154 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1155 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. 1156 * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. 1157 * @return int|false The number of rows inserted, or false on error. 1158 */ 1159 function insert( $table, $data, $format = null ) { 1160 return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' ); 1161 } 1162 1163 /** 1164 * Replace a row into a table. 1165 * 1166 * <code> 1167 * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) 1168 * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) 1169 * </code> 1170 * 1171 * @since 3.0.0 1172 * @see wpdb::prepare() 1173 * @see wpdb::$field_types 1174 * @see wp_set_wpdb_vars() 1175 * 1176 * @param string $table table name 1177 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1178 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. 1179 * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. 1180 * @return int|false The number of rows affected, or false on error. 1181 */ 1182 function replace( $table, $data, $format = null ) { 1183 return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' ); 1184 } 1185 1186 /** 1187 * Helper function for insert and replace. 1188 * 1189 * Runs an insert or replace query based on $type argument. 1190 * 1191 * @access private 1192 * @since 3.0.0 1193 * @see wpdb::prepare() 1194 * @see wpdb::$field_types 1195 * @see wp_set_wpdb_vars() 1196 * 1197 * @param string $table table name 1198 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1199 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. 1200 * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. 1201 * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT. 1202 * @return int|false The number of rows affected, or false on error. 1203 */ 1204 function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { 1205 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) 1206 return false; 1207 $formats = $format = (array) $format; 1208 $fields = array_keys( $data ); 1209 $formatted_fields = array(); 1210 foreach ( $fields as $field ) { 1211 if ( !empty( $format ) ) 1212 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1213 elseif ( isset( $this->field_types[$field] ) ) 1214 $form = $this->field_types[$field]; 1215 else 1216 $form = '%s'; 1217 $formatted_fields[] = $form; 1218 } 1219 $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")"; 1220 return $this->query( $this->prepare( $sql, $data ) ); 1221 } 1222 1223 /** 1224 * Update a row in the table 1225 * 1226 * <code> 1227 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) ) 1228 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) 1229 * </code> 1230 * 1231 * @since 2.5.0 1232 * @see wpdb::prepare() 1233 * @see wpdb::$field_types 1234 * @see wp_set_wpdb_vars() 1235 * 1236 * @param string $table table name 1237 * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). 1238 * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". 1239 * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data. 1240 * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. 1241 * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings. 1242 * @return int|false The number of rows updated, or false on error. 1243 */ 1244 function update( $table, $data, $where, $format = null, $where_format = null ) { 1245 if ( ! is_array( $data ) || ! is_array( $where ) ) 1246 return false; 1247 1248 $formats = $format = (array) $format; 1249 $bits = $wheres = array(); 1250 foreach ( (array) array_keys( $data ) as $field ) { 1251 if ( !empty( $format ) ) 1252 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; 1253 elseif ( isset($this->field_types[$field]) ) 1254 $form = $this->field_types[$field]; 1255 else 1256 $form = '%s'; 1257 $bits[] = "`$field` = {$form}"; 1258 } 1259 1260 $where_formats = $where_format = (array) $where_format; 1261 foreach ( (array) array_keys( $where ) as $field ) { 1262 if ( !empty( $where_format ) ) 1263 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1264 elseif ( isset( $this->field_types[$field] ) ) 1265 $form = $this->field_types[$field]; 1266 else 1267 $form = '%s'; 1268 $wheres[] = "`$field` = {$form}"; 1269 } 1270 1271 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); 1272 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 1273 } 1274 1275 /** 1276 * Delete a row in the table 1277 * 1278 * <code> 1279 * wpdb::delete( 'table', array( 'ID' => 1 ) ) 1280 * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) ) 1281 * </code> 1282 * 1283 * @since 3.4.0 1284 * @see wpdb::prepare() 1285 * @see wpdb::$field_types 1286 * @see wp_set_wpdb_vars() 1287 * 1288 * @param string $table table name 1289 * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". 1290 * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types. 1291 * @return int|false The number of rows updated, or false on error. 1292 */ 1293 function delete( $table, $where, $where_format = null ) { 1294 if ( ! is_array( $where ) ) 1295 return false; 1296 1297 $bits = $wheres = array(); 1298 1299 $where_formats = $where_format = (array) $where_format; 1300 1301 foreach ( array_keys( $where ) as $field ) { 1302 if ( !empty( $where_format ) ) { 1303 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 1304 } elseif ( isset( $this->field_types[ $field ] ) ) { 1305 $form = $this->field_types[ $field ]; 1306 } else { 1307 $form = '%s'; 1308 } 1309 1310 $wheres[] = "$field = $form"; 1311 } 1312 1313 $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres ); 1314 return $this->query( $this->prepare( $sql, $where ) ); 1315 } 1316 1317 1318 /** 1319 * Retrieve one variable from the database. 1320 * 1321 * Executes a SQL query and returns the value from the SQL result. 1322 * If the SQL result contains more than one column and/or more than one row, this function returns the value in the column and row specified. 1323 * If $query is null, this function returns the value in the specified column and row from the previous SQL result. 1324 * 1325 * @since 0.71 1326 * 1327 * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query. 1328 * @param int $x Optional. Column of value to return. Indexed from 0. 1329 * @param int $y Optional. Row of value to return. Indexed from 0. 1330 * @return string|null Database query result (as string), or null on failure 1331 */ 1332 function get_var( $query = null, $x = 0, $y = 0 ) { 1333 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; 1334 if ( $query ) 1335 $this->query( $query ); 1336 1337 // Extract var out of cached results based x,y vals 1338 if ( !empty( $this->last_result[$y] ) ) { 1339 $values = array_values( get_object_vars( $this->last_result[$y] ) ); 1340 } 1341 1342 // If there is a value return it else return null 1343 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; 1344 } 1345 1346 /** 1347 * Retrieve one row from the database. 1348 * 1349 * Executes a SQL query and returns the row from the SQL result. 1350 * 1351 * @since 0.71 1352 * 1353 * @param string|null $query SQL query. 1354 * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...), 1355 * a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively. 1356 * @param int $y Optional. Row to return. Indexed from 0. 1357 * @return mixed Database query result in format specified by $output or null on failure 1358 */ 1359 function get_row( $query = null, $output = OBJECT, $y = 0 ) { 1360 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 1361 if ( $query ) 1362 $this->query( $query ); 1363 else 1364 return null; 1365 1366 if ( !isset( $this->last_result[$y] ) ) 1367 return null; 1368 1369 if ( $output == OBJECT ) { 1370 return $this->last_result[$y] ? $this->last_result[$y] : null; 1371 } elseif ( $output == ARRAY_A ) { 1372 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; 1373 } elseif ( $output == ARRAY_N ) { 1374 return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; 1375 } else { 1376 $this->print_error( " \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N" ); 1377 } 1378 } 1379 1380 /** 1381 * Retrieve one column from the database. 1382 * 1383 * Executes a SQL query and returns the column from the SQL result. 1384 * If the SQL result contains more than one column, this function returns the column specified. 1385 * If $query is null, this function returns the specified column from the previous SQL result. 1386 * 1387 * @since 0.71 1388 * 1389 * @param string|null $query Optional. SQL query. Defaults to previous query. 1390 * @param int $x Optional. Column to return. Indexed from 0. 1391 * @return array Database query result. Array indexed from 0 by SQL result row number. 1392 */ 1393 function get_col( $query = null , $x = 0 ) { 1394 if ( $query ) 1395 $this->query( $query ); 1396 1397 $new_array = array(); 1398 // Extract the column values 1399 for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) { 1400 $new_array[$i] = $this->get_var( null, $x, $i ); 1401 } 1402 return $new_array; 1403 } 1404 1405 /** 1406 * Retrieve an entire SQL result set from the database (i.e., many rows) 1407 * 1408 * Executes a SQL query and returns the entire SQL result. 1409 * 1410 * @since 0.71 1411 * 1412 * @param string $query SQL query. 1413 * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. With one of the first three, return an array of rows indexed from 0 by SQL result row number. 1414 * Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively. 1415 * With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. Duplicate keys are discarded. 1416 * @return mixed Database query results 1417 */ 1418 function get_results( $query = null, $output = OBJECT ) { 1419 $this->func_call = "\$db->get_results(\"$query\", $output)"; 1420 1421 if ( $query ) 1422 $this->query( $query ); 1423 else 1424 return null; 1425 1426 $new_array = array(); 1427 if ( $output == OBJECT ) { 1428 // Return an integer-keyed array of row objects 1429 return $this->last_result; 1430 } elseif ( $output == OBJECT_K ) { 1431 // Return an array of row objects with keys from column 1 1432 // (Duplicates are discarded) 1433 foreach ( $this->last_result as $row ) { 1434 $var_by_ref = get_object_vars( $row ); 1435 $key = array_shift( $var_by_ref ); 1436 if ( ! isset( $new_array[ $key ] ) ) 1437 $new_array[ $key ] = $row; 1438 } 1439 return $new_array; 1440 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { 1441 // Return an integer-keyed array of... 1442 if ( $this->last_result ) { 1443 foreach( (array) $this->last_result as $row ) { 1444 if ( $output == ARRAY_N ) { 1445 // ...integer-keyed row arrays 1446 $new_array[] = array_values( get_object_vars( $row ) ); 1447 } else { 1448 // ...column name-keyed row arrays 1449 $new_array[] = get_object_vars( $row ); 1450 } 1451 } 1452 } 1453 return $new_array; 1454 } 1455 return null; 1456 } 1457 1458 /** 1459 * Retrieve column metadata from the last query. 1460 * 1461 * @since 0.71 1462 * 1463 * @param string $info_type Optional. Type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill 1464 * @param int $col_offset Optional. 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 1465 * @return mixed Column Results 1466 */ 1467 function get_col_info( $info_type = 'name', $col_offset = -1 ) { 1468 if ( $this->col_info ) { 1469 if ( $col_offset == -1 ) { 1470 $i = 0; 1471 $new_array = array(); 1472 foreach( (array) $this->col_info as $col ) { 1473 $new_array[$i] = $col->{$info_type}; 1474 $i++; 1475 } 1476 return $new_array; 1477 } else { 1478 return $this->col_info[$col_offset]->{$info_type}; 1479 } 1480 } 1481 } 1482 1483 /** 1484 * Starts the timer, for debugging purposes. 1485 * 1486 * @since 1.5.0 1487 * 1488 * @return true 1489 */ 1490 function timer_start() { 1491 $this->time_start = microtime( true ); 1492 return true; 1493 } 1494 1495 /** 1496 * Stops the debugging timer. 1497 * 1498 * @since 1.5.0 1499 * 1500 * @return float Total time spent on the query, in seconds 1501 */ 1502 function timer_stop() { 1503 return ( microtime( true ) - $this->time_start ); 1504 } 1505 1506 /** 1507 * Wraps errors in a nice header and footer and dies. 1508 * 1509 * Will not die if wpdb::$show_errors is true 1510 * 1511 * @since 1.5.0 1512 * 1513 * @param string $message The Error message 1514 * @param string $error_code Optional. A Computer readable string to identify the error. 1515 * @return false|void 1516 */ 1517 function bail( $message, $error_code = '500' ) { 1518 if ( !$this->show_errors ) { 1519 if ( class_exists( 'WP_Error' ) ) 1520 $this->error = new WP_Error($error_code, $message); 1521 else 1522 $this->error = $message; 1523 return false; 1524 } 1525 wp_die($message); 1526 } 1527 1528 /** 1529 * Whether MySQL database is at least the required minimum version. 1530 * 1531 * @since 2.5.0 1532 * @uses $wp_version 1533 * @uses $required_mysql_version 1534 * 1535 * @return WP_Error 1536 */ 1537 function check_database_version() { 1538 global $wp_version, $required_mysql_version; 1539 // Make sure the server has the required MySQL version 1540 if ( version_compare($this->db_version(), $required_mysql_version, '<') ) 1541 return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version )); 1542 } 1543 1544 /** 1545 * Whether the database supports collation. 1546 * 1547 * Called when WordPress is generating the table scheme. 1548 * 1549 * @since 2.5.0 1550 * 1551 * @return bool True if collation is supported, false if version does not 1552 */ 1553 function supports_collation() { 1554 return $this->has_cap( 'collation' ); 1555 } 1556 1557 /** 1558 * Determine if a database supports a particular feature 1559 * 1560 * @since 2.7.0 1561 * @see wpdb::db_version() 1562 * 1563 * @param string $db_cap the feature 1564 * @return bool 1565 */ 1566 function has_cap( $db_cap ) { 1567 $version = $this->db_version(); 1568 1569 switch ( strtolower( $db_cap ) ) { 1570 case 'collation' : // @since 2.5.0 1571 case 'group_concat' : // @since 2.7 1572 case 'subqueries' : // @since 2.7 1573 return version_compare( $version, '4.1', '>=' ); 1574 case 'set_charset' : 1575 return version_compare($version, '5.0.7', '>='); 1576 }; 1577 1578 return false; 1579 } 1580 1581 /** 1582 * Retrieve the name of the function that called wpdb. 1583 * 1584 * Searches up the list of functions until it reaches 1585 * the one that would most logically had called this method. 1586 * 1587 * @since 2.5.0 1588 * 1589 * @return string The name of the calling function 1590 */ 1591 function get_caller() { 1592 return wp_debug_backtrace_summary( __CLASS__ ); 1593 } 1594 1595 /** 1596 * The database version number. 1597 * 1598 * @since 2.7.0 1599 * 1600 * @return false|string false on failure, version number on success 1601 */ 1602 function db_version() { 1603 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); 1604 } 1605}