]> BookStack Code Mirror - bookstack/blob - app/Search/SearchOptions.php
Added method for using enity ownership in relation queries
[bookstack] / app / Search / SearchOptions.php
1 <?php
2
3 namespace BookStack\Search;
4
5 use Illuminate\Http\Request;
6
7 class SearchOptions
8 {
9     public array $searches = [];
10     public array $exacts = [];
11     public array $tags = [];
12     public array $filters = [];
13
14     /**
15      * Create a new instance from a search string.
16      */
17     public static function fromString(string $search): self
18     {
19         $decoded = static::decode($search);
20         $instance = new SearchOptions();
21         foreach ($decoded as $type => $value) {
22             $instance->$type = $value;
23         }
24
25         return $instance;
26     }
27
28     /**
29      * Create a new instance from a request.
30      * Will look for a classic string term and use that
31      * Otherwise we'll use the details from an advanced search form.
32      */
33     public static function fromRequest(Request $request): self
34     {
35         if (!$request->has('search') && !$request->has('term')) {
36             return static::fromString('');
37         }
38
39         if ($request->has('term')) {
40             return static::fromString($request->get('term'));
41         }
42
43         $instance = new SearchOptions();
44         $inputs = $request->only(['search', 'types', 'filters', 'exact', 'tags']);
45
46         $parsedStandardTerms = static::parseStandardTermString($inputs['search'] ?? '');
47         $instance->searches = $parsedStandardTerms['terms'];
48         $instance->exacts = $parsedStandardTerms['exacts'];
49
50         array_push($instance->exacts, ...array_filter($inputs['exact'] ?? []));
51
52         $instance->tags = array_filter($inputs['tags'] ?? []);
53
54         foreach (($inputs['filters'] ?? []) as $filterKey => $filterVal) {
55             if (empty($filterVal)) {
56                 continue;
57             }
58             $instance->filters[$filterKey] = $filterVal === 'true' ? '' : $filterVal;
59         }
60
61         if (isset($inputs['types']) && count($inputs['types']) < 4) {
62             $instance->filters['type'] = implode('|', $inputs['types']);
63         }
64
65         return $instance;
66     }
67
68     /**
69      * Decode a search string into an array of terms.
70      */
71     protected static function decode(string $searchString): array
72     {
73         $terms = [
74             'searches' => [],
75             'exacts'   => [],
76             'tags'     => [],
77             'filters'  => [],
78         ];
79
80         $patterns = [
81             'exacts'  => '/"(.*?)"/',
82             'tags'    => '/\[(.*?)\]/',
83             'filters' => '/\{(.*?)\}/',
84         ];
85
86         // Parse special terms
87         foreach ($patterns as $termType => $pattern) {
88             $matches = [];
89             preg_match_all($pattern, $searchString, $matches);
90             if (count($matches) > 0) {
91                 $terms[$termType] = $matches[1];
92                 $searchString = preg_replace($pattern, '', $searchString);
93             }
94         }
95
96         // Parse standard terms
97         $parsedStandardTerms = static::parseStandardTermString($searchString);
98         array_push($terms['searches'], ...$parsedStandardTerms['terms']);
99         array_push($terms['exacts'], ...$parsedStandardTerms['exacts']);
100
101         // Split filter values out
102         $splitFilters = [];
103         foreach ($terms['filters'] as $filter) {
104             $explodedFilter = explode(':', $filter, 2);
105             $splitFilters[$explodedFilter[0]] = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
106         }
107         $terms['filters'] = $splitFilters;
108
109         return $terms;
110     }
111
112     /**
113      * Parse a standard search term string into individual search terms and
114      * extract any exact terms searches to be made.
115      *
116      * @return array{terms: array<string>, exacts: array<string>}
117      */
118     protected static function parseStandardTermString(string $termString): array
119     {
120         $terms = explode(' ', $termString);
121         $indexDelimiters = SearchIndex::$delimiters;
122         $parsed = [
123             'terms'  => [],
124             'exacts' => [],
125         ];
126
127         foreach ($terms as $searchTerm) {
128             if ($searchTerm === '') {
129                 continue;
130             }
131
132             $parsedList = (strpbrk($searchTerm, $indexDelimiters) === false) ? 'terms' : 'exacts';
133             $parsed[$parsedList][] = $searchTerm;
134         }
135
136         return $parsed;
137     }
138
139     /**
140      * Encode this instance to a search string.
141      */
142     public function toString(): string
143     {
144         $string = implode(' ', $this->searches ?? []);
145
146         foreach ($this->exacts as $term) {
147             $string .= ' "' . $term . '"';
148         }
149
150         foreach ($this->tags as $term) {
151             $string .= " [{$term}]";
152         }
153
154         foreach ($this->filters as $filterName => $filterVal) {
155             $string .= ' {' . $filterName . ($filterVal ? ':' . $filterVal : '') . '}';
156         }
157
158         return $string;
159     }
160 }