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