PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/wp-db.php

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