From bc354e8b12a19ed96eab669f0926f517671c7c3e Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 18 Dec 2023 18:12:36 +0000 Subject: [PATCH] Input WYSIWYG: Updated reference link updating for descriptions --- app/Entities/Models/BookChild.php | 2 +- app/Entities/Repos/BaseRepo.php | 2 +- app/Entities/Repos/PageRepo.php | 2 +- app/References/ReferenceUpdater.php | 35 +++++++++++++++++++++++------ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/Entities/Models/BookChild.php b/app/Entities/Models/BookChild.php index ed08f16e6..18735e56b 100644 --- a/app/Entities/Models/BookChild.php +++ b/app/Entities/Models/BookChild.php @@ -65,7 +65,7 @@ abstract class BookChild extends Entity $this->refresh(); if ($oldUrl !== $this->getUrl()) { - app()->make(ReferenceUpdater::class)->updateEntityPageReferences($this, $oldUrl); + app()->make(ReferenceUpdater::class)->updateEntityReferences($this, $oldUrl); } // Update all child pages if a chapter diff --git a/app/Entities/Repos/BaseRepo.php b/app/Entities/Repos/BaseRepo.php index dbdaa9213..3d3d16732 100644 --- a/app/Entities/Repos/BaseRepo.php +++ b/app/Entities/Repos/BaseRepo.php @@ -74,7 +74,7 @@ class BaseRepo $this->referenceStore->updateForEntity($entity); if ($oldUrl !== $entity->getUrl()) { - $this->referenceUpdater->updateEntityPageReferences($entity, $oldUrl); + $this->referenceUpdater->updateEntityReferences($entity, $oldUrl); } } diff --git a/app/Entities/Repos/PageRepo.php b/app/Entities/Repos/PageRepo.php index d491b7f2c..7b14ea7d2 100644 --- a/app/Entities/Repos/PageRepo.php +++ b/app/Entities/Repos/PageRepo.php @@ -305,7 +305,7 @@ class PageRepo $this->revisionRepo->storeNewForPage($page, $summary); if ($oldUrl !== $page->getUrl()) { - $this->referenceUpdater->updateEntityPageReferences($page, $oldUrl); + $this->referenceUpdater->updateEntityReferences($page, $oldUrl); } Activity::add(ActivityType::PAGE_RESTORE, $page); diff --git a/app/References/ReferenceUpdater.php b/app/References/ReferenceUpdater.php index db1bce4fb..db355f211 100644 --- a/app/References/ReferenceUpdater.php +++ b/app/References/ReferenceUpdater.php @@ -4,6 +4,7 @@ namespace BookStack\References; use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Entity; +use BookStack\Entities\Models\HasHtmlDescription; use BookStack\Entities\Models\Page; use BookStack\Entities\Repos\RevisionRepo; use BookStack\Util\HtmlDocument; @@ -12,20 +13,19 @@ class ReferenceUpdater { public function __construct( protected ReferenceFetcher $referenceFetcher, - protected RevisionRepo $revisionRepo + protected RevisionRepo $revisionRepo, ) { } - public function updateEntityPageReferences(Entity $entity, string $oldLink) + public function updateEntityReferences(Entity $entity, string $oldLink): void { $references = $this->getReferencesToUpdate($entity); $newLink = $entity->getUrl(); - /** @var Reference $reference */ foreach ($references as $reference) { - /** @var Page $page */ - $page = $reference->from; - $this->updateReferencesWithinPage($page, $oldLink, $newLink); + /** @var Entity $entity */ + $entity = $reference->from; + $this->updateReferencesWithinEntity($entity, $oldLink, $newLink); } } @@ -57,7 +57,28 @@ class ReferenceUpdater return array_values($deduped); } - protected function updateReferencesWithinPage(Page $page, string $oldLink, string $newLink) + protected function updateReferencesWithinEntity(Entity $entity, string $oldLink, string $newLink): void + { + if ($entity instanceof Page) { + $this->updateReferencesWithinPage($entity, $oldLink, $newLink); + return; + } + + if (in_array(HasHtmlDescription::class, class_uses($entity))) { + $this->updateReferencesWithinDescription($entity, $oldLink, $newLink); + } + } + + protected function updateReferencesWithinDescription(Entity $entity, string $oldLink, string $newLink): void + { + /** @var HasHtmlDescription&Entity $entity */ + $entity = (clone $entity)->refresh(); + $html = $this->updateLinksInHtml($entity->description_html ?: '', $oldLink, $newLink); + $entity->description_html = $html; + $entity->save(); + } + + protected function updateReferencesWithinPage(Page $page, string $oldLink, string $newLink): void { $page = (clone $page)->refresh(); $html = $this->updateLinksInHtml($page->html, $oldLink, $newLink);