Fix image cache clearing with new style code.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 16 Sep 2010 21:25:51 +0000 (17:25 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 16 Sep 2010 21:25:51 +0000 (17:25 -0400)
Need to store all style hashes that the server knows about, so we can
be sure to delete all cached thumbnails from the VFS.

ansel/lib/Image.php
ansel/lib/Storage.php
ansel/lib/View/GalleryProperties.php

index 7c625f1..da77f7c 100644 (file)
@@ -753,24 +753,13 @@ class Ansel_Image Implements Iterator
             }
 
             if ($view == 'all' || $view == 'prettythumb') {
-
-                /* No need to try to delete a hash we already removed */
-                $deleted = array();
-
-                /* Need to generate hashes for each possible style */
-                // @TODO: Need to save the style hash to the share entry
-                //        since we don't require the use of predefined
-                //        style definitions now.
                 $styles = Horde::loadConfiguration('styles.php', 'styles', 'ansel');
-                foreach ($styles as $style)
+                $hashes = $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope()->getHashes();
+                foreach ($hashes as $hash)
                 {
-                    $hash =  md5($style['thumbstyle'] . '.' . $style['background']);
-                    if (empty($deleted[$hash]))
-                    {
-                        $GLOBALS['injector']->getInstance('Horde_Vfs')->getVfs('images')->deleteFile(
-                            $this->getVFSPath($hash), $this->getVFSName($hash));
-                        $deleted[$hash] = true;
-                    }
+                    $GLOBALS['injector']->getInstance('Horde_Vfs')
+                        ->getVfs('images')
+                        ->deleteFile($this->getVFSPath($hash), $this->getVFSName($hash));
                 }
             }
         } catch (VFS_Exception $e) {}
index e1da9d2..805302e 100644 (file)
@@ -1145,4 +1145,37 @@ class Ansel_Storage
         return implode(', ', $fields);
     }
 
+    /**
+     * Ensure the style hash is recorded in the database.
+     *
+     * @param string $hash  The hash to record.
+     *
+     * @return void
+     */
+    public function ensureHash($hash)
+    {
+        $query = $this->_db->prepare('SELECT COUNT(*) FROM ansel_hashes WHERE style_hash = ?');
+        $results = $query->execute($hash);
+        if ($results instanceof PEAR_Error) {
+            throw new Ansel_Exception($results->getMessage());
+        }
+        if (!$results->fetchOne()) {
+            $query = $this->_db->prepare('INSERT INTO ansel_hashes (style_hash) VALUES(?)');
+            $results = $query->execute($hash);
+            if ($results instanceof PEAR_Error) {
+                throw new Ansel_Exception($results->getMessage());
+            }
+        }
+    }
+
+    /**
+     * Get a list of all known styleHashes.
+     *
+     * @return array  An array of style hashes.
+     */
+    public function getHashes()
+    {
+        $hashes = $this->_db->query('SELECT style_hash FROM ansel_hashes;');
+    }
+
 }
index 067f61d..d94adab 100644 (file)
@@ -364,6 +364,11 @@ class Ansel_View_GalleryProperties
 
         }
 
+        // Make sure that the style hash is recorded, ignoring non-styled thumbs
+        if ($style->thumbstyle != 'Thumb') {
+            $GLOBALS['injector']->getInstance('Ansel_Storage')->getScope()->ensureHash($gallery->getViewHash('prettythumb'));
+        }
+
         // Clear the OtherGalleries widget cache
         if ($GLOBALS['conf']['ansel_cache']['usecache']) {
             $GLOBALS['injector']->getInstance('Horde_Cache')->expire('Ansel_OtherGalleries' . $gallery->get('owner'));