2022-08-20 16:09:07 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\References;
|
|
|
|
|
2022-08-30 17:00:32 -04:00
|
|
|
use BookStack\Entities\Models\Book;
|
2022-08-20 16:09:07 -04:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2023-12-18 13:12:36 -05:00
|
|
|
use BookStack\Entities\Models\HasHtmlDescription;
|
2022-08-20 16:09:07 -04:00
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
use BookStack\Entities\Repos\RevisionRepo;
|
2023-11-14 10:46:32 -05:00
|
|
|
use BookStack\Util\HtmlDocument;
|
2022-08-20 16:09:07 -04:00
|
|
|
|
2022-08-21 13:05:19 -04:00
|
|
|
class ReferenceUpdater
|
2022-08-20 16:09:07 -04:00
|
|
|
{
|
2023-11-14 10:46:32 -05:00
|
|
|
public function __construct(
|
|
|
|
protected ReferenceFetcher $referenceFetcher,
|
2023-12-18 13:12:36 -05:00
|
|
|
protected RevisionRepo $revisionRepo,
|
2023-11-14 10:46:32 -05:00
|
|
|
) {
|
2022-08-20 16:09:07 -04:00
|
|
|
}
|
|
|
|
|
2023-12-18 13:12:36 -05:00
|
|
|
public function updateEntityReferences(Entity $entity, string $oldLink): void
|
2022-08-20 16:09:07 -04:00
|
|
|
{
|
2022-08-30 17:00:32 -04:00
|
|
|
$references = $this->getReferencesToUpdate($entity);
|
2022-08-20 16:09:07 -04:00
|
|
|
$newLink = $entity->getUrl();
|
|
|
|
|
|
|
|
foreach ($references as $reference) {
|
2023-12-18 13:12:36 -05:00
|
|
|
/** @var Entity $entity */
|
|
|
|
$entity = $reference->from;
|
|
|
|
$this->updateReferencesWithinEntity($entity, $oldLink, $newLink);
|
2022-08-20 16:09:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 17:00:32 -04:00
|
|
|
/**
|
|
|
|
* @return Reference[]
|
|
|
|
*/
|
|
|
|
protected function getReferencesToUpdate(Entity $entity): array
|
|
|
|
{
|
|
|
|
/** @var Reference[] $references */
|
2023-12-18 11:23:40 -05:00
|
|
|
$references = $this->referenceFetcher->getReferencesToEntity($entity)->values()->all();
|
2022-08-30 17:00:32 -04:00
|
|
|
|
|
|
|
if ($entity instanceof Book) {
|
|
|
|
$pages = $entity->pages()->get(['id']);
|
|
|
|
$chapters = $entity->chapters()->get(['id']);
|
|
|
|
$children = $pages->concat($chapters);
|
|
|
|
foreach ($children as $bookChild) {
|
2023-12-10 09:58:05 -05:00
|
|
|
/** @var Reference[] $childRefs */
|
2023-12-18 11:23:40 -05:00
|
|
|
$childRefs = $this->referenceFetcher->getReferencesToEntity($bookChild)->values()->all();
|
2022-08-30 17:00:32 -04:00
|
|
|
array_push($references, ...$childRefs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$deduped = [];
|
|
|
|
foreach ($references as $reference) {
|
|
|
|
$key = $reference->from_id . ':' . $reference->from_type;
|
|
|
|
$deduped[$key] = $reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_values($deduped);
|
|
|
|
}
|
|
|
|
|
2023-12-18 13:12:36 -05:00
|
|
|
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
|
2022-08-20 16:09:07 -04:00
|
|
|
{
|
|
|
|
$page = (clone $page)->refresh();
|
2022-08-21 06:29:34 -04:00
|
|
|
$html = $this->updateLinksInHtml($page->html, $oldLink, $newLink);
|
|
|
|
$markdown = $this->updateLinksInMarkdown($page->markdown, $oldLink, $newLink);
|
2022-08-20 16:09:07 -04:00
|
|
|
|
|
|
|
$page->html = $html;
|
|
|
|
$page->markdown = $markdown;
|
|
|
|
$page->revision_count++;
|
|
|
|
$page->save();
|
|
|
|
|
2022-08-21 06:29:34 -04:00
|
|
|
$summary = trans('entities.pages_references_update_revision');
|
2022-08-20 16:09:07 -04:00
|
|
|
$this->revisionRepo->storeNewForPage($page, $summary);
|
|
|
|
}
|
2022-08-21 06:29:34 -04:00
|
|
|
|
|
|
|
protected function updateLinksInMarkdown(string $markdown, string $oldLink, string $newLink): string
|
|
|
|
{
|
|
|
|
if (empty($markdown)) {
|
|
|
|
return $markdown;
|
|
|
|
}
|
|
|
|
|
2022-08-21 13:05:19 -04:00
|
|
|
$commonLinkRegex = '/(\[.*?\]\()' . preg_quote($oldLink, '/') . '(.*?\))/i';
|
2022-08-21 06:29:34 -04:00
|
|
|
$markdown = preg_replace($commonLinkRegex, '$1' . $newLink . '$2', $markdown);
|
|
|
|
|
2022-08-21 13:05:19 -04:00
|
|
|
$referenceLinkRegex = '/(\[.*?\]:\s?)' . preg_quote($oldLink, '/') . '(.*?)($|\s)/i';
|
2022-08-21 06:29:34 -04:00
|
|
|
$markdown = preg_replace($referenceLinkRegex, '$1' . $newLink . '$2$3', $markdown);
|
|
|
|
|
|
|
|
return $markdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function updateLinksInHtml(string $html, string $oldLink, string $newLink): string
|
|
|
|
{
|
|
|
|
if (empty($html)) {
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2023-11-14 10:46:32 -05:00
|
|
|
$doc = new HtmlDocument($html);
|
|
|
|
$anchors = $doc->queryXPath('//a[@href]');
|
2022-08-21 06:29:34 -04:00
|
|
|
|
|
|
|
/** @var \DOMElement $anchor */
|
|
|
|
foreach ($anchors as $anchor) {
|
|
|
|
$link = $anchor->getAttribute('href');
|
|
|
|
$updated = str_ireplace($oldLink, $newLink, $link);
|
|
|
|
$anchor->setAttribute('href', $updated);
|
|
|
|
}
|
|
|
|
|
2023-11-14 10:46:32 -05:00
|
|
|
return $doc->getBodyInnerHtml();
|
2022-08-21 06:29:34 -04:00
|
|
|
}
|
2022-08-29 12:46:41 -04:00
|
|
|
}
|