Skip to content

Commit d1ef738

Browse files
committed
import links
1 parent 165a234 commit d1ef738

5 files changed

Lines changed: 185 additions & 5 deletions

File tree

files/lib/system/wsdb/exporter/LinkDatabaseExporter.class.php

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ public function countPermissions(): int
138138

139139
public function exportPermissions(int $offset, int $limit): void
140140
{
141-
$sql = "SELECT *
142-
FROM wcf1_user_group";
141+
$sql = "SELECT *
142+
FROM wcf1_user_group
143+
ORDER BY groupID";
143144

144145
$statement = $this->database->prepare($sql, $limit, $offset);
145146
$statement->execute();
@@ -378,7 +379,7 @@ public function exportCategoryACLs(int $offset, int $limit): void
378379
AND acl_option.objectTypeID = ?
379380
)
380381
ORDER BY optionID, objectID, userID, groupID";
381-
$statement = $this->database->prepareUnmanaged($sql, $limit, $offset);
382+
$statement = $this->database->prepare($sql, $limit, $offset);
382383
$statement->execute([$objectType->objectTypeID, $objectType->objectTypeID]);
383384
while ($row = $statement->fetchArray()) {
384385
$acls = $aclMap[$row['optionName']] ?? [];
@@ -425,8 +426,9 @@ public function exportOptions(int $offset, int $limit): void
425426
$statement->execute();
426427
$categories = $statement->fetchMap('optionID', 'categoryID', false);
427428

428-
$sql = "SELECT *
429-
FROM wcf1_links_option";
429+
$sql = "SELECT *
430+
FROM wcf1_links_option
431+
ORDER BY optionID";
430432
$statement = $this->database->prepare($sql, $limit, $offset);
431433
$statement->execute();
432434

@@ -443,6 +445,65 @@ public function exportOptions(int $offset, int $limit): void
443445
}
444446
}
445447

448+
public function countLinks(): int
449+
{
450+
$sql = "SELECT COUNT(*)
451+
FROM wcf1_links";
452+
453+
$statement = $this->database->prepare($sql);
454+
$statement->execute();
455+
456+
return $statement->fetchSingleColumn();
457+
}
458+
459+
public function exportLinks(int $offset, int $limit): void
460+
{
461+
$sql = "SELECT *
462+
FROM wcf1_links
463+
ORDER BY linkID";
464+
$statement = $this->database->prepare($sql, $limit, $offset);
465+
$statement->execute();
466+
467+
$tags = $this->getLinkTags();
468+
469+
while ($row = $statement->fetchArray()) {
470+
ImportHandler::getInstance()
471+
->getImporter('dev.hanashi.wsdb.links.links')
472+
->import(
473+
$row['linkID'],
474+
$row,
475+
[
476+
'tags' => $tags[$row['linkID']] ?? [],
477+
]
478+
);
479+
}
480+
}
481+
482+
/**
483+
* @return array<int, string[]>
484+
*/
485+
private function getLinkTags(): array
486+
{
487+
$objectType = ObjectTypeCache::getInstance()->getObjectTypeByName(
488+
'com.woltlab.wcf.tagging.taggableObject',
489+
'de.pehbeh.links.linkEntry'
490+
);
491+
if ($objectType === null) {
492+
return [];
493+
}
494+
495+
$sql = "SELECT tag_to_object.objectID,
496+
tag.name
497+
FROM wcf1_tag tag
498+
INNER JOIN wcf1_tag_to_object tag_to_object
499+
ON tag_to_object.tagID = tag.tagID
500+
WHERE tag_to_object.objectTypeID";
501+
$statement = $this->database->prepare($sql);
502+
$statement->execute();
503+
504+
return $statement->fetchMap('objectID', 'name', false);
505+
}
506+
446507
/**
447508
* @param string[] $i18nValues
448509
* @return string[][][]
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

language/de.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<item name="wcf.acp.dataImport.data.dev.hanashi.wsdb.links.category"><![CDATA[Kategorien]]></item>
2222
<item name="wcf.acp.dataImport.data.dev.hanashi.wsdb.links.category.acl"><![CDATA[Kategorie-Berechtigungen]]></item>
2323
<item name="wcf.acp.dataImport.data.dev.hanashi.wsdb.links.option"><![CDATA[Eingabefelder]]></item>
24+
<item name="wcf.acp.dataImport.data.dev.hanashi.wsdb.links.links"><![CDATA[Links]]></item>
2425
</category>
2526
</import>
2627
</language>

language/en.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<item name="wcf.acp.dataImport.data.dev.hanashi.wsdb.links.category"><![CDATA[Categories]]></item>
2222
<item name="wcf.acp.dataImport.data.dev.hanashi.wsdb.links.category.acl"><![CDATA[Category permissions]]></item>
2323
<item name="wcf.acp.dataImport.data.dev.hanashi.wsdb.links.option"><![CDATA[Input options]]></item>
24+
<item name="wcf.acp.dataImport.data.dev.hanashi.wsdb.links.links"><![CDATA[Links]]></item>
2425
</category>
2526
</import>
2627
</language>

objectType.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,10 @@
3131
<definitionname>com.woltlab.wcf.importer</definitionname>
3232
<classname>wcf\system\wsdb\importer\LinkOptionsImporter</classname>
3333
</type>
34+
<type>
35+
<name>dev.hanashi.wsdb.links.links</name>
36+
<definitionname>com.woltlab.wcf.importer</definitionname>
37+
<classname>wcf\system\wsdb\importer\LinkLinkImporter</classname>
38+
</type>
3439
</import>
3540
</data>

0 commit comments

Comments
 (0)