mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-10-26 17:31:27 +03:00
As per PR #5800 * DB: Planned out new entity table format via migrations * DB: Created entity migration logic Made some other tweaks/fixes while testing. * DB: Added change of entity relation columns to suit new entities table * DB: Got most view queries working for new structure * Entities: Started logic change to new structure Updated base entity class, and worked through BaseRepo. Need to go through other repos next. Removed a couple of redundant interfaces as part of this since we can move the logic onto the shared ContainerData model as needed. * Entities: Been through repos to update for new format * Entities: Updated repos to act on refreshed clones Changes to core entity models are now done on clones to ensure clean state before save, and those clones are returned back if changes are needed after that action. * Entities: Updated model classes & relations for changes * Entities: Changed from *Data to a common "contents" system Added smart loading from builder instances which should hydrate with "contents()" loaded via join, while keeping the core model original. * Entities: Moved entity description/covers to own non-model classes Added back some interfaces. * Entities: Removed use of contents system for data access * Entities: Got most queries back to working order * Entities: Reverted back to data from contents, fixed various issues * Entities: Started addressing issues from tests * Entities: Addressed further tests/issues * Entities: Been through tests to get all passing in dev Fixed issues and needed test changes along the way. * Entities: Addressed phpstan errors * Entities: Reviewed TODO notes * Entities: Ensured book/shelf relation data removed on destroy * Entities: Been through API responses & adjusted field visibility * Entities: Added type index to massively improve query speed
159 lines
4.9 KiB
PHP
159 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Entities\Tools;
|
|
|
|
use BookStack\Activity\Models\Tag;
|
|
use BookStack\Entities\Models\Book;
|
|
use BookStack\Entities\Models\Bookshelf;
|
|
use BookStack\Entities\Models\Chapter;
|
|
use BookStack\Entities\Models\HasCoverInterface;
|
|
use BookStack\Entities\Models\Entity;
|
|
use BookStack\Entities\Models\Page;
|
|
use BookStack\Entities\Repos\BookRepo;
|
|
use BookStack\Entities\Repos\ChapterRepo;
|
|
use BookStack\Entities\Repos\PageRepo;
|
|
use BookStack\Permissions\Permission;
|
|
use BookStack\Uploads\Image;
|
|
use BookStack\Uploads\ImageService;
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
class Cloner
|
|
{
|
|
public function __construct(
|
|
protected PageRepo $pageRepo,
|
|
protected ChapterRepo $chapterRepo,
|
|
protected BookRepo $bookRepo,
|
|
protected ImageService $imageService,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Clone the given page into the given parent using the provided name.
|
|
*/
|
|
public function clonePage(Page $original, Entity $parent, string $newName): Page
|
|
{
|
|
$copyPage = $this->pageRepo->getNewDraftPage($parent);
|
|
$pageData = $this->entityToInputData($original);
|
|
$pageData['name'] = $newName;
|
|
|
|
return $this->pageRepo->publishDraft($copyPage, $pageData);
|
|
}
|
|
|
|
/**
|
|
* Clone the given page into the given parent using the provided name.
|
|
* Clones all child pages.
|
|
*/
|
|
public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
|
|
{
|
|
$chapterDetails = $this->entityToInputData($original);
|
|
$chapterDetails['name'] = $newName;
|
|
|
|
$copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
|
|
|
|
if (userCan(Permission::PageCreate, $copyChapter)) {
|
|
/** @var Page $page */
|
|
foreach ($original->getVisiblePages() as $page) {
|
|
$this->clonePage($page, $copyChapter, $page->name);
|
|
}
|
|
}
|
|
|
|
return $copyChapter;
|
|
}
|
|
|
|
/**
|
|
* Clone the given book.
|
|
* Clones all child chapters and pages.
|
|
*/
|
|
public function cloneBook(Book $original, string $newName): Book
|
|
{
|
|
$bookDetails = $this->entityToInputData($original);
|
|
$bookDetails['name'] = $newName;
|
|
|
|
// Clone book
|
|
$copyBook = $this->bookRepo->create($bookDetails);
|
|
|
|
// Clone contents
|
|
$directChildren = $original->getDirectVisibleChildren();
|
|
foreach ($directChildren as $child) {
|
|
if ($child instanceof Chapter && userCan(Permission::ChapterCreate, $copyBook)) {
|
|
$this->cloneChapter($child, $copyBook, $child->name);
|
|
}
|
|
|
|
if ($child instanceof Page && !$child->draft && userCan(Permission::PageCreate, $copyBook)) {
|
|
$this->clonePage($child, $copyBook, $child->name);
|
|
}
|
|
}
|
|
|
|
// Clone bookshelf relationships
|
|
/** @var Bookshelf $shelf */
|
|
foreach ($original->shelves as $shelf) {
|
|
if (userCan(Permission::BookshelfUpdate, $shelf)) {
|
|
$shelf->appendBook($copyBook);
|
|
}
|
|
}
|
|
|
|
return $copyBook;
|
|
}
|
|
|
|
/**
|
|
* Convert an entity to a raw data array of input data.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function entityToInputData(Entity $entity): array
|
|
{
|
|
$inputData = $entity->getAttributes();
|
|
$inputData['tags'] = $this->entityTagsToInputArray($entity);
|
|
|
|
// Add a cover to the data if existing on the original entity
|
|
if ($entity instanceof HasCoverInterface) {
|
|
$cover = $entity->coverInfo()->getImage();
|
|
if ($cover) {
|
|
$inputData['image'] = $this->imageToUploadedFile($cover);
|
|
}
|
|
}
|
|
|
|
return $inputData;
|
|
}
|
|
|
|
/**
|
|
* Copy the permission settings from the source entity to the target entity.
|
|
*/
|
|
public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
|
|
{
|
|
$permissions = $sourceEntity->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
|
|
$targetEntity->permissions()->delete();
|
|
$targetEntity->permissions()->createMany($permissions);
|
|
$targetEntity->rebuildPermissions();
|
|
}
|
|
|
|
/**
|
|
* Convert an image instance to an UploadedFile instance to mimic
|
|
* a file being uploaded.
|
|
*/
|
|
protected function imageToUploadedFile(Image $image): ?UploadedFile
|
|
{
|
|
$imgData = $this->imageService->getImageData($image);
|
|
$tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
|
|
file_put_contents($tmpImgFilePath, $imgData);
|
|
|
|
return new UploadedFile($tmpImgFilePath, basename($image->path));
|
|
}
|
|
|
|
/**
|
|
* Convert the tags on the given entity to the raw format
|
|
* that's used for incoming request data.
|
|
*/
|
|
protected function entityTagsToInputArray(Entity $entity): array
|
|
{
|
|
$tags = [];
|
|
|
|
/** @var Tag $tag */
|
|
foreach ($entity->tags as $tag) {
|
|
$tags[] = ['name' => $tag->name, 'value' => $tag->value];
|
|
}
|
|
|
|
return $tags;
|
|
}
|
|
}
|