Added importer for articles / media
authorMarcel Werk <burntime@woltlab.com>
Sun, 2 Oct 2016 14:29:43 +0000 (16:29 +0200)
committerMarcel Werk <burntime@woltlab.com>
Sun, 2 Oct 2016 14:29:50 +0000 (16:29 +0200)
com.woltlab.wcf/objectType.xml
wcfsetup/install/files/lib/system/importer/ArticleCategoryImporter.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/importer/ArticleCommentImporter.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/importer/ArticleCommentResponseImporter.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/importer/ArticleImporter.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/importer/MediaImporter.class.php [new file with mode: 0644]

index a967df4b5d52d3313647a8334f9501201f69f85d..83917747659dab45dcdc2c15605c43b093fdb204 100644 (file)
                        <definitionname>com.woltlab.wcf.importer</definitionname>
                        <classname>wcf\system\importer\SmileyCategoryImporter</classname>
                </type>
+               <type>
+                       <name>com.woltlab.wcf.article.category</name>
+                       <definitionname>com.woltlab.wcf.importer</definitionname>
+                       <classname>wcf\system\importer\ArticleCategoryImporter</classname>
+               </type>
+               <type>
+                       <name>com.woltlab.wcf.article</name>
+                       <definitionname>com.woltlab.wcf.importer</definitionname>
+                       <classname>wcf\system\importer\ArticleImporter</classname>
+               </type>
+               <type>
+                       <name>com.woltlab.wcf.media</name>
+                       <definitionname>com.woltlab.wcf.importer</definitionname>
+                       <classname>wcf\system\importer\MediaImporter</classname>
+               </type>
+               <type>
+                       <name>com.woltlab.wcf.article.comment</name>
+                       <definitionname>com.woltlab.wcf.importer</definitionname>
+                       <classname>wcf\system\importer\ArticleCommentImporter</classname>
+               </type>
+               <type>
+                       <name>com.woltlab.wcf.article.comment.response</name>
+                       <definitionname>com.woltlab.wcf.importer</definitionname>
+                       <classname>wcf\system\importer\ArticleCommentResponseImporter</classname>
+               </type>
                <!-- /importers -->
                
                <!-- rebuild data workers -->
