From: Dan Brown Date: Tue, 25 Nov 2025 17:52:26 +0000 (+0000) Subject: Copying: Added logic to find & update references X-Git-Url: http://source.bookstackapp.com/bookstack/commitdiff_plain/959981a676e222aa9a7a3d0f163e1a21509086f2 Copying: Added logic to find & update references --- diff --git a/app/Entities/Models/Page.php b/app/Entities/Models/Page.php index 88c59bd1b..a1d3fc7b4 100644 --- a/app/Entities/Models/Page.php +++ b/app/Entities/Models/Page.php @@ -124,6 +124,14 @@ class Page extends BookChild return url('/' . implode('/', $parts)); } + /** + * Get the ID-based permalink for this page. + */ + public function getPermalink(): string + { + return url("/link/{$this->id}"); + } + /** * Get this page for JSON display. */ diff --git a/app/Entities/Tools/Cloner.php b/app/Entities/Tools/Cloner.php index 916ce42c5..64c48c351 100644 --- a/app/Entities/Tools/Cloner.php +++ b/app/Entities/Tools/Cloner.php @@ -78,10 +78,12 @@ class Cloner if (userCan(Permission::PageCreate, $copyChapter)) { /** @var Page $page */ foreach ($original->getVisiblePages() as $page) { - $this->clonePage($page, $copyChapter, $page->name); + $this->createPageClone($page, $copyChapter, $page->name); } } + $this->referenceChangeContext->add($original, $copyChapter); + return $copyChapter; } @@ -109,11 +111,11 @@ class Cloner $directChildren = $original->getDirectVisibleChildren(); foreach ($directChildren as $child) { if ($child instanceof Chapter && userCan(Permission::ChapterCreate, $copyBook)) { - $this->cloneChapter($child, $copyBook, $child->name); + $this->createChapterClone($child, $copyBook, $child->name); } if ($child instanceof Page && !$child->draft && userCan(Permission::PageCreate, $copyBook)) { - $this->clonePage($child, $copyBook, $child->name); + $this->createPageClone($child, $copyBook, $child->name); } } @@ -125,6 +127,8 @@ class Cloner } } + $this->referenceChangeContext->add($original, $copyBook); + return $copyBook; } diff --git a/app/References/ReferenceChangeContext.php b/app/References/ReferenceChangeContext.php index df3028b93..f11619813 100644 --- a/app/References/ReferenceChangeContext.php +++ b/app/References/ReferenceChangeContext.php @@ -16,4 +16,41 @@ class ReferenceChangeContext { $this->changes[] = [$oldEntity, $newEntity]; } + + /** + * Get all the change pairs. + * Returned array is an array of pairs, where the first item is the old entity + * and the second is the new entity. + * @return array + */ + public function getChanges(): array + { + return $this->changes; + } + + /** + * Get all the new entities from the changes. + */ + public function getNewEntities(): array + { + return array_column($this->changes, 1); + } + + /** + * Get all the old entities from the changes. + */ + public function getOldEntities(): array + { + return array_column($this->changes, 0); + } + + public function getNewForOld(Entity $oldEntity): ?Entity + { + foreach ($this->changes as [$old, $new]) { + if ($old->id === $oldEntity->id && $old->type === $oldEntity->type) { + return $new; + } + } + return null; + } } diff --git a/app/References/ReferenceUpdater.php b/app/References/ReferenceUpdater.php index 3a6db05ef..b811fe868 100644 --- a/app/References/ReferenceUpdater.php +++ b/app/References/ReferenceUpdater.php @@ -5,7 +5,6 @@ namespace BookStack\References; use BookStack\Entities\Models\Book; use BookStack\Entities\Models\HasDescriptionInterface; use BookStack\Entities\Models\Entity; -use BookStack\Entities\Models\EntityContainerData; use BookStack\Entities\Models\Page; use BookStack\Entities\Repos\RevisionRepo; use BookStack\Util\HtmlDocument; @@ -30,12 +29,45 @@ class ReferenceUpdater } } + /** + * Change existing references for a range of entities using the given context. + */ public function changeReferencesUsingContext(ReferenceChangeContext $context): void { - // TODO + $bindings = []; + foreach ($context->getOldEntities() as $old) { + $bindings[] = $old->getMorphClass(); + $bindings[] = $old->id; + } - // We should probably have references by this point, so we could use those for efficient - // discovery instead of scanning each item within the context. + // No targets to update within the context, so no need to continue. + if (count($bindings) < 2) { + return; + } + + $toReferenceQuery = '(to_type, to_id) IN (' . rtrim(str_repeat('(?,?),', count($bindings) / 2), ',') . ')'; + + // Cycle each new entity in the context + foreach ($context->getNewEntities() as $new) { + // For each, get all references from it which lead to other items within the context of the change + $newReferencesInContext = $new->referencesFrom()->whereRaw($toReferenceQuery, $bindings)->get(); + // For each reference, update the URL and the reference entry + foreach ($newReferencesInContext as $reference) { + $oldToEntity = $reference->to; + $newToEntity = $context->getNewForOld($oldToEntity); + if ($newToEntity === null) { + continue; + } + + $this->updateReferencesWithinEntity($new, $oldToEntity->getUrl(), $newToEntity->getUrl()); + if ($newToEntity instanceof Page && $oldToEntity instanceof Page) { + $this->updateReferencesWithinPage($newToEntity, $oldToEntity->getPermalink(), $newToEntity->getPermalink()); + } + $reference->to_id = $newToEntity->id; + $reference->to_type = $newToEntity->getMorphClass(); + $reference->save(); + } + } } /** diff --git a/tests/Entity/CopyTest.php b/tests/Entity/CopyTest.php index 258b538f5..43fe12083 100644 --- a/tests/Entity/CopyTest.php +++ b/tests/Entity/CopyTest.php @@ -214,12 +214,12 @@ class CopyTest extends TestCase 'html' => '

