2022-08-17 09:39:53 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\References;
|
|
|
|
|
2023-12-18 11:23:40 -05:00
|
|
|
use BookStack\Entities\EntityProvider;
|
|
|
|
use BookStack\Entities\Models\Entity;
|
2022-08-17 09:39:53 -04:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
|
2022-08-20 07:07:38 -04:00
|
|
|
class ReferenceStore
|
2022-08-17 09:39:53 -04:00
|
|
|
{
|
2023-12-18 11:23:40 -05:00
|
|
|
public function __construct(
|
|
|
|
protected EntityProvider $entityProvider
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-08-17 09:39:53 -04:00
|
|
|
/**
|
2023-12-18 11:23:40 -05:00
|
|
|
* Update the outgoing references for the given entity.
|
2022-08-17 09:39:53 -04:00
|
|
|
*/
|
2023-12-18 11:23:40 -05:00
|
|
|
public function updateForEntity(Entity $entity): void
|
2022-08-17 09:39:53 -04:00
|
|
|
{
|
2023-12-18 11:23:40 -05:00
|
|
|
$this->updateForEntities([$entity]);
|
2022-08-17 09:39:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-18 11:23:40 -05:00
|
|
|
* Update the outgoing references for all entities in the system.
|
2022-08-17 09:39:53 -04:00
|
|
|
*/
|
2023-12-18 11:23:40 -05:00
|
|
|
public function updateForAll(): void
|
2022-08-17 09:39:53 -04:00
|
|
|
{
|
2023-12-18 11:23:40 -05:00
|
|
|
Reference::query()->delete();
|
2022-08-17 09:39:53 -04:00
|
|
|
|
2023-12-18 11:23:40 -05:00
|
|
|
foreach ($this->entityProvider->all() as $entity) {
|
|
|
|
$entity->newQuery()->select(['id', $entity->htmlField])->chunk(100, function (Collection $entities) {
|
|
|
|
$this->updateForEntities($entities->all());
|
|
|
|
});
|
|
|
|
}
|
2022-08-17 09:39:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-18 11:23:40 -05:00
|
|
|
* Update the outgoing references for the entities in the given array.
|
2022-08-17 09:39:53 -04:00
|
|
|
*
|
2023-12-18 11:23:40 -05:00
|
|
|
* @param Entity[] $entities
|
2022-08-17 09:39:53 -04:00
|
|
|
*/
|
2023-12-18 11:23:40 -05:00
|
|
|
protected function updateForEntities(array $entities): void
|
2022-08-17 09:39:53 -04:00
|
|
|
{
|
2023-12-18 11:23:40 -05:00
|
|
|
if (count($entities) === 0) {
|
2022-08-17 09:39:53 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parser = CrossLinkParser::createWithEntityResolvers();
|
|
|
|
$references = [];
|
|
|
|
|
2023-12-18 11:23:40 -05:00
|
|
|
$this->dropReferencesFromEntities($entities);
|
2022-08-17 09:39:53 -04:00
|
|
|
|
2023-12-18 11:23:40 -05:00
|
|
|
foreach ($entities as $entity) {
|
|
|
|
$models = $parser->extractLinkedModels($entity->getAttribute($entity->htmlField));
|
2022-08-17 09:39:53 -04:00
|
|
|
|
|
|
|
foreach ($models as $model) {
|
|
|
|
$references[] = [
|
2023-12-18 11:23:40 -05:00
|
|
|
'from_id' => $entity->id,
|
|
|
|
'from_type' => $entity->getMorphClass(),
|
2022-08-29 12:46:41 -04:00
|
|
|
'to_id' => $model->id,
|
|
|
|
'to_type' => $model->getMorphClass(),
|
2022-08-17 09:39:53 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (array_chunk($references, 1000) as $referenceDataChunk) {
|
|
|
|
Reference::query()->insert($referenceDataChunk);
|
|
|
|
}
|
|
|
|
}
|
2023-12-18 11:23:40 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all the existing references originating from the given entities.
|
|
|
|
* @param Entity[] $entities
|
|
|
|
*/
|
|
|
|
protected function dropReferencesFromEntities(array $entities): void
|
|
|
|
{
|
|
|
|
$IdsByType = [];
|
|
|
|
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
$type = $entity->getMorphClass();
|
|
|
|
if (!isset($IdsByType[$type])) {
|
|
|
|
$IdsByType[$type] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$IdsByType[$type][] = $entity->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($IdsByType as $type => $entityIds) {
|
|
|
|
Reference::query()
|
|
|
|
->where('from_type', '=', $type)
|
|
|
|
->whereIn('from_id', $entityIds)
|
|
|
|
->delete();
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 12:46:41 -04:00
|
|
|
}
|