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;
{
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);
}
}
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);