1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
6
|
|
|
* |
7
|
|
|
* @license GNU General Public License version 3 or later. |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.txt file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Kitodo\Dlf\Controller; |
13
|
|
|
|
14
|
|
|
use Kitodo\Dlf\Common\Doc; |
15
|
|
|
use Kitodo\Dlf\Common\DocumentList; |
16
|
|
|
use Kitodo\Dlf\Common\Helper; |
17
|
|
|
use Kitodo\Dlf\Common\Indexer; |
18
|
|
|
use Kitodo\Dlf\Common\Solr; |
19
|
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
20
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
21
|
|
|
use Kitodo\Dlf\Domain\Repository\CollectionRepository; |
22
|
|
|
use Kitodo\Dlf\Domain\Repository\MetadataRepository; |
23
|
|
|
|
24
|
|
|
class SearchController extends AbstractController |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var CollectionRepository |
28
|
|
|
*/ |
29
|
|
|
protected $collectionRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param CollectionRepository $collectionRepository |
33
|
|
|
*/ |
34
|
|
|
public function injectCollectionRepository(CollectionRepository $collectionRepository) |
35
|
|
|
{ |
36
|
|
|
$this->collectionRepository = $collectionRepository; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MetadataRepository |
41
|
|
|
*/ |
42
|
|
|
protected $metadataRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param MetadataRepository $metadataRepository |
46
|
|
|
*/ |
47
|
|
|
public function injectMetadataRepository(MetadataRepository $metadataRepository) |
48
|
|
|
{ |
49
|
|
|
$this->metadataRepository = $metadataRepository; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Search Action |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function searchAction() |
58
|
|
|
{ |
59
|
|
|
// if search was triggered, get search parameters from POST variables |
60
|
|
|
$searchParams = $this->getParametersSafely('searchParameter'); |
61
|
|
|
|
62
|
|
|
// output is done by main action |
63
|
|
|
$this->forward('main', null, null, ['searchParameter' => $searchParams]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Main action |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function mainAction() |
72
|
|
|
{ |
73
|
|
|
// Quit without doing anything if required variables are not set. |
74
|
|
|
if (empty($this->settings['solrcore'])) { |
75
|
|
|
$this->logger->warning('Incomplete plugin configuration'); |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// if search was triggered, get search parameters from POST variables |
80
|
|
|
$searchParams = $this->getParametersSafely('searchParameter'); |
81
|
|
|
|
82
|
|
|
// get all sortable metadata records |
83
|
|
|
$sortableMetadata = $this->metadataRepository->findByIsSortable(true); |
|
|
|
|
84
|
|
|
|
85
|
|
|
// get all metadata records to be shown in results |
86
|
|
|
$listedMetadata = $this->metadataRepository->findByIsListed(true); |
|
|
|
|
87
|
|
|
|
88
|
|
|
// get results from search |
89
|
|
|
// find all documents from Solr |
90
|
|
|
$solrResults = $this->documentRepository->findSolrByCollection('', $this->settings, $searchParams, $listedMetadata); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// Pagination of Results: Pass the currentPage to the fluid template to calculate current index of search result. |
93
|
|
|
$widgetPage = $this->getParametersSafely('@widget_0'); |
94
|
|
|
if (empty($widgetPage)) { |
95
|
|
|
$widgetPage = ['currentPage' => 1]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$documents = $solrResults['documents']; |
99
|
|
|
//$this->view->assign('metadata', $sortableMetadata); |
100
|
|
|
$this->view->assign('documents', $documents); |
101
|
|
|
$this->view->assign('widgetPage', $widgetPage); |
102
|
|
|
$this->view->assign('lastSearch', $searchParams); |
103
|
|
|
|
104
|
|
|
$this->view->assign('listedMetadata', $listedMetadata); |
105
|
|
|
$this->view->assign('sortableMetadata', $sortableMetadata); |
106
|
|
|
|
107
|
|
|
// ABTODO: facets and extended search might fail |
108
|
|
|
// Add the facets menu |
109
|
|
|
$this->addFacetsMenu(); |
110
|
|
|
|
111
|
|
|
// Get additional fields for extended search. |
112
|
|
|
$this->addExtendedSearch(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Adds the current document's UID or parent ID to the search form |
117
|
|
|
* |
118
|
|
|
* @access protected |
119
|
|
|
* |
120
|
|
|
* @return string HTML input fields with current document's UID |
121
|
|
|
*/ |
122
|
|
|
protected function addCurrentDocument() |
123
|
|
|
{ |
124
|
|
|
// Load current list object. |
125
|
|
|
$list = GeneralUtility::makeInstance(DocumentList::class); |
126
|
|
|
// Load current document. |
127
|
|
|
if ( |
128
|
|
|
!empty($this->requestData['id']) |
129
|
|
|
&& \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->requestData['id']) |
130
|
|
|
) { |
131
|
|
|
$this->loadDocument($this->requestData); |
132
|
|
|
// Get document's UID |
133
|
|
|
if ($this->document) { |
134
|
|
|
$this->view->assign('DOCUMENT_ID', $this->document->getUid()); |
135
|
|
|
} |
136
|
|
|
} elseif (!empty($list->metadata['options']['params']['filterquery'])) { |
137
|
|
|
// Get document's UID from search metadata. |
138
|
|
|
// The string may be e.g. "{!join from=uid to=partof}uid:{!join from=uid to=partof}uid:2" OR {!join from=uid to=partof}uid:2 OR uid:2" |
139
|
|
|
// or "collection_faceting:("Some Collection Title")" |
140
|
|
|
foreach ($list->metadata['options']['params']['filterquery'] as $facet) { |
141
|
|
|
if (($lastUidPos = strrpos($facet['query'], 'uid:')) !== false) { |
142
|
|
|
$facetKeyVal = explode(':', substr($facet['query'], $lastUidPos)); |
143
|
|
|
if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($facetKeyVal[1])) { |
144
|
|
|
$documentId = (int) $facetKeyVal[1]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
if (!empty($documentId)) { |
149
|
|
|
$this->view->assign('DOCUMENT_ID', $documentId); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Adds the current collection's UID to the search form |
157
|
|
|
* |
158
|
|
|
* @access protected |
159
|
|
|
* |
160
|
|
|
* @return string HTML input fields with current document's UID and parent ID |
161
|
|
|
*/ |
162
|
|
|
protected function addCurrentCollection() |
163
|
|
|
{ |
164
|
|
|
// Load current collection. |
165
|
|
|
$list = GeneralUtility::makeInstance(DocumentList::class); |
166
|
|
|
if ( |
167
|
|
|
!empty($list->metadata['options']['source']) |
168
|
|
|
&& $list->metadata['options']['source'] == 'collection' |
169
|
|
|
) { |
170
|
|
|
$this->view->assign('COLLECTION_ID', $list->metadata['options']['select']); |
171
|
|
|
// Get collection's UID. |
172
|
|
|
} elseif (!empty($list->metadata['options']['params']['filterquery'])) { |
173
|
|
|
// Get collection's UID from search metadata. |
174
|
|
|
foreach ($list->metadata['options']['params']['filterquery'] as $facet) { |
175
|
|
|
$facetKeyVal = explode(':', $facet['query'], 2); |
176
|
|
|
if ( |
177
|
|
|
$facetKeyVal[0] == 'collection_faceting' |
178
|
|
|
&& !strpos($facetKeyVal[1], '" OR "') |
179
|
|
|
) { |
180
|
|
|
$collectionId = Helper::getUidFromIndexName(trim($facetKeyVal[1], '(")'), 'tx_dlf_collections'); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
$this->view->assign('COLLECTION_ID', $collectionId); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Adds the facets menu to the search form |
189
|
|
|
* |
190
|
|
|
* @access protected |
191
|
|
|
* |
192
|
|
|
* @return string HTML output of facets menu |
193
|
|
|
*/ |
194
|
|
|
protected function addFacetsMenu() |
195
|
|
|
{ |
196
|
|
|
// Check for typoscript configuration to prevent fatal error. |
197
|
|
|
if (empty($this->settings['facetsConf'])) { |
198
|
|
|
$this->logger->warning('Incomplete plugin configuration'); |
199
|
|
|
return ''; |
200
|
|
|
} |
201
|
|
|
// Quit without doing anything if no facets are selected. |
202
|
|
|
if (empty($this->settings['facets']) && empty($this->settings['facetCollections'])) { |
203
|
|
|
return ''; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// Get facets from plugin configuration. |
207
|
|
|
$facets = []; |
208
|
|
|
foreach (GeneralUtility::trimExplode(',', $this->settings['facets'], true) as $facet) { |
209
|
|
|
$facets[$facet . '_faceting'] = Helper::translate($facet, 'tx_dlf_metadata', $this->settings['storagePid']); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$this->view->assign('facetsMenu', $this->makeFacetsMenuArray($facets)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* This builds a menu array for HMENU |
217
|
|
|
* |
218
|
|
|
* @access public |
219
|
|
|
* |
220
|
|
|
* @param string $content: The PlugIn content |
221
|
|
|
* @param array $conf: The PlugIn configuration |
222
|
|
|
* |
223
|
|
|
* @return array HMENU array |
224
|
|
|
*/ |
225
|
|
|
public function makeFacetsMenuArray($facets) |
226
|
|
|
{ |
227
|
|
|
$menuArray = []; |
228
|
|
|
// Set default value for facet search. |
229
|
|
|
$search = [ |
230
|
|
|
'query' => '*', |
231
|
|
|
'params' => [ |
232
|
|
|
'component' => [ |
233
|
|
|
'facetset' => [ |
234
|
|
|
'facet' => [] |
235
|
|
|
] |
236
|
|
|
] |
237
|
|
|
] |
238
|
|
|
]; |
239
|
|
|
// Extract query and filter from last search. |
240
|
|
|
$list = GeneralUtility::makeInstance(DocumentList::class); |
241
|
|
|
if (!empty($list->metadata['options']['source'])) { |
242
|
|
|
if ($list->metadata['options']['source'] == 'search') { |
243
|
|
|
$search['query'] = $list->metadata['options']['select']; |
244
|
|
|
} |
245
|
|
|
$search['params'] = $list->metadata['options']['params']; |
246
|
|
|
} |
247
|
|
|
// Get applicable facets. |
248
|
|
|
$solr = Solr::getInstance($this->settings['solrcore']); |
249
|
|
|
if (!$solr->ready) { |
250
|
|
|
$this->logger->error('Apache Solr not available'); |
251
|
|
|
return []; |
252
|
|
|
} |
253
|
|
|
// Set needed parameters for facet search. |
254
|
|
|
if (empty($search['params']['filterquery'])) { |
255
|
|
|
$search['params']['filterquery'] = []; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
foreach (array_keys($facets) as $field) { |
259
|
|
|
$search['params']['component']['facetset']['facet'][] = [ |
260
|
|
|
'type' => 'field', |
261
|
|
|
'key' => $field, |
262
|
|
|
'field' => $field, |
263
|
|
|
'limit' => $this->settings['limitFacets'], |
264
|
|
|
'sort' => isset($this->settings['sortingFacets']) ? $this->settings['sortingFacets'] : 'count' |
265
|
|
|
]; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
// Set additional query parameters. |
269
|
|
|
$search['params']['start'] = 0; |
270
|
|
|
$search['params']['rows'] = 0; |
271
|
|
|
// Set query. |
272
|
|
|
$search['params']['query'] = $search['query']; |
273
|
|
|
// Perform search. |
274
|
|
|
$selectQuery = $solr->service->createSelect($search['params']); |
275
|
|
|
$results = $solr->service->select($selectQuery); |
276
|
|
|
$facet = $results->getFacetSet(); |
277
|
|
|
|
278
|
|
|
$facetCollectionArray = []; |
279
|
|
|
|
280
|
|
|
// replace everything expect numbers and comma |
281
|
|
|
$facetCollections = preg_replace('/[^0-9,]/', '', $this->settings['facetCollections']); |
282
|
|
|
|
283
|
|
|
if (!empty($facetCollections)) { |
284
|
|
|
$collections = $this->collectionRepository->findCollectionsBySettings(['collections' => $facetCollections]); |
285
|
|
|
|
286
|
|
|
/** @var Collection $collection */ |
287
|
|
|
foreach ($collections as $collection) { |
288
|
|
|
$facetCollectionArray[] = $collection->getIndexName(); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
// Process results. |
293
|
|
|
if ($facet) { |
294
|
|
|
foreach ($facet as $field => $values) { |
295
|
|
|
$entryArray = []; |
296
|
|
|
$entryArray['title'] = htmlspecialchars($facets[$field]); |
297
|
|
|
$entryArray['count'] = 0; |
298
|
|
|
$entryArray['_OVERRIDE_HREF'] = ''; |
299
|
|
|
$entryArray['doNotLinkIt'] = 1; |
300
|
|
|
$entryArray['ITEM_STATE'] = 'NO'; |
301
|
|
|
// Count number of facet values. |
302
|
|
|
$i = 0; |
303
|
|
|
foreach ($values as $value => $count) { |
304
|
|
|
if ($count > 0) { |
305
|
|
|
// check if facet collection configuration exists |
306
|
|
|
if (!empty($this->settings['facetCollections'])) { |
307
|
|
|
if ($field == "collection_faceting" && !in_array($value, $facetCollectionArray)) { |
308
|
|
|
continue; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
$entryArray['count']++; |
312
|
|
|
if ($entryArray['ITEM_STATE'] == 'NO') { |
313
|
|
|
$entryArray['ITEM_STATE'] = 'IFSUB'; |
314
|
|
|
} |
315
|
|
|
$entryArray['_SUB_MENU'][] = $this->getFacetsMenuEntry($field, $value, $count, $search, $entryArray['ITEM_STATE']); |
316
|
|
|
if (++$i == $this->settings['limit']) { |
317
|
|
|
break; |
318
|
|
|
} |
319
|
|
|
} else { |
320
|
|
|
break; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
$menuArray[] = $entryArray; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
return $menuArray; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Creates an array for a HMENU entry of a facet value. |
331
|
|
|
* |
332
|
|
|
* @access protected |
333
|
|
|
* |
334
|
|
|
* @param string $field: The facet's index_name |
335
|
|
|
* @param string $value: The facet's value |
336
|
|
|
* @param int $count: Number of hits for this facet |
337
|
|
|
* @param array $search: The parameters of the current search query |
338
|
|
|
* @param string &$state: The state of the parent item |
339
|
|
|
* |
340
|
|
|
* @return array The array for the facet's menu entry |
341
|
|
|
*/ |
342
|
|
|
protected function getFacetsMenuEntry($field, $value, $count, $search, &$state) |
343
|
|
|
{ |
344
|
|
|
$entryArray = []; |
345
|
|
|
// Translate value. |
346
|
|
|
if ($field == 'owner_faceting') { |
347
|
|
|
// Translate name of holding library. |
348
|
|
|
$entryArray['title'] = htmlspecialchars(Helper::translate($value, 'tx_dlf_libraries', $this->settings['storagePid'])); |
349
|
|
|
} elseif ($field == 'type_faceting') { |
350
|
|
|
// Translate document type. |
351
|
|
|
$entryArray['title'] = htmlspecialchars(Helper::translate($value, 'tx_dlf_structures', $this->settings['storagePid'])); |
352
|
|
|
} elseif ($field == 'collection_faceting') { |
353
|
|
|
// Translate name of collection. |
354
|
|
|
$entryArray['title'] = htmlspecialchars(Helper::translate($value, 'tx_dlf_collections', $this->settings['storagePid'])); |
355
|
|
|
} elseif ($field == 'language_faceting') { |
356
|
|
|
// Translate ISO 639 language code. |
357
|
|
|
$entryArray['title'] = htmlspecialchars(Helper::getLanguageName($value)); |
358
|
|
|
} else { |
359
|
|
|
$entryArray['title'] = htmlspecialchars($value); |
360
|
|
|
} |
361
|
|
|
$entryArray['count'] = $count; |
362
|
|
|
$entryArray['doNotLinkIt'] = 0; |
363
|
|
|
// Check if facet is already selected. |
364
|
|
|
$queryColumn = array_column($search['params']['filterquery'], 'query'); |
365
|
|
|
$index = array_search($field . ':("' . Solr::escapeQuery($value) . '")', $queryColumn); |
366
|
|
|
if ($index !== false) { |
367
|
|
|
// Facet is selected, thus remove it from filter. |
368
|
|
|
unset($queryColumn[$index]); |
369
|
|
|
$queryColumn = array_values($queryColumn); |
370
|
|
|
$entryArray['ITEM_STATE'] = 'CUR'; |
371
|
|
|
$state = 'ACTIFSUB'; |
372
|
|
|
//Reset facets |
373
|
|
|
if ($this->settings['resetFacets']) { |
374
|
|
|
//remove ($count) for selected facet in template |
375
|
|
|
$entryArray['count'] = false; |
376
|
|
|
//build link to delete selected facet |
377
|
|
|
$uri = $this->uriBuilder->reset() |
378
|
|
|
->setTargetPageUid($GLOBALS['TSFE']->id) |
379
|
|
|
->setArguments(['tx_dlf' => ['query' => $search['query'], 'fq' => $queryColumn], 'tx_dlf_search' => ['action' => 'search']]) |
380
|
|
|
->setAddQueryString(true) |
381
|
|
|
->build(); |
382
|
|
|
$entryArray['_OVERRIDE_HREF'] = $uri; |
383
|
|
|
$entryArray['title'] = sprintf(LocalizationUtility::translate('search.resetFacet', 'dlf'), $entryArray['title']); |
|
|
|
|
384
|
|
|
} |
385
|
|
|
} else { |
386
|
|
|
// Facet is not selected, thus add it to filter. |
387
|
|
|
$queryColumn[] = $field . ':("' . Solr::escapeQuery($value) . '")'; |
388
|
|
|
$entryArray['ITEM_STATE'] = 'NO'; |
389
|
|
|
} |
390
|
|
|
$uri = $this->uriBuilder->reset() |
391
|
|
|
->setTargetPageUid($GLOBALS['TSFE']->id) |
392
|
|
|
->setArguments(['tx_dlf' => ['query' => $search['query'], 'fq' => $queryColumn], 'tx_dlf_search' => ['action' => 'search']]) |
393
|
|
|
->setArgumentPrefix('tx_dlf') |
394
|
|
|
->build(); |
395
|
|
|
$entryArray['_OVERRIDE_HREF'] = $uri; |
396
|
|
|
|
397
|
|
|
return $entryArray; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Returns the extended search form and adds the JS files necessary for extended search. |
402
|
|
|
* |
403
|
|
|
* @access protected |
404
|
|
|
* |
405
|
|
|
* @return string The extended search form or an empty string |
406
|
|
|
*/ |
407
|
|
|
protected function addExtendedSearch() |
408
|
|
|
{ |
409
|
|
|
// Quit without doing anything if no fields for extended search are selected. |
410
|
|
|
if ( |
411
|
|
|
empty($this->settings['extendedSlotCount']) |
412
|
|
|
|| empty($this->settings['extendedFields']) |
413
|
|
|
) { |
414
|
|
|
return ''; |
415
|
|
|
} |
416
|
|
|
// Get operator options. |
417
|
|
|
$operatorOptions = []; |
418
|
|
|
foreach (['AND', 'OR', 'NOT'] as $operator) { |
419
|
|
|
$operatorOptions[$operator] = htmlspecialchars(LocalizationUtility::translate('search.' . $operator, 'dlf')); |
|
|
|
|
420
|
|
|
} |
421
|
|
|
// Get field selector options. |
422
|
|
|
$fieldSelectorOptions = []; |
423
|
|
|
$searchFields = GeneralUtility::trimExplode(',', $this->settings['extendedFields'], true); |
424
|
|
|
foreach ($searchFields as $searchField) { |
425
|
|
|
$fieldSelectorOptions[$searchField] = Helper::translate($searchField, 'tx_dlf_metadata', $this->settings['storagePid']); |
426
|
|
|
} |
427
|
|
|
$slotCountArray = []; |
428
|
|
|
for ($i = 0; $i < $this->settings['extendedSlotCount']; $i++) { |
429
|
|
|
$slotCountArray[] = $i; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
$this->view->assign('extendedSlotCount', $slotCountArray); |
433
|
|
|
$this->view->assign('extendedFields', $this->settings['extendedFields']); |
434
|
|
|
$this->view->assign('operators', $operatorOptions); |
435
|
|
|
$this->view->assign('searchFields', $fieldSelectorOptions); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|