Move all of the style deletion logic into StyleEditor
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 20 Oct 2021 08:50:31 +0000 (10:50 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 20 Oct 2021 09:08:26 +0000 (11:08 +0200)
This ensures that all the files on the filesystem are deleted no matter how the
style is deleted. Previously the style's image folder remained when
StyleEditor::delete() was used, for example within the style PIP.

wcfsetup/install/files/lib/data/style/StyleAction.class.php
wcfsetup/install/files/lib/data/style/StyleEditor.class.php

index acf6c7b4ca0d2334ddf875943ec727bad13530c8..a801ec94392b580499e762ff80344a480a6f4312 100644 (file)
@@ -127,35 +127,7 @@ class StyleAction extends AbstractDatabaseObjectAction implements IToggleAction
     }
 
     /**
-     * @inheritDoc
-     */
-    public function delete()
-    {
-        $count = parent::delete();
-
-        foreach ($this->getObjects() as $style) {
-            // remove custom images
-            if ($style->imagePath && $style->imagePath != 'images/') {
-                $this->removeDirectory($style->imagePath);
-            }
-
-            // remove preview image
-            $previewImage = WCF_DIR . 'images/' . $style->image;
-            if (\file_exists($previewImage)) {
-                @\unlink($previewImage);
-            }
-
-            // remove stylesheet
-            StyleHandler::getInstance()->resetStylesheet($style->getDecoratedObject());
-        }
-
-        return $count;
-    }
-
-    /**
-     * Recursively removes a directory and all it's contents.
-     *
-     * @param string $pathComponent
+     * @deprecated 5.4 This method is unused.
      */
     protected function removeDirectory($pathComponent)
     {
index 045f2fec16e46a7dd582bc69667c7bb7dee25b0e..082cca61aba6b3839b550775869df4abec6b4b7d 100644 (file)
@@ -88,18 +88,18 @@ class StyleEditor extends DatabaseObjectEditor implements IEditableCachedObject
      */
     public function delete()
     {
-        parent::delete();
+        $sql = "DELETE FROM wcf1_style
+                WHERE       styleID = ?";
+        $statement = WCF::getDB()->prepare($sql);
+        $statement->execute([$this->styleID]);
 
-        // delete style files
-        $files = @\glob(WCF_DIR . 'style/style-' . $this->styleID . '*.css');
-        if (\is_array($files)) {
-            foreach ($files as $file) {
-                @\unlink($file);
-            }
-        }
+        // delete CSS files
+        StyleHandler::getInstance()->resetStylesheet($this->getDecoratedObject());
 
-        // delete preload data
-        @\unlink(WCF_DIR . 'style/style-' . $this->styleID . '-preload.json');
+        // remove custom images
+        if ($this->imagePath && $this->imagePath != 'images/') {
+            $this->removeDirectory($this->imagePath);
+        }
 
         // delete preview image
         if ($this->image) {
@@ -113,6 +113,47 @@ class StyleEditor extends DatabaseObjectEditor implements IEditableCachedObject
         $statement->execute(['wcf.style.styleDescription' . $this->styleID]);
     }
 
+    /**
+     * @inheritDoc
+     */
+    public static function deleteAll(array $objectIDs = [])
+    {
+        $styleList = new StyleList();
+        $styleList->decoratorClassName = static::class;
+        $styleList->setObjectIDs($objectIDs);
+        $styleList->readObjects();
+
+        foreach ($styleList as $style) {
+            \assert($style instanceof self);
+            $style->delete();
+        }
+    }
+
+    /**
+     * Recursively removes a directory and all it's contents.
+     *
+     * @since 5.4
+     */
+    private function removeDirectory(string $pathComponent)
+    {
+        $dir = WCF_DIR . $pathComponent;
+        if (\is_dir($dir)) {
+            $iterator = new \RecursiveIteratorIterator(
+                new \RecursiveDirectoryIterator($dir),
+                \RecursiveIteratorIterator::CHILD_FIRST
+            );
+            foreach ($iterator as $path) {
+                if ($path->isDir()) {
+                    @\rmdir($path);
+                } else {
+                    @\unlink($path);
+                }
+            }
+
+            @\rmdir($dir);
+        }
+    }
+
     /**
      * Sets this style as default style.
      */