2024-10-21 02:56:56 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exports;
|
|
|
|
|
|
|
|
use BookStack\App\Model;
|
2024-10-21 20:59:15 +08:00
|
|
|
use BookStack\Entities\Models\Page;
|
2024-10-21 02:56:56 +08:00
|
|
|
use BookStack\Exports\ZipExportModels\ZipExportAttachment;
|
2024-10-21 20:59:15 +08:00
|
|
|
use BookStack\Exports\ZipExportModels\ZipExportImage;
|
|
|
|
use BookStack\Exports\ZipExportModels\ZipExportModel;
|
2024-10-21 02:56:56 +08:00
|
|
|
use BookStack\Exports\ZipExportModels\ZipExportPage;
|
2024-10-21 20:59:15 +08:00
|
|
|
use BookStack\Uploads\Attachment;
|
|
|
|
use BookStack\Uploads\Image;
|
2024-10-21 02:56:56 +08:00
|
|
|
|
|
|
|
class ZipExportReferences
|
|
|
|
{
|
|
|
|
/** @var ZipExportPage[] */
|
|
|
|
protected array $pages = [];
|
|
|
|
protected array $books = [];
|
|
|
|
protected array $chapters = [];
|
|
|
|
|
|
|
|
/** @var ZipExportAttachment[] */
|
|
|
|
protected array $attachments = [];
|
|
|
|
|
2024-10-21 20:59:15 +08:00
|
|
|
/** @var ZipExportImage[] */
|
|
|
|
protected array $images = [];
|
|
|
|
|
2024-10-21 02:56:56 +08:00
|
|
|
public function __construct(
|
|
|
|
protected ZipReferenceParser $parser,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addPage(ZipExportPage $page): void
|
|
|
|
{
|
|
|
|
if ($page->id) {
|
|
|
|
$this->pages[$page->id] = $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($page->attachments as $attachment) {
|
|
|
|
if ($attachment->id) {
|
|
|
|
$this->attachments[$attachment->id] = $attachment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 20:59:15 +08:00
|
|
|
public function buildReferences(ZipExportFiles $files): void
|
2024-10-21 02:56:56 +08:00
|
|
|
{
|
|
|
|
// TODO - Parse page MD & HTML
|
|
|
|
foreach ($this->pages as $page) {
|
2024-10-21 20:59:15 +08:00
|
|
|
$page->html = $this->parser->parse($page->html ?? '', function (Model $model) use ($files, $page) {
|
|
|
|
return $this->handleModelReference($model, $page, $files);
|
2024-10-21 02:56:56 +08:00
|
|
|
});
|
|
|
|
// TODO - markdown
|
|
|
|
}
|
|
|
|
|
2024-10-21 19:13:41 +08:00
|
|
|
// dd('end');
|
2024-10-21 02:56:56 +08:00
|
|
|
// TODO - Parse chapter desc html
|
|
|
|
// TODO - Parse book desc html
|
|
|
|
}
|
2024-10-21 20:59:15 +08:00
|
|
|
|
|
|
|
protected function handleModelReference(Model $model, ZipExportModel $exportModel, ZipExportFiles $files): ?string
|
|
|
|
{
|
|
|
|
// TODO - References to other entities
|
|
|
|
|
|
|
|
// Handle attachment references
|
|
|
|
// No permission check needed here since they would only already exist in this
|
|
|
|
// reference context if already allowed via their entity access.
|
|
|
|
if ($model instanceof Attachment) {
|
|
|
|
if (isset($this->attachments[$model->id])) {
|
|
|
|
return "[[bsexport:attachment:{$model->id}]]";
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle image references
|
|
|
|
if ($model instanceof Image) {
|
|
|
|
// Only handle gallery and drawio images
|
|
|
|
if ($model->type !== 'gallery' && $model->type !== 'drawio') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't expect images to be part of book/chapter content
|
|
|
|
if (!($exportModel instanceof ZipExportPage)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$page = $model->getPage();
|
|
|
|
if ($page && userCan('view', $page)) {
|
|
|
|
if (!isset($this->images[$model->id])) {
|
|
|
|
$exportImage = ZipExportImage::fromModel($model, $files);
|
|
|
|
$this->images[$model->id] = $exportImage;
|
|
|
|
$exportModel->images[] = $exportImage;
|
|
|
|
}
|
|
|
|
return "[[bsexport:image:{$model->id}]]";
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2024-10-21 02:56:56 +08:00
|
|
|
}
|