PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/nobunobuta/xoops_mod_WordPress
PHP | 341 lines | 216 code | 50 blank | 75 comment | 49 complexity | a8959171bd2d8d1a5d6352590b4a5243 MD5 | raw file
  1. <?php
  2. if( ! defined( 'WP_WP_DB_INCLUDED' ) ) {
  3. define( 'WP_WP_DB_INCLUDED' , 1 ) ;
  4. // ==================================================================
  5. // Author: Justin Vincent (justin@visunet.ie)
  6. // Web: http://php.justinvincent.com
  7. // Name: ezSQL
  8. // Desc: Class to make it very easy to deal with mySQL database connections.
  9. // WordPress is using this class to make the code cleaner and faster.
  10. // We highly recommend it.
  11. // We have modified the HTML it returns slightly.
  12. define('EZSQL_VERSION', '1.21');
  13. define('OBJECT', 'OBJECT', true);
  14. define('ARRAY_A', 'ARRAY_A', true);
  15. define('ARRAY_N', 'ARRAY_N', true);
  16. define('SAVEQUERIES', true);
  17. // The Main Class, renamed to avoid conflicts.
  18. class wpdb {
  19. var $debug_called;
  20. var $vardump_called;
  21. var $show_errors = true;
  22. var $querycount;
  23. // Our tables
  24. var $posts;
  25. var $users;
  26. var $categories;
  27. var $post2cat;
  28. var $comments;
  29. var $links;
  30. var $linkcategories;
  31. var $options;
  32. var $optiontypes;
  33. var $optionvalues;
  34. var $optiongroups;
  35. var $optiongroup_options;
  36. var $fp;
  37. // ==================================================================
  38. // DB Constructor - connects to the server and selects a database
  39. function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
  40. if (isset($GLOBALS['xoopsDB'])) {
  41. $this->dbh =& $GLOBALS['xoopsDB']->conn;
  42. $this->querycount = 0;
  43. } else {
  44. $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);
  45. if ( ! $this->dbh ) {
  46. die("<div>
  47. <p>" . _LANG_WA_WPDB_GUIDE1 . "</p>
  48. <ul>
  49. <li>" . _LANG_WA_WPDB_GUIDE2 . "</li>
  50. <li>" . _LANG_WA_WPDB_GUIDE3 . "</li>
  51. <li>" . _LANG_WA_WPDB_GUIDE4 . "</li>
  52. </ul>
  53. <p><a href='http://wordpress.xwd.jp/'>WordPress Japan</a></p>
  54. </div></body></html>");
  55. }
  56. $this->select($dbname);
  57. $this->querycount = 0;
  58. }
  59. }
  60. // ==================================================================
  61. // Select a DB (if another one needs to be selected)
  62. function select($db) {
  63. if ( !@mysql_select_db($db,$this->dbh)) {
  64. die("
  65. <p>We're having a little trouble selecting the proper database for WordPress.</p>
  66. <ul>
  67. <li>Are you sure it exists?</li>
  68. <li>Your database name is currently specified as <code>" . WP_DB_NAME ."</code>. Is this correct?</li>
  69. <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>
  70. </ul>
  71. <p><a href='http://wordpress.xwd.jp/'>WordPress Japan</a></p>");
  72. }
  73. }
  74. // ====================================================================
  75. // Format a string correctly for safe insert under all PHP conditions
  76. function escape($str)
  77. {
  78. return mysql_escape_string(stripslashes($str));
  79. }
  80. // ==================================================================
  81. // Print SQL/DB error.
  82. function print_error($str = '') {
  83. // All errors go to the global error array $EZSQL_ERROR..
  84. global $EZSQL_ERROR;
  85. // If no special error string then use mysql default..
  86. if ( !$str ) $str = mysql_error();
  87. // Log this error to the global array..
  88. $EZSQL_ERROR[] = array (
  89. 'query' => $this->last_query,
  90. 'error_str' => $str
  91. );
  92. // Is error output turned on or not..
  93. if ( $this->show_errors ) {
  94. // If there is an error then take note of it
  95. print "<div id='error'>
  96. <p><strong>SQL/DB Error:</strong><br />
  97. [<span style='color: #007;'>$str</span>]<br />
  98. <code>$this->last_query</code></p>
  99. </div>";
  100. } else {
  101. return false;
  102. }
  103. }
  104. // ==================================================================
  105. // Turn error handling on or off..
  106. function show_errors() {
  107. $this->show_errors = true;
  108. }
  109. function hide_errors() {
  110. $this->show_errors = false;
  111. }
  112. // ==================================================================
  113. // Kill cached query results
  114. function flush() {
  115. $this->last_result = null;
  116. $this->col_info = null;
  117. $this->last_query = null;
  118. }
  119. // ==================================================================
  120. // Basic Query - see docs for more detail
  121. function query($query) {
  122. // Flush cached values..
  123. $this->flush();
  124. // Log how the function was called
  125. $this->func_call = "\$db->query(\"$query\")";
  126. // Keep track of the last query for debug..
  127. $this->last_query = $query;
  128. if (0 && $GLOBALS['wp_debug']) {
  129. $this->fp = fopen($GLOBALS['wp_base']['-'].'/log/wp-db.log', 'a');
  130. fwrite($this->fp, "$query\n");
  131. fclose($this->fp);
  132. }
  133. // Perform the query via std mysql_query function..
  134. $this->result = mysql_query($query, $this->dbh);
  135. ++$this->querycount;
  136. if (SAVEQUERIES) {
  137. $this->savedqueries[] = $query;
  138. }
  139. // If there was an insert, delete or update see how many rows were affected
  140. // (Also, If there there was an insert take note of the insert_id
  141. $query_type = array('insert','delete','update','replace');
  142. // loop through the above array
  143. foreach ( $query_type as $word ) {
  144. // This is true if the query starts with insert, delete or update
  145. if ( preg_match("/^\\s*$word /i",$query) ) {
  146. $this->rows_affected = mysql_affected_rows();
  147. // This gets the insert ID
  148. if ( $word == 'insert' || $word == 'replace' ) {
  149. $this->insert_id = mysql_insert_id($this->dbh);
  150. }
  151. $this->result = false;
  152. }
  153. }
  154. if ( mysql_error() ){
  155. // If there is an error then take note of it..
  156. $this->print_error();
  157. } else {
  158. // In other words if this was a select statement..
  159. if ( $this->result ) {
  160. // =======================================================
  161. // Take note of column info
  162. $i=0;
  163. while ($i < @mysql_num_fields($this->result)) {
  164. $this->col_info[$i] = @mysql_fetch_field($this->result);
  165. $i++;
  166. }
  167. // =======================================================
  168. // Store Query Results
  169. $i=0;
  170. while ( $row = @mysql_fetch_object($this->result) ) {
  171. // Store relults as an objects within main array
  172. $this->last_result[$i] = $row;
  173. $i++;
  174. }
  175. // Log number of rows the query returned
  176. $this->num_rows = $i;
  177. @mysql_free_result($this->result);
  178. // If there were results then return true for $db->query
  179. if ( $i ) {
  180. return true;
  181. } else {
  182. return false;
  183. }
  184. } else {
  185. // Update insert etc. was good..
  186. return true;
  187. }
  188. }
  189. }
  190. // ==================================================================
  191. // Get one variable from the DB - see docs for more detail
  192. function get_var($query=null, $x=0, $y=0) {
  193. // Log how the function was called
  194. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  195. // If there is a query then perform it if not then use cached results..
  196. if ( $query ) {
  197. $this->query($query);
  198. }
  199. // Extract var out of cached results based x,y vals
  200. if ( $this->last_result[$y] ) {
  201. $values = array_values(get_object_vars($this->last_result[$y]));
  202. }
  203. // If there is a value return it else return null
  204. return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  205. }
  206. // ==================================================================
  207. // Get one row from the DB - see docs for more detail
  208. function get_row($query=null, $output=OBJECT, $y=0) {
  209. // Log how the function was called
  210. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  211. // If there is a query then perform it if not then use cached results..
  212. if ( $query ) {
  213. $this->query($query);
  214. }
  215. // If the output is an object then return object using the row offset..
  216. if ( $output == OBJECT ) {
  217. return $this->last_result[$y]?$this->last_result[$y]:null;
  218. // If the output is an associative array then return row as such..
  219. } elseif ( $output == ARRAY_A ) {
  220. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  221. // If the output is an numerical array then return row as such..
  222. } elseif ( $output == ARRAY_N ) {
  223. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  224. // If invalid output type was specified..
  225. } else {
  226. $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  227. }
  228. }
  229. // ==================================================================
  230. // Function to get 1 column from the cached result set based in X index
  231. // se docs for usage and info
  232. function get_col($query=null,$x=0) {
  233. // If there is a query then perform it if not then use cached results..
  234. if ( $query ) {
  235. $this->query($query);
  236. }
  237. // Extract the column values
  238. for ( $i=0; $i < count($this->last_result); $i++ ) {
  239. $new_array[$i] = $this->get_var(null,$x,$i);
  240. }
  241. return $new_array;
  242. }
  243. // ==================================================================
  244. // Return the the query as a result set - see docs for more details
  245. function get_results($query=null, $output = OBJECT) {
  246. // Log how the function was called
  247. $this->func_call = "\$db->get_results(\"$query\", $output)";
  248. // If there is a query then perform it if not then use cached results..
  249. if ( $query ) {
  250. $this->query($query);
  251. }
  252. // Send back array of objects. Each row is an object
  253. if ( $output == OBJECT ) {
  254. return $this->last_result;
  255. } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  256. if ( $this->last_result ) {
  257. $i=0;
  258. foreach( $this->last_result as $row ) {
  259. $new_array[$i] = get_object_vars($row);
  260. if ( $output == ARRAY_N ) {
  261. $new_array[$i] = array_values($new_array[$i]);
  262. }
  263. $i++;
  264. }
  265. return $new_array;
  266. } else {
  267. return null;
  268. }
  269. }
  270. }
  271. // ==================================================================
  272. // Function to get column meta data info pertaining to the last query
  273. // see docs for more info and usage
  274. function get_col_info($info_type='name', $col_offset=-1) {
  275. if ( $this->col_info ) {
  276. if ( $col_offset == -1 ) {
  277. $i=0;
  278. foreach($this->col_info as $col ) {
  279. $new_array[$i] = $col->{$info_type};
  280. $i++;
  281. }
  282. return $new_array;
  283. } else {
  284. return $this->col_info[$col_offset]->{$info_type};
  285. }
  286. }
  287. }
  288. }
  289. $wpdb = new wpdb(WP_DB_USER, WP_DB_PASSWORD, WP_DB_NAME, WP_DB_HOST);
  290. }
  291. ?>