3 namespace BookStack\Http\Controllers;
5 use BookStack\Entities\Queries\Popular;
6 use BookStack\Entities\Tools\SearchOptions;
7 use BookStack\Entities\Tools\SearchResultsFormatter;
8 use BookStack\Entities\Tools\SearchRunner;
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(SearchRunner $searchRunner)
19 $this->searchRunner = $searchRunner;
23 * Searches all entities.
25 public function search(Request $request, SearchResultsFormatter $formatter)
27 $searchOpts = SearchOptions::fromRequest($request);
28 $fullSearchString = $searchOpts->toString();
29 $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
31 $page = intval($request->get('page', '0')) ?: 1;
32 $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page + 1));
34 $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
35 $formatter->format($results['results']->all(), $searchOpts);
37 return view('search.all', [
38 'entities' => $results['results'],
39 'totalResults' => $results['total'],
40 'searchTerm' => $fullSearchString,
41 'hasNextPage' => $results['has_more'],
42 'nextPageLink' => $nextPageLink,
43 'options' => $searchOpts,
48 * Searches all entities within a book.
50 public function searchBook(Request $request, int $bookId)
52 $term = $request->get('term', '');
53 $results = $this->searchRunner->searchBook($bookId, $term);
55 return view('entities.list', ['entities' => $results]);
59 * Searches all entities within a chapter.
61 public function searchChapter(Request $request, int $chapterId)
63 $term = $request->get('term', '');
64 $results = $this->searchRunner->searchChapter($chapterId, $term);
66 return view('entities.list', ['entities' => $results]);
70 * Search for a list of entities and return a partial HTML response of matching entities.
71 * Returns the most popular entities if no search is provided.
73 public function searchEntitiesAjax(Request $request)
75 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
76 $searchTerm = $request->get('term', false);
77 $permission = $request->get('permission', 'view');
79 // Search for entities otherwise show most popular
80 if ($searchTerm !== false) {
81 $searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
82 $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20, $permission)['results'];
84 $entities = (new Popular())->run(20, 0, $entityTypes, $permission);
87 return view('search.parts.entity-ajax-list', ['entities' => $entities]);
91 * Search siblings items in the system.
93 public function searchSiblings(Request $request)
95 $type = $request->get('entity_type', null);
96 $id = $request->get('entity_id', null);
98 $entities = (new SiblingFetcher())->fetch($type, $id);
100 return view('entities.list-basic', ['entities' => $entities, 'style' => 'compact']);