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