Fix the style preview images
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 11 Jan 2021 08:44:05 +0000 (09:44 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 11 Jan 2021 08:48:09 +0000 (09:48 +0100)
The update 5.3.1 -> 5.3.2 detached all the style preview images in the
database, while leaving the actual image files in the file system. This new
update script *should* fix this situation again, by scanning the asset folder
and reattaching the newest image.

com.woltlab.wcf/package.xml
wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.2_style.php [deleted file]
wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.3_style.php [new file with mode: 0644]

index c01670c3351ca0e8d365c4be8da9ed0d66274ec8..5a71c03d0ab25a361ed63f132b5d73b6c10bac7f 100644 (file)
                <instruction type="script">acp/update_com.woltlab.wcf_5.3_packageServer.php</instruction>
        </instructions>
        
-       <!-- Run update_com.woltlab.wcf_5.3.2_style.php for 5.3.1 -> 5.3.2 -->
-       <!-- Add a note about the updated CSS in email_html.tpl to the release notes:
-               https://github.com/WoltLab/WCF/commit/1ac55ad408d5bfab4f69714c6259cc5d09ceb344
-               Administrators are highly encouraged to apply the CSS adjustments to their custom template version.
-       -->
-
-       <instructions type="update" fromversion="5.3.1">
-               <instruction type="acpTemplate">acptemplates_update.tar</instruction>
-               <instruction type="file">files_update.tar</instruction>
-               <instruction type="template">templates_update.tar</instruction>
-
-               <instruction type="language" />
-
-               <instruction type="userNotificationEvent" />
-               
-               <instruction type="script" run="standalone">acp/update_com.woltlab.wcf_5.3.2_style.php</instruction>
-       </instructions>
+       <!-- Run update_com.woltlab.wcf_5.3.3_style.php for 5.3.2 -> 5.3.3 -->
 </package>
diff --git a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.2_style.php b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.2_style.php
deleted file mode 100644 (file)
index 7fa8fa9..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-<?php
-
-use wcf\data\style\StyleEditor;
-use wcf\data\style\StyleList;
-use wcf\util\FileUtil;
-
-/**
- * @author     Tim Duesterhus
- * @copyright  2001-2020 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @package    WoltLabSuite\Core
- */
-
-$styleList = new StyleList();
-$styleList->readObjects();
-
-foreach ($styleList as $style) {
-       $styleEditor = new StyleEditor($style);
-       
-       // Fix the style preview.
-       if (
-               $style->image === FileUtil::getRelativePath(WCF_DIR.'images/', $style->getAssetPath()) ||
-               !is_file($style->image)
-       ) {
-               $styleEditor->update([
-                       'image' => '',
-               ]);
-       }
-       
-       if (
-               $style->image2x === FileUtil::getRelativePath(WCF_DIR.'images/', $style->getAssetPath()) ||
-               !is_file($style->image2x)
-       ) {
-               $styleEditor->update([
-                       'image2x' => '',
-               ]);
-       }
-}
diff --git a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.3_style.php b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.3_style.php
new file mode 100644 (file)
index 0000000..83213d6
--- /dev/null
@@ -0,0 +1,88 @@
+<?php
+
+use wcf\data\style\StyleEditor;
+use wcf\data\style\StyleList;
+use wcf\util\FileUtil;
+
+/**
+ * @author     Tim Duesterhus
+ * @copyright  2001-2020 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core
+ */
+
+$styleList = new StyleList();
+$styleList->readObjects();
+
+foreach ($styleList as $style) {
+       $styleEditor = new StyleEditor($style);
+       
+       // Fix the style preview.
+       if (!$style->image) {
+               $basename = 'stylePreview';
+               
+               // Check all possible extensions for preview images on the file system.
+               $files = [];
+               foreach ([
+                       'png',
+                       'gif',
+                       'jpg',
+                       'jpeg',
+                       'svg',
+               ] as $extension) {
+                       $fileName = $style->getAssetPath().$basename.'.'.$extension;
+                       if (is_readable($fileName)) {
+                               $files[$extension] = filemtime($fileName);
+                       }
+               }
+               
+               // Sort by modification time in descending order.
+               arsort($files);
+               
+               if (!empty($files)) {
+                       // This loop will pick the newest file first.
+                       foreach ($files as $extension => $unused) {
+                               $newName = $basename.'.'.$extension;
+                               
+                               $styleEditor->update([
+                                       'image' => FileUtil::getRelativePath(WCF_DIR.'images/', $style->getAssetPath()).$newName,
+                               ]);
+                               
+                               // break after handling the newest file, simulating
+                               // array_key_first().
+                               break;
+                       }
+               }
+       }
+       
+       if (!$style->image2x) {
+               $basename = 'stylePreview@2x';
+               
+               $files = [];
+               foreach ([
+                       'png',
+                       'gif',
+                       'jpg',
+                       'jpeg',
+                       'svg',
+               ] as $extension) {
+                       $fileName = $style->getAssetPath().$basename.'.'.$extension;
+                       if (is_readable($fileName)) {
+                               $files[$extension] = filemtime($fileName);
+                       }
+               }
+               arsort($files);
+               
+               if (!empty($files)) {
+                       foreach ($files as $extension => $unused) {
+                               $newName = $basename.'.'.$extension;
+                               
+                               $styleEditor->update([
+                                       'image2x' => FileUtil::getRelativePath(WCF_DIR.'images/', $style->getAssetPath()).$newName,
+                               ]);
+                               
+                               break;
+                       }
+               }
+       }
+}