3 namespace BookStack\Http\Controllers;
5 use BookStack\Entities\Queries\Popular;
6 use BookStack\Entities\Tools\SearchOptions;
7 use BookStack\Entities\Tools\SearchRunner;
8 use BookStack\Entities\Tools\ShelfContext;
9 use BookStack\Entities\Tools\SiblingFetcher;
10 use Illuminate\Http\Request;
12 class SearchController extends Controller
14 protected $searchRunner;
15 protected $entityContextManager;
17 public function __construct(
18 SearchRunner $searchRunner,
19 ShelfContext $entityContextManager
21 $this->searchRunner = $searchRunner;
22 $this->entityContextManager = $entityContextManager;
26 * Searches all entities.
28 public function search(Request $request)
30 $searchOpts = SearchOptions::fromRequest($request);
31 $fullSearchString = $searchOpts->toString();
32 $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
34 $page = intval($request->get('page', '0')) ?: 1;
35 $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page + 1));
37 $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
39 return view('search.all', [
40 'entities' => $results['results'],
41 'totalResults' => $results['total'],
42 'searchTerm' => $fullSearchString,
43 'hasNextPage' => $results['has_more'],
44 'nextPageLink' => $nextPageLink,
45 'options' => $searchOpts,
50 * Searches all entities within a book.
52 public function searchBook(Request $request, int $bookId)
54 $term = $request->get('term', '');
55 $results = $this->searchRunner->searchBook($bookId, $term);
57 return view('partials.entity-list', ['entities' => $results]);
61 * Searches all entities within a chapter.
63 public function searchChapter(Request $request, int $chapterId)
65 $term = $request->get('term', '');
66 $results = $this->searchRunner->searchChapter($chapterId, $term);
68 return view('partials.entity-list', ['entities' => $results]);
72 * Search for a list of entities and return a partial HTML response of matching entities.
73 * Returns the most popular entities if no search is provided.
75 public function searchEntitiesAjax(Request $request)
77 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
78 $searchTerm = $request->get('term', false);
79 $permission = $request->get('permission', 'view');
81 // Search for entities otherwise show most popular
82 if ($searchTerm !== false) {
83 $searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
84 $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20, $permission)['results'];
86 $entities = (new Popular())->run(20, 0, $entityTypes, $permission);
89 return view('search.entity-ajax-list', ['entities' => $entities]);
93 * Search siblings items in the system.
95 public function searchSiblings(Request $request)
97 $type = $request->get('entity_type', null);
98 $id = $request->get('entity_id', null);
100 $entities = (new SiblingFetcher())->fetch($type, $id);
102 return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);