3 namespace BookStack\References;
5 use BookStack\Entities\Models\Page;
6 use Illuminate\Database\Eloquent\Collection;
11 * Update the outgoing references for the given page.
13 public function updateForPage(Page $page): void
15 $this->updateForPages([$page]);
19 * Update the outgoing references for all pages in the system.
21 public function updateForAllPages(): void
24 ->where('from_type', '=', (new Page())->getMorphClass())
27 Page::query()->select(['id', 'html'])->chunk(100, function (Collection $pages) {
28 $this->updateForPages($pages->all());
33 * Update the outgoing references for the pages in the given array.
35 * @param Page[] $pages
37 protected function updateForPages(array $pages): void
39 if (count($pages) === 0) {
43 $parser = CrossLinkParser::createWithEntityResolvers();
46 $pageIds = array_map(fn (Page $page) => $page->id, $pages);
48 ->where('from_type', '=', $pages[0]->getMorphClass())
49 ->whereIn('from_id', $pageIds)
52 foreach ($pages as $page) {
53 $models = $parser->extractLinkedModels($page->html);
55 foreach ($models as $model) {
57 'from_id' => $page->id,
58 'from_type' => $page->getMorphClass(),
59 'to_id' => $model->id,
60 'to_type' => $model->getMorphClass(),
65 foreach (array_chunk($references, 1000) as $referenceDataChunk) {
66 Reference::query()->insert($referenceDataChunk);