PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/WebKitSite/blog/wp-includes/wp-db.php

https://github.com/kvlasov/qtwebkit
PHP | 970 lines | 362 code | 108 blank | 500 comment | 75 complexity | 7027b212f4a3b19e5de9ad81e1f13cff MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-2.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 {@internal Version Unknown}}
  21. */
  22. define('OBJECT_K', 'OBJECT_K', false);
  23. /**
  24. * @since 0.71
  25. */
  26. define('ARRAY_A', 'ARRAY_A', false);
  27. /**
  28. * @since 0.71
  29. */
  30. define('ARRAY_N', 'ARRAY_N', false);
  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/wpdb.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. * @final
  46. */
  47. class wpdb {
  48. /**
  49. * Whether to show SQL/DB errors
  50. *
  51. * @since 0.71
  52. * @access private
  53. * @var bool
  54. */
  55. var $show_errors = false;
  56. /**
  57. * Whether to suppress errors during the DB bootstrapping.
  58. *
  59. * @access private
  60. * @since {@internal Version Unknown}}
  61. * @var bool
  62. */
  63. var $suppress_errors = false;
  64. /**
  65. * The last error during query.
  66. *
  67. * @since {@internal Version Unknown}}
  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. * Saved result of the last query made
  81. *
  82. * @since 1.2.0
  83. * @access private
  84. * @var array
  85. */
  86. var $last_query;
  87. /**
  88. * Saved info on the table column
  89. *
  90. * @since 1.2.0
  91. * @access private
  92. * @var array
  93. */
  94. var $col_info;
  95. /**
  96. * Saved queries that were executed
  97. *
  98. * @since 1.5.0
  99. * @access private
  100. * @var array
  101. */
  102. var $queries;
  103. /**
  104. * WordPress table prefix
  105. *
  106. * You can set this to have multiple WordPress installations
  107. * in a single database. The second reason is for possible
  108. * security precautions.
  109. *
  110. * @since 0.71
  111. * @access private
  112. * @var string
  113. */
  114. var $prefix = '';
  115. /**
  116. * Whether the database queries are ready to start executing.
  117. *
  118. * @since 2.5.0
  119. * @access private
  120. * @var bool
  121. */
  122. var $ready = false;
  123. /**
  124. * WordPress Posts table
  125. *
  126. * @since 1.5.0
  127. * @access public
  128. * @var string
  129. */
  130. var $posts;
  131. /**
  132. * WordPress Users table
  133. *
  134. * @since 1.5.0
  135. * @access public
  136. * @var string
  137. */
  138. var $users;
  139. /**
  140. * WordPress Categories table
  141. *
  142. * @since 1.5.0
  143. * @access public
  144. * @var string
  145. */
  146. var $categories;
  147. /**
  148. * WordPress Post to Category table
  149. *
  150. * @since 1.5.0
  151. * @access public
  152. * @var string
  153. */
  154. var $post2cat;
  155. /**
  156. * WordPress Comments table
  157. *
  158. * @since 1.5.0
  159. * @access public
  160. * @var string
  161. */
  162. var $comments;
  163. /**
  164. * WordPress Links table
  165. *
  166. * @since 1.5.0
  167. * @access public
  168. * @var string
  169. */
  170. var $links;
  171. /**
  172. * WordPress Options table
  173. *
  174. * @since 1.5.0
  175. * @access public
  176. * @var string
  177. */
  178. var $options;
  179. /**
  180. * WordPress Post Metadata table
  181. *
  182. * @since {@internal Version Unknown}}
  183. * @access public
  184. * @var string
  185. */
  186. var $postmeta;
  187. /**
  188. * WordPress User Metadata table
  189. *
  190. * @since 2.3.0
  191. * @access public
  192. * @var string
  193. */
  194. var $usermeta;
  195. /**
  196. * WordPress Terms table
  197. *
  198. * @since 2.3.0
  199. * @access public
  200. * @var string
  201. */
  202. var $terms;
  203. /**
  204. * WordPress Term Taxonomy table
  205. *
  206. * @since 2.3.0
  207. * @access public
  208. * @var string
  209. */
  210. var $term_taxonomy;
  211. /**
  212. * WordPress Term Relationships table
  213. *
  214. * @since 2.3.0
  215. * @access public
  216. * @var string
  217. */
  218. var $term_relationships;
  219. /**
  220. * List of WordPress tables
  221. *
  222. * @since {@internal Version Unknown}}
  223. * @access private
  224. * @var array
  225. */
  226. var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
  227. 'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
  228. /**
  229. * Database table columns charset
  230. *
  231. * @since 2.2.0
  232. * @access public
  233. * @var string
  234. */
  235. var $charset;
  236. /**
  237. * Database table columns collate
  238. *
  239. * @since 2.2.0
  240. * @access public
  241. * @var string
  242. */
  243. var $collate;
  244. /**
  245. * Connects to the database server and selects a database
  246. *
  247. * PHP4 compatibility layer for calling the PHP5 constructor.
  248. *
  249. * @uses wpdb::__construct() Passes parameters and returns result
  250. * @since 0.71
  251. *
  252. * @param string $dbuser MySQL database user
  253. * @param string $dbpassword MySQL database password
  254. * @param string $dbname MySQL database name
  255. * @param string $dbhost MySQL database host
  256. */
  257. function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
  258. return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
  259. }
  260. /**
  261. * Connects to the database server and selects a database
  262. *
  263. * PHP5 style constructor for compatibility with PHP5. Does
  264. * the actual setting up of the class properties and connection
  265. * to the database.
  266. *
  267. * @since 2.0.8
  268. *
  269. * @param string $dbuser MySQL database user
  270. * @param string $dbpassword MySQL database password
  271. * @param string $dbname MySQL database name
  272. * @param string $dbhost MySQL database host
  273. */
  274. function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
  275. register_shutdown_function(array(&$this, "__destruct"));
  276. if ( defined('WP_DEBUG') and WP_DEBUG == true )
  277. $this->show_errors();
  278. if ( defined('DB_CHARSET') )
  279. $this->charset = DB_CHARSET;
  280. if ( defined('DB_COLLATE') )
  281. $this->collate = DB_COLLATE;
  282. $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
  283. if (!$this->dbh) {
  284. $this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/"
  285. <h1>Error establishing a database connection</h1>
  286. <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>
  287. <ul>
  288. <li>Are you sure you have the correct username and password?</li>
  289. <li>Are you sure that you have typed the correct hostname?</li>
  290. <li>Are you sure that the database server is running?</li>
  291. </ul>
  292. <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>
  293. "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost));
  294. return;
  295. }
  296. $this->ready = true;
  297. if ( $this->supports_collation() ) {
  298. $collation_query = '';
  299. if ( !empty($this->charset) ) {
  300. $collation_query = "SET NAMES '{$this->charset}'";
  301. if (!empty($this->collate) )
  302. $collation_query .= " COLLATE '{$this->collate}'";
  303. }
  304. if ( !empty($collation_query) )
  305. $this->query($collation_query);
  306. }
  307. $this->select($dbname);
  308. }
  309. /**
  310. * PHP5 style destructor and will run when database object is destroyed.
  311. *
  312. * @since 2.0.8
  313. *
  314. * @return bool Always true
  315. */
  316. function __destruct() {
  317. return true;
  318. }
  319. /**
  320. * Sets the table prefix for the WordPress tables.
  321. *
  322. * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
  323. * override the WordPress users and usersmeta tables.
  324. *
  325. * @since 2.5.0
  326. *
  327. * @param string $prefix Alphanumeric name for the new prefix.
  328. * @return string Old prefix
  329. */
  330. function set_prefix($prefix) {
  331. if ( preg_match('|[^a-z0-9_]|i', $prefix) )
  332. return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
  333. $old_prefix = $this->prefix;
  334. $this->prefix = $prefix;
  335. foreach ( $this->tables as $table )
  336. $this->$table = $this->prefix . $table;
  337. if ( defined('CUSTOM_USER_TABLE') )
  338. $this->users = CUSTOM_USER_TABLE;
  339. if ( defined('CUSTOM_USER_META_TABLE') )
  340. $this->usermeta = CUSTOM_USER_META_TABLE;
  341. return $old_prefix;
  342. }
  343. /**
  344. * Selects a database using the current database connection.
  345. *
  346. * The database name will be changed based on the current database
  347. * connection. On failure, the execution will bail and display an DB error.
  348. *
  349. * @since 0.71
  350. *
  351. * @param string $db MySQL database name
  352. * @return null Always null.
  353. */
  354. function select($db) {
  355. if (!@mysql_select_db($db, $this->dbh)) {
  356. $this->ready = false;
  357. $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
  358. <h1>Can&#8217;t select database</h1>
  359. <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>
  360. <ul>
  361. <li>Are you sure it exists?</li>
  362. <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
  363. <li>On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?</li>
  364. </ul>
  365. <p>If you don\'t know how to setup 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, DB_USER));
  366. return;
  367. }
  368. }
  369. /**
  370. * Escapes content for insertion into the database, for security
  371. *
  372. * @since 0.71
  373. *
  374. * @param string $string
  375. * @return string query safe string
  376. */
  377. function escape($string) {
  378. return addslashes( $string );
  379. // Disable rest for now, causing problems
  380. /*
  381. if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )
  382. return mysql_escape_string( $string );
  383. else
  384. return mysql_real_escape_string( $string, $this->dbh );
  385. */
  386. }
  387. /**
  388. * Escapes content by reference for insertion into the database, for security
  389. *
  390. * @since 2.3.0
  391. *
  392. * @param string $s
  393. */
  394. function escape_by_ref(&$s) {
  395. $s = $this->escape($s);
  396. }
  397. /**
  398. * Prepares a SQL query for safe use, using sprintf() syntax.
  399. *
  400. * @link http://php.net/sprintf See for syntax to use for query string.
  401. * @since 2.3.0
  402. *
  403. * @param null|string $args If string, first parameter must be query statement
  404. * @param mixed $args,... If additional parameters, they will be set inserted into the query.
  405. * @return null|string Sanitized query string
  406. */
  407. function prepare($args=null) {
  408. if ( is_null( $args ) )
  409. return;
  410. $args = func_get_args();
  411. $query = array_shift($args);
  412. $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it
  413. $query = str_replace('"%s"', '%s', $query); // doublequote unquoting
  414. $query = str_replace('%s', "'%s'", $query); // quote the strings
  415. array_walk($args, array(&$this, 'escape_by_ref'));
  416. return @vsprintf($query, $args);
  417. }
  418. /**
  419. * Print SQL/DB error.
  420. *
  421. * @since 0.71
  422. * @global array $EZSQL_ERROR Stores error information of query and error string
  423. *
  424. * @param string $str The error to display
  425. * @return bool False if the showing of errors is disabled.
  426. */
  427. function print_error($str = '') {
  428. global $EZSQL_ERROR;
  429. if (!$str) $str = mysql_error($this->dbh);
  430. $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);
  431. if ( $this->suppress_errors )
  432. return false;
  433. if ( $caller = $this->get_caller() )
  434. $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);
  435. else
  436. $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);
  437. $log_error = true;
  438. if ( ! function_exists('error_log') )
  439. $log_error = false;
  440. $log_file = @ini_get('error_log');
  441. if ( !empty($log_file) && ('syslog' != $log_file) && !is_writable($log_file) )
  442. $log_error = false;
  443. if ( $log_error )
  444. @error_log($error_str, 0);
  445. // Is error output turned on or not..
  446. if ( !$this->show_errors )
  447. return false;
  448. $str = htmlspecialchars($str, ENT_QUOTES);
  449. $query = htmlspecialchars($this->last_query, ENT_QUOTES);
  450. // If there is an error then take note of it
  451. print "<div id='error'>
  452. <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
  453. <code>$query</code></p>
  454. </div>";
  455. }
  456. /**
  457. * Enables showing of database errors.
  458. *
  459. * This function should be used only to enable showing of errors.
  460. * wpdb::hide_errors() should be used instead for hiding of errors. However,
  461. * this function can be used to enable and disable showing of database
  462. * errors.
  463. *
  464. * @since 0.71
  465. *
  466. * @param bool $show Whether to show or hide errors
  467. * @return bool Old value for showing errors.
  468. */
  469. function show_errors( $show = true ) {
  470. $errors = $this->show_errors;
  471. $this->show_errors = $show;
  472. return $errors;
  473. }
  474. /**
  475. * Disables showing of database errors.
  476. *
  477. * @since 0.71
  478. *
  479. * @return bool Whether showing of errors was active or not
  480. */
  481. function hide_errors() {
  482. $show = $this->show_errors;
  483. $this->show_errors = false;
  484. return $show;
  485. }
  486. /**
  487. * Whether to suppress database errors.
  488. *
  489. * @param unknown_type $suppress
  490. * @return unknown
  491. */
  492. function suppress_errors( $suppress = true ) {
  493. $errors = $this->suppress_errors;
  494. $this->suppress_errors = $suppress;
  495. return $errors;
  496. }
  497. /**
  498. * Kill cached query results.
  499. *
  500. * @since 0.71
  501. */
  502. function flush() {
  503. $this->last_result = array();
  504. $this->col_info = null;
  505. $this->last_query = null;
  506. }
  507. /**
  508. * Perform a MySQL database query, using current database connection.
  509. *
  510. * More information can be found on the codex page.
  511. *
  512. * @since 0.71
  513. *
  514. * @param string $query
  515. * @return unknown
  516. */
  517. function query($query) {
  518. if ( ! $this->ready )
  519. return false;
  520. // filter the query, if filters are available
  521. // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
  522. if ( function_exists('apply_filters') )
  523. $query = apply_filters('query', $query);
  524. // initialise return
  525. $return_val = 0;
  526. $this->flush();
  527. // Log how the function was called
  528. $this->func_call = "\$db->query(\"$query\")";
  529. // Keep track of the last query for debug..
  530. $this->last_query = $query;
  531. // Perform the query via std mysql_query function..
  532. if ( defined('SAVEQUERIES') && SAVEQUERIES )
  533. $this->timer_start();
  534. $this->result = @mysql_query($query, $this->dbh);
  535. ++$this->num_queries;
  536. if ( defined('SAVEQUERIES') && SAVEQUERIES )
  537. $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
  538. // If there is an error then take note of it..
  539. if ( $this->last_error = mysql_error($this->dbh) ) {
  540. $this->print_error();
  541. return false;
  542. }
  543. if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
  544. $this->rows_affected = mysql_affected_rows($this->dbh);
  545. // Take note of the insert_id
  546. if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
  547. $this->insert_id = mysql_insert_id($this->dbh);
  548. }
  549. // Return number of rows affected
  550. $return_val = $this->rows_affected;
  551. } else {
  552. $i = 0;
  553. while ($i < @mysql_num_fields($this->result)) {
  554. $this->col_info[$i] = @mysql_fetch_field($this->result);
  555. $i++;
  556. }
  557. $num_rows = 0;
  558. while ( $row = @mysql_fetch_object($this->result) ) {
  559. $this->last_result[$num_rows] = $row;
  560. $num_rows++;
  561. }
  562. @mysql_free_result($this->result);
  563. // Log number of rows the query returned
  564. $this->num_rows = $num_rows;
  565. // Return number of rows selected
  566. $return_val = $this->num_rows;
  567. }
  568. return $return_val;
  569. }
  570. /**
  571. * Insert an array of data into a table.
  572. *
  573. * @since 2.5.0
  574. *
  575. * @param string $table WARNING: not sanitized!
  576. * @param array $data Should not already be SQL-escaped
  577. * @return mixed Results of $this->query()
  578. */
  579. function insert($table, $data) {
  580. $data = add_magic_quotes($data);
  581. $fields = array_keys($data);
  582. return $this->query("INSERT INTO $table (`" . implode('`,`',$fields) . "`) VALUES ('".implode("','",$data)."')");
  583. }
  584. /**
  585. * Update a row in the table with an array of data.
  586. *
  587. * @since 2.5.0
  588. *
  589. * @param string $table WARNING: not sanitized!
  590. * @param array $data Should not already be SQL-escaped
  591. * @param array $where A named array of WHERE column => value relationships. Multiple member pairs will be joined with ANDs. WARNING: the column names are not currently sanitized!
  592. * @return mixed Results of $this->query()
  593. */
  594. function update($table, $data, $where){
  595. $data = add_magic_quotes($data);
  596. $bits = $wheres = array();
  597. foreach ( array_keys($data) as $k )
  598. $bits[] = "`$k` = '$data[$k]'";
  599. if ( is_array( $where ) )
  600. foreach ( $where as $c => $v )
  601. $wheres[] = "$c = '" . $this->escape( $v ) . "'";
  602. else
  603. return false;
  604. return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) );
  605. }
  606. /**
  607. * Retrieve one variable from the database.
  608. *
  609. * This combines the functionality of wpdb::get_row() and wpdb::get_col(),
  610. * so both the column and row can be picked.
  611. *
  612. * It is possible to use this function without executing more queries. If
  613. * you already made a query, you can set the $query to 'null' value and just
  614. * retrieve either the column and row of the last query result.
  615. *
  616. * @since 0.71
  617. *
  618. * @param string $query Can be null as well, for caching
  619. * @param int $x Column num to return
  620. * @param int $y Row num to return
  621. * @return mixed Database query results
  622. */
  623. function get_var($query=null, $x = 0, $y = 0) {
  624. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  625. if ( $query )
  626. $this->query($query);
  627. // Extract var out of cached results based x,y vals
  628. if ( !empty( $this->last_result[$y] ) ) {
  629. $values = array_values(get_object_vars($this->last_result[$y]));
  630. }
  631. // If there is a value return it else return null
  632. return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
  633. }
  634. /**
  635. * Retrieve one row from the database.
  636. *
  637. * @since 0.71
  638. *
  639. * @param string $query SQL query
  640. * @param string $output ARRAY_A | ARRAY_N | OBJECT
  641. * @param int $y Row num to return
  642. * @return mixed Database query results
  643. */
  644. function get_row($query = null, $output = OBJECT, $y = 0) {
  645. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  646. if ( $query )
  647. $this->query($query);
  648. else
  649. return null;
  650. if ( !isset($this->last_result[$y]) )
  651. return null;
  652. if ( $output == OBJECT ) {
  653. return $this->last_result[$y] ? $this->last_result[$y] : null;
  654. } elseif ( $output == ARRAY_A ) {
  655. return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
  656. } elseif ( $output == ARRAY_N ) {
  657. return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
  658. } else {
  659. $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*/);
  660. }
  661. }
  662. /**
  663. * Retrieve one column from the database.
  664. *
  665. * @since 0.71
  666. *
  667. * @param string $query Can be null as well, for caching
  668. * @param int $x Col num to return. Starts from 0.
  669. * @return array Column results
  670. */
  671. function get_col($query = null , $x = 0) {
  672. if ( $query )
  673. $this->query($query);
  674. $new_array = array();
  675. // Extract the column values
  676. for ( $i=0; $i < count($this->last_result); $i++ ) {
  677. $new_array[$i] = $this->get_var(null, $x, $i);
  678. }
  679. return $new_array;
  680. }
  681. /**
  682. * Retrieve an entire result set from the database.
  683. *
  684. * @since 0.71
  685. *
  686. * @param string|null $query Can also be null to pull from the cache
  687. * @param string $output ARRAY_A | ARRAY_N | OBJECT_K | OBJECT
  688. * @return mixed Database query results
  689. */
  690. function get_results($query = null, $output = OBJECT) {
  691. $this->func_call = "\$db->get_results(\"$query\", $output)";
  692. if ( $query )
  693. $this->query($query);
  694. else
  695. return null;
  696. if ( $output == OBJECT ) {
  697. // Return an integer-keyed array of row objects
  698. return $this->last_result;
  699. } elseif ( $output == OBJECT_K ) {
  700. // Return an array of row objects with keys from column 1
  701. // (Duplicates are discarded)
  702. foreach ( $this->last_result as $row ) {
  703. $key = array_shift( get_object_vars( $row ) );
  704. if ( !isset( $new_array[ $key ] ) )
  705. $new_array[ $key ] = $row;
  706. }
  707. return $new_array;
  708. } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  709. // Return an integer-keyed array of...
  710. if ( $this->last_result ) {
  711. $i = 0;
  712. foreach( $this->last_result as $row ) {
  713. if ( $output == ARRAY_N ) {
  714. // ...integer-keyed row arrays
  715. $new_array[$i] = array_values( get_object_vars( $row ) );
  716. } else {
  717. // ...column name-keyed row arrays
  718. $new_array[$i] = get_object_vars( $row );
  719. }
  720. ++$i;
  721. }
  722. return $new_array;
  723. }
  724. }
  725. }
  726. /**
  727. * Retrieve column metadata from the last query.
  728. *
  729. * @since 0.71
  730. *
  731. * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
  732. * @param int $col_offset 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
  733. * @return mixed Column Results
  734. */
  735. function get_col_info($info_type = 'name', $col_offset = -1) {
  736. if ( $this->col_info ) {
  737. if ( $col_offset == -1 ) {
  738. $i = 0;
  739. foreach($this->col_info as $col ) {
  740. $new_array[$i] = $col->{$info_type};
  741. $i++;
  742. }
  743. return $new_array;
  744. } else {
  745. return $this->col_info[$col_offset]->{$info_type};
  746. }
  747. }
  748. }
  749. /**
  750. * Starts the timer, for debugging purposes.
  751. *
  752. * @since 1.5.0
  753. *
  754. * @return bool Always returns true
  755. */
  756. function timer_start() {
  757. $mtime = microtime();
  758. $mtime = explode(' ', $mtime);
  759. $this->time_start = $mtime[1] + $mtime[0];
  760. return true;
  761. }
  762. /**
  763. * Stops the debugging timer.
  764. *
  765. * @since 1.5.0
  766. *
  767. * @return int Total time spent on the query, in milliseconds
  768. */
  769. function timer_stop() {
  770. $mtime = microtime();
  771. $mtime = explode(' ', $mtime);
  772. $time_end = $mtime[1] + $mtime[0];
  773. $time_total = $time_end - $this->time_start;
  774. return $time_total;
  775. }
  776. /**
  777. * Wraps fatal errors in a nice header and footer and dies.
  778. *
  779. * @since 1.5.0
  780. *
  781. * @param string $message
  782. * @return unknown
  783. */
  784. function bail($message) {
  785. if ( !$this->show_errors ) {
  786. if ( class_exists('WP_Error') )
  787. $this->error = new WP_Error('500', $message);
  788. else
  789. $this->error = $message;
  790. return false;
  791. }
  792. wp_die($message);
  793. }
  794. /**
  795. * Whether or not MySQL database is minimal required version.
  796. *
  797. * @since 2.5.0
  798. * @uses $wp_version
  799. *
  800. * @return WP_Error
  801. */
  802. function check_database_version()
  803. {
  804. global $wp_version;
  805. // Make sure the server has MySQL 4.0
  806. $mysql_version = preg_replace('|[^0-9\.]|', '', @mysql_get_server_info($this->dbh));
  807. if ( version_compare($mysql_version, '4.0.0', '<') )
  808. return new WP_Error('database_version',sprintf(__('<strong>ERROR</strong>: WordPress %s requires MySQL 4.0.0 or higher'), $wp_version));
  809. }
  810. /**
  811. * Whether of not the database version supports collation.
  812. *
  813. * Called when WordPress is generating the table scheme.
  814. *
  815. * @since 2.5.0
  816. *
  817. * @return bool True if collation is supported, false if version does not
  818. */
  819. function supports_collation()
  820. {
  821. return ( version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=') );
  822. }
  823. /**
  824. * Retrieve the name of the function that called wpdb.
  825. *
  826. * Requires PHP 4.3 and searches up the list of functions until it reaches
  827. * the one that would most logically had called this method.
  828. *
  829. * @since 2.5.0
  830. *
  831. * @return string The name of the calling function
  832. */
  833. function get_caller() {
  834. // requires PHP 4.3+
  835. if ( !is_callable('debug_backtrace') )
  836. return '';
  837. $bt = debug_backtrace();
  838. $caller = '';
  839. foreach ( $bt as $trace ) {
  840. if ( @$trace['class'] == __CLASS__ )
  841. continue;
  842. elseif ( strtolower(@$trace['function']) == 'call_user_func_array' )
  843. continue;
  844. elseif ( strtolower(@$trace['function']) == 'apply_filters' )
  845. continue;
  846. elseif ( strtolower(@$trace['function']) == 'do_action' )
  847. continue;
  848. $caller = $trace['function'];
  849. break;
  850. }
  851. return $caller;
  852. }
  853. }
  854. if ( ! isset($wpdb) ) {
  855. /**
  856. * WordPress Database Object, if it isn't set already in wp-content/wpdb.php
  857. * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database
  858. * @since 0.71
  859. */
  860. $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
  861. }
  862. ?>