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

/wp-includes/wp-db.php

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