Merge pull request #12644 from civicrm/5.5
authorEileen McNaughton <eileen@mcnaughty.com>
Sat, 11 Aug 2018 02:12:00 +0000 (14:12 +1200)
committerGitHub <noreply@github.com>
Sat, 11 Aug 2018 02:12:00 +0000 (14:12 +1200)
5.5 to master

CRM/Core/BAO/Cache.php
CRM/Utils/Cache.php
tests/phpunit/CRM/Core/BAO/CacheTest.php

index ce1d63ede35a9a3106ea2f10f13963330fc5645d..d1f97f63f661a483becb6e05016fece69d483e3c 100644 (file)
@@ -73,7 +73,8 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
     $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
     if (!array_key_exists($argString, self::$_cache)) {
       $cache = CRM_Utils_Cache::singleton();
-      self::$_cache[$argString] = $cache->get(self::cleanKey($argString));
+      $cleanKey = self::cleanKey($argString);
+      self::$_cache[$argString] = $cache->get($cleanKey);
       if (!self::$_cache[$argString]) {
         $table = self::getTableName();
         $where = self::whereCache($group, $path, $componentID);
@@ -81,7 +82,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
         $data = $rawData ? self::decode($rawData) : NULL;
 
         self::$_cache[$argString] = $data;
-        $cache->set(self::cleanKey($argString), self::$_cache[$argString]);
+        $cache->set($cleanKey, self::$_cache[$argString]);
       }
     }
     return self::$_cache[$argString];
@@ -106,7 +107,8 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
     $argString = "CRM_CT_CI_{$group}_{$componentID}";
     if (!array_key_exists($argString, self::$_cache)) {
       $cache = CRM_Utils_Cache::singleton();
-      self::$_cache[$argString] = $cache->get(self::cleanKey($argString));
+      $cleanKey = self::cleanKey($argString);
+      self::$_cache[$argString] = $cache->get($cleanKey);
       if (!self::$_cache[$argString]) {
         $table = self::getTableName();
         $where = self::whereCache($group, NULL, $componentID);
@@ -119,7 +121,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
         $dao->free();
 
         self::$_cache[$argString] = $result;
-        $cache->set(self::cleanKey($argString), self::$_cache[$argString]);
+        $cache->set($cleanKey, self::$_cache[$argString]);
       }
     }
 
@@ -448,7 +450,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
       return $escape . md5($key);
     }
 
-    $r = preg_replace_callback(';[^A-Za-z0-9_\. ];', function($m) use ($escape) {
+    $r = preg_replace_callback(';[^A-Za-z0-9_\.];', function($m) use ($escape) {
       return $escape . dechex(ord($m[0]));
     }, $key);
 
index 358f260181e9b593e44d967613c5f26141bd5893..0ce1128c83ba2019c878996d45845742c7923f81 100644 (file)
@@ -160,6 +160,9 @@ class CRM_Utils_Cache {
    * @param array $params
    *   Array with keys:
    *   - name: string, unique symbolic name.
+   *     For a naming convention, use `snake_case` or `CamelCase` to maximize
+   *     portability/cleanliness. Any other punctuation or whitespace
+   *     should function correctly, but it can be harder to inspect/debug.
    *   - type: array|string, list of acceptable cache types, in order of preference.
    *   - prefetch: bool, whether to prefetch all data in cache (if possible).
    * @return CRM_Utils_Cache_Interface
@@ -169,6 +172,10 @@ class CRM_Utils_Cache {
   public static function create($params = array()) {
     $types = (array) $params['type'];
 
+    if (!empty($params['name'])) {
+      $params['name'] = CRM_Core_BAO_Cache::cleanKey($params['name']);
+    }
+
     foreach ($types as $type) {
       switch ($type) {
         case '*memory*':
index 900b96dc17e0c1da2cb1a8b34567f87cfbe55b04..51c4d537e5f19e781c238bd228ff5217cb9f5f37 100644 (file)
@@ -84,11 +84,14 @@ class CRM_Core_BAO_CacheTest extends CiviUnitTestCase {
 
   public function getCleanKeyExamples() {
     $es = [];
-    $es[] = ['hello_world and other.planets', 'hello_world and other.planets']; // allowed chars
+    $es[] = ['hello_world and/other.planets', 'hello_world-20and-2fother.planets']; // allowed chars
     $es[] = ['hello/world+-#@{}', 'hello-2fworld-2b-2d-23-40-7b-7d']; // escaped chars
-    $es[] = ['123456789 123456789 123456789 123456789 123456789 123456789 123', '123456789 123456789 123456789 123456789 123456789 123456789 123']; // long but allowed
-    $es[] = ['123456789 123456789 123456789 123456789 123456789 123456789 1234', '-2a008e182a4dcd1a78f405f30119e5f2']; // too long, md5 fallback
-    $es[] = ['123456789 /23456789 +23456789 -23456789 123456789 123456789', '-1b6baab5961431ed443ab321f5dfa0fb']; // too long, md5 fallback
+    $es[] = ["LF-\nTAB-\tCR-\remojiskullđź’€", 'LF-2d-aTAB-2d-9CR-2d-demojiskull-f0-9f-92-80']; // short with emoji
+    $es[] = ["LF-\nTAB-\tCR-\remojibombđź’Łemojiskullđź’€", '-5d9324e052f6e10240dce5029c5e8525']; // long with emoji
+    $es[] = ['123456789 123456789 123456789 123456789 123456789 123', '123456789-20123456789-20123456789-20123456789-20123456789-20123']; // spaces are escaped
+    $es[] = ['123456789_123456789_123456789_123456789_123456789_123456789_123', '123456789_123456789_123456789_123456789_123456789_123456789_123']; // long but allowed
+    $es[] = ['123456789_123456789_123456789_123456789_123456789_123456789_1234', '-e02b981aff954fdcc9a81c25f5ec9681']; // too long, md5 fallback
+    $es[] = ['123456789-/23456789-+23456789--23456789_123456789_123456789', '-43b6dec1026187ae6f6a8fe4d56ab22e']; // too long, md5 fallback
     return $es;
   }