Skip to content

Commit 674bb84

Browse files
committed
Copying: Added reference change context tracking
Added core wiring in the cloning logic, just need to implement core logic in the updater now.
1 parent ba675b6 commit 674bb84

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

app/Entities/Tools/Cloner.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,62 @@
1313
use BookStack\Entities\Repos\ChapterRepo;
1414
use BookStack\Entities\Repos\PageRepo;
1515
use BookStack\Permissions\Permission;
16+
use BookStack\References\ReferenceChangeContext;
17+
use BookStack\References\ReferenceUpdater;
1618
use BookStack\Uploads\Image;
1719
use BookStack\Uploads\ImageService;
1820
use Illuminate\Http\UploadedFile;
1921

2022
class Cloner
2123
{
24+
protected ReferenceChangeContext $referenceChangeContext;
25+
2226
public function __construct(
2327
protected PageRepo $pageRepo,
2428
protected ChapterRepo $chapterRepo,
2529
protected BookRepo $bookRepo,
2630
protected ImageService $imageService,
31+
protected ReferenceUpdater $referenceUpdater,
2732
) {
33+
$this->referenceChangeContext = new ReferenceChangeContext();
2834
}
2935

3036
/**
3137
* Clone the given page into the given parent using the provided name.
3238
*/
3339
public function clonePage(Page $original, Entity $parent, string $newName): Page
40+
{
41+
$context = $this->newReferenceChangeContext();
42+
$page = $this->createPageClone($original, $parent, $newName);
43+
$this->referenceUpdater->changeReferencesUsingContext($context);
44+
return $page;
45+
}
46+
47+
protected function createPageClone(Page $original, Entity $parent, string $newName): Page
3448
{
3549
$copyPage = $this->pageRepo->getNewDraftPage($parent);
3650
$pageData = $this->entityToInputData($original);
3751
$pageData['name'] = $newName;
3852

39-
return $this->pageRepo->publishDraft($copyPage, $pageData);
53+
$newPage = $this->pageRepo->publishDraft($copyPage, $pageData);
54+
$this->referenceChangeContext->add($original, $newPage);
55+
56+
return $newPage;
4057
}
4158

4259
/**
4360
* Clone the given page into the given parent using the provided name.
4461
* Clones all child pages.
4562
*/
4663
public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
64+
{
65+
$context = $this->newReferenceChangeContext();
66+
$chapter = $this->createChapterClone($original, $parent, $newName);
67+
$this->referenceUpdater->changeReferencesUsingContext($context);
68+
return $chapter;
69+
}
70+
71+
protected function createChapterClone(Chapter $original, Book $parent, string $newName): Chapter
4772
{
4873
$chapterDetails = $this->entityToInputData($original);
4974
$chapterDetails['name'] = $newName;
@@ -65,6 +90,14 @@ public function cloneChapter(Chapter $original, Book $parent, string $newName):
6590
* Clones all child chapters and pages.
6691
*/
6792
public function cloneBook(Book $original, string $newName): Book
93+
{
94+
$context = $this->newReferenceChangeContext();
95+
$book = $this->createBookClone($original, $newName);
96+
$this->referenceUpdater->changeReferencesUsingContext($context);
97+
return $book;
98+
}
99+
100+
protected function createBookClone(Book $original, string $newName): Book
68101
{
69102
$bookDetails = $this->entityToInputData($original);
70103
$bookDetails['name'] = $newName;
@@ -155,4 +188,10 @@ protected function entityTagsToInputArray(Entity $entity): array
155188

156189
return $tags;
157190
}
191+
192+
protected function newReferenceChangeContext(): ReferenceChangeContext
193+
{
194+
$this->referenceChangeContext = new ReferenceChangeContext();
195+
return $this->referenceChangeContext;
196+
}
158197
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace BookStack\References;
4+
5+
use BookStack\Entities\Models\Entity;
6+
7+
class ReferenceChangeContext
8+
{
9+
/**
10+
* Entity pairs where the first is the old entity and the second is the new entity.
11+
* @var array<array{0: Entity, 1: Entity}>
12+
*/
13+
protected array $changes = [];
14+
15+
public function add(Entity $oldEntity, Entity $newEntity): void
16+
{
17+
$this->changes[] = [$oldEntity, $newEntity];
18+
}
19+
}

app/References/ReferenceUpdater.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public function updateEntityReferences(Entity $entity, string $oldLink): void
3030
}
3131
}
3232

33+
public function changeReferencesUsingContext(ReferenceChangeContext $context): void
34+
{
35+
// TODO
36+
37+
// We should probably have references by this point, so we could use those for efficient
38+
// discovery instead of scanning each item within the context.
39+
}
40+
3341
/**
3442
* @return Reference[]
3543
*/

0 commit comments

Comments
 (0)