From e105d925cf295b125d695daba959679d5aa77356 Mon Sep 17 00:00:00 2001 From: Mark Pavlitski Date: Thu, 16 May 2013 21:21:36 +0100 Subject: [PATCH] Issue #238250 by markpavlitski: Check bin is a cache table before truncating. --- includes/cache.inc | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/includes/cache.inc b/includes/cache.inc index f76164b..96a4aa5 100644 --- a/includes/cache.inc +++ b/includes/cache.inc @@ -505,7 +505,12 @@ class DrupalDatabaseCache implements DrupalCacheInterface { else { if ($wildcard) { if ($cid == '*') { - db_truncate($this->bin)->execute(); + // Check if $this->bin is a cache table before truncating. Other + // cache operations throw a PDO error in this situation, so we don't + // need to verify them first. + if ($this->isValidBin()) { + db_truncate($this->bin)->execute(); + } } else { db_delete($this->bin) @@ -542,4 +547,18 @@ class DrupalDatabaseCache implements DrupalCacheInterface { ->fetchField(); return empty($result); } + + /** + * Checks if $this->bin represents a valid cache table. + * + * @return boolean + */ + function isValidBin() { + // These fields are required for any cache table. + $fields = array('cid', 'data', 'expire', 'created', 'serialized'); + // Load the table schema. + $schema = drupal_get_schema($this->bin); + // Confirm that all fields are present. + return !array_diff($fields, array_keys($schema['fields'])); + } } -- 1.8.1.2