BookStack/app/References/CrossLinkReplacer.php
Dan Brown 26ccb7b644
Started work on reference on-change-updates
Refactored out revision-specific actions within PageRepo for
organisition and re-use for cross-linking work.
2022-08-20 21:09:07 +01:00

47 lines
1.4 KiB
PHP

<?php
namespace BookStack\References;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\RevisionRepo;
class CrossLinkReplacer
{
protected ReferenceFetcher $referenceFetcher;
protected RevisionRepo $revisionRepo;
public function __construct(ReferenceFetcher $referenceFetcher, RevisionRepo $revisionRepo)
{
$this->referenceFetcher = $referenceFetcher;
$this->revisionRepo = $revisionRepo;
}
public function updateEntityPageReferences(Entity $entity, string $oldLink)
{
$references = $this->referenceFetcher->getPageReferencesToEntity($entity);
$newLink = $entity->getUrl();
/** @var Reference $reference */
foreach ($references as $reference) {
/** @var Page $page */
$page = $reference->from;
$this->updateReferencesWithinPage($page, $oldLink, $newLink);
}
}
protected function updateReferencesWithinPage(Page $page, string $oldLink, string $newLink)
{
$page = (clone $page)->refresh();
$html = '';// TODO - Update HTML content
$markdown = '';// TODO - Update markdown content
$page->html = $html;
$page->markdown = $markdown;
$page->revision_count++;
$page->save();
$summary = ''; // TODO - Get default summary from translations
$this->revisionRepo->storeNewForPage($page, $summary);
}
}