3 namespace BookStack\Exports\ZipExports;
5 use BookStack\App\Model;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Repos\BaseRepo;
11 use BookStack\Entities\Repos\PageRepo;
12 use BookStack\Exports\ZipExports\Models\ZipExportBook;
13 use BookStack\Exports\ZipExports\Models\ZipExportChapter;
14 use BookStack\Exports\ZipExports\Models\ZipExportPage;
15 use BookStack\Uploads\Attachment;
16 use BookStack\Uploads\Image;
17 use BookStack\Uploads\ImageResizer;
19 class ZipImportReferences
22 protected array $pages = [];
24 protected array $chapters = [];
26 protected array $books = [];
27 /** @var Attachment[] */
28 protected array $attachments = [];
30 protected array $images = [];
32 /** @var array<string, Model> */
33 protected array $referenceMap = [];
35 /** @var array<int, ZipExportPage> */
36 protected array $zipExportPageMap = [];
37 /** @var array<int, ZipExportChapter> */
38 protected array $zipExportChapterMap = [];
39 /** @var array<int, ZipExportBook> */
40 protected array $zipExportBookMap = [];
42 public function __construct(
43 protected ZipReferenceParser $parser,
44 protected BaseRepo $baseRepo,
45 protected PageRepo $pageRepo,
46 protected ImageResizer $imageResizer,
50 protected function addReference(string $type, Model $model, ?int $importId): void
53 $key = $type . ':' . $importId;
54 $this->referenceMap[$key] = $model;
58 public function addPage(Page $page, ZipExportPage $exportPage): void
60 $this->pages[] = $page;
61 $this->zipExportPageMap[$page->id] = $exportPage;
62 $this->addReference('page', $page, $exportPage->id);
65 public function addChapter(Chapter $chapter, ZipExportChapter $exportChapter): void
67 $this->chapters[] = $chapter;
68 $this->zipExportChapterMap[$chapter->id] = $exportChapter;
69 $this->addReference('chapter', $chapter, $exportChapter->id);
72 public function addBook(Book $book, ZipExportBook $exportBook): void
74 $this->books[] = $book;
75 $this->zipExportBookMap[$book->id] = $exportBook;
76 $this->addReference('book', $book, $exportBook->id);
79 public function addAttachment(Attachment $attachment, ?int $importId): void
81 $this->attachments[] = $attachment;
82 $this->addReference('attachment', $attachment, $importId);
85 public function addImage(Image $image, ?int $importId): void
87 $this->images[] = $image;
88 $this->addReference('image', $image, $importId);
91 protected function handleReference(string $type, int $id): ?string
93 $key = $type . ':' . $id;
94 $model = $this->referenceMap[$key] ?? null;
95 if ($model instanceof Entity) {
96 return $model->getUrl();
97 } else if ($model instanceof Image) {
98 if ($model->type === 'gallery') {
99 $this->imageResizer->loadGalleryThumbnailsForImage($model, false);
100 return $model->thumbs['gallery'] ?? $model->url;
109 public function replaceReferences(): void
111 foreach ($this->books as $book) {
112 $exportBook = $this->zipExportBookMap[$book->id];
113 $content = $exportBook->description_html ?? '';
114 $parsed = $this->parser->parseReferences($content, $this->handleReference(...));
116 $this->baseRepo->update($book, [
117 'description_html' => $parsed,
121 foreach ($this->chapters as $chapter) {
122 $exportChapter = $this->zipExportChapterMap[$chapter->id];
123 $content = $exportChapter->description_html ?? '';
124 $parsed = $this->parser->parseReferences($content, $this->handleReference(...));
126 $this->baseRepo->update($chapter, [
127 'description_html' => $parsed,
131 foreach ($this->pages as $page) {
132 $exportPage = $this->zipExportPageMap[$page->id];
133 $contentType = $exportPage->markdown ? 'markdown' : 'html';
134 $content = $exportPage->markdown ?: ($exportPage->html ?: '');
135 $parsed = $this->parser->parseReferences($content, $this->handleReference(...));
137 $this->pageRepo->setContentFromInput($page, [
138 $contentType => $parsed,