From b702fb2e82f814bf06d0cdba4a3d3b8269cff72c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 11 Jan 2021 09:44:05 +0100 Subject: [PATCH] Fix the style preview images 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 | 18 +--- .../update_com.woltlab.wcf_5.3.2_style.php | 38 -------- .../update_com.woltlab.wcf_5.3.3_style.php | 88 +++++++++++++++++++ 3 files changed, 89 insertions(+), 55 deletions(-) delete mode 100644 wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.2_style.php create mode 100644 wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.3_style.php diff --git a/com.woltlab.wcf/package.xml b/com.woltlab.wcf/package.xml index c01670c335..5a71c03d0a 100644 --- a/com.woltlab.wcf/package.xml +++ b/com.woltlab.wcf/package.xml @@ -106,21 +106,5 @@ acp/update_com.woltlab.wcf_5.3_packageServer.php - - - - - acptemplates_update.tar - files_update.tar - templates_update.tar - - - - - - acp/update_com.woltlab.wcf_5.3.2_style.php - + 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 index 7fa8fa9224..0000000000 --- a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.2_style.php +++ /dev/null @@ -1,38 +0,0 @@ - - * @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 index 0000000000..83213d63ce --- /dev/null +++ b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.3.3_style.php @@ -0,0 +1,88 @@ + + * @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; + } + } + } +} -- 2.20.1