PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/wp-db.php

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