Added attachment import
authorMarcel Werk <burntime@woltlab.com>
Wed, 3 Jul 2013 15:44:16 +0000 (17:44 +0200)
committerMarcel Werk <burntime@woltlab.com>
Wed, 3 Jul 2013 15:44:16 +0000 (17:44 +0200)
wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/importer/AbstractCommentImporter.class.php
wcfsetup/install/files/lib/system/importer/AbstractCommentResponseImporter.class.php
wcfsetup/install/files/lib/system/importer/ImportHandler.class.php

diff --git a/wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php b/wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php
new file mode 100644 (file)
index 0000000..fbf65f9
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+namespace wcf\system\importer;
+use wcf\data\attachment\AttachmentAction;
+use wcf\data\attachment\AttachmentEditor;
+use wcf\system\exception\SystemException;
+
+/**
+ * Imports attachments.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2013 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.importer
+ * @category   Community Framework
+ */
+class AbstractAttachmentImporter implements IImporter {
+       /**
+        * object type id for attachment
+        * @var integer
+        */
+       protected $objectTypeID = 0;
+       
+       /**
+        * @see wcf\system\importer\IImporter::import()
+        */
+       public function import($oldID, array $data) {
+               $fileLocation = $data['fileLocation'];
+               unset($data['fileLocation']);
+               
+               // check file location
+               if (!@file_exists($fileLocation)) return 0;
+               
+               // get file hash
+               if (empty($data['fileHash'])) $data['fileHash'] = sha1_file($fileLocation);
+               
+               // get user id
+               if ($data['userID']) $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']);
+               
+               // save attachment
+               $action = new AttachmentAction(array(), 'create', array(
+                       'data' => array_merge($data, array('objectTypeID' => $this->objectTypeID))              
+               ));
+               $returnValues = $action->executeAction();
+               $attachment = $returnValues['returnValues'];
+               
+               // check attachment directory
+               // and create subdirectory if necessary
+               $dir = dirname($attachment->getLocation());
+               if (!@file_exists($dir)) {
+                       @mkdir($dir, 0777);
+               }
+               
+               // copy file
+               try {
+                       if (!copy($fileLocation, $attachment->getLocation())) {
+                               throw new SystemException();
+                       }
+                               
+                       // create thumbnails
+                       if (ATTACHMENT_ENABLE_THUMBNAILS) {
+                               if ($attachment->isImage) {
+                                       $action = new AttachmentAction(array($attachment), 'generateThumbnails');
+                                       $action->executeAction();
+                               }
+                       }
+                       
+                       return $attachment->attachmentID;
+               }
+               catch (SystemException $e) {
+                       // copy failed; delete attachment
+                       $editor = new AttachmentEditor($attachment);
+                       $editor->delete();
+               }
+               
+               return 0;
+       }
+}
index 07c1e310148c7e82acab077cfc71dfc0177a0948..607ece88da5aa5bdba1c59fb00a9640b26063cc5 100644 (file)
@@ -30,7 +30,6 @@ class AbstractCommentImporter implements IImporter {
         */
        public function import($oldID, array $data) {
                if ($data['userID']) $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']);
-               if (!$data['userID']) $data['userID'] = null;
                
                $action = new CommentAction(array(), 'create', array(
                        'data' => array_merge($data, array('objectTypeID' => $this->objectTypeID))              
index b0d0fe72662da76b87834a550a3aedf02e347764..54f64209368bf60a03062569b93cca666e44908c 100644 (file)
@@ -24,7 +24,6 @@ class AbstractCommentResponseImporter implements IImporter {
         */
        public function import($oldID, array $data) {
                if ($data['userID']) $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']);
-               if (!$data['userID']) $data['userID'] = null;
                
                $data['commentID'] = ImportHandler::getInstance()->getNewID($this->objectTypeName, $data['commentID']);
                if (!$data['commentID']) return 0;
index e4614849509c4f014b6c6426c6297156e62f7899..8cd75a40596bee738622cf6cc153f909198d2736 100644 (file)
@@ -70,7 +70,7 @@ class ImportHandler extends SingletonFactory {
                $objectTypeID = $this->objectTypes[$type]->objectTypeID;
                
                if (!isset($this->idMappingCache[$objectTypeID][$oldID])) {
-                       $this->idMappingCache[$objectTypeID][$oldID] = 0;
+                       $this->idMappingCache[$objectTypeID][$oldID] = null;
                        
                        $sql = "SELECT  newID
                                FROM    wcf".WCF_N."_import_mapping