Skip to content

Commit 959981a

Browse files
committed
Copying: Added logic to find & update references
1 parent 674bb84 commit 959981a

File tree

5 files changed

+96
-15
lines changed

5 files changed

+96
-15
lines changed

app/Entities/Models/Page.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ public function getUrl(string $path = ''): string
124124
return url('/' . implode('/', $parts));
125125
}
126126

127+
/**
128+
* Get the ID-based permalink for this page.
129+
*/
130+
public function getPermalink(): string
131+
{
132+
return url("/link/{$this->id}");
133+
}
134+
127135
/**
128136
* Get this page for JSON display.
129137
*/

app/Entities/Tools/Cloner.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ protected function createChapterClone(Chapter $original, Book $parent, string $n
7878
if (userCan(Permission::PageCreate, $copyChapter)) {
7979
/** @var Page $page */
8080
foreach ($original->getVisiblePages() as $page) {
81-
$this->clonePage($page, $copyChapter, $page->name);
81+
$this->createPageClone($page, $copyChapter, $page->name);
8282
}
8383
}
8484

85+
$this->referenceChangeContext->add($original, $copyChapter);
86+
8587
return $copyChapter;
8688
}
8789

@@ -109,11 +111,11 @@ protected function createBookClone(Book $original, string $newName): Book
109111
$directChildren = $original->getDirectVisibleChildren();
110112
foreach ($directChildren as $child) {
111113
if ($child instanceof Chapter && userCan(Permission::ChapterCreate, $copyBook)) {
112-
$this->cloneChapter($child, $copyBook, $child->name);
114+
$this->createChapterClone($child, $copyBook, $child->name);
113115
}
114116

115117
if ($child instanceof Page && !$child->draft && userCan(Permission::PageCreate, $copyBook)) {
116-
$this->clonePage($child, $copyBook, $child->name);
118+
$this->createPageClone($child, $copyBook, $child->name);
117119
}
118120
}
119121

@@ -125,6 +127,8 @@ protected function createBookClone(Book $original, string $newName): Book
125127
}
126128
}
127129

130+
$this->referenceChangeContext->add($original, $copyBook);
131+
128132
return $copyBook;
129133
}
130134

app/References/ReferenceChangeContext.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,41 @@ public function add(Entity $oldEntity, Entity $newEntity): void
1616
{
1717
$this->changes[] = [$oldEntity, $newEntity];
1818
}
19+
20+
/**
21+
* Get all the change pairs.
22+
* Returned array is an array of pairs, where the first item is the old entity
23+
* and the second is the new entity.
24+
* @return array<array{0: Entity, 1: Entity}>
25+
*/
26+
public function getChanges(): array
27+
{
28+
return $this->changes;
29+
}
30+
31+
/**
32+
* Get all the new entities from the changes.
33+
*/
34+
public function getNewEntities(): array
35+
{
36+
return array_column($this->changes, 1);
37+
}
38+
39+
/**
40+
* Get all the old entities from the changes.
41+
*/
42+
public function getOldEntities(): array
43+
{
44+
return array_column($this->changes, 0);
45+
}
46+
47+
public function getNewForOld(Entity $oldEntity): ?Entity
48+
{
49+
foreach ($this->changes as [$old, $new]) {
50+
if ($old->id === $oldEntity->id && $old->type === $oldEntity->type) {
51+
return $new;
52+
}
53+
}
54+
return null;
55+
}
1956
}

app/References/ReferenceUpdater.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use BookStack\Entities\Models\Book;
66
use BookStack\Entities\Models\HasDescriptionInterface;
77
use BookStack\Entities\Models\Entity;
8-
use BookStack\Entities\Models\EntityContainerData;
98
use BookStack\Entities\Models\Page;
109
use BookStack\Entities\Repos\RevisionRepo;
1110
use BookStack\Util\HtmlDocument;
@@ -30,12 +29,45 @@ public function updateEntityReferences(Entity $entity, string $oldLink): void
3029
}
3130
}
3231

