3 namespace BookStack\Search;
5 use BookStack\Entities\Queries\Popular;
6 use BookStack\Entities\Tools\SiblingFetcher;
7 use BookStack\Http\Controllers\Controller;
8 use Illuminate\Http\Request;
10 class SearchController extends Controller
12 protected SearchRunner $searchRunner;
14 public function __construct(SearchRunner $searchRunner)
16 $this->searchRunner = $searchRunner;
20 * Searches all entities.
22 public function search(Request $request, SearchResultsFormatter $formatter)
24 $searchOpts = SearchOptions::fromRequest($request);
25 $fullSearchString = $searchOpts->toString();
26 $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
28 $page = intval($request->get('page', '0')) ?: 1;
29 $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page + 1));
31 $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
32 $formatter->format($results['results']->all(), $searchOpts);
34 return view('search.all', [
35 'entities' => $results['results'],
36 'totalResults' => $results['total'],
37 'searchTerm' => $fullSearchString,
38 'hasNextPage' => $results['has_more'],
39 'nextPageLink' => $nextPageLink,
40 'options' => $searchOpts,
45 * Searches all entities within a book.
47 public function searchBook(Request $request, int $bookId)
49 $term = $request->get('term', '');
50 $results = $this->searchRunner->searchBook($bookId, $term);
52 return view('entities.list', ['entities' => $results]);
56 * Searches all entities within a chapter.
58 public function searchChapter(Request $request, int $chapterId)
60 $term = $request->get('term', '');
61 $results = $this->searchRunner->searchChapter($chapterId, $term);
63 return view('entities.list', ['entities' => $results]);
67 * Search for a list of entities and return a partial HTML response of matching entities.
68 * Returns the most popular entities if no search is provided.
70 public function searchForSelector(Request $request)
72 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
73 $searchTerm = $request->get('term', false);
74 $permission = $request->get('permission', 'view');
76 // Search for entities otherwise show most popular
77 if ($searchTerm !== false) {
78 $searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
79 $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20)['results'];
81 $entities = (new Popular())->run(20, 0, $entityTypes);
84 return view('search.parts.entity-selector-list', ['entities' => $entities, 'permission' => $permission]);
88 * Search for a list of entities and return a partial HTML response of matching entities
89 * to be used as a result preview suggestion list for global system searches.
91 public function searchSuggestions(Request $request)
93 $searchTerm = $request->get('term', '');
94 $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 5)['results'];
96 foreach ($entities as $entity) {
97 $entity->setAttribute('preview_content', '');
100 return view('search.parts.entity-suggestion-list', [
101 'entities' => $entities->slice(0, 5)
106 * Search siblings items in the system.
108 public function searchSiblings(Request $request)
110 $type = $request->get('entity_type', null);
111 $id = $request->get('entity_id', null);
113 $entities = (new SiblingFetcher())->fetch($type, $id);
115 return view('entities.list-basic', ['entities' => $entities, 'style' => 'compact']);