]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/Cloner.php
Added more complexity in an attempt to make ldap host failover fit
[bookstack] / app / Entities / Tools / Cloner.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Actions\Tag;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Entity;
10 use BookStack\Entities\Models\Page;
11 use BookStack\Entities\Repos\BookRepo;
12 use BookStack\Entities\Repos\ChapterRepo;
13 use BookStack\Entities\Repos\PageRepo;
14 use BookStack\Uploads\Image;
15 use BookStack\Uploads\ImageService;
16 use Illuminate\Http\UploadedFile;
17
18 class Cloner
19 {
20     protected PageRepo $pageRepo;
21     protected ChapterRepo $chapterRepo;
22     protected BookRepo $bookRepo;
23     protected ImageService $imageService;
24
25     public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
26     {
27         $this->pageRepo = $pageRepo;
28         $this->chapterRepo = $chapterRepo;
29         $this->bookRepo = $bookRepo;
30         $this->imageService = $imageService;
31     }
32
33     /**
34      * Clone the given page into the given parent using the provided name.
35      */
36     public function clonePage(Page $original, Entity $parent, string $newName): Page
37     {
38         $copyPage = $this->pageRepo->getNewDraftPage($parent);
39         $pageData = $this->entityToInputData($original);
40         $pageData['name'] = $newName;
41
42         return $this->pageRepo->publishDraft($copyPage, $pageData);
43     }
44
45     /**
46      * Clone the given page into the given parent using the provided name.
47      * Clones all child pages.
48      */
49     public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
50     {
51         $chapterDetails = $this->entityToInputData($original);
52         $chapterDetails['name'] = $newName;
53
54         $copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
55
56         if (userCan('page-create', $copyChapter)) {
57             /** @var Page $page */
58             foreach ($original->getVisiblePages() as $page) {
59                 $this->clonePage($page, $copyChapter, $page->name);
60             }
61         }
62
63         return $copyChapter;
64     }
65
66     /**
67      * Clone the given book.
68      * Clones all child chapters & pages.
69      */
70     public function cloneBook(Book $original, string $newName): Book
71     {
72         $bookDetails = $this->entityToInputData($original);
73         $bookDetails['name'] = $newName;
74
75         // Clone book
76         $copyBook = $this->bookRepo->create($bookDetails);
77
78         // Clone contents
79         $directChildren = $original->getDirectChildren();
80         foreach ($directChildren as $child) {
81             if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
82                 $this->cloneChapter($child, $copyBook, $child->name);
83             }
84
85             if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
86                 $this->clonePage($child, $copyBook, $child->name);
87             }
88         }
89
90         // Clone bookshelf relationships
91         /** @var Bookshelf $shelf */
92         foreach ($original->shelves as $shelf) {
93             if (userCan('bookshelf-update', $shelf)) {
94                 $shelf->appendBook($copyBook);
95             }
96         }
97
98         return $copyBook;
99     }
100
101     /**
102      * Convert an entity to a raw data array of input data.
103      *
104      * @return array<string, mixed>
105      */
106     public function entityToInputData(Entity $entity): array
107     {
108         $inputData = $entity->getAttributes();
109         $inputData['tags'] = $this->entityTagsToInputArray($entity);
110
111         // Add a cover to the data if existing on the original entity
112         if ($entity->cover instanceof Image) {
113             $uploadedFile = $this->imageToUploadedFile($entity->cover);
114             $inputData['image'] = $uploadedFile;
115         }
116
117         return $inputData;
118     }
119
120     /**
121      * Copy the permission settings from the source entity to the target entity.
122      */
123     public function copyEntityPermissions(Entity $sourceEntity, Entity $targetEntity): void
124     {
125         $permissions = $sourceEntity->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
126         $targetEntity->permissions()->delete();
127         $targetEntity->permissions()->createMany($permissions);
128         $targetEntity->rebuildPermissions();
129     }
130
131     /**
132      * Convert an image instance to an UploadedFile instance to mimic
133      * a file being uploaded.
134      */
135     protected function imageToUploadedFile(Image $image): ?UploadedFile
136     {
137         $imgData = $this->imageService->getImageData($image);
138         $tmpImgFilePath = tempnam(sys_get_temp_dir(), 'bs_cover_clone_');
139         file_put_contents($tmpImgFilePath, $imgData);
140
141         return new UploadedFile($tmpImgFilePath, basename($image->path));
142     }
143
144     /**
145      * Convert the tags on the given entity to the raw format
146      * that's used for incoming request data.
147      */
148     protected function entityTagsToInputArray(Entity $entity): array
149     {
150         $tags = [];
151
152         /** @var Tag $tag */
153         foreach ($entity->tags as $tag) {
154             $tags[] = ['name' => $tag->name, 'value' => $tag->value];
155         }
156
157         return $tags;
158     }
159 }