Add import of blog attachments (vB 5)
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 22 Sep 2020 13:23:08 +0000 (15:23 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 7 Oct 2020 07:47:14 +0000 (09:47 +0200)
files/lib/system/exporter/VB5xExporter.class.php

index b73366f5d3f2b991ab5acf2313f34752a0642357..c4c45f0b2e3cab17ff99508b9ff9c9d34ba9e754 100644 (file)
@@ -89,7 +89,8 @@ class VB5xExporter extends AbstractExporter {
                'com.woltlab.wcf.conversation.attachment' => 100,
                'com.woltlab.wbb.thread' => 200,
                'com.woltlab.wbb.attachment' => 100,
-               'com.woltlab.wbb.acl' => 50
+               'com.woltlab.wbb.acl' => 50,
+               'com.woltlab.blog.entry.attachment' => 100,
        ];
        
        /**
@@ -119,8 +120,8 @@ class VB5xExporter extends AbstractExporter {
                        'com.woltlab.wcf.smiley' => [],
                        
                        'com.woltlab.blog.entry' => [
-                       /*      'com.woltlab.blog.category',
-                               'com.woltlab.blog.entry.attachment',*/
+                       /*      'com.woltlab.blog.category',*/
+                               'com.woltlab.blog.entry.attachment',
                                'com.woltlab.blog.entry.comment',
                        /*      'com.woltlab.blog.entry.like'*/
                        ],
@@ -218,7 +219,7 @@ class VB5xExporter extends AbstractExporter {
                        $queue[] = 'com.woltlab.blog.blog';
                /*      if (in_array('com.woltlab.blog.category', $this->selectedData)) $queue[] = 'com.woltlab.blog.category';*/
                        $queue[] = 'com.woltlab.blog.entry';
-               /*      if (in_array('com.woltlab.blog.entry.attachment', $this->selectedData)) $queue[] = 'com.woltlab.blog.entry.attachment';*/
+                       if (in_array('com.woltlab.blog.entry.attachment', $this->selectedData)) $queue[] = 'com.woltlab.blog.entry.attachment';
                        if (in_array('com.woltlab.blog.entry.comment', $this->selectedData)) {
                                $queue[] = 'com.woltlab.blog.entry.comment';
                /*              $queue[] = 'com.woltlab.blog.entry.comment.response';*/
@@ -902,6 +903,76 @@ class VB5xExporter extends AbstractExporter {
                }
        }
        
+       /**
+        * Counts blog attachments.
+        */
+       public function countBlogAttachments() {
+               return $this->__getMaxID($this->databasePrefix."node", 'nodeid');
+       }
+       
+       /**
+        * Exports blog attachments.
+        *
+        * @param       integer         $offset
+        * @param       integer         $limit
+        */
+       public function exportBlogAttachments($offset, $limit) {
+               $sql = "SELECT          child.*, attach.*, filedata.*
+                       FROM            ".$this->databasePrefix."node child
+                       INNER JOIN      ".$this->databasePrefix."node parent
+                       ON              child.parentid = parent.nodeid
+                       INNER JOIN      ".$this->databasePrefix."node grandparent
+                       ON              parent.parentid = grandparent.nodeid
+                       INNER JOIN      ".$this->databasePrefix."attach attach
+                       ON              child.nodeid = attach.nodeid
+                       INNER JOIN      ".$this->databasePrefix."filedata filedata
+                       ON              attach.filedataid = filedata.filedataid
+                       
+                       INNER JOIN      (SELECT contenttypeid FROM ".$this->databasePrefix."contenttype WHERE class IN(?)) x
+                       ON              x.contenttypeid = grandparent.contenttypeid
+                       INNER JOIN      (SELECT contenttypeid FROM ".$this->databasePrefix."contenttype WHERE class = ?) y
+                       ON              y.contenttypeid = parent.contenttypeid
+                       INNER JOIN      (SELECT contenttypeid FROM ".$this->databasePrefix."contenttype WHERE class = ?) z
+                       ON              z.contenttypeid = child.contenttypeid
+                       
+                       WHERE           child.nodeid BETWEEN ? AND ?
+                       ORDER BY        child.nodeid ASC";
+               $statement = $this->database->prepareStatement($sql);
+               
+               $statement->execute(['Channel', 'Text', 'Attach', $offset + 1, $offset + $limit]);
+               while ($row = $statement->fetchArray()) {
+                       $file = null;
+                       
+                       try {
+                               switch ($this->readOption('attachfile')) {
+                                       case self::ATTACHFILE_DATABASE:
+                                               $file = FileUtil::getTemporaryFilename('attachment_');
+                                               file_put_contents($file, $row['filedata']);
+                                       break;
+                               }
+                               
+                               // unable to read file -> abort
+                               if (!is_file($file) || !is_readable($file)) continue;
+                               
+                               ImportHandler::getInstance()->getImporter('com.woltlab.blog.entry.attachment')->import($row['nodeid'], [
+                                       'objectID' => $row['parentid'],
+                                       'userID' => $row['userid'] ?: null,
+                                       'filename' => $row['filename'],
+                                       'downloads' => $row['counter'],
+                                       'uploadTime' => $row['dateline'],
+                                       'showOrder' => isset($row['displayOrder']) ? $row['displayOrder'] : 0
+                               ], ['fileLocation' => $file]);
+                               
+                               if ($this->readOption('attachfile') == self::ATTACHFILE_DATABASE) unlink($file);
+                       }
+                       catch (\Exception $e) {
+                               if ($this->readOption('attachfile') == self::ATTACHFILE_DATABASE && $file) @unlink($file);
+                       
+                               throw $e;
+                       }
+               }
+       }
+       
        /**
         * Counts blog comments.
         */