]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/wp-db.php
Wordpress 3.1.4
[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                         $blog_id = (int) $blog_id;
648                         if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) )
649                                 return $this->base_prefix;
650                         else
651                                 return $this->base_prefix . $blog_id . '_';
652                 } else {
653                         return $this->base_prefix;
654                 }
655         }
656
657         /**
658          * Returns an array of WordPress tables.
659          *
660          * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
661          * override the WordPress users and usersmeta tables that would otherwise
662          * be determined by the prefix.
663          *
664          * The scope argument can take one of the following:
665          *
666          * 'all' - returns 'all' and 'global' tables. No old tables are returned.
667          * 'blog' - returns the blog-level tables for the queried blog.
668          * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite.
669          * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite.
670          * 'old' - returns tables which are deprecated.
671          *
672          * @since 3.0.0
673          * @uses wpdb::$tables
674          * @uses wpdb::$old_tables
675          * @uses wpdb::$global_tables
676          * @uses wpdb::$ms_global_tables
677          * @uses is_multisite()
678          *
679          * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
680          * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
681          *      prefix is requested, then the custom users and usermeta tables will be mapped.
682          * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested.
683          * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
684          */
685         function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
686                 switch ( $scope ) {
687                         case 'all' :
688                                 $tables = array_merge( $this->global_tables, $this->tables );
689                                 if ( is_multisite() )
690                                         $tables = array_merge( $tables, $this->ms_global_tables );
691                                 break;
692                         case 'blog' :
693                                 $tables = $this->tables;
694                                 break;
695                         case 'global' :
696                                 $tables = $this->global_tables;
697                                 if ( is_multisite() )
698                                         $tables = array_merge( $tables, $this->ms_global_tables );
699                                 break;
700                         case 'ms_global' :
701                                 $tables = $this->ms_global_tables;
702                                 break;
703                         case 'old' :
704                                 $tables = $this->old_tables;
705                                 break;
706                         default :
707                                 return array();
708                                 break;
709                 }
710
711                 if ( $prefix ) {
712                         if ( ! $blog_id )
713                                 $blog_id = $this->blogid;
714                         $blog_prefix = $this->get_blog_prefix( $blog_id );
715                         $base_prefix = $this->base_prefix;
716                         $global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
717                         foreach ( $tables as $k => $table ) {
718                                 if ( in_array( $table, $global_tables ) )
719                                         $tables[ $table ] = $base_prefix . $table;
720                                 else
721                                         $tables[ $table ] = $blog_prefix . $table;
722                                 unset( $tables[ $k ] );
723                         }
724
725                         if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
726                                 $tables['users'] = CUSTOM_USER_TABLE;
727
728                         if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
729                                 $tables['usermeta'] = CUSTOM_USER_META_TABLE;
730                 }
731
732                 return $tables;
733         }
734
735         /**
736          * Selects a database using the current database connection.
737          *
738          * The database name will be changed based on the current database
739          * connection. On failure, the execution will bail and display an DB error.
740          *
741          * @since 0.71
742          *
743          * @param string $db MySQL database name
744          * @param resource $dbh Optional link identifier.
745          * @return null Always null.
746          */
747         function select( $db, $dbh = null) {
748                 if ( is_null($dbh) )
749                         $dbh = $this->dbh;
750
751                 if ( !@mysql_select_db( $db, $dbh ) ) {
752                         $this->ready = false;
753                         $this->bail( sprintf( /*WP_I18N_DB_SELECT_DB*/'<h1>Can&#8217;t select database</h1>
754 <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>
755 <ul>
756 <li>Are you sure it exists?</li>
757 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
758 <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>
759 </ul>
760 <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' );
761                         return;
762                 }
763         }
764
765         /**
766          * Weak escape, using addslashes()
767          *
768          * @see addslashes()
769          * @since 2.8.0
770          * @access private
771          *
772          * @param string $string
773          * @return string
774          */
775         function _weak_escape( $string ) {
776                 return addslashes( $string );
777         }
778
779         /**
780          * Real escape, using mysql_real_escape_string() or addslashes()
781          *
782          * @see mysql_real_escape_string()
783          * @see addslashes()
784          * @since 2.8.0
785          * @access private
786          *
787          * @param  string $string to escape
788          * @return string escaped
789          */
790         function _real_escape( $string ) {
791                 if ( $this->dbh && $this->real_escape )
792                         return mysql_real_escape_string( $string, $this->dbh );
793                 else
794                         return addslashes( $string );
795         }
796
797         /**
798          * Escape data. Works on arrays.
799          *
800          * @uses wpdb::_escape()
801          * @uses wpdb::_real_escape()
802          * @since  2.8.0
803          * @access private
804          *
805          * @param  string|array $data
806          * @return string|array escaped
807          */
808         function _escape( $data ) {
809                 if ( is_array( $data ) ) {
810                         foreach ( (array) $data as $k => $v ) {
811                                 if ( is_array($v) )
812                                         $data[$k] = $this->_escape( $v );
813                                 else
814                                         $data[$k] = $this->_real_escape( $v );
815                         }
816                 } else {
817                         $data = $this->_real_escape( $data );
818                 }
819
820                 return $data;
821         }
822
823         /**
824          * Escapes content for insertion into the database using addslashes(), for security.
825          *
826          * Works on arrays.
827          *
828          * @since 0.71
829          * @param string|array $data to escape
830          * @return string|array escaped as query safe string
831          */
832         function escape( $data ) {
833                 if ( is_array( $data ) ) {
834                         foreach ( (array) $data as $k => $v ) {
835                                 if ( is_array( $v ) )
836                                         $data[$k] = $this->escape( $v );
837                                 else
838                                         $data[$k] = $this->_weak_escape( $v );
839                         }
840                 } else {
841                         $data = $this->_weak_escape( $data );
842                 }
843
844                 return $data;
845         }
846
847         /**
848          * Escapes content by reference for insertion into the database, for security
849          *
850          * @uses wpdb::_real_escape()
851          * @since 2.3.0
852          * @param string $string to escape
853          * @return void
854          */
855         function escape_by_ref( &$string ) {
856                 $string = $this->_real_escape( $string );
857         }
858
859         /**
860          * Prepares a SQL query for safe execution. Uses sprintf()-like syntax.
861          *
862          * The following directives can be used in the query format string:
863          *   %d (decimal number)
864          *   %s (string)
865          *   %% (literal percentage sign - no argument needed)
866          *
867          * Both %d and %s are to be left unquoted in the query string and they need an argument passed for them.
868          * Literals (%) as parts of the query must be properly written as %%.
869          *
870          * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string).
871          * Does not support sign, padding, alignment, width or precision specifiers.
872          * Does not support argument numbering/swapping.
873          *
874          * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}.
875          *
876          * Both %d and %s should be left unquoted in the query string.
877          *
878          * <code>
879          * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
880          * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
881          * </code>
882          *
883          * @link http://php.net/sprintf Description of syntax.
884          * @since 2.3.0
885          *
886          * @param string $query Query statement with sprintf()-like placeholders
887          * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like
888          *      {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if
889          *      being called like {@link http://php.net/sprintf sprintf()}.
890          * @param mixed $args,... further variables to substitute into the query's placeholders if being called like
891          *      {@link http://php.net/sprintf sprintf()}.
892          * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
893          *      if there was something to prepare
894          */
895         function prepare( $query = null ) { // ( $query, *$args )
896                 if ( is_null( $query ) )
897                         return;
898
899                 $args = func_get_args();
900                 array_shift( $args );
901                 // If args were passed as an array (as in vsprintf), move them up
902                 if ( isset( $args[0] ) && is_array($args[0]) )
903                         $args = $args[0];
904                 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
905                 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
906                 $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
907                 array_walk( $args, array( &$this, 'escape_by_ref' ) );
908                 return @vsprintf( $query, $args );
909         }
910
911         /**
912          * Print SQL/DB error.
913          *
914          * @since 0.71
915          * @global array $EZSQL_ERROR Stores error information of query and error string
916          *
917          * @param string $str The error to display
918          * @return bool False if the showing of errors is disabled.
919          */
920         function print_error( $str = '' ) {
921                 global $EZSQL_ERROR;
922
923                 if ( !$str )
924                         $str = mysql_error( $this->dbh );
925                 $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
926
927                 if ( $this->suppress_errors )
928                         return false;
929
930                 if ( $caller = $this->get_caller() )
931                         $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 );
932                 else
933                         $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 );
934
935                 if ( function_exists( 'error_log' )
936                         && ( $log_file = @ini_get( 'error_log' ) )
937                         && ( 'syslog' == $log_file || @is_writable( $log_file ) )
938                         )
939                         @error_log( $error_str );
940
941                 // Are we showing errors?
942                 if ( ! $this->show_errors )
943                         return false;
944
945                 // If there is an error then take note of it
946                 if ( is_multisite() ) {
947                         $msg = "WordPress database error: [$str]\n{$this->last_query}\n";
948                         if ( defined( 'ERRORLOGFILE' ) )
949                                 error_log( $msg, 3, ERRORLOGFILE );
950                         if ( defined( 'DIEONDBERROR' ) )
951                                 wp_die( $msg );
952                 } else {
953                         $str   = htmlspecialchars( $str, ENT_QUOTES );
954                         $query = htmlspecialchars( $this->last_query, ENT_QUOTES );
955
956                         print "<div id='error'>
957                         <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
958                         <code>$query</code></p>
959                         </div>";
960                 }
961         }
962
963         /**
964          * Enables showing of database errors.
965          *
966          * This function should be used only to enable showing of errors.
967          * wpdb::hide_errors() should be used instead for hiding of errors. However,
968          * this function can be used to enable and disable showing of database
969          * errors.
970          *
971          * @since 0.71
972          * @see wpdb::hide_errors()
973          *
974          * @param bool $show Whether to show or hide errors
975          * @return bool Old value for showing errors.
976          */
977         function show_errors( $show = true ) {
978                 $errors = $this->show_errors;
979                 $this->show_errors = $show;
980                 return $errors;
981         }
982
983         /**
984          * Disables showing of database errors.
985          *
986          * By default database errors are not shown.
987          *
988          * @since 0.71
989          * @see wpdb::show_errors()
990          *
991          * @return bool Whether showing of errors was active
992          */
993         function hide_errors() {
994                 $show = $this->show_errors;
995                 $this->show_errors = false;
996                 return $show;
997         }
998
999         /**
1000          * Whether to suppress database errors.
1001          *
1002          * By default database errors are suppressed, with a simple
1003          * call to this function they can be enabled.
1004          *
1005          * @since 2.5.0
1006          * @see wpdb::hide_errors()
1007          * @param bool $suppress Optional. New value. Defaults to true.
1008          * @return bool Old value
1009          */
1010         function suppress_errors( $suppress = true ) {
1011                 $errors = $this->suppress_errors;
1012                 $this->suppress_errors = (bool) $suppress;
1013                 return $errors;
1014         }
1015
1016         /**
1017          * Kill cached query results.
1018          *
1019          * @since 0.71
1020          * @return void
1021          */
1022         function flush() {
1023                 $this->last_result = array();
1024                 $this->col_info    = null;
1025                 $this->last_query  = null;
1026         }
1027
1028         /**
1029          * Connect to and select database
1030          *
1031          * @since 3.0.0
1032          */
1033         function db_connect() {
1034                 global $db_list, $global_db_list;
1035
1036                 if ( WP_DEBUG ) {
1037                         $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
1038                 } else {
1039                         $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
1040                 }
1041
1042                 if ( !$this->dbh ) {
1043                         $this->bail( sprintf( /*WP_I18N_DB_CONN_ERROR*/"
1044 <h1>Error establishing a database connection</h1>
1045 <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>
1046 <ul>
1047         <li>Are you sure you have the correct username and password?</li>
1048         <li>Are you sure that you have typed the correct hostname?</li>
1049         <li>Are you sure that the database server is running?</li>
1050 </ul>
1051 <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>
1052 "/*/WP_I18N_DB_CONN_ERROR*/, $this->dbhost ), 'db_connect_fail' );
1053
1054                         return;
1055                 }
1056
1057                 $this->set_charset( $this->dbh );
1058
1059                 $this->ready = true;
1060
1061                 $this->select( $this->dbname, $this->dbh );
1062         }
1063
1064         /**
1065          * Perform a MySQL database query, using current database connection.
1066          *
1067          * More information can be found on the codex page.
1068          *
1069          * @since 0.71
1070          *
1071          * @param string $query Database query
1072          * @return int|false Number of rows affected/selected or false on error
1073          */
1074         function query( $query ) {
1075                 if ( ! $this->ready )
1076                         return false;
1077
1078                 // some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
1079                 if ( function_exists( 'apply_filters' ) )
1080                         $query = apply_filters( 'query', $query );
1081
1082                 $return_val = 0;
1083                 $this->flush();
1084
1085                 // Log how the function was called
1086                 $this->func_call = "\$db->query(\"$query\")";
1087
1088                 // Keep track of the last query for debug..
1089                 $this->last_query = $query;
1090
1091                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
1092                         $this->timer_start();
1093
1094                 $this->result = @mysql_query( $query, $this->dbh );
1095                 $this->num_queries++;
1096
1097                 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
1098                         $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
1099
1100                 // If there is an error then take note of it..
1101                 if ( $this->last_error = mysql_error( $this->dbh ) ) {
1102                         $this->print_error();
1103                         return false;
1104                 }
1105
1106                 if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
1107                         $this->rows_affected = mysql_affected_rows( $this->dbh );
1108                         // Take note of the insert_id
1109                         if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
1110                                 $this->insert_id = mysql_insert_id($this->dbh);
1111                         }
1112                         // Return number of rows affected
1113                         $return_val = $this->rows_affected;
1114                 } else {
1115                         $i = 0;
1116                         while ( $i < @mysql_num_fields( $this->result ) ) {
1117                                 $this->col_info[$i] = @mysql_fetch_field( $this->result );
1118                                 $i++;
1119                         }
1120                         $num_rows = 0;
1121                         while ( $row = @mysql_fetch_object( $this->result ) ) {
1122                                 $this->last_result[$num_rows] = $row;
1123                                 $num_rows++;
1124                         }
1125
1126                         @mysql_free_result( $this->result );
1127
1128                         // Log number of rows the query returned
1129                         // and return number of rows selected
1130                         $this->num_rows = $num_rows;
1131                         $return_val     = $num_rows;
1132                 }
1133
1134                 return $return_val;
1135         }
1136
1137         /**
1138          * Insert a row into a table.
1139          *
1140          * <code>
1141          * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
1142          * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
1143          * </code>
1144          *
1145          * @since 2.5.0
1146          * @see wpdb::prepare()
1147          * @see wpdb::$field_types
1148          * @see wp_set_wpdb_vars()
1149          *
1150          * @param string $table table name
1151          * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
1152          * @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.
1153          *      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.
1154          * @return int|false The number of rows inserted, or false on error.
1155          */
1156         function insert( $table, $data, $format = null ) {
1157                 return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
1158         }
1159
1160         /**
1161          * Replace a row into a table.
1162          *
1163          * <code>
1164          * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
1165          * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
1166          * </code>
1167          *
1168          * @since 3.0.0
1169          * @see wpdb::prepare()
1170          * @see wpdb::$field_types
1171          * @see wp_set_wpdb_vars()
1172          *
1173          * @param string $table table name
1174          * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
1175          * @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.
1176          *      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.
1177          * @return int|false The number of rows affected, or false on error.
1178          */
1179         function replace( $table, $data, $format = null ) {
1180                 return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
1181         }
1182
1183         /**
1184          * Helper function for insert and replace.
1185          *
1186          * Runs an insert or replace query based on $type argument.
1187          *
1188          * @access private
1189          * @since 3.0.0
1190          * @see wpdb::prepare()
1191          * @see wpdb::$field_types
1192          * @see wp_set_wpdb_vars()
1193          *
1194          * @param string $table table name
1195          * @param array $data Data to insert (in column => value pairs).  Both $data columns and $data values should be "raw" (neither should be SQL escaped).
1196          * @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.
1197          *      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.
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', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
1237          * @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.
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 specifed 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                                 $key = array_shift( $var_by_ref = get_object_vars( $row ) );
1388                                 if ( ! isset( $new_array[ $key ] ) )
1389                                         $new_array[ $key ] = $row;
1390                         }
1391                         return $new_array;
1392                 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
1393                         // Return an integer-keyed array of...
1394                         if ( $this->last_result ) {
1395                                 foreach( (array) $this->last_result as $row ) {
1396                                         if ( $output == ARRAY_N ) {
1397                                                 // ...integer-keyed row arrays
1398                                                 $new_array[] = array_values( get_object_vars( $row ) );
1399                                         } else {
1400                                                 // ...column name-keyed row arrays
1401                                                 $new_array[] = get_object_vars( $row );
1402                                         }
1403                                 }
1404                         }
1405                         return $new_array;
1406                 }
1407                 return null;
1408         }
1409
1410         /**
1411          * Retrieve column metadata from the last query.
1412          *
1413          * @since 0.71
1414          *
1415          * @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
1416          * @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
1417          * @return mixed Column Results
1418          */
1419         function get_col_info( $info_type = 'name', $col_offset = -1 ) {
1420                 if ( $this->col_info ) {
1421                         if ( $col_offset == -1 ) {
1422                                 $i = 0;
1423                                 $new_array = array();
1424                                 foreach( (array) $this->col_info as $col ) {
1425                                         $new_array[$i] = $col->{$info_type};
1426                                         $i++;
1427                                 }
1428                                 return $new_array;
1429                         } else {
1430                                 return $this->col_info[$col_offset]->{$info_type};
1431                         }
1432                 }
1433         }
1434
1435         /**
1436          * Starts the timer, for debugging purposes.
1437          *
1438          * @since 1.5.0
1439          *
1440          * @return true
1441          */
1442         function timer_start() {
1443                 $mtime            = explode( ' ', microtime() );
1444                 $this->time_start = $mtime[1] + $mtime[0];
1445                 return true;
1446         }
1447
1448         /**
1449          * Stops the debugging timer.
1450          *
1451          * @since 1.5.0
1452          *
1453          * @return int Total time spent on the query, in milliseconds
1454          */
1455         function timer_stop() {
1456                 $mtime      = explode( ' ', microtime() );
1457                 $time_end   = $mtime[1] + $mtime[0];
1458                 $time_total = $time_end - $this->time_start;
1459                 return $time_total;
1460         }
1461
1462         /**
1463          * Wraps errors in a nice header and footer and dies.
1464          *
1465          * Will not die if wpdb::$show_errors is true
1466          *
1467          * @since 1.5.0
1468          *
1469          * @param string $message The Error message
1470          * @param string $error_code Optional. A Computer readable string to identify the error.
1471          * @return false|void
1472          */
1473         function bail( $message, $error_code = '500' ) {
1474                 if ( !$this->show_errors ) {
1475                         if ( class_exists( 'WP_Error' ) )
1476                                 $this->error = new WP_Error($error_code, $message);
1477                         else
1478                                 $this->error = $message;
1479                         return false;
1480                 }
1481                 wp_die($message);
1482         }
1483
1484         /**
1485          * Whether MySQL database is at least the required minimum version.
1486          *
1487          * @since 2.5.0
1488          * @uses $wp_version
1489          * @uses $required_mysql_version
1490          *
1491          * @return WP_Error
1492          */
1493         function check_database_version() {
1494                 global $wp_version, $required_mysql_version;
1495                 // Make sure the server has the required MySQL version
1496                 if ( version_compare($this->db_version(), $required_mysql_version, '<') )
1497                         return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ));
1498         }
1499
1500         /**
1501          * Whether the database supports collation.
1502          *
1503          * Called when WordPress is generating the table scheme.
1504          *
1505          * @since 2.5.0
1506          *
1507          * @return bool True if collation is supported, false if version does not
1508          */
1509         function supports_collation() {
1510                 return $this->has_cap( 'collation' );
1511         }
1512
1513         /**
1514          * Determine if a database supports a particular feature
1515          *
1516          * @since 2.7.0
1517          * @see   wpdb::db_version()
1518          *
1519          * @param string $db_cap the feature
1520          * @return bool
1521          */
1522         function has_cap( $db_cap ) {
1523                 $version = $this->db_version();
1524
1525                 switch ( strtolower( $db_cap ) ) {
1526                         case 'collation' :    // @since 2.5.0
1527                         case 'group_concat' : // @since 2.7
1528                         case 'subqueries' :   // @since 2.7
1529                                 return version_compare( $version, '4.1', '>=' );
1530                         case 'set_charset' :
1531                                 return version_compare($version, '5.0.7', '>=');
1532                 };
1533
1534                 return false;
1535         }
1536
1537         /**
1538          * Retrieve the name of the function that called wpdb.
1539          *
1540          * Searches up the list of functions until it reaches
1541          * the one that would most logically had called this method.
1542          *
1543          * @since 2.5.0
1544          *
1545          * @return string The name of the calling function
1546          */
1547         function get_caller() {
1548                 $trace  = array_reverse( debug_backtrace() );
1549                 $caller = array();
1550
1551                 foreach ( $trace as $call ) {
1552                         if ( isset( $call['class'] ) && __CLASS__ == $call['class'] )
1553                                 continue; // Filter out wpdb calls.
1554                         $caller[] = isset( $call['class'] ) ? "{$call['class']}->{$call['function']}" : $call['function'];
1555                 }
1556
1557                 return join( ', ', $caller );
1558         }
1559
1560         /**
1561          * The database version number.
1562          *
1563          * @since 2.7.0
1564          *
1565          * @return false|string false on failure, version number on success
1566          */
1567         function db_version() {
1568                 return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
1569         }
1570 }
1571
1572 ?>