This is a test book link

', ]); - $html = '

This is a test page link

'; - // Quick pre-update to get stable slug $this->put($book->getUrl(), ['name' => 'Internal ref test']); $book->refresh(); + $page->refresh(); + $html = '

This is a test page link

'; $this->put($book->getUrl(), ['name' => 'Internal ref test', 'description_html' => $html]); $this->post($book->getUrl('/copy'), ['name' => 'My copied book']); @@ -245,12 +245,12 @@ class CopyTest extends TestCase 'html' => '

This is a test chapter link

', ]); - $html = '

This is a test page link

'; - // Quick pre-update to get stable slug $this->put($chapter->getUrl(), ['name' => 'Internal ref test']); $chapter->refresh(); + $page->refresh(); + $html = '

This is a test page link

'; $this->put($chapter->getUrl(), ['name' => 'Internal ref test', 'description_html' => $html]); $this->post($chapter->getUrl('/copy'), ['name' => 'My copied chapter']); @@ -258,11 +258,11 @@ class CopyTest extends TestCase $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first(); $newPage = $newChapter->pages()->where('name', '=', 'reference test page')->first(); - $this->assertStringContainsString($newChapter->getUrl(), $newPage->html); - $this->assertStringContainsString($newPage->getUrl(), $newChapter->description_html); + $this->assertStringContainsString($newChapter->getUrl() . '"', $newPage->html); + $this->assertStringContainsString($newPage->getUrl() . '"', $newChapter->description_html); - $this->assertStringNotContainsString($chapter->getUrl(), $newPage->html); - $this->assertStringNotContainsString($page->getUrl(), $newChapter->description_html); + $this->assertStringNotContainsString($chapter->getUrl() . '"', $newPage->html); + $this->assertStringNotContainsString($page->getUrl() . '"', $newChapter->description_html); } public function test_page_copy_updates_internal_self_references()