Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/Db/BookmarkMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,10 @@ public function insert(Entity $entity): Bookmark {
return $entity;
} catch (Exception $e) {
if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
if ($this->db->inTransaction()) {
$this->db->commit();
$this->db->beginTransaction();
}
throw new AlreadyExistsError('A bookmark with this URL already exists');
}
throw $e;
Expand Down
31 changes: 12 additions & 19 deletions lib/Service/HtmlExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public function exportFolder(string $userId, int $folderId): string {
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>';

$file .= $this->serializeFolder($userId, $folderId, true);
$file .= "<DL><p>\n";
$file .= $this->serializeFolder($userId, $folderId);
$file .= "</DL><p>\n";

return $file;
}
Expand All @@ -84,14 +86,15 @@ public function exportFolder(string $userId, int $folderId): string {
* @throws MultipleObjectsReturnedException
* @throws UnauthorizedAccessError
*/
protected function serializeFolder(string $userId, int $id, bool $onlyContent = false): string {
if ($onlyContent) {
$output = '';
} else {
$folder = $this->folderMapper->find($id);
$output = '<DT><h3>' . htmlspecialchars($folder->getTitle()) . '</h3>' . "\n"
. '<DL><p>';
protected function serializeFolder(string $userId, int $id, string $indent = ''): string {
$output = '';
$nextIndent = $indent . ' ';
$childFolders = $this->treeMapper->findChildren(TreeMapper::TYPE_FOLDER, $id);
foreach ($childFolders as $childFolder) {
$output .= $indent . '<DT><H3>' . htmlspecialchars($childFolder->getTitle()) . '</H3>' . "\n";
$output .= $indent . '<DL><p>' . "\n" . $this->serializeFolder($userId, $childFolder->getId(), $nextIndent) . '</DL><p>' . "\n";
}

$childBookmarks = $this->treeMapper->findChildren(TreeMapper::TYPE_BOOKMARK, $id);
foreach ($childBookmarks as $bookmark) {
// discards records with no URL. This should not happen but
Expand All @@ -105,25 +108,15 @@ protected function serializeFolder(string $userId, int $id, bool $onlyContent =
$tags = Util::sanitizeHTML(implode(',', $tags));
$title = trim($bookmark->getTitle());
$url = Util::sanitizeHTML($bookmark->getUrl());
if ($title === '') {
$title = $url;
}
$title = Util::sanitizeHTML($title);
$description = Util::sanitizeHTML($bookmark->getDescription());

$output .= '<DT><A HREF="' . $url . '" TAGS="' . $tags . '" ADD_DATE="' . $bookmark->getAdded() . '">' . $title . '</A>' . "\n";
$output .= $indent . '<DT><A HREF="' . $url . '" TAGS="' . $tags . '" ADD_DATE="' . $bookmark->getAdded() . '">' . $title . '</A>' . "\n";
if ($description !== '') {
$output .= '<DD>' . $description . '</DD>';
}
$output .= "\n";
}

$childFolders = $this->treeMapper->findChildren(TreeMapper::TYPE_FOLDER, $id);
foreach ($childFolders as $childFolder) {
$output .= $this->serializeFolder($userId, $childFolder->getId());
}

$output .= '</p></DL>';
return $output;
}
}
35 changes: 27 additions & 8 deletions tests/HtmlImportExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function testExport(...$bookmarks): void {
// Set up database
for ($i = 0; $i < 4; $i++) {
$f = new Db\Folder();
$f->setTitle($i);
$f->setTitle(md5($i));
$f->setUserId($this->userId);
$f = $this->folderMapper->insert($f);
$this->treeMapper->move(Db\TreeMapper::TYPE_FOLDER, $f->getId(), $rootFolder->getId());
Expand All @@ -143,13 +143,32 @@ public function testExport(...$bookmarks): void {

$exported = $this->htmlExporter->exportFolder($this->userId, $rootFolder->getId());

$rootFolders = $this->treeMapper->findChildren(Db\TreeMapper::TYPE_FOLDER, $rootFolder->getId());
$this->assertCount(4, $rootFolders);
foreach ($rootFolders as $rootFolder) {
foreach ($this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $rootFolder->getId()) as $bookmark) {
$exportedRootFolders = $this->treeMapper->findChildren(Db\TreeMapper::TYPE_FOLDER, $rootFolder->getId());
$this->assertCount(4, $exportedRootFolders);
foreach ($exportedRootFolders as $exportedRootFolder) {
foreach ($this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $exportedRootFolder->getId()) as $bookmark) {
$this->assertStringContainsString($exportedRootFolder->getTitle(), $exported);
$this->assertStringContainsString($bookmark->getUrl(), $exported);
}
}

$f = new Db\Folder();
$f->setTitle('Testimport');
$f->setUserId($this->userId);
$f = $this->folderMapper->insert($f);
$this->htmlImporter->import($this->userId, $exported, $f->getId());
$importedRootFolders = $this->treeMapper->findChildren(Db\TreeMapper::TYPE_FOLDER, $f->getId());
foreach ($importedRootFolders as $i => $importedRootFolder) {
$exportedRootFolder = $exportedRootFolders[$i];
$this->assertEquals($importedRootFolder->getTitle(), $exportedRootFolder->getTitle());
$exportedBookmarks = $this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $exportedRootFolder->getId());
foreach ($this->treeMapper->findChildren(Db\TreeMapper::TYPE_BOOKMARK, $importedRootFolder->getId()) as $j => $bookmark) {
$this->assertEquals($bookmark->getUrl(), $exportedBookmarks[$j]->getUrl());
$this->assertEquals($bookmark->getTitle(), $exportedBookmarks[$j]->getTitle());
$this->assertEquals($bookmark->getDescription(), $exportedBookmarks[$j]->getDescription());
$this->assertEquals($this->tagMapper->findByBookmark($bookmark->getId()), $this->tagMapper->findByBookmark($exportedBookmarks[$j]->getId()));
}
}
}

public function importProvider(): array {
Expand All @@ -166,10 +185,10 @@ public function exportProvider(): array {
return Db\Bookmark::fromArray($props);
}, [
['url' => 'https://google.com/', 'title' => 'Google', 'description' => 'Search engine'],
['url' => 'https://nextcloud.com/', 'title' => 'Nextcloud', 'description' => ''],
['url' => 'https://nextcloud.com/', 'title' => 'Nextcloud', 'description' => 'cloud cloud cloud'],
['url' => 'https://php.net/', 'title' => '', 'description' => ''],
['url' => 'https://de.wikipedia.org/wiki/%C3%9C', 'title' => '', 'description' => ''],
['url' => 'https://github.com/nextcloud/bookmarks/projects/1', 'title' => '', 'description' => ''],
['url' => 'https://de.wikipedia.org/wiki/%C3%9C', 'title' => '', 'description' => '<H1>Hello</H1>'],
['url' => 'https://github.com/nextcloud/bookmarks/projects/1', 'title' => '', 'description' => '</DL>'],
]),
];
}
Expand Down
Loading