|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace wcf\system\wsdb\importer; |
| 4 | + |
| 5 | +use wcf\command\wsdb\record\SetRecordContent; |
| 6 | +use wcf\data\attachment\AttachmentAction; |
| 7 | +use wcf\data\file\FileEditor; |
| 8 | +use wcf\data\page\Page; |
| 9 | +use wcf\data\wsdb\record\Record; |
| 10 | +use wcf\data\wsdb\record\RecordAction; |
| 11 | +use wcf\system\attachment\AttachmentHandler; |
| 12 | +use wcf\system\html\input\HtmlInputProcessor; |
| 13 | +use wcf\system\importer\AbstractImporter; |
| 14 | +use wcf\system\importer\ImportHandler; |
| 15 | + |
| 16 | +final class LinkLinkImporter extends AbstractImporter |
| 17 | +{ |
| 18 | + #[\Override] |
| 19 | + public function import($oldID, array $data, array $additionalData = []): int |
| 20 | + { |
| 21 | + $databaseID = ImportHandler::getInstance()->getNewID('dev.hanashi.wsdb.links', 1); |
| 22 | + $newCategoryID = ImportHandler::getInstance()->getNewID('dev.hanashi.wsdb.links.category', $data['categoryID']); |
| 23 | + if (!$newCategoryID) { |
| 24 | + return 0; |
| 25 | + } |
| 26 | + |
| 27 | + $contentData = [ |
| 28 | + 'title' => $data['subject'], |
| 29 | + 'teaser' => '', |
| 30 | + 'description' => $data['message'], |
| 31 | + 'tags' => $additionalData['tags'], |
| 32 | + ]; |
| 33 | + |
| 34 | + $action = new RecordAction([], 'create', [ |
| 35 | + 'data' => [ |
| 36 | + 'databaseID' => $databaseID, |
| 37 | + 'categoryID' => $newCategoryID, |
| 38 | + 'time' => $data['time'], |
| 39 | + 'userID' => $data['userID'], |
| 40 | + 'username' => $data['username'], |
| 41 | + 'isDisabled' => $data['isDisabled'], |
| 42 | + 'comments' => $data['comments'], |
| 43 | + 'coverPhotoID' => $this->getCoverPhotoID($data['imageFile']), |
| 44 | + 'externalURL' => $this->getLink($data), |
| 45 | + ], |
| 46 | + 'content' => [ |
| 47 | + 0 => $contentData, |
| 48 | + ], |
| 49 | + ]); |
| 50 | + $record = $action->executeAction()['returnValues']; |
| 51 | + \assert($record instanceof Record); |
| 52 | + |
| 53 | + ImportHandler::getInstance()->saveNewID('dev.hanashi.wsdb.links.links', $oldID, $record->recordID); |
| 54 | + |
| 55 | + if ($data['attachments']) { |
| 56 | + try { |
| 57 | + $action = new AttachmentAction([], 'copy', [ |
| 58 | + 'sourceObjectType' => 'de.pehbeh.links.linkEntry', |
| 59 | + 'targetObjectType' => 'com.woltlab.wsdb.record', |
| 60 | + 'sourceObjectID' => $oldID, |
| 61 | + 'targetObjectID' => $record->recordID, |
| 62 | + ]); |
| 63 | + $action->executeAction()['returnValues']; |
| 64 | + |
| 65 | + $attachmentHandler = new AttachmentHandler('com.woltlab.wsdb.record', $record->recordID); |
| 66 | + $contentData['attachmentHandler'] = $attachmentHandler; |
| 67 | + } catch (\Throwable) { |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + $htmlInputProcessor = new HtmlInputProcessor(); |
| 72 | + $htmlInputProcessor->process($data['message'], 'com.woltlab.wsdb.record', $record->recordID); |
| 73 | + $contentData['htmlInputProcessor'] = $htmlInputProcessor; |
| 74 | + |
| 75 | + (new SetRecordContent($record, [0 => $contentData]))(); |
| 76 | + |
| 77 | + return $record->recordID; |
| 78 | + } |
| 79 | + |
| 80 | + private function getCoverPhotoID(?string $imageFile): ?int |
| 81 | + { |
| 82 | + if (!$imageFile) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + $pathname = WCF_DIR . 'images/links/' . $imageFile; |
| 87 | + if (!\file_exists($pathname)) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + $file = FileEditor::createFromExistingFile($pathname, $imageFile, 'com.woltlab.wsdb.coverPhoto', true); |
| 92 | + if ($file === null) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + return $file->fileID; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param array<mixed> $data |
| 101 | + */ |
| 102 | + private function getLink(array $data): string |
| 103 | + { |
| 104 | + if (empty($data['pageID'])) { |
| 105 | + return $data['url']; |
| 106 | + } |
| 107 | + |
| 108 | + $page = new Page($data['pageID']); |
| 109 | + |
| 110 | + return $page->getLink(); |
| 111 | + } |
| 112 | +} |
0 commit comments