From 0352fe44f10fa2a20027b6ca7d583232019781cb Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 27 Jun 2017 11:31:35 -0700 Subject: [PATCH] Avoid checking double-prefixed table names in mysql tableExists() Change-Id: I9d91a5305227171d0776ffa443d4e538fbe1b15f --- includes/libs/rdbms/database/Database.php | 6 ++++-- .../libs/rdbms/database/DatabaseMysqlBase.php | 20 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index afeffb3048..559c28b7ad 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -1822,8 +1822,10 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware } /** - * @param string $name Table name - * @return array (DB name, schema name, table prefix, table name) + * Get the table components needed for a query given the currently selected database + * + * @param string $name Table name in the form of db.schema.table, db.table, or table + * @return array (DB name or "" for default, schema name, table prefix, table name) */ protected function qualifiedTableComponents( $name ) { # We reverse the explode so that database.table and table both output the correct table. diff --git a/includes/libs/rdbms/database/DatabaseMysqlBase.php b/includes/libs/rdbms/database/DatabaseMysqlBase.php index e237ef4899..8d19bc1b34 100644 --- a/includes/libs/rdbms/database/DatabaseMysqlBase.php +++ b/includes/libs/rdbms/database/DatabaseMysqlBase.php @@ -527,25 +527,23 @@ abstract class DatabaseMysqlBase extends Database { } public function tableExists( $table, $fname = __METHOD__ ) { - $table = $this->tableName( $table, 'raw' ); - if ( isset( $this->mSessionTempTables[$table] ) ) { - return true; // already known to exist and won't show in SHOW TABLES anyway - } - // Split database and table into proper variables as Database::tableName() returns // shared tables prefixed with their database, which do not work in SHOW TABLES statements - list( $database, $schema, $prefix, $table ) = $this->qualifiedTableComponents( $table ); + list( $database, , $prefix, $table ) = $this->qualifiedTableComponents( $table ); + $tableName = "{$prefix}{$table}"; - $table = $prefix . $table; + if ( isset( $this->mSessionTempTables[$tableName] ) ) { + return true; // already known to exist and won't show in SHOW TABLES anyway + } // We can't use buildLike() here, because it specifies an escape character // other than the backslash, which is the only one supported by SHOW TABLES - $encLike = $this->escapeLikeInternal( $table, '\\' ); + $encLike = $this->escapeLikeInternal( $tableName, '\\' ); - // If the database has been specified (such as for shared tables), add a FROM $database clause + // If the database has been specified (such as for shared tables), use "FROM" if ( $database !== '' ) { - $database = $this->addIdentifierQuotes( $database ); - $query = "SHOW TABLES FROM $database LIKE '$encLike'"; + $encDatabase = $this->addIdentifierQuotes( $database ); + $query = "SHOW TABLES FROM $encDatabase LIKE '$encLike'"; } else { $query = "SHOW TABLES LIKE '$encLike'"; } -- 2.20.1