3 namespace BookStack\References;
5 use BookStack\Entities\Models\Page;
6 use Illuminate\Database\Eloquent\Collection;
12 * Update the outgoing references for the given page.
14 public function updateForPage(Page $page): void
16 $this->updateForPages([$page]);
20 * Update the outgoing references for all pages in the system.
22 public function updateForAllPages(): void
25 ->where('from_type', '=', (new Page())->getMorphClass())
28 Page::query()->select(['id', 'html'])->chunk(100, function(Collection $pages) {
29 $this->updateForPages($pages->all());
34 * Update the outgoing references for the pages in the given array.
36 * @param Page[] $pages
38 protected function updateForPages(array $pages): void
40 if (count($pages) === 0) {
44 $parser = CrossLinkParser::createWithEntityResolvers();
47 $pageIds = array_map(fn(Page $page) => $page->id, $pages);
49 ->where('from_type', '=', $pages[0]->getMorphClass())
50 ->whereIn('from_id', $pageIds)
53 foreach ($pages as $page) {
54 $models = $parser->extractLinkedModels($page->html);
56 foreach ($models as $model) {
58 'from_id' => $page->id,
59 'from_type' => $page->getMorphClass(),
60 'to_id' => $model->id,
61 'to_type' => $model->getMorphClass(),
66 foreach (array_chunk($references, 1000) as $referenceDataChunk) {
67 Reference::query()->insert($referenceDataChunk);