~nick-moffitt/wordpress/wordpress-upstream

« back to all changes in this revision

Viewing changes to wp-includes/wp-db.php

  • Committer: Nick Moffitt
  • Date: 2015-01-15 11:04:26 UTC
  • Revision ID: nick.moffitt@canonical.com-20150115110426-5stm1p14cfnxrtme
New Upstream Version 4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
567
567
         * the actual setting up of the class properties and connection
568
568
         * to the database.
569
569
         *
570
 
         * @link http://core.trac.wordpress.org/ticket/3354
 
570
         * @link https://core.trac.wordpress.org/ticket/3354
571
571
         * @since 2.0.8
572
572
         *
573
573
         * @param string $dbuser MySQL database user
632
632
         * @return mixed The private member
633
633
         */
634
634
        public function __get( $name ) {
635
 
                if ( 'col_info' == $name )
 
635
                if ( 'col_info' === $name )
636
636
                        $this->load_col_info();
637
637
 
638
638
                return $this->$name;
700
700
         * @since 3.1.0
701
701
         *
702
702
         * @param resource $dbh     The resource given by mysql_connect
703
 
         * @param string   $charset The character set (optional)
704
 
         * @param string   $collate The collation (optional)
 
703
         * @param string   $charset Optional. The character set. Default null.
 
704
         * @param string   $collate Optional. The collation. Default null.
705
705
         */
706
706
        public function set_charset( $dbh, $charset = null, $collate = null ) {
707
707
                if ( ! isset( $charset ) )
777
777
                 *
778
778
                 * @since 3.9.0
779
779
                 *
780
 
                 * @see wpdb::$incompatible_modes
781
 
                 *
782
780
                 * @param array $incompatible_modes An array of incompatible modes.
783
781
                 */
784
782
                $incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
844
842
         * @access public
845
843
         * @param int $blog_id
846
844
         * @param int $site_id Optional.
847
 
         * @return string previous blog id
 
845
         * @return int previous blog id
848
846
         */
849
847
        public function set_blog_id( $blog_id, $site_id = 0 ) {
850
848
                if ( ! empty( $site_id ) )
867
865
        /**
868
866
         * Gets blog prefix.
869
867
         *
870
 
         * @uses is_multisite()
871
868
         * @since 3.0.0
872
869
         * @param int $blog_id Optional.
873
870
         * @return string Blog prefix.
906
903
         * @uses wpdb::$old_tables
907
904
         * @uses wpdb::$global_tables
908
905
         * @uses wpdb::$ms_global_tables
909
 
         * @uses is_multisite()
910
906
         *
911
907
         * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
912
908
         * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
1042
1038
                }
1043
1039
 
1044
1040
                $class = get_class( $this );
1045
 
                _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE );
 
1041
                if ( function_exists( '__' ) ) {
 
1042
                        _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), E_USER_NOTICE );
 
1043
                } else {
 
1044
                        _doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), E_USER_NOTICE );
 
1045
                }
1046
1046
                return addslashes( $string );
1047
1047
        }
1048
1048
 
1134
1134
         *
1135
1135
         * Both %d and %s should be left unquoted in the query string.
1136
1136
         *
1137
 
         * <code>
1138
 
         * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
1139
 
         * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
1140
 
         * </code>
 
1137
         *     wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
 
1138
         *     wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
1141
1139
         *
1142
1140
         * @link http://php.net/sprintf Description of syntax.
1143
1141
         * @since 2.3.0
1206
1204
         * @global array $EZSQL_ERROR Stores error information of query and error string
1207
1205
         *
1208
1206
         * @param string $str The error to display
1209
 
         * @return bool False if the showing of errors is disabled.
 
1207
         * @return false|null False if the showing of errors is disabled.
1210
1208
         */
1211
1209
        public function print_error( $str = '' ) {
1212
1210
                global $EZSQL_ERROR;
1320
1318
                $this->rows_affected = $this->num_rows = 0;
1321
1319
                $this->last_error  = '';
1322
1320
 
1323
 
                if ( is_resource( $this->result ) ) {
1324
 
                        if ( $this->use_mysqli ) {
1325
 
                                mysqli_free_result( $this->result );
1326
 
                        } else {
1327
 
                                mysql_free_result( $this->result );
1328
 
                        }
 
1321
                if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
 
1322
                        mysqli_free_result( $this->result );
 
1323
                        $this->result = null;
 
1324
 
 
1325
                        // Sanity check before using the handle
 
1326
                        if ( empty( $this->dbh ) || !( $this->dbh instanceof mysqli ) ) {
 
1327
                                return;
 
1328
                        }
 
1329
 
 
1330
                        // Clear out any results from a multi-query
 
1331
                        while ( mysqli_more_results( $this->dbh ) ) {
 
1332
                                mysqli_next_result( $this->dbh );
 
1333
                        }
 
1334
                } else if ( is_resource( $this->result ) ) {
 
1335
                        mysql_free_result( $this->result );
1329
1336
                }
1330
1337
        }
1331
1338
 
1339
1346
         * @since 3.9.0 $allow_bail parameter added.
1340
1347
         *
1341
1348
         * @param bool $allow_bail Optional. Allows the function to bail. Default true.
1342
 
         * @return bool True with a successful connection, false on failure.
 
1349
         * @return null|bool True with a successful connection, false on failure.
1343
1350
         */
1344
1351
        public function db_connect( $allow_bail = true ) {
1345
1352
 
1458
1465
         * @since 3.9.0
1459
1466
         *
1460
1467
         * @param bool $allow_bail Optional. Allows the function to bail. Default true.
1461
 
         * @return bool True if the connection is up.
 
1468
         * @return bool|null True if the connection is up.
1462
1469
         */
1463
1470
        public function check_connection( $allow_bail = true ) {
1464
1471
                if ( $this->use_mysqli ) {
1533
1540
         * @return int|false Number of rows affected/selected or false on error
1534
1541
         */
1535
1542
        public function query( $query ) {
1536
 
                if ( ! $this->ready )
 
1543
                if ( ! $this->ready ) {
1537
1544
                        return false;
 
1545
                }
1538
1546
 
1539
1547
                /**
1540
1548
                 * Filter the database query.
1613
1621
                        $return_val = $this->rows_affected;
1614
1622
                } else {
1615
1623
                        $num_rows = 0;
1616
 
                        if ( $this->use_mysqli ) {
 
1624
                        if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
1617
1625
                                while ( $row = @mysqli_fetch_object( $this->result ) ) {
1618
1626
                                        $this->last_result[$num_rows] = $row;
1619
1627
                                        $num_rows++;
1620
1628
                                }
1621
 
                        } else {
 
1629
                        } else if ( is_resource( $this->result ) ) {
1622
1630
                                while ( $row = @mysql_fetch_object( $this->result ) ) {
1623
1631
                                        $this->last_result[$num_rows] = $row;
1624
1632
                                        $num_rows++;
1664
1672
        /**
1665
1673
         * Insert a row into a table.
1666
1674
         *
1667
 
         * <code>
1668
 
         * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
1669
 
         * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
1670
 
         * </code>
 
1675
         *     wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
 
1676
         *     wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
1671
1677
         *
1672
1678
         * @since 2.5.0
1673
1679
         * @see wpdb::prepare()
1687
1693
        /**
1688
1694
         * Replace a row into a table.
1689
1695
         *
1690
 
         * <code>
1691
 
         * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
1692
 
         * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
1693
 
         * </code>
 
1696
         *     wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
 
1697
         *     wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
1694
1698
         *
1695
1699
         * @since 3.0.0
1696
1700
         * @see wpdb::prepare()
1748
1752
        /**
1749
1753
         * Update a row in the table
1750
1754
         *
1751
 
         * <code>
1752
 
         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
1753
 
         * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
1754
 
         * </code>
 
1755
         *     wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
 
1756
         *     wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
1755
1757
         *
1756
1758
         * @since 2.5.0
1757
1759
         * @see wpdb::prepare()
1800
1802
        /**
1801
1803
         * Delete a row in the table
1802
1804
         *
1803
 
         * <code>
1804
 
         * wpdb::delete( 'table', array( 'ID' => 1 ) )
1805
 
         * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
1806
 
         * </code>
 
1805
         *     wpdb::delete( 'table', array( 'ID' => 1 ) )
 
1806
         *     wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
1807
1807
         *
1808
1808
         * @since 3.4.0
1809
1809
         * @see wpdb::prepare()
1839
1839
                return $this->query( $this->prepare( $sql, $where ) );
1840
1840
        }
1841
1841
 
1842
 
 
1843
1842
        /**
1844
1843
         * Retrieve one variable from the database.
1845
1844
         *
1856
1855
         */
1857
1856
        public function get_var( $query = null, $x = 0, $y = 0 ) {
1858
1857
                $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
1859
 
                if ( $query )
 
1858
 
 
1859
                if ( $query ) {
1860
1860
                        $this->query( $query );
 
1861
                }
1861
1862
 
1862
1863
                // Extract var out of cached results based x,y vals
1863
1864
                if ( !empty( $this->last_result[$y] ) ) {
1883
1884
         */
1884
1885
        public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
1885
1886
                $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
1886
 
                if ( $query )
 
1887
                if ( $query ) {
1887
1888
                        $this->query( $query );
1888
 
                else
 
1889
                } else {
1889
1890
                        return null;
 
1891
                }
1890
1892
 
1891
1893
                if ( !isset( $this->last_result[$y] ) )
1892
1894
                        return null;
1919
1921
         * @return array Database query result. Array indexed from 0 by SQL result row number.
1920
1922
         */
1921
1923
        public function get_col( $query = null , $x = 0 ) {
1922
 
                if ( $query )
 
1924
                if ( $query ) {
1923
1925
                        $this->query( $query );
 
1926
                }
1924
1927
 
1925
1928
                $new_array = array();
1926
1929
                // Extract the column values
1946
1949
        public function get_results( $query = null, $output = OBJECT ) {
1947
1950
                $this->func_call = "\$db->get_results(\"$query\", $output)";
1948
1951
 
1949
 
                if ( $query )
 
1952
                if ( $query ) {
1950
1953
                        $this->query( $query );
1951
 
                else
 
1954
                } else {
1952
1955
                        return null;
 
1956
                }
1953
1957
 
1954
1958
                $new_array = array();
1955
1959
                if ( $output == OBJECT ) {
2040
2044
         *
2041
2045
         * @since 1.5.0
2042
2046
         *
2043
 
         * @return true
 
2047
         * @return bool
2044
2048
         */
2045
2049
        public function timer_start() {
2046
2050
                $this->time_start = microtime( true );
2134
2138
         * Determine if a database supports a particular feature.
2135
2139
         *
2136
2140
         * @since 2.7.0
 
2141
         * @since 4.1.0 Support was added for the 'utf8mb4' feature.
 
2142
         *
2137
2143
         * @see wpdb::db_version()
2138
2144
         *
2139
 
         * @param string $db_cap The feature to check for.
2140
 
         * @return bool
 
2145
         * @param string $db_cap The feature to check for. Accepts 'collation',
 
2146
         *                       'group_concat', 'subqueries', 'set_charset',
 
2147
         *                       or 'utf8mb4'.
 
2148
         * @return bool Whether the database feature is supported, false otherwise.
2141
2149
         */
2142
2150
        public function has_cap( $db_cap ) {
2143
2151
                $version = $this->db_version();
2149
2157
                                return version_compare( $version, '4.1', '>=' );
2150
2158
                        case 'set_charset' :
2151
2159
                                return version_compare( $version, '5.0.7', '>=' );
2152
 
                };
 
2160
                        case 'utf8mb4' :      // @since 4.1.0
 
2161
                                return version_compare( $version, '5.5.3', '>=' );
 
2162
                }
2153
2163
 
2154
2164
                return false;
2155
2165
        }
2173
2183
         *
2174
2184
         * @since 2.7.0
2175
2185
         *
2176
 
         * @return false|string false on failure, version number on success
 
2186
         * @return null|string Null on failure, version number on success.
2177
2187
         */
2178
2188
        public function db_version() {
2179
2189
                if ( $this->use_mysqli ) {