diff --git a/wcfsetup/install/files/lib/system/importer/ArticleCategoryImporter.class.php b/wcfsetup/install/files/lib/system/importer/ArticleCategoryImporter.class.php
new file mode 100644 (file)
index 0000000..a0d8f17
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+namespace wcf\system\importer;
+use wcf\data\object\type\ObjectTypeCache;
+
+/**
+ * Imports article categories.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Importer
+ */
+class ArticleCategoryImporter extends AbstractCategoryImporter {
+       /**
+        * @inheritDoc
+        */
+       protected $objectTypeName = 'com.woltlab.wcf.article.category';
+       
+       /**
+        * Creates a new ArticleCategoryImporter object.
+        */
+       public function __construct() {
+               $objectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.category', 'com.woltlab.wcf.article.category');
+               $this->objectTypeID = $objectType->objectTypeID;
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/importer/ArticleCommentImporter.class.php b/wcfsetup/install/files/lib/system/importer/ArticleCommentImporter.class.php
new file mode 100644 (file)
index 0000000..040097d
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+namespace wcf\system\importer;
+use wcf\data\article\Article;
+use wcf\data\object\type\ObjectTypeCache;
+
+/**
+ * Imports article comments.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Importer
+ */
+class ArticleCommentImporter extends AbstractCommentImporter {
+       /**
+        * @inheritDoc
+        */
+       protected $objectTypeName = 'com.woltlab.wcf.article.comment';
+       
+       /**
+        * Creates a new ArticleCommentImporter object.
+        */
+       public function __construct() {
+               $objectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.comment.commentableContent', 'com.woltlab.wcf.articleComment');
+               $this->objectTypeID = $objectType->objectTypeID;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function import($oldID, array $data, array $additionalData = []) {
+               $articleID = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.article', $additionalData['articleID']);
+               if (!$articleID) return 0;
+               $article = new Article($articleID);
+               $contents = $article->getArticleContents();
+               $data['objectID'] = reset($contents)->articleContentID;
+               
+               return parent::import($oldID, $data);
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/importer/ArticleCommentResponseImporter.class.php b/wcfsetup/install/files/lib/system/importer/ArticleCommentResponseImporter.class.php
new file mode 100644 (file)
index 0000000..0cc57c8
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+namespace wcf\system\importer;
+
+/**
+ * Imports article comment response.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Importer
+ */
+class ArticleCommentResponseImporter extends AbstractCommentResponseImporter {
+       /**
+        * @inheritDoc
+        */
+       protected $objectTypeName = 'com.woltlab.wcf.article.comment';
+}
diff --git a/wcfsetup/install/files/lib/system/importer/ArticleImporter.class.php b/wcfsetup/install/files/lib/system/importer/ArticleImporter.class.php
new file mode 100644 (file)
index 0000000..d693737
--- /dev/null
@@ -0,0 +1,141 @@
+<?php
+namespace wcf\system\importer;
+use wcf\data\article\Article;
+use wcf\data\article\ArticleEditor;
+use wcf\data\article\content\ArticleContentEditor;
+use wcf\data\object\type\ObjectTypeCache;
+use wcf\system\language\LanguageFactory;
+use wcf\system\tagging\TagEngine;
+use wcf\system\WCF;
+
+/**
+ * Imports cms articles.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Importer
+ */
+class ArticleImporter extends AbstractImporter {
+       /**
+        * @inheritDoc
+        */
+       protected $className = Article::class;
+       
+       /**
+        * category for orphaned articles
+        * @var integer
+        */
+       private $importCategoryID = 0;
+       
+       /**
+        * @inheritDoc
+        */
+       public function import($oldID, array $data, array $additionalData = []) {
+               $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']);
+               
+               $contents = [];
+               foreach ($additionalData['contents'] as $languageCode => $contentData) {
+                       $languageID = 0;
+                       if ($languageCode) {
+                               if (($language = LanguageFactory::getInstance()->getLanguageByCode($languageCode)) !== null) {
+                                       $languageID = $language->languageID;
+                               }
+                               else {
+                                       continue;
+                               }
+                       }
+                       
+                       $imageID = null;
+                       if (!empty($contentData['imageID'])) {
+                               $imageID = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.media', $contentData['imageID']);
+                       }
+                       
+                       $contents[$languageID] = [
+                               'title' => (!empty($contentData['title']) ? $contentData['title'] : ''),
+                               'teaser' => (!empty($contentData['teaser']) ? $contentData['teaser'] : ''),
+                               'content' => (!empty($contentData['content']) ? $contentData['content'] : ''),
+                               'imageID' => $imageID,
+                               'tags' => (!empty($contentData['tags']) ? $contentData['tags'] : [])
+                               
+                       ];
+               }
+               if (empty($contents)) return 0;
+               if (count($contents) > 1) {
+                       $data['isMultilingual'] = 1;
+               }
+               
+               // check old id
+               if (is_numeric($oldID)) {
+                       $article = new Article($oldID);
+                       if (!$article->articleID) $data['articleID'] = $oldID;
+               }
+               
+               // category
+               $categoryID = 0;
+               if (!empty($data['categoryID'])) {
+                       $categoryID = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.article.category', $data['categoryID']);
+               }
+               if (!$categoryID) {
+                       $categoryID = $this->getImportCategoryID();
+               }
+               $data['categoryID'] = $categoryID;
+               
+               // save article
+               $article = ArticleEditor::create($data);
+               
+               // save article content
+               foreach ($contents as $languageID => $contentData) {
+                       $articleContent = ArticleContentEditor::create([
+                               'articleID' => $article->articleID,
+                               'languageID' => $languageID ?: null,
+                               'title' => $contentData['title'],
+                               'teaser' => $contentData['teaser'],
+                               'content' => $contentData['content'],
+                               'imageID' => $contentData['imageID']
+                       ]);
+                       
+                       // save tags
+                       if (!empty($contentData['tags'])) {
+                               TagEngine::getInstance()->addObjectTags('com.woltlab.wcf.article', $articleContent->articleContentID, $contentData['tags'], $languageID ?: LanguageFactory::getInstance()->getDefaultLanguageID());
+                       }
+               }
+               
+               ImportHandler::getInstance()->saveNewID('com.woltlab.wcf.article', $oldID, $article->articleID);
+               return $article->articleID;
+       }
+       
+       /**
+        * Returns the id of the category used for articles without previous categories.
+        * 
+        * @return      integer
+        */
+       private function getImportCategoryID() {
+               if (!$this->importCategoryID) {
+                       $objectTypeID = ObjectTypeCache::getInstance()->getObjectTypeIDByName('com.woltlab.wcf.category', 'com.woltlab.wcf.article.category');
+                       
+                       $sql = "SELECT          categoryID
+                               FROM            wcf".WCF_N."_category
+                               WHERE           objectTypeID = ?
+                                               AND parentCategoryID = ?
+                                               AND title = ?
+                               ORDER BY        categoryID";
+                       $statement = WCF::getDB()->prepareStatement($sql, 1);
+                       $statement->execute([$objectTypeID, 0, 'Import']);
+                       $categoryID = $statement->fetchSingleColumn();
+                       if ($categoryID) {
+                               $this->importCategoryID = $categoryID;
+                       }
+                       else {
+                               $sql = "INSERT INTO     wcf".WCF_N."_category
+                                                       (objectTypeID, parentCategoryID, title, showOrder, time)
+                                       VALUES          (?, ?, ?, ?, ?)";
+                               $statement = WCF::getDB()->prepareStatement($sql);
+                               $statement->execute([$objectTypeID, 0, 'Import', 0, TIME_NOW]);
+                               $this->importCategoryID = WCF::getDB()->getInsertID("wcf".WCF_N."_category", 'categoryID');
+                       }
+               }
+               
+               return $this->importCategoryID;
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/importer/MediaImporter.class.php b/wcfsetup/install/files/lib/system/importer/MediaImporter.class.php
new file mode 100644 (file)
index 0000000..48e2fc4
--- /dev/null
@@ -0,0 +1,123 @@
+<?php
+namespace wcf\system\importer;
+use wcf\data\media\Media;
+use wcf\data\media\MediaAction;
+use wcf\data\media\MediaEditor;
+use wcf\system\exception\SystemException;
+use wcf\system\language\LanguageFactory;
+use wcf\system\upload\DefaultUploadFileSaveStrategy;
+use wcf\system\WCF;
+
+/**
+ * Imports cms media.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Importer
+ */
+class MediaImporter extends AbstractImporter {
+       /**
+        * @inheritDoc
+        */
+       protected $className = Media::class;
+       
+       /**
+        * @var DefaultUploadFileSaveStrategy
+        */
+       private $saveStrategy;
+       
+       /**
+        * @inheritDoc
+        */
+       public function import($oldID, array $data, array $additionalData = []) {
+               $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']);
+               
+               $contents = [];
+               foreach ($additionalData['contents'] as $languageCode => $contentData) {
+                       $languageID = 0;
+                       if (!$languageCode) {
+                               if (($language = LanguageFactory::getInstance()->getLanguageByCode($languageCode)) !== null) {
+                                       $languageID = $language->languageID;
+                               }
+                               else {
+                                       continue;
+                               }
+                       }
+                       
+                       $contents[$languageID] = [
+                               'title' => (!empty($contentData['title']) ? $contentData['title'] : ''),
+                               'caption' => (!empty($contentData['caption']) ? $contentData['caption'] : ''),
+                               'altText' => (!empty($contentData['altText']) ? $contentData['altText'] : '')
+                       ];
+               }
+               if (count($contents) > 1) {
+                       $data['isMultilingual'] = 1;
+               }
+               
+               // handle language
+               if (!empty($additionalData['languageCode'])) {
+                       if (($language = LanguageFactory::getInstance()->getLanguageByCode($additionalData['languageCode'])) !== null) {
+                               $data['languageID'] = $language->languageID;
+                       }
+               }
+               
+               // check old id
+               if (is_numeric($oldID)) {
+                       $media = new Media($oldID);
+                       if (!$media->mediaID) $data['mediaID'] = $oldID;
+               }
+               
+               // save media
+               $media = MediaEditor::create($data);
+               
+               // check media directory
+               // and create subdirectory if necessary
+               $dir = dirname($media->getLocation());
+               if (!@file_exists($dir)) {
+                       @mkdir($dir, 0777);
+               }
+               
+               // copy file
+               try {
+                       if (!copy($additionalData['fileLocation'], $media->getLocation())) {
+                               throw new SystemException();
+                       }
+               }
+               catch (SystemException $e) {
+                       // copy failed; delete media
+                       $editor = new MediaEditor($media);
+                       $editor->delete();
+                       
+                       return 0;
+               }
+               
+               // save media content
+               $sql = "INSERT INTO     wcf".WCF_N."_media_content
+                                       (mediaID, languageID, title, caption, altText)
+                       VALUES          (?, ?, ?, ?, ?)";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               foreach ($contents as $languageID => $contentData) {
+                       $statement->execute([$media->mediaID, $languageID ?: null, $contentData['title'], $contentData['caption'], $contentData['altText']]);
+               }
+               
+               // create thumbnails
+               if ($media->isImage) {
+                       $this->getSaveStrategy()->generateThumbnails($media);
+               }
+               
+               ImportHandler::getInstance()->saveNewID('com.woltlab.wcf.media', $oldID, $media->mediaID);
+               return $media->mediaID;
+       }
+       
+       /**
+        * @return DefaultUploadFileSaveStrategy
+        */
+       private function getSaveStrategy() {
+               if ($this->saveStrategy === null) {
+                       $this->saveStrategy = new DefaultUploadFileSaveStrategy(MediaAction::class); 
+               }
+               
+               return $this->saveStrategy;
+       }
+}