1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Entities\Queries\Popular;
4 use BookStack\Entities\Tools\SearchRunner;
5 use BookStack\Entities\Tools\ShelfContext;
6 use BookStack\Entities\Tools\SearchOptions;
7 use BookStack\Entities\Tools\SiblingFetcher;
8 use Illuminate\Http\Request;
10 class SearchController extends Controller
12 protected $searchRunner;
13 protected $entityContextManager;
15 public function __construct(
16 SearchRunner $searchRunner,
17 ShelfContext $entityContextManager
19 $this->searchRunner = $searchRunner;
20 $this->entityContextManager = $entityContextManager;
24 * Searches all entities.
26 public function search(Request $request)
28 $searchOpts = SearchOptions::fromRequest($request);
29 $fullSearchString = $searchOpts->toString();
30 $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
32 $page = intval($request->get('page', '0')) ?: 1;
33 $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page+1));
35 $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
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);
54 return view('partials.entity-list', ['entities' => $results]);
58 * Searches all entities within a chapter.
60 public function searchChapter(Request $request, int $chapterId)
62 $term = $request->get('term', '');
63 $results = $this->searchRunner->searchChapter($chapterId, $term);
64 return view('partials.entity-list', ['entities' => $results]);
68 * Search for a list of entities and return a partial HTML response of matching entities.
69 * Returns the most popular entities if no search is provided.
71 public function searchEntitiesAjax(Request $request)
73 $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
74 $searchTerm = $request->get('term', false);
75 $permission = $request->get('permission', 'view');
77 // Search for entities otherwise show most popular
78 if ($searchTerm !== false) {
79 $searchTerm .= ' {type:'. implode('|', $entityTypes) .'}';
80 $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20, $permission)['results'];
82 $entities = (new Popular)->run(20, 0, $entityTypes, $permission);
85 return view('search.entity-ajax-list', ['entities' => $entities]);
89 * Search siblings items in the system.
91 public function searchSiblings(Request $request)
93 $type = $request->get('entity_type', null);
94 $id = $request->get('entity_id', null);
96 $entities = (new SiblingFetcher)->fetch($type, $id);
97 return view('partials.entity-list-basic', ['entities' => $entities, 'style' => 'compact']);