WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/17/2016 04:20:22 AM (5 years ago)
Author:
pento
Message:

Database: Add support for LIKE-escaped tables in ::get_table_from_query().

The SHOW TABLES LIKE query can be used to search for tables that match a pattern, wp\_123\_%, for example. While this isn't the name of an actual table, the wp_123_ prefix can be used by database drop-ins to direct the query correctly. This change removes the escaping and % modifier, to provide this usable prefix.

Props andy, pento.
Fixes #38751.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r39188 r39275  
    30383038        }
    30393039
    3040         // SHOW TABLE STATUS and SHOW TABLES
    3041         if ( preg_match( '/^\s*(?:'
    3042                 . 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    3043                 . '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    3044                 . ')\W((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)\W/is', $query, $maybe ) ) {
    3045             return str_replace( '`', '', $maybe[1] );
     3040        // SHOW TABLE STATUS and SHOW TABLES WHERE Name = 'wp_posts'
     3041        if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES).+WHERE\s+Name\s*=\s*("|\')((?:[0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)\\1/is', $query, $maybe ) ) {
     3042            return $maybe[2];
     3043        }
     3044
     3045        // SHOW TABLE STATUS LIKE and SHOW TABLES LIKE 'wp\_123\_%'
     3046        // This quoted LIKE operand seldom holds a full table name.
     3047        // It is usually a pattern for matching a prefix so we just
     3048        // strip the trailing % and unescape the _ to get 'wp_123_'
     3049        // which drop-ins can use for routing these SQL statements.
     3050        if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES)\s+(?:WHERE\s+Name\s+)?LIKE\s*("|\')((?:[\\\\0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)%?\\1/is', $query, $maybe ) ) {
     3051            return str_replace( '\\_', '_', $maybe[2] );
    30463052        }
    30473053
Note: See TracChangeset for help on using the changeset viewer.