PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/2.0/wp-includes/wp-db.php

#
PHP | 380 lines | 268 code | 63 blank | 49 comment | 44 complexity | 99a081921d82a2696df7d8c0b912fdd5 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. // WordPress DB Class
  3. // ORIGINAL CODE FROM:
  4. // Justin Vincent (justin@visunet.ie)
  5. // http://php.justinvincent.com
  6. define('EZSQL_VERSION', 'WP1.25');
  7. define('OBJECT', 'OBJECT', true);
  8. define('ARRAY_A', 'ARRAY_A', false);
  9. define('ARRAY_N', 'ARRAY_N', false);
  10. if (!defined('SAVEQUERIES'))
  11. define('SAVEQUERIES', false);
  12. class wpdb {
  13. var $show_errors = true;
  14. var $num_queries = 0;
  15. var $last_query;
  16. var $col_info;
  17. var $queries;
  18. // Our tables
  19. var $posts;
  20. var $users;
  21. var $categories;
  22. var $post2cat;
  23. var $comments;
  24. var $links;
  25. var $linkcategories;
  26. var $options;
  27. var $optiontypes;
  28. var $optionvalues;
  29. var $optiongroups;
  30. var $optiongroup_options;
  31. var $postmeta;
  32. // ==================================================================
  33. // DB Constructor - connects to the server and selects a database
  34. function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
  35. return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
  36. }
  37. function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
  38. register_shutdown_function(array(&$this, "__destruct"));
  39. $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword);
  40. if (!$this->dbh) {
  41. $this->bail("
  42. <h1>Error establishing a database connection</h1>
  43. <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>$dbhost</code>. This could mean your host's database server is down.</p>
  44. <ul>
  45. <li>Are you sure you have the correct username and password?</li>
  46. <li>Are you sure that you have typed the correct hostname?</li>
  47. <li>Are you sure that the database server is running?</li>
  48. </ul>
  49. <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>
  50. ");
  51. }
  52. $this->select($dbname);
  53. }
  54. function __destruct() {
  55. return true;
  56. }
  57. // ==================================================================
  58. // Select a DB (if another one needs to be selected)
  59. function select($db) {
  60. if (!@mysql_select_db($db, $this->dbh)) {
  61. $this->bail("
  62. <h1>Can&#8217;t select database</h1>
  63. <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>$db</code> database.</p>
  64. <ul>
  65. <li>Are you sure it exists?</li>
  66. <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>
  67. </ul>
  68. <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>");
  69. }
  70. }
  71. // ====================================================================
  72. // Format a string correctly for safe insert under all PHP conditions
  73. function escape($string) {
  74. return addslashes( $string ); // Disable rest for now, causing problems
  75. if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )
  76. return mysql_escape_string( $string );
  77. else
  78. return mysql_real_escape_string( $string, $this->dbh );
  79. }
  80. // ==================================================================
  81. // Print SQL/DB error.
  82. function print_error($str = '') {
  83. global $EZSQL_ERROR;
  84. if (!$str) $str = mysql_error();
  85. $EZSQL_ERROR[] =
  86. array ('query' => $this->last_query, 'error_str' => $str);
  87. $str = htmlspecialchars($str, ENT_QUOTES);
  88. $query = htmlspecialchars($this->last_query, ENT_QUOTES);
  89. // Is error output turned on or not..
  90. if ( $this->show_errors ) {
  91. // If there is an error then take note of it
  92. print "<div id='error'>
  93. <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
  94. <code>$query</code></p>
  95. </div>";
  96. } else {
  97. return false;
  98. }
  99. }
  100. // ==================================================================
  101. // Turn error handling on or off..
  102. function show_errors() {
  103. $this->show_errors = true;
  104. }
  105. function hide_errors() {
  106. $this->show_errors = false;
  107. }
  108. // ==================================================================
  109. // Kill cached query results
  110. function flush() {
  111. $this->last_result = array();
  112. $this->col_info = null;
  113. $this->last_query = null;
  114. }
  115. // ==================================================================
  116. // Basic Query - see docs for more detail
  117. function query($query) {
  118. // filter the query, if filters are available
  119. // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
  120. if ( function_exists('apply_filters') )
  121. $query = apply_filters('query', $query);
  122. // initialise return
  123. $return_val = 0;
  124. $this->flush();
  125. // Log how the function was called
  126. $this->func_call = "\$db->query(\"$query\")";
  127. // Keep track of the last query for debug..
  128. $this->last_query = $query;
  129. // Perform the query via std mysql_query function..
  130. if (SAVEQUERIES)
  131. $this->timer_start();
  132. $this->result = @mysql_query($query, $this->dbh);
  133. ++$this->num_queries;
  134. if (SAVEQUERIES)
  135. $this->queries[] = array( $query, $this->timer_stop() );
  136. // If there is an error then take note of it..
  137. if ( mysql_error() ) {
  138. $this->print_error();
  139. return false;
  140. }
  141. if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
  142. $this->rows_affected = mysql_affected_rows();
  143. // Take note of the insert_id
  144. if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
  145. $this->insert_id = mysql_insert_id($this->dbh);
  146. }
  147. // Return number of rows affected
  148. $return_val = $this->rows_affected;
  149. } else {
  150. $i = 0;
  151. while ($i < @mysql_num_fields($this->result)) {
  152. $this->col_info[$i] = @mysql_fetch_field($this->result);
  153. $i++;
  154. }
  155. $num_rows = 0;
  156. while ( $row = @mysql_fetch_object($this->result) ) {
  157. $this->last_result[$num_rows] = $row;
  158. $num_rows++;
  159. }
  160. @mysql_free_result($this->result);
  161. // Log number of rows the query returned
  162. $this->num_rows = $num_rows;
  163. // Return number of rows selected
  164. $return_val = $this->num_rows;
  165. }
  166. return $return_val;
  167. }
  168. // ==================================================================
  169. // Get one variable from the DB - see docs for more detail
  170. function get_var($query=null, $x = 0, $y = 0) {
  171. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  172. if ( $query )
  173. $this->query($query);
  174. // Extract var out of cached results based x,y vals
  175. if ( $this->last_result[$y] ) {
  176. $values = array_values(get_object_vars($this->last_result[$y]));
  177. }
  178. // If there is a value return it else return null
  179. return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
  180. }
  181. // ==================================================================
  182. // Get one row from the DB - see docs for more detail
  183. function get_row($query = null, $output = OBJECT, $y = 0) {
  184. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  185. if ( $query )
  186. $this->query($query);
  187. if ( $output == OBJECT ) {
  188. return $this->last_result[$y] ? $this->last_result[$y] : null;
  189. } elseif ( $output == ARRAY_A ) {
  190. return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
  191. } elseif ( $output == ARRAY_N ) {
  192. return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
  193. } else {
  194. $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  195. }
  196. }
  197. // ==================================================================
  198. // Function to get 1 column from the cached result set based in X index
  199. // se docs for usage and info
  200. function get_col($query = null , $x = 0) {
  201. if ( $query )
  202. $this->query($query);
  203. // Extract the column values
  204. for ( $i=0; $i < count($this->last_result); $i++ ) {
  205. $new_array[$i] = $this->get_var(null, $x, $i);
  206. }
  207. return $new_array;
  208. }
  209. // ==================================================================
  210. // Return the the query as a result set - see docs for more details
  211. function get_results($query = null, $output = OBJECT) {
  212. $this->func_call = "\$db->get_results(\"$query\", $output)";
  213. if ( $query )
  214. $this->query($query);
  215. // Send back array of objects. Each row is an object
  216. if ( $output == OBJECT ) {
  217. return $this->last_result;
  218. } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  219. if ( $this->last_result ) {
  220. $i = 0;
  221. foreach( $this->last_result as $row ) {
  222. $new_array[$i] = (array) $row;
  223. if ( $output == ARRAY_N ) {
  224. $new_array[$i] = array_values($new_array[$i]);
  225. }
  226. $i++;
  227. }
  228. return $new_array;
  229. } else {
  230. return null;
  231. }
  232. }
  233. }
  234. // ==================================================================
  235. // Function to get column meta data info pertaining to the last query
  236. // see docs for more info and usage
  237. function get_col_info($info_type = 'name', $col_offset = -1) {
  238. if ( $this->col_info ) {
  239. if ( $col_offset == -1 ) {
  240. $i = 0;
  241. foreach($this->col_info as $col ) {
  242. $new_array[$i] = $col->{$info_type};
  243. $i++;
  244. }
  245. return $new_array;
  246. } else {
  247. return $this->col_info[$col_offset]->{$info_type};
  248. }
  249. }
  250. }
  251. function timer_start() {
  252. $mtime = microtime();
  253. $mtime = explode(' ', $mtime);
  254. $this->time_start = $mtime[1] + $mtime[0];
  255. return true;
  256. }
  257. function timer_stop($precision = 3) {
  258. $mtime = microtime();
  259. $mtime = explode(' ', $mtime);
  260. $time_end = $mtime[1] + $mtime[0];
  261. $time_total = $time_end - $this->time_start;
  262. return $time_total;
  263. }
  264. function bail($message) { // Just wraps errors in a nice header and footer
  265. if ( !$this->show_errors )
  266. return false;
  267. header( 'Content-Type: text/html; charset=utf-8');
  268. echo <<<HEAD
  269. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  270. <html xmlns="http://www.w3.org/1999/xhtml">
  271. <head>
  272. <title>WordPress &rsaquo; Error</title>
  273. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  274. <style media="screen" type="text/css">
  275. <!--
  276. html {
  277. background: #eee;
  278. }
  279. body {
  280. background: #fff;
  281. color: #000;
  282. font-family: Georgia, "Times New Roman", Times, serif;
  283. margin-left: 25%;
  284. margin-right: 25%;
  285. padding: .2em 2em;
  286. }
  287. h1 {
  288. color: #006;
  289. font-size: 18px;
  290. font-weight: lighter;
  291. }
  292. h2 {
  293. font-size: 16px;
  294. }
  295. p, li, dt {
  296. line-height: 140%;
  297. padding-bottom: 2px;
  298. }
  299. ul, ol {
  300. padding: 5px 5px 5px 20px;
  301. }
  302. #logo {
  303. margin-bottom: 2em;
  304. }
  305. -->
  306. </style>
  307. </head>
  308. <body>
  309. <h1 id="logo"><img alt="WordPress" src="http://static.wordpress.org/logo.png" /></h1>
  310. HEAD;
  311. echo $message;
  312. echo "</body></html>";
  313. die();
  314. }
  315. }
  316. $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
  317. ?>