Input WYSIWYG: Updated reference link updating for descriptions

This commit is contained in:
Dan Brown 2023-12-18 18:12:36 +00:00
parent 307fae39c4
commit bc354e8b12
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 31 additions and 10 deletions

View File

@ -65,7 +65,7 @@ abstract class BookChild extends Entity
$this->refresh(); $this->refresh();
if ($oldUrl !== $this->getUrl()) { 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 // Update all child pages if a chapter

View File

@ -74,7 +74,7 @@ class BaseRepo
$this->referenceStore->updateForEntity($entity); $this->referenceStore->updateForEntity($entity);
if ($oldUrl !== $entity->getUrl()) { if ($oldUrl !== $entity->getUrl()) {
$this->referenceUpdater->updateEntityPageReferences($entity, $oldUrl); $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
} }
} }

View File

@ -305,7 +305,7 @@ class PageRepo
$this->revisionRepo->storeNewForPage($page, $summary); $this->revisionRepo->storeNewForPage($page, $summary);
if ($oldUrl !== $page->getUrl()) { if ($oldUrl !== $page->getUrl()) {
$this->referenceUpdater->updateEntityPageReferences($page, $oldUrl); $this->referenceUpdater->updateEntityReferences($page, $oldUrl);
} }
Activity::add(ActivityType::PAGE_RESTORE, $page); Activity::add(ActivityType::PAGE_RESTORE, $page);

View File

@ -4,6 +4,7 @@ namespace BookStack\References;
use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Entity; use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\HasHtmlDescription;
use BookStack\Entities\Models\Page; use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\RevisionRepo; use BookStack\Entities\Repos\RevisionRepo;
use BookStack\Util\HtmlDocument; use BookStack\Util\HtmlDocument;
@ -12,20 +13,19 @@ class ReferenceUpdater
{ {
public function __construct( public function __construct(
protected ReferenceFetcher $referenceFetcher, 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); $references = $this->getReferencesToUpdate($entity);
$newLink = $entity->getUrl(); $newLink = $entity->getUrl();
/** @var Reference $reference */
foreach ($references as $reference) { foreach ($references as $reference) {
/** @var Page $page */ /** @var Entity $entity */
$page = $reference->from; $entity = $reference->from;
$this->updateReferencesWithinPage($page, $oldLink, $newLink); $this->updateReferencesWithinEntity($entity, $oldLink, $newLink);
} }
} }
@ -57,7 +57,28 @@ class ReferenceUpdater
return array_values($deduped); 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(); $page = (clone $page)->refresh();
$html = $this->updateLinksInHtml($page->html, $oldLink, $newLink); $html = $this->updateLinksInHtml($page->html, $oldLink, $newLink);