BookStack/app/References/ReferenceService.php
Dan Brown 3290ab3ac9
Added regenerate-references command test
Also updated model resolvers to only fetch model ID, to prevent bringing
back way more data from database than desired.
2022-08-17 16:59:23 +01:00

71 lines
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);
}
}
}