~canonical-sysadmins/wordpress/4.2.2

« back to all changes in this revision

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

  • Committer: Haw Loeung (hloeung)
  • Date: 2015-04-28 00:05:40 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: haw.loeung@canonical.com-20150428000540-drm3hw2825fequas
Merge WP4.2.1 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1946
1946
         */
1947
1947
        protected function process_fields( $table, $data, $format ) {
1948
1948
                $data = $this->process_field_formats( $data, $format );
 
1949
                if ( false === $data ) {
 
1950
                        return false;
 
1951
                }
 
1952
 
1949
1953
                $data = $this->process_field_charsets( $data, $table );
1950
1954
                if ( false === $data ) {
1951
1955
                        return false;
1952
1956
                }
1953
1957
 
 
1958
                $data = $this->process_field_lengths( $data, $table );
 
1959
                if ( false === $data ) {
 
1960
                        return false;
 
1961
                }
 
1962
 
1954
1963
                $converted_data = $this->strip_invalid_text( $data );
1955
1964
 
1956
1965
                if ( $data !== $converted_data ) {
2032
2041
        }
2033
2042
 
2034
2043
        /**
 
2044
         * For string fields, record the maximum string length that field can safely save.
 
2045
         *
 
2046
         * @since 4.2.1
 
2047
         * @access protected
 
2048
         *
 
2049
         * @param array  $data  As it comes from the wpdb::process_field_charsets() method.
 
2050
         * @param string $table Table name.
 
2051
         * @return array|False The same array as $data with additional 'length' keys, or false if
 
2052
         *                     any of the values were too long for their corresponding field.
 
2053
         */
 
2054
        protected function process_field_lengths( $data, $table ) {
 
2055
                foreach ( $data as $field => $value ) {
 
2056
                        if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
 
2057
                                // We can skip this field if we know it isn't a string.
 
2058
                                // This checks %d/%f versus ! %s because it's sprintf() could take more.
 
2059
                                $value['length'] = false;
 
2060
                        } else {
 
2061
                                $value['length'] = $this->get_col_length( $table, $field );
 
2062
                                if ( is_wp_error( $value['length'] ) ) {
 
2063
                                        return false;
 
2064
                                }
 
2065
                        }
 
2066
 
 
2067
                        if ( false !== $value['length'] && mb_strlen( $value['value'] ) > $value['length'] ) {
 
2068
                                return false;
 
2069
                        }
 
2070
 
 
2071
                        $data[ $field ] = $value;
 
2072
                }
 
2073
 
 
2074
                return $data;
 
2075
        }
 
2076
 
 
2077
        /**
2035
2078
         * Retrieve one variable from the database.
2036
2079
         *
2037
2080
         * Executes a SQL query and returns the value from the SQL result.
2362
2405
        }
2363
2406
 
2364
2407
        /**
 
2408
         * Retrieve the maximum string length allowed in a given column.
 
2409
         *
 
2410
         * @since 4.2.1
 
2411
         * @access public
 
2412
         *
 
2413
         * @param string $table  Table name.
 
2414
         * @param string $column Column name.
 
2415
         * @return mixed Max column length as an int. False if the column has no
 
2416
         *               length. WP_Error object if there was an error.
 
2417
         */
 
2418
        public function get_col_length( $table, $column ) {
 
2419
                $tablekey = strtolower( $table );
 
2420
                $columnkey = strtolower( $column );
 
2421
 
 
2422
                // Skip this entirely if this isn't a MySQL database.
 
2423
                if ( false === $this->is_mysql ) {
 
2424
                        return false;
 
2425
                }
 
2426
 
 
2427
                if ( empty( $this->col_meta[ $tablekey ] ) ) {
 
2428
                        // This primes column information for us.
 
2429
                        $table_charset = $this->get_table_charset( $table );
 
2430
                        if ( is_wp_error( $table_charset ) ) {
 
2431
                                return $table_charset;
 
2432
                        }
 
2433
                }
 
2434
 
 
2435
                if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) {
 
2436
                        return false;
 
2437
                }
 
2438
 
 
2439
                $typeinfo = explode( '(', $this->col_meta[ $tablekey ][ $columnkey ]->Type );
 
2440
 
 
2441
                $type = strtolower( $typeinfo[0] );
 
2442
                if ( ! empty( $typeinfo[1] ) ) {
 
2443
                        $length = trim( $typeinfo[1], ')' );
 
2444
                } else {
 
2445
                        $length = false;
 
2446
                }
 
2447
 
 
2448
                switch( $type ) {
 
2449
                        case 'binary':
 
2450
                        case 'char':
 
2451
                        case 'varbinary':
 
2452
                        case 'varchar':
 
2453
                                return $length;
 
2454
                                break;
 
2455
                        case 'tinyblob':
 
2456
                        case 'tinytext':
 
2457
                                return 255; // 2^8 - 1
 
2458
                                break;
 
2459
                        case 'blob':
 
2460
                        case 'text':
 
2461
                                return 65535; // 2^16 - 1
 
2462
                                break;
 
2463
                        case 'mediumblob':
 
2464
                        case 'mediumtext':
 
2465
                                return 16777215; // 2^24 - 1
 
2466
                                break;
 
2467
                        case 'longblob':
 
2468
                        case 'longtext':
 
2469
                                return 4294967295; // 2^32 - 1
 
2470
                                break;
 
2471
                        default:
 
2472
                                return false;
 
2473
                }
 
2474
 
 
2475
                return false;
 
2476
        }
 
2477
 
 
2478
        /**
2365
2479
         * Check if a string is ASCII.
2366
2480
         *
2367
2481
         * The negative regex is faster for non-ASCII strings, as it allows