| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\References; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  | use BookStack\Entities\EntityProvider; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  | class ReferenceStore | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected EntityProvider $entityProvider | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |      * Update the outgoing references for the given entity. | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |     public function updateForEntity(Entity $entity): void | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |         $this->updateForEntities([$entity]); | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |      * Update the outgoing references for all entities in the system. | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |     public function updateForAll(): void | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |         Reference::query()->delete(); | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08: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 21:39:53 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |      * Update the outgoing references for the entities in the given array. | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |      * @param Entity[] $entities | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |     protected function updateForEntities(array $entities): void | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |         if (count($entities) === 0) { | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $parser = CrossLinkParser::createWithEntityResolvers(); | 
					
						
							|  |  |  |         $references = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |         $this->dropReferencesFromEntities($entities); | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |         foreach ($entities as $entity) { | 
					
						
							|  |  |  |             $models = $parser->extractLinkedModels($entity->getAttribute($entity->htmlField)); | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             foreach ($models as $model) { | 
					
						
							|  |  |  |                 $references[] = [ | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |                     'from_id'   => $entity->id, | 
					
						
							|  |  |  |                     'from_type' => $entity->getMorphClass(), | 
					
						
							| 
									
										
										
										
											2022-08-30 00:46:41 +08:00
										 |  |  |                     'to_id'     => $model->id, | 
					
						
							|  |  |  |                     'to_type'   => $model->getMorphClass(), | 
					
						
							| 
									
										
										
										
											2022-08-17 21:39:53 +08:00
										 |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach (array_chunk($references, 1000) as $referenceDataChunk) { | 
					
						
							|  |  |  |             Reference::query()->insert($referenceDataChunk); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08: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-30 00:46:41 +08:00
										 |  |  | } |