1111use BookStack \Users \Models \User ;
1212use Illuminate \Database \Connection ;
1313use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
14- use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
1514use Illuminate \Database \Query \Builder ;
1615use Illuminate \Database \Query \JoinClause ;
1716use Illuminate \Support \Collection ;
@@ -30,17 +29,15 @@ public function __construct(
3029 protected EntityProvider $ entityProvider ,
3130 protected PermissionApplicator $ permissions ,
3231 protected EntityQueries $ entityQueries ,
32+ protected EntityHydrator $ entityHydrator ,
3333 ) {
3434 $ this ->termAdjustmentCache = new WeakMap ();
3535 }
3636
3737 /**
3838 * Search all entities in the system.
39- * The provided count is for each entity to search,
40- * Total returned could be larger and not guaranteed.
41- * // TODO - Update this comment
4239 *
43- * @return array{total: int, count: int, has_more: bool, results: Collection<Entity>}
40+ * @return array{total: int, results: Collection<Entity>}
4441 */
4542 public function searchEntities (SearchOptions $ searchOpts , string $ entityType = 'all ' , int $ page = 1 , int $ count = 20 ): array
4643 {
@@ -58,14 +55,9 @@ public function searchEntities(SearchOptions $searchOpts, string $entityType = '
5855 $ total = $ searchQuery ->count ();
5956 $ results = $ this ->getPageOfDataFromQuery ($ searchQuery , $ page , $ count );
6057
61- // TODO - Pagination?
62- $ hasMore = ($ total > ($ page * $ count ));
63-
6458 return [
6559 'total ' => $ total ,
66- 'count ' => count ($ results ),
67- 'has_more ' => $ hasMore ,
68- 'results ' => $ results ->sortByDesc ('score ' )->values (),
60+ 'results ' => $ results ->values (),
6961 ];
7062 }
7163
@@ -79,15 +71,8 @@ public function searchBook(int $bookId, string $searchString): Collection
7971 $ filterMap = $ opts ->filters ->toValueMap ();
8072 $ entityTypesToSearch = isset ($ filterMap ['type ' ]) ? explode ('| ' , $ filterMap ['type ' ]) : $ entityTypes ;
8173
82- $ results = collect ();
83- foreach ($ entityTypesToSearch as $ entityType ) {
84- if (!in_array ($ entityType , $ entityTypes )) {
85- continue ;
86- }
87-
88- $ search = $ this ->buildQuery ($ opts , $ entityType )->where ('book_id ' , '= ' , $ bookId )->take (20 )->get ();
89- $ results = $ results ->merge ($ search );
90- }
74+ $ filteredTypes = array_intersect ($ entityTypesToSearch , $ entityTypes );
75+ $ results = $ this ->buildQuery ($ opts , $ filteredTypes )->where ('book_id ' , '= ' , $ bookId )->take (20 )->get ();
9176
9277 return $ results ->sortByDesc ('score ' )->take (20 );
9378 }
@@ -98,7 +83,7 @@ public function searchBook(int $bookId, string $searchString): Collection
9883 public function searchChapter (int $ chapterId , string $ searchString ): Collection
9984 {
10085 $ opts = SearchOptions::fromString ($ searchString );
101- $ pages = $ this ->buildQuery ($ opts , 'page ' )->where ('chapter_id ' , '= ' , $ chapterId )->take (20 )->get ();
86+ $ pages = $ this ->buildQuery ($ opts , [ 'page ' ] )->where ('chapter_id ' , '= ' , $ chapterId )->take (20 )->get ();
10287
10388 return $ pages ->sortByDesc ('score ' );
10489 }
@@ -113,7 +98,7 @@ protected function getPageOfDataFromQuery(EloquentBuilder $query, int $page, int
11398 ->take ($ count )
11499 ->get ();
115100
116- $ hydrated = ( new EntityHydrator ( $ entities ->all (), true , true ))-> hydrate ( );
101+ $ hydrated = $ this -> entityHydrator -> hydrate ( $ entities ->all (), true , true );
117102
118103 return collect ($ hydrated );
119104 }
0 commit comments