]> BookStack Code Mirror - bookstack/blob - app/References/ReferenceUpdater.php
HTML: Aligned and standardised DOMDocument usage
[bookstack] / app / References / ReferenceUpdater.php
1 <?php
2
3 namespace BookStack\References;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\RevisionRepo;
9 use BookStack\Util\HtmlDocument;
10
11 class ReferenceUpdater
12 {
13     public function __construct(
14         protected ReferenceFetcher $referenceFetcher,
15         protected RevisionRepo $revisionRepo
16     ) {
17     }
18
19     public function updateEntityPageReferences(Entity $entity, string $oldLink)
20     {
21         $references = $this->getReferencesToUpdate($entity);
22         $newLink = $entity->getUrl();
23
24         /** @var Reference $reference */
25         foreach ($references as $reference) {
26             /** @var Page $page */
27             $page = $reference->from;
28             $this->updateReferencesWithinPage($page, $oldLink, $newLink);
29         }
30     }
31
32     /**
33      * @return Reference[]
34      */
35     protected function getReferencesToUpdate(Entity $entity): array
36     {
37         /** @var Reference[] $references */
38         $references = $this->referenceFetcher->getPageReferencesToEntity($entity)->values()->all();
39
40         if ($entity instanceof Book) {
41             $pages = $entity->pages()->get(['id']);
42             $chapters = $entity->chapters()->get(['id']);
43             $children = $pages->concat($chapters);
44             foreach ($children as $bookChild) {
45                 $childRefs = $this->referenceFetcher->getPageReferencesToEntity($bookChild)->values()->all();
46                 array_push($references, ...$childRefs);
47             }
48         }
49
50         $deduped = [];
51         foreach ($references as $reference) {
52             $key = $reference->from_id . ':' . $reference->from_type;
53             $deduped[$key] = $reference;
54         }
55
56         return array_values($deduped);
57     }
58
59     protected function updateReferencesWithinPage(Page $page, string $oldLink, string $newLink)
60     {
61         $page = (clone $page)->refresh();
62         $html = $this->updateLinksInHtml($page->html, $oldLink, $newLink);
63         $markdown = $this->updateLinksInMarkdown($page->markdown, $oldLink, $newLink);
64
65         $page->html = $html;
66         $page->markdown = $markdown;
67         $page->revision_count++;
68         $page->save();
69
70         $summary = trans('entities.pages_references_update_revision');
71         $this->revisionRepo->storeNewForPage($page, $summary);
72     }
73
74     protected function updateLinksInMarkdown(string $markdown, string $oldLink, string $newLink): string
75     {
76         if (empty($markdown)) {
77             return $markdown;
78         }
79
80         $commonLinkRegex = '/(\[.*?\]\()' . preg_quote($oldLink, '/') . '(.*?\))/i';
81         $markdown = preg_replace($commonLinkRegex, '$1' . $newLink . '$2', $markdown);
82
83         $referenceLinkRegex = '/(\[.*?\]:\s?)' . preg_quote($oldLink, '/') . '(.*?)($|\s)/i';
84         $markdown = preg_replace($referenceLinkRegex, '$1' . $newLink . '$2$3', $markdown);
85
86         return $markdown;
87     }
88
89     protected function updateLinksInHtml(string $html, string $oldLink, string $newLink): string
90     {
91         if (empty($html)) {
92             return $html;
93         }
94
95         $doc = new HtmlDocument($html);
96         $anchors = $doc->queryXPath('//a[@href]');
97
98         /** @var \DOMElement $anchor */
99         foreach ($anchors as $anchor) {
100             $link = $anchor->getAttribute('href');
101             $updated = str_ireplace($oldLink, $newLink, $link);
102             $anchor->setAttribute('href', $updated);
103         }
104
105         return $doc->getBodyInnerHtml();
106     }
107 }