|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.helper.search |
|
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Search handler |
|
13
|
|
|
* |
|
14
|
|
|
* @package midcom.helper.search |
|
15
|
|
|
*/ |
|
16
|
|
|
class midcom_helper_search_handler_search extends midcom_baseclasses_components_handler |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Search form handler, nothing to do here. |
|
20
|
|
|
* |
|
21
|
|
|
* It uses the handler ID to distinguish between basic and advanced search forms. |
|
22
|
|
|
*/ |
|
23
|
2 |
|
public function _handler_searchform(Request $request, string $handler_id) |
|
24
|
|
|
{ |
|
25
|
2 |
|
$this->prepare_formdata($handler_id, $request); |
|
26
|
2 |
|
$this->populate_toolbar($request); |
|
27
|
2 |
|
return $this->show('search_form'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
4 |
|
private function prepare_formdata(string $handler_id, Request $request) |
|
31
|
|
|
{ |
|
32
|
4 |
|
$this->_request_data['query'] = $this->fetch($request, 'query', ''); |
|
33
|
4 |
|
if ($handler_id === 'advanced') { |
|
34
|
2 |
|
$this->_request_data['request_topic'] = trim($this->fetch($request, 'topic', '')); |
|
35
|
2 |
|
$this->_request_data['component'] = trim($this->fetch($request, 'component', '')); |
|
36
|
2 |
|
$this->_request_data['lastmodified'] = (int) trim($this->fetch($request, 'lastmodified', '0')); |
|
37
|
|
|
|
|
38
|
2 |
|
$this->_request_data['topics'] = ['' => $this->_l10n->get('search anywhere')]; |
|
39
|
2 |
|
$this->_request_data['components'] = ['' => $this->_l10n->get('search all content types')]; |
|
40
|
|
|
|
|
41
|
2 |
|
$nap = new midcom_helper_nav(); |
|
42
|
2 |
|
$this->search_nodes($nap->get_node($nap->get_root_node()), $nap, ''); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
4 |
|
$this->_request_data['type'] = $handler_id; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
private function fetch(Request $request, string $field, $default = null, bool $is_array = false) |
|
48
|
|
|
{ |
|
49
|
4 |
|
if ($is_array) { |
|
50
|
1 |
|
$method = 'all'; |
|
51
|
1 |
|
$default ??= []; |
|
52
|
|
|
} else { |
|
53
|
4 |
|
$method = 'get'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
if ($request->request->has($field)) { |
|
57
|
|
|
return $request->request->$method($field); |
|
58
|
|
|
} |
|
59
|
4 |
|
return $request->query->$method($field, $default); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Prepare the topic and component listings, this is a bit work intensive though, |
|
64
|
|
|
* we need to traverse everything. |
|
65
|
|
|
*/ |
|
66
|
2 |
|
private function search_nodes(array $node, midcom_helper_nav $nap, string $prefix) |
|
67
|
|
|
{ |
|
68
|
2 |
|
if ( !array_key_exists($node[MIDCOM_NAV_COMPONENT], $this->_request_data['components']) |
|
69
|
2 |
|
&& $node[MIDCOM_NAV_COMPONENT] != 'midcom.helper.search') { |
|
70
|
|
|
$l10n = $this->_i18n->get_l10n($node[MIDCOM_NAV_COMPONENT]); |
|
71
|
|
|
$this->_request_data['components'][$node[MIDCOM_NAV_COMPONENT]] = $l10n->get($node[MIDCOM_NAV_COMPONENT]); |
|
72
|
|
|
} |
|
73
|
2 |
|
$this->_request_data['topics'][$node[MIDCOM_NAV_FULLURL]] = "{$prefix}{$node[MIDCOM_NAV_NAME]}"; |
|
74
|
|
|
|
|
75
|
|
|
// Recurse |
|
76
|
2 |
|
$prefix .= "{$node[MIDCOM_NAV_NAME]} › "; |
|
77
|
2 |
|
foreach ($nap->get_nodes($node[MIDCOM_NAV_ID]) as $sub_node) { |
|
78
|
|
|
$this->search_nodes($sub_node, $nap, $prefix); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Expand arrays of custom rules to end of query |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $final_query reference to the query string to be passed on to the indexer. |
|
86
|
|
|
* @param mixed $terms array or string to append |
|
87
|
|
|
*/ |
|
88
|
1 |
|
private function append_terms_recursive(string &$final_query, $terms) |
|
89
|
|
|
{ |
|
90
|
1 |
|
if (is_array($terms)) { |
|
91
|
1 |
|
foreach ($terms as $term) { |
|
92
|
1 |
|
$this->append_terms_recursive($final_query, $term); |
|
93
|
|
|
} |
|
94
|
1 |
|
} elseif (is_string($terms)) { |
|
95
|
1 |
|
$final_query .= $terms; |
|
96
|
|
|
} else { |
|
97
|
|
|
debug_add('Don\'t know how to handle terms of type: ' . gettype($terms), MIDCOM_LOG_ERROR); |
|
98
|
|
|
debug_print_r('$terms', $terms); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Queries the information from the index and prepares to display the result page. |
|
104
|
|
|
*/ |
|
105
|
2 |
|
public function _handler_result(Request $request, array &$data) |
|
106
|
|
|
{ |
|
107
|
2 |
|
$type = $this->fetch($request, 'type', 'basic'); |
|
108
|
|
|
// If we don't have a query string, relocate to empty search form |
|
109
|
2 |
|
if (!$request->request->has('query') && !$request->query->has('query')) { |
|
110
|
|
|
debug_add('$_REQUEST["query"] is not set, relocating back to form', MIDCOM_LOG_INFO); |
|
111
|
|
|
return new midcom_response_relocate($this->router->generate($type)); |
|
112
|
|
|
} |
|
113
|
2 |
|
$this->prepare_formdata($type, $request); |
|
114
|
|
|
|
|
115
|
2 |
|
if ( count(explode(' ', $data['query'])) == 1 |
|
116
|
2 |
|
&& !str_contains($data['query'], '*') |
|
117
|
2 |
|
&& $this->_config->get('single_term_auto_wildcard')) { |
|
118
|
|
|
//If there is only one search term append * to the query if auto_wildcard is enabled |
|
119
|
|
|
$data['query'] .= '*'; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
if ($type == 'basic') { |
|
123
|
1 |
|
$indexer = midcom::get()->indexer; |
|
124
|
1 |
|
$final_query = $data['query']; |
|
125
|
1 |
|
debug_add("Final query: {$final_query}"); |
|
126
|
1 |
|
$result = $indexer->query($final_query); |
|
127
|
1 |
|
} elseif ($type == 'advanced') { |
|
128
|
1 |
|
$result = $this->do_advanced_query($data, $this->fetch($request, 'append_terms', is_array: true)); |
|
129
|
|
|
} else { |
|
130
|
|
|
throw new midcom_error_notfound('unknown query type'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
$this->process_results($result, (int) $this->fetch($request, 'page', 1)); |
|
134
|
2 |
|
$this->populate_toolbar($request); |
|
135
|
|
|
|
|
136
|
2 |
|
if ($data['document_count'] > 0) { |
|
137
|
|
|
return $this->show('results'); |
|
138
|
|
|
} |
|
139
|
2 |
|
return $this->show('no_match'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
4 |
|
private function populate_toolbar(Request $request) |
|
143
|
|
|
{ |
|
144
|
4 |
|
$other_type = ($this->_request_data['type'] == 'advanced') ? 'basic' : 'advanced'; |
|
145
|
4 |
|
$this->_request_data['params'] = ''; |
|
146
|
4 |
|
if ($request->query->count() > 0) { |
|
147
|
2 |
|
$request->query->set('type', $other_type); |
|
148
|
2 |
|
$this->_request_data['params'] = '?' . $request->getQueryString(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
4 |
|
$url = ''; |
|
152
|
4 |
|
if ($this->_request_data['type'] == 'basic') { |
|
153
|
2 |
|
$url = 'advanced/'; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
4 |
|
$this->_view_toolbar->add_item([ |
|
157
|
4 |
|
MIDCOM_TOOLBAR_URL => $url . $this->_request_data['params'], |
|
158
|
4 |
|
MIDCOM_TOOLBAR_LABEL => $this->_l10n->get($other_type . ' search'), |
|
159
|
4 |
|
MIDCOM_TOOLBAR_GLYPHICON => 'search', |
|
160
|
4 |
|
]); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
2 |
|
private function process_results(array $result, int $page) |
|
164
|
|
|
{ |
|
165
|
2 |
|
$count = count($result); |
|
166
|
2 |
|
$this->_request_data['document_count'] = $count; |
|
167
|
|
|
|
|
168
|
2 |
|
if ($count == 0) { |
|
169
|
2 |
|
midcom::get()->cache->content->uncached(); |
|
170
|
|
|
} else { |
|
171
|
|
|
$results_per_page = $this->_config->get('results_per_page'); |
|
172
|
|
|
$max_pages = ceil($count / $results_per_page); |
|
173
|
|
|
$page = min($page, $max_pages); |
|
174
|
|
|
$first_document_id = ($page - 1) * $results_per_page; |
|
175
|
|
|
$last_document_id = min(($count - 1), (($page * $results_per_page) - 1)); |
|
176
|
|
|
|
|
177
|
|
|
$this->_request_data['page'] = $page; |
|
178
|
|
|
$this->_request_data['max_pages'] = $max_pages; |
|
179
|
|
|
$this->_request_data['first_document_number'] = $first_document_id + 1; |
|
180
|
|
|
$this->_request_data['last_document_number'] = $last_document_id + 1; |
|
181
|
|
|
$this->_request_data['shown_documents'] = $last_document_id - $first_document_id + 1; |
|
182
|
|
|
$this->_request_data['result'] = array_slice($result, $first_document_id, $results_per_page); |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
// Register GUIDs for cache engine |
|
185
|
|
|
foreach ($this->_request_data['result'] as $doc) { |
|
186
|
|
|
if (!mgd_is_guid($doc->source)) { |
|
187
|
|
|
// Non-Midgard results don't need to go through cache registration |
|
188
|
|
|
continue; |
|
189
|
|
|
} |
|
190
|
|
|
midcom::get()->cache->content->register($doc->source); |
|
191
|
|
|
} |
|
192
|
|
|
reset($this->_request_data['result']); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
1 |
|
private function do_advanced_query(array &$data, $append_terms) : array |
|
197
|
|
|
{ |
|
198
|
1 |
|
$filter = new midcom_services_indexer_filter_chained; |
|
199
|
1 |
|
if ($data['lastmodified'] > 0) { |
|
200
|
1 |
|
$filter->add_filter(new midcom_services_indexer_filter_date('__EDITED', $data['lastmodified'], 0)); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
$final_query = ''; |
|
204
|
1 |
|
if ($data['query'] != '') { |
|
205
|
1 |
|
$final_query = (midcom::get()->config->get('indexer_backend') == 'solr') ? $data['query'] : "({$data['query']})"; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
1 |
|
if ($data['request_topic'] != '') { |
|
209
|
|
|
$filter->add_filter(new midcom_services_indexer_filter_string('__TOPIC_URL', '"' . $data['request_topic'] . '*"')); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
1 |
|
if ($data['component'] != '') { |
|
213
|
|
|
$filter->add_filter(new midcom_services_indexer_filter_string('__COMPONENT', $data['component'])); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
// Way to add very custom terms |
|
217
|
1 |
|
if ($append_terms) { |
|
218
|
1 |
|
$this->append_terms_recursive($final_query, $append_terms); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
1 |
|
debug_add("Final query: {$final_query}"); |
|
222
|
1 |
|
$indexer = midcom::get()->indexer; |
|
223
|
|
|
|
|
224
|
1 |
|
if ($filter->count() == 0) { |
|
225
|
|
|
$filter = null; |
|
226
|
|
|
} |
|
227
|
1 |
|
return $indexer->query($final_query, $filter); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Prepare OpenSearch data file for browser search bar integration. |
|
232
|
|
|
*/ |
|
233
|
1 |
|
public function _handler_opensearchdescription(array &$data) |
|
234
|
|
|
{ |
|
235
|
1 |
|
midcom::get()->cache->content->content_type("application/opensearchdescription+xml; charset=UTF-8"); |
|
236
|
1 |
|
midcom::get()->skip_page_style = true; |
|
237
|
|
|
|
|
238
|
1 |
|
$data['node'] = $this->_topic; |
|
239
|
1 |
|
return $this->show('opensearch_description'); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|