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); |
26
|
2 |
|
$this->populate_toolbar($request); |
27
|
2 |
|
return $this->show('search_form'); |
28
|
|
|
} |
29
|
|
|
|
30
|
4 |
|
private function prepare_formdata(string $handler_id) |
31
|
|
|
{ |
32
|
4 |
|
$this->_request_data['query'] = (array_key_exists('query', $_REQUEST) ? $_REQUEST['query'] : ''); |
33
|
4 |
|
if ($handler_id === 'advanced') { |
34
|
2 |
|
$this->_request_data['request_topic'] = (array_key_exists('topic', $_REQUEST) ? $_REQUEST['topic'] : ''); |
35
|
2 |
|
$this->_request_data['component'] = (array_key_exists('component', $_REQUEST) ? $_REQUEST['component'] : ''); |
36
|
2 |
|
$this->_request_data['lastmodified'] = (array_key_exists('lastmodified', $_REQUEST) ? ((integer) $_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
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Prepare the topic and component listings, this is a bit work intensive though, |
49
|
|
|
* we need to traverse everything. |
50
|
|
|
*/ |
51
|
2 |
|
private function search_nodes(array $node, midcom_helper_nav $nap, string $prefix) |
52
|
|
|
{ |
53
|
2 |
|
if ( !array_key_exists($node[MIDCOM_NAV_COMPONENT], $this->_request_data['components']) |
54
|
2 |
|
&& $node[MIDCOM_NAV_COMPONENT] != 'midcom.helper.search') { |
55
|
|
|
$l10n = $this->_i18n->get_l10n($node[MIDCOM_NAV_COMPONENT]); |
56
|
|
|
$this->_request_data['components'][$node[MIDCOM_NAV_COMPONENT]] = $l10n->get($node[MIDCOM_NAV_COMPONENT]); |
57
|
|
|
} |
58
|
2 |
|
$this->_request_data['topics'][$node[MIDCOM_NAV_FULLURL]] = "{$prefix}{$node[MIDCOM_NAV_NAME]}"; |
59
|
|
|
|
60
|
|
|
// Recurse |
61
|
2 |
|
$prefix .= "{$node[MIDCOM_NAV_NAME]} › "; |
62
|
2 |
|
foreach ($nap->get_nodes($node[MIDCOM_NAV_ID]) as $sub_node) { |
63
|
|
|
$this->search_nodes($sub_node, $nap, $prefix); |
64
|
|
|
} |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Expand arrays of custom rules to end of query |
69
|
|
|
* |
70
|
|
|
* @param string $final_query reference to the query string to be passed on to the indexer. |
71
|
|
|
* @param mixed $terms array or string to append |
72
|
|
|
*/ |
73
|
1 |
|
private function append_terms_recursive(string &$final_query, $terms) |
74
|
|
|
{ |
75
|
1 |
|
if (is_array($terms)) { |
76
|
1 |
|
foreach ($terms as $term) { |
77
|
1 |
|
$this->append_terms_recursive($final_query, $term); |
78
|
|
|
} |
79
|
1 |
|
} elseif (is_string($terms)) { |
80
|
1 |
|
$final_query .= $terms; |
81
|
|
|
} else { |
82
|
|
|
debug_add('Don\'t know how to handle terms of type: ' . gettype($terms), MIDCOM_LOG_ERROR); |
83
|
|
|
debug_print_r('$terms', $terms); |
84
|
|
|
} |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Queries the information from the index and prepares to display the result page. |
89
|
|
|
*/ |
90
|
2 |
|
public function _handler_result(Request $request, array &$data) |
91
|
|
|
{ |
92
|
2 |
|
$this->prepare_query_data(); |
93
|
|
|
// If we don't have a query string, relocate to empty search form |
94
|
2 |
|
if (!isset($_REQUEST['query'])) { |
95
|
|
|
debug_add('$_REQUEST["query"] is not set, relocating back to form', MIDCOM_LOG_INFO); |
96
|
|
|
$url = ($_REQUEST['type'] == 'basic') ? '' : 'advanced/'; |
97
|
|
|
return new midcom_response_relocate($url); |
98
|
|
|
} |
99
|
2 |
|
$this->prepare_formdata($_REQUEST['type']); |
100
|
|
|
|
101
|
2 |
|
if ( count(explode(' ', $data['query'])) == 1 |
102
|
2 |
|
&& !str_contains($data['query'], '*') |
103
|
2 |
|
&& $this->_config->get('single_term_auto_wildcard')) { |
104
|
|
|
//If there is only one search term append * to the query if auto_wildcard is enabled |
105
|
|
|
$data['query'] .= '*'; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
if ($data['type'] == 'basic') { |
109
|
1 |
|
$indexer = midcom::get()->indexer; |
110
|
1 |
|
$final_query = $data['query']; |
111
|
1 |
|
debug_add("Final query: {$final_query}"); |
112
|
1 |
|
$result = $indexer->query($final_query); |
113
|
1 |
|
} elseif ($data['type'] == 'advanced') { |
114
|
1 |
|
$result = $this->do_advanced_query($data); |
115
|
|
|
} else { |
116
|
|
|
throw new midcom_error_notfound('unknown query type'); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
$this->process_results($result); |
120
|
2 |
|
$this->populate_toolbar($request); |
121
|
2 |
|
} |
122
|
|
|
|
123
|
4 |
|
private function populate_toolbar(Request $request) |
124
|
|
|
{ |
125
|
4 |
|
$other_type = ($this->_request_data['type'] == 'advanced') ? 'basic' : 'advanced'; |
126
|
4 |
|
$this->_request_data['params'] = ''; |
127
|
4 |
|
if ($request->query->count() > 0) { |
128
|
|
|
$request->query->set('type', $other_type); |
129
|
|
|
$this->_request_data['params'] = '?' . $request->getQueryString(); |
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
$url = ''; |
133
|
4 |
|
if ($this->_request_data['type'] == 'basic') { |
134
|
2 |
|
$url = 'advanced/'; |
135
|
|
|
} |
136
|
|
|
|
137
|
4 |
|
$this->_view_toolbar->add_item([ |
138
|
4 |
|
MIDCOM_TOOLBAR_URL => $url . $this->_request_data['params'], |
139
|
4 |
|
MIDCOM_TOOLBAR_LABEL => $this->_l10n->get($other_type . ' search'), |
140
|
4 |
|
MIDCOM_TOOLBAR_GLYPHICON => 'search', |
141
|
|
|
]); |
142
|
4 |
|
} |
143
|
|
|
|
144
|
2 |
|
private function process_results(array $result) |
145
|
|
|
{ |
146
|
2 |
|
$count = count($result); |
147
|
2 |
|
$this->_request_data['document_count'] = $count; |
148
|
|
|
|
149
|
2 |
|
if ($count == 0) { |
150
|
2 |
|
midcom::get()->cache->content->uncached(); |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
if ($count > 0) { |
154
|
|
|
$results_per_page = $this->_config->get('results_per_page'); |
155
|
|
|
$max_pages = ceil($count / $results_per_page); |
156
|
|
|
$page = min($_REQUEST['page'], $max_pages); |
157
|
|
|
$first_document_id = ($page - 1) * $results_per_page; |
158
|
|
|
$last_document_id = min(($count - 1), (($page * $results_per_page) - 1)); |
159
|
|
|
|
160
|
|
|
$this->_request_data['page'] = $page; |
161
|
|
|
$this->_request_data['max_pages'] = $max_pages; |
162
|
|
|
$this->_request_data['first_document_number'] = $first_document_id + 1; |
163
|
|
|
$this->_request_data['last_document_number'] = $last_document_id + 1; |
164
|
|
|
$this->_request_data['shown_documents'] = $last_document_id - $first_document_id + 1; |
165
|
|
|
$this->_request_data['result'] = array_slice($result, $first_document_id, $results_per_page); |
|
|
|
|
166
|
|
|
|
167
|
|
|
// Register GUIDs for cache engine |
168
|
|
|
foreach ($this->_request_data['result'] as $doc) { |
169
|
|
|
if (!mgd_is_guid($doc->source)) { |
170
|
|
|
// Non-Midgard results don't need to go through cache registration |
171
|
|
|
continue; |
172
|
|
|
} |
173
|
|
|
midcom::get()->cache->content->register($doc->source); |
174
|
|
|
} |
175
|
|
|
reset($this->_request_data['result']); |
176
|
|
|
} |
177
|
2 |
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sane defaults for REQUEST vars |
181
|
|
|
*/ |
182
|
2 |
|
private function prepare_query_data() |
183
|
|
|
{ |
184
|
|
|
$defaults = [ |
185
|
2 |
|
'type' => 'basic', |
186
|
|
|
'page' => 1, |
187
|
|
|
'component' => '', |
188
|
|
|
'topic' => '', |
189
|
|
|
'lastmodified' => 0 |
190
|
|
|
]; |
191
|
|
|
|
192
|
2 |
|
$_REQUEST = array_merge($defaults, $_REQUEST); |
193
|
2 |
|
} |
194
|
|
|
|
195
|
1 |
|
private function do_advanced_query(array &$data) : array |
196
|
|
|
{ |
197
|
1 |
|
$data['request_topic'] = trim($_REQUEST['topic']); |
198
|
1 |
|
$data['component'] = trim($_REQUEST['component']); |
199
|
1 |
|
$data['lastmodified'] = (integer) trim($_REQUEST['lastmodified']); |
200
|
1 |
|
$filter = new midcom_services_indexer_filter_chained; |
201
|
1 |
|
if ($data['lastmodified'] > 0) { |
202
|
1 |
|
$filter->add_filter(new midcom_services_indexer_filter_date('__EDITED', $data['lastmodified'], 0)); |
203
|
|
|
} |
204
|
|
|
|
205
|
1 |
|
$final_query = ''; |
206
|
1 |
|
if ($data['query'] != '') { |
207
|
1 |
|
$final_query = (midcom::get()->config->get('indexer_backend') == 'solr') ? $data['query'] : "({$data['query']})"; |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
if ($data['request_topic'] != '') { |
211
|
|
|
$filter->add_filter(new midcom_services_indexer_filter_string('__TOPIC_URL', '"' . $data['request_topic'] . '*"')); |
212
|
|
|
} |
213
|
|
|
|
214
|
1 |
|
if ($data['component'] != '') { |
215
|
|
|
$filter->add_filter(new midcom_services_indexer_filter_string('__COMPONENT', $data['component'])); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// Way to add very custom terms |
219
|
1 |
|
if (isset($_REQUEST['append_terms'])) { |
220
|
1 |
|
$this->append_terms_recursive($final_query, $_REQUEST['append_terms']); |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
debug_add("Final query: {$final_query}"); |
224
|
1 |
|
$indexer = midcom::get()->indexer; |
225
|
|
|
|
226
|
1 |
|
if ($filter->count() == 0) { |
227
|
|
|
$filter = null; |
228
|
|
|
} |
229
|
1 |
|
return $indexer->query($final_query, $filter); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Displays the resultset. |
234
|
|
|
*/ |
235
|
2 |
|
public function _show_result(string $handler_id, array &$data) |
236
|
|
|
{ |
237
|
2 |
|
if ($data['document_count'] > 0) { |
238
|
|
|
midcom_show_style('results'); |
239
|
|
|
} else { |
240
|
2 |
|
midcom_show_style('no_match'); |
241
|
|
|
} |
242
|
2 |
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Prepare OpenSearch data file for browser search bar integration. |
246
|
|
|
*/ |
247
|
1 |
|
public function _handler_opensearchdescription(array &$data) |
248
|
|
|
{ |
249
|
1 |
|
midcom::get()->cache->content->content_type("application/opensearchdescription+xml; charset=UTF-8"); |
250
|
1 |
|
midcom::get()->skip_page_style = true; |
251
|
|
|
|
252
|
1 |
|
$data['node'] = $this->_topic; |
253
|
1 |
|
return $this->show('opensearch_description'); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|