From: Marcel Werk Date: Sun, 2 Oct 2016 14:29:43 +0000 (+0200) Subject: Added importer for articles / media X-Git-Tag: 3.0.0_Beta_2~14 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=44f9dba4c98359b53679cdac3bb43982e716a7e1;p=GitHub%2FWoltLab%2FWCF.git Added importer for articles / media --- diff --git a/com.woltlab.wcf/objectType.xml b/com.woltlab.wcf/objectType.xml index a967df4b5d..8391774765 100644 --- a/com.woltlab.wcf/objectType.xml +++ b/com.woltlab.wcf/objectType.xml @@ -304,6 +304,31 @@ com.woltlab.wcf.importer wcf\system\importer\SmileyCategoryImporter + + com.woltlab.wcf.article.category + com.woltlab.wcf.importer + wcf\system\importer\ArticleCategoryImporter + + + com.woltlab.wcf.article + com.woltlab.wcf.importer + wcf\system\importer\ArticleImporter + + + com.woltlab.wcf.media + com.woltlab.wcf.importer + wcf\system\importer\MediaImporter + + + com.woltlab.wcf.article.comment + com.woltlab.wcf.importer + wcf\system\importer\ArticleCommentImporter + + + com.woltlab.wcf.article.comment.response + com.woltlab.wcf.importer + wcf\system\importer\ArticleCommentResponseImporter + 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 index 0000000000..a0d8f1718d --- /dev/null +++ b/wcfsetup/install/files/lib/system/importer/ArticleCategoryImporter.class.php @@ -0,0 +1,26 @@ + + * @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 index 0000000000..040097da70 --- /dev/null +++ b/wcfsetup/install/files/lib/system/importer/ArticleCommentImporter.class.php @@ -0,0 +1,40 @@ + + * @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 index 0000000000..0cc57c8cce --- /dev/null +++ b/wcfsetup/install/files/lib/system/importer/ArticleCommentResponseImporter.class.php @@ -0,0 +1,17 @@ + + * @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 index 0000000000..d693737aa3 --- /dev/null +++ b/wcfsetup/install/files/lib/system/importer/ArticleImporter.class.php @@ -0,0 +1,141 @@ + + * @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 index 0000000000..48e2fc46fc --- /dev/null +++ b/wcfsetup/install/files/lib/system/importer/MediaImporter.class.php @@ -0,0 +1,123 @@ + + * @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; + } +}