mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-04-16 09:41:05 +00:00

Also updated model resolvers to only fetch model ID, to prevent bringing back way more data from database than desired.
71 lines
No EOL
1.8 KiB
PHP
71 lines
No EOL
1.8 KiB
PHP
<?php
|
|
|
|
namespace BookStack\References;
|
|
|
|
use BookStack\Entities\Models\Page;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class ReferenceService
|
|
{
|
|
|
|
/**
|
|
* Update the outgoing references for the given page.
|
|
*/
|
|
public function updateForPage(Page $page): void
|
|
{
|
|
$this->updateForPages([$page]);
|
|
}
|
|
|
|
/**
|
|
* Update the outgoing references for all pages in the system.
|
|
*/
|
|
public function updateForAllPages(): void
|
|
{
|
|
Reference::query()
|
|
->where('from_type', '=', (new Page())->getMorphClass())
|
|
->delete();
|
|
|
|
Page::query()->select(['id', 'html'])->chunk(100, function(Collection $pages) {
|
|
$this->updateForPages($pages->all());
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Update the outgoing references for the pages in the given array.
|
|
*
|
|
* @param Page[] $pages
|
|
*/
|
|
protected function updateForPages(array $pages): void
|
|
{
|
|
if (count($pages) === 0) {
|
|
return;
|
|
}
|
|
|
|
$parser = CrossLinkParser::createWithEntityResolvers();
|
|
$references = [];
|
|
|
|
$pageIds = array_map(fn(Page $page) => $page->id, $pages);
|
|
Reference::query()
|
|
->where('from_type', '=', $pages[0]->getMorphClass())
|
|
->whereIn('from_id', $pageIds)
|
|
->delete();
|
|
|
|
foreach ($pages as $page) {
|
|
$models = $parser->extractLinkedModels($page->html);
|
|
|
|
foreach ($models as $model) {
|
|
$references[] = [
|
|
'from_id' => $page->id,
|
|
'from_type' => $page->getMorphClass(),
|
|
'to_id' => $model->id,
|
|
'to_type' => $model->getMorphClass(),
|
|
];
|
|
}
|
|
}
|
|
|
|
foreach (array_chunk($references, 1000) as $referenceDataChunk) {
|
|
Reference::query()->insert($referenceDataChunk);
|
|
}
|
|
}
|
|
|
|
} |