PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wordpress/wp-includes/wp-db.php

http://ichitaso.googlecode.com/
PHP | 1571 lines | 562 code | 157 blank | 852 comment | 120 complexity | d9960cb1ba51c861b0d91f8ee0512721 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  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*/'????????????'/*/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>??????????????</h1>
  669. <p>?????????????????? (???????????????????????) ???????? <code>%1$s</code> ????????????</p>
  670. <ul>
  671. <li>???????????????????? ?</li>
  672. <li>???? <code>%2$s</code> ???????? <code>%1$s</code> ????????????? ?</li>
  673. <li>????????? <code>username_%1$s</code> ????????????????????????????????????????????????? ?</li>
  674. </ul>
  675. <p>??????????????????????<strong>?????????????</strong>?????????????????????<a href="http://ja.forums.wordpress.org/">WordPress ?????????</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 ?????????: %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 ?????????: %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>??????????????</h1>
  936. <p>??? <code>wp-config.php</code> ?????????????????????????<code>%s</code> ??????????????????????????????????????????????????????????????</p>
  937. <ul>
  938. <li>?????????????????????? ?</li>
  939. <li>??????????????? ?</li>
  940. <li>?????????????????? ?</li>
  941. </ul>
  942. <p>???????????????????????????????????????????????? <a href=\'http://ja.forums.wordpress.org/\'>WordPress ????????</a> ??? <a href=\'http://wordpress.org/support/\'>WordPress ????????? (??)</a> ?????</p>
  943. '/*/WP_I18N_DB_CONN_ERROR*/, $this->dbhost ), 'db_connect_fail' );
  944. return;
  945. }
  946. $this->set_charset( $this->dbh );
  947. $this->ready = true;
  948. $this->select( $this->dbname, $this->dbh );
  949. }
  950. /**
  951. * Perform a MySQL database query, using current database connection.
  952. *
  953. * More information can be found on the codex page.
  954. *
  955. * @since 0.71
  956. *
  957. * @param string $query Database query
  958. * @return int|false Number of rows affected/selected or false on error
  959. */
  960. function query( $query ) {
  961. if ( ! $this->ready )
  962. return false;
  963. // some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
  964. if ( function_exists( 'apply_filters' ) )
  965. $query = apply_filters( 'query', $query );
  966. $return_val = 0;
  967. $this->flush();
  968. // Log how the function was called
  969. $this->func_call = "\$db->query(\"$query\")";
  970. // Keep track of the last query for debug..
  971. $this->last_query = $query;
  972. if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
  973. $this->timer_start();
  974. $this->result = @mysql_query( $query, $this->dbh );
  975. $this->num_queries++;
  976. if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
  977. $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
  978. // If there is an error then take note of it..
  979. if ( $this->last_error = mysql_error( $this->dbh ) ) {
  980. $this->print_error();
  981. return false;
  982. }
  983. if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
  984. $this->rows_affected = mysql_affected_rows( $this->dbh );
  985. // Take note of the insert_id
  986. if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
  987. $this->insert_id = mysql_insert_id($this->dbh);
  988. }
  989. // Return number of rows affected
  990. $return_val = $this->rows_affected;
  991. } else {
  992. $i = 0;
  993. while ( $i < @mysql_num_fields( $this->result ) ) {
  994. $this->col_info[$i] = @mysql_fetch_field( $this->result );
  995. $i++;
  996. }
  997. $num_rows = 0;
  998. while ( $row = @mysql_fetch_object( $this->result ) ) {
  999. $this->last_result[$num_rows] = $row;
  1000. $num_rows++;
  1001. }
  1002. @mysql_free_result( $this->result );
  1003. // Log number of rows the query returned
  1004. // and return number of rows selected
  1005. $this->num_rows = $num_rows;
  1006. $return_val = $num_rows;
  1007. }
  1008. return $return_val;
  1009. }
  1010. /**
  1011. * Insert a row into a table.
  1012. *
  1013. * <code>
  1014. * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  1015. * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  1016. * </code>
  1017. *
  1018. * @since 2.5.0
  1019. * @see wpdb::prepare()
  1020. * @see wpdb::$field_types
  1021. * @see wp_set_wpdb_vars()
  1022. *
  1023. * @param string $table table name
  1024. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1025. * @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.
  1026. * 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.
  1027. * @return int|false The number of rows inserted, or false on error.
  1028. */
  1029. function insert( $table, $data, $format = null ) {
  1030. return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
  1031. }
  1032. /**
  1033. * Replace a row into a table.
  1034. *
  1035. * <code>
  1036. * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  1037. * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  1038. * </code>
  1039. *
  1040. * @since 3.0.0
  1041. * @see wpdb::prepare()
  1042. * @see wpdb::$field_types
  1043. * @see wp_set_wpdb_vars()
  1044. *
  1045. * @param string $table table name
  1046. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1047. * @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.
  1048. * 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.
  1049. * @return int|false The number of rows affected, or false on error.
  1050. */
  1051. function replace( $table, $data, $format = null ) {
  1052. return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
  1053. }
  1054. /**
  1055. * Helper function for insert and replace.
  1056. *
  1057. * Runs an insert or replace query based on $type argument.
  1058. *
  1059. * @access private
  1060. * @since 3.0.0
  1061. * @see wpdb::prepare()
  1062. * @see wpdb::$field_types
  1063. * @see wp_set_wpdb_vars()
  1064. *
  1065. * @param string $table table name
  1066. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1067. * @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.
  1068. * 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.
  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', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
  1107. * @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.
  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 specifed 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) -- ?????????????????????: 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. $key = array_shift( $var_by_ref = get_object_vars( $row ) );
  1244. if ( ! isset( $new_array[ $key ] ) )
  1245. $new_array[ $key ] = $row;
  1246. }
  1247. return $new_array;
  1248. } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  1249. // Return an integer-keyed array of...
  1250. if ( $this->last_result ) {
  1251. foreach( (array) $this->last_result as $row ) {
  1252. if ( $output == ARRAY_N ) {
  1253. // ...integer-keyed row arrays
  1254. $new_array[] = array_values( get_object_vars( $row ) );
  1255. } else {
  1256. // ...column name-keyed row arrays
  1257. $new_array[] = get_object_vars( $row );
  1258. }
  1259. }
  1260. }
  1261. return $new_array;
  1262. }
  1263. return null;
  1264. }
  1265. /**
  1266. * Retrieve column metadata from the last query.
  1267. *
  1268. * @since 0.71
  1269. *
  1270. * @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
  1271. * @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
  1272. * @return mixed Column Results
  1273. */
  1274. function get_col_info( $info_type = 'name', $col_offset = -1 ) {
  1275. if ( $this->col_info ) {
  1276. if ( $col_offset == -1 ) {
  1277. $i = 0;
  1278. $new_array = array();
  1279. foreach( (array) $this->col_info as $col ) {
  1280. $new_array[$i] = $col->{$info_type};
  1281. $i++;
  1282. }
  1283. return $new_array;
  1284. } else {
  1285. return $this->col_info[$col_offset]->{$info_type};
  1286. }
  1287. }
  1288. }
  1289. /**
  1290. * Starts the timer, for debugging purposes.
  1291. *
  1292. * @since 1.5.0
  1293. *
  1294. * @return true
  1295. */
  1296. function timer_start() {
  1297. $mtime = explode( ' ', microtime() );
  1298. $this->time_start = $mtime[1] + $mtime[0];
  1299. return true;
  1300. }
  1301. /**
  1302. * Stops the debugging timer.
  1303. *
  1304. * @since 1.5.0
  1305. *
  1306. * @return int Total time spent on the query, in milliseconds
  1307. */
  1308. function timer_stop() {
  1309. $mtime = explode( ' ', microtime() );
  1310. $time_end = $mtime[1] + $mtime[0];
  1311. $time_total = $time_end - $this->time_start;
  1312. return $time_total;
  1313. }
  1314. /**
  1315. * Wraps errors in a nice header and footer and dies.
  1316. *
  1317. * Will not die if wpdb::$show_errors is true
  1318. *
  1319. * @since 1.5.0
  1320. *
  1321. * @param string $message The Error message
  1322. * @param string $error_code Optional. A Computer readable string to identify the error.
  1323. * @return false|void
  1324. */
  1325. function bail( $message, $error_code = '500' ) {
  1326. if ( !$this->show_errors ) {
  1327. if ( class_exists( 'WP_Error' ) )
  1328. $this->error = new WP_Error($error_code, $message);
  1329. else
  1330. $this->error = $message;
  1331. return false;
  1332. }
  1333. wp_die($message);
  1334. }
  1335. /**
  1336. * Whether MySQL database is at least the required minimum version.
  1337. *
  1338. * @since 2.5.0
  1339. * @uses $wp_version
  1340. * @uses $required_mysql_version
  1341. *
  1342. * @return WP_Error
  1343. */
  1344. function check_database_version() {
  1345. global $wp_version, $required_mysql_version;
  1346. // Make sure the server has the required MySQL version
  1347. if ( version_compare($this->db_version(), $required_mysql_version, '<') )
  1348. return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ));
  1349. }
  1350. /**
  1351. * Whether the database supports collation.
  1352. *
  1353. * Called when WordPress is generating the table scheme.
  1354. *
  1355. * @since 2.5.0
  1356. *
  1357. * @return bool True if collation is supported, false if version does not
  1358. */
  1359. function supports_collation() {
  1360. return $this->has_cap( 'collation' );
  1361. }
  1362. /**
  1363. * Determine if a database supports a particular feature
  1364. *
  1365. * @since 2.7.0
  1366. * @see wpdb::db_version()
  1367. *
  1368. * @param string $db_cap the feature
  1369. * @return bool
  1370. */
  1371. function has_cap( $db_cap ) {
  1372. $version = $this->db_version();
  1373. switch ( strtolower( $db_cap ) ) {
  1374. case 'collation' : // @since 2.5.0
  1375. case 'group_concat' : // @since 2.7
  1376. case 'subqueries' : // @since 2.7
  1377. return version_compare( $version, '4.1', '>=' );
  1378. case 'set_charset' :
  1379. return version_compare($version, '5.0.7', '>=');
  1380. };
  1381. return false;
  1382. }
  1383. /**
  1384. * Retrieve the name of the function that called wpdb.
  1385. *
  1386. * Searches up the list of functions until it reaches
  1387. * the one that would most logically had called this method.
  1388. *
  1389. * @since 2.5.0
  1390. *
  1391. * @return string The name of the calling function
  1392. */
  1393. function get_caller() {
  1394. $trace = array_reverse( debug_backtrace() );
  1395. $caller = array();
  1396. foreach ( $trace as $call ) {
  1397. if ( isset( $call['class'] ) && __CLASS__ == $call['class'] )
  1398. continue; // Filter out wpdb calls.
  1399. $caller[] = isset( $call['class'] ) ? "{$call['class']}->{$call['function']}" : $call['function'];
  1400. }
  1401. return join( ', ', $caller );
  1402. }
  1403. /**
  1404. * The database version number.
  1405. *
  1406. * @since 2.7.0
  1407. *
  1408. * @return false|string false on failure, version number on success
  1409. */
  1410. function db_version() {
  1411. return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
  1412. }
  1413. }
  1414. ?>