32+
/**
33+
* Change existing references for a range of entities using the given context.
34+
*/
3335
public function changeReferencesUsingContext(ReferenceChangeContext $context): void
3436
{
35-
// TODO
37+
$bindings = [];
38+
foreach ($context->getOldEntities() as $old) {
39+
$bindings[] = $old->getMorphClass();
40+
$bindings[] = $old->id;
41+
}
3642

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.
43+
// No targets to update within the context, so no need to continue.
44+
if (count($bindings) < 2) {
45+
return;
46+
}
47+
48+
$toReferenceQuery = '(to_type, to_id) IN (' . rtrim(str_repeat('(?,?),', count($bindings) / 2), ',') . ')';
49+
50+
// Cycle each new entity in the context
51+
foreach ($context->getNewEntities() as $new) {
52+
// For each, get all references from it which lead to other items within the context of the change
53+
$newReferencesInContext = $new->referencesFrom()->whereRaw($toReferenceQuery, $bindings)->get();
54+
// For each reference, update the URL and the reference entry
55+
foreach ($newReferencesInContext as $reference) {
56+
$oldToEntity = $reference->to;
57+
$newToEntity = $context->getNewForOld($oldToEntity);
58+
if ($newToEntity === null) {
59+
continue;
60+
}
61+
62+
$this->updateReferencesWithinEntity($new, $oldToEntity->getUrl(), $newToEntity->getUrl());
63+
if ($newToEntity instanceof Page && $oldToEntity instanceof Page) {
64+
$this->updateReferencesWithinPage($newToEntity, $oldToEntity->getPermalink(), $newToEntity->getPermalink());
65+
}
66+
$reference->to_id = $newToEntity->id;
67+
$reference->to_type = $newToEntity->getMorphClass();
68+
$reference->save();
69+
}
70+
}
3971
}
4072

4173
/**

tests/Entity/CopyTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ public function test_book_copy_updates_internal_references()
214214
'html' => '<p>This is a test <a href="' . $book->getUrl() . '">book link</a></p>',
215215
]);
216216

217-
$html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
218-
219217
// Quick pre-update to get stable slug
220218
$this->put($book->getUrl(), ['name' => 'Internal ref test']);
221219
$book->refresh();
220+
$page->refresh();
222221

222+
$html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
223223
$this->put($book->getUrl(), ['name' => 'Internal ref test', 'description_html' => $html]);
224224

225225
$this->post($book->getUrl('/copy'), ['name' => 'My copied book']);
@@ -245,24 +245,24 @@ public function test_chapter_copy_updates_internal_references()
245245
'html' => '<p>This is a test <a href="' . $chapter->getUrl() . '">chapter link</a></p>',
246246
]);
247247

248-
$html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
249-
250248
// Quick pre-update to get stable slug
251249
$this->put($chapter->getUrl(), ['name' => 'Internal ref test']);
252250
$chapter->refresh();
251+
$page->refresh();
253252

253+
$html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
254254
$this->put($chapter->getUrl(), ['name' => 'Internal ref test', 'description_html' => $html]);
255255

256256
$this->post($chapter->getUrl('/copy'), ['name' => 'My copied chapter']);
257257

258258
$newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
259259
$newPage = $newChapter->pages()->where('name', '=', 'reference test page')->first();
260260

261-
$this->assertStringContainsString($newChapter->getUrl(), $newPage->html);
262-
$this->assertStringContainsString($newPage->getUrl(), $newChapter->description_html);
261+
$this->assertStringContainsString($newChapter->getUrl() . '"', $newPage->html);
262+
$this->assertStringContainsString($newPage->getUrl() . '"', $newChapter->description_html);
263263

264-
$this->assertStringNotContainsString($chapter->getUrl(), $newPage->html);
265-
$this->assertStringNotContainsString($page->getUrl(), $newChapter->description_html);
264+
$this->assertStringNotContainsString($chapter->getUrl() . '"', $newPage->html);
265+
$this->assertStringNotContainsString($page->getUrl() . '"', $newChapter->description_html);
266266
}
267267

268268
public function test_page_copy_updates_internal_self_references()

0 commit comments

Comments
 (0)