1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-09 10:22:51 +03:00

Added entity meta link to reference page

Not totally happy with implementation as is requires extra service to be
injected to core controllers, but does the job.
Included test to cover.
Updated some controller properties to be typed while there.
This commit is contained in:
Dan Brown
2022-08-20 12:07:38 +01:00
parent d198332d3c
commit f634b4ea57
12 changed files with 143 additions and 63 deletions

View File

@@ -0,0 +1,71 @@
<?php
namespace BookStack\References;
use BookStack\Entities\Models\Page;
use Illuminate\Database\Eloquent\Collection;
class ReferenceStore
{
/**
* 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);
}
}
}