Export cover photos for entries
authorCyperghost <olaf_schmitz_1@t-online.de>
Tue, 19 Dec 2023 11:58:31 +0000 (12:58 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Tue, 19 Dec 2023 11:58:31 +0000 (12:58 +0100)
files/lib/system/exporter/WBB4xExporter.class.php

index e82b3f11ca32e540215915bb87e396a2cee440a5..1750db52cc4f54a9b00820e1f1746075bc9bfd1f 100644 (file)
@@ -2058,8 +2058,7 @@ final class WBB4xExporter extends AbstractExporter
                 $additionalData['languageCode'] = $row['languageCode'];
             }
             if ($sourceVersion52 && $destVersion52 && $row['coverPhotoID']) {
-                $directory = \substr($row["fileHash"], 0, 2);
-                $additionalData['coverPhoto'] = "{$blogFilePath}images/coverPhotos/{$directory}/{$row["coverPhotoID"]}-{$row["fileHash"]}.{$row["fileExtension"]}";
+                $additionalData['coverPhoto'] = $this->getCoverPhotoPath($blogFilePath, $row);
             }
 
             ImportHandler::getInstance()
@@ -2122,6 +2121,24 @@ final class WBB4xExporter extends AbstractExporter
             '2.1.0 Alpha 1',
             '>='
         );
+        $sourceVersion52 = \version_compare(
+            $this->getPackageVersion('com.woltlab.blog'),
+            '5.2.0 Alpha 1',
+            '>='
+        );
+        $destVersion52 = \version_compare(
+            BLOGCore::getInstance()->getPackage()->packageVersion,
+            '5.2.0 Alpha 1',
+            '>='
+        );
+
+        $sql = "SELECT  packageDir
+                FROM    wcf" . $this->dbNo . "_package
+                WHERE   package = ?";
+        $statement = $this->database->prepareStatement($sql, 1);
+        $statement->execute(['com.woltlab.blog']);
+        $packageDir = $statement->fetchColumn();
+        $blogFilePath = FileUtil::getRealPath($this->fileSystemPath . '/' . $packageDir);
 
         // get entry ids
         $entryIDs = [];
@@ -2163,11 +2180,21 @@ final class WBB4xExporter extends AbstractExporter
         $conditionBuilder = new PreparedStatementConditionBuilder();
         $conditionBuilder->add('entry.entryID IN (?)', [$entryIDs]);
 
-        $sql = "SELECT      entry.*, language.languageCode
+        if ($sourceVersion52 && $destVersion52) {
+            $sql = "SELECT      entry.*, language.languageCode, coverPhoto.fileExtension, coverPhoto.fileHash
+                FROM        blog" . $this->dbNo . "_entry entry
+                LEFT JOIN   wcf" . $this->dbNo . "_language language
+                ON          language.languageID = entry.languageID
+                LEFT JOIN   blog" . $this->dbNo . "_cover_photo coverPhoto
+                ON          entry.coverPhotoID = entry.coverPhotoID
+                " . $conditionBuilder;
+        } else {
+            $sql = "SELECT      entry.*, language.languageCode
                 FROM        blog" . $this->dbNo . "_entry entry
                 LEFT JOIN   wcf" . $this->dbNo . "_language language
                 ON          language.languageID = entry.languageID
                 " . $conditionBuilder;
+        }
         $statement = $this->database->prepareStatement($sql);
         $statement->execute($conditionBuilder->getParameters());
         while ($row = $statement->fetchArray()) {
@@ -2181,6 +2208,9 @@ final class WBB4xExporter extends AbstractExporter
             if (isset($categories[$row['entryID']])) {
                 $additionalData['categories'] = $categories[$row['entryID']];
             }
+            if ($sourceVersion52 && $destVersion52 && $row['coverPhotoID']) {
+                $additionalData['coverPhoto'] = $this->getCoverPhotoPath($blogFilePath, $row);
+            }
 
             $data = [
                 'userID' => $row['userID'],
@@ -2215,6 +2245,16 @@ final class WBB4xExporter extends AbstractExporter
         }
     }
 
+    private function getCoverPhotoPath(string $blogFilePath, array $row): string
+    {
+        $coverPhotoID = $row["coverPhotoID"];
+        $fileHash = $row["fileHash"];
+        $fileExtension = $row["fileExtension"];
+        $directory = \substr($fileHash, 0, 2);
+
+        return "{$blogFilePath}images/coverPhotos/$directory/$coverPhotoID-$fileHash.$fileExtension";
+    }
+
     /**
      * Counts blog attachments.
      */