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