1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
7
|
|
|
* |
8
|
|
|
* @license GNU General Public License version 3 or later. |
9
|
|
|
* For the full copyright and license information, please read the |
10
|
|
|
* LICENSE.txt file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Kitodo\Dlf\Domain\Repository; |
14
|
|
|
|
15
|
|
|
use Kitodo\Dlf\Common\Doc; |
16
|
|
|
use Kitodo\Dlf\Common\Helper; |
17
|
|
|
use Kitodo\Dlf\Common\Solr; |
18
|
|
|
use Kitodo\Dlf\Domain\Model\Document; |
19
|
|
|
use Kitodo\Dlf\Common\SolrSearchResult\ResultDocument; |
20
|
|
|
use TYPO3\CMS\Core\Cache\CacheManager; |
21
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
22
|
|
|
use TYPO3\CMS\Core\Database\Connection; |
23
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
24
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
25
|
|
|
use TYPO3\CMS\Extbase\Persistence\QueryInterface; |
26
|
|
|
|
27
|
|
|
class DocumentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The controller settings passed to the repository for some special actions. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
* @access protected |
34
|
|
|
*/ |
35
|
|
|
protected $settings; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Find one document by given parameters |
39
|
|
|
* |
40
|
|
|
* GET parameters may be: |
41
|
|
|
* |
42
|
|
|
* - 'id': the uid of the document |
43
|
|
|
* - 'location': the URL of the location of the XML file |
44
|
|
|
* - 'recordId': the record_id of the document |
45
|
|
|
* |
46
|
|
|
* @param array $parameters |
47
|
|
|
* |
48
|
|
|
* @return \Kitodo\Dlf\Domain\Model\Document|null |
49
|
|
|
*/ |
50
|
|
|
public function findOneByParameters($parameters) |
51
|
|
|
{ |
52
|
|
|
$doc = null; |
53
|
|
|
$document = null; |
54
|
|
|
|
55
|
|
|
if (isset($parameters['id']) && MathUtility::canBeInterpretedAsInteger($parameters['id'])) { |
56
|
|
|
|
57
|
|
|
$document = $this->findOneByIdAndSettings($parameters['id']); |
58
|
|
|
|
59
|
|
|
} else if (isset($parameters['recordId'])) { |
60
|
|
|
|
61
|
|
|
$document = $this->findOneByRecordId($parameters['recordId']); |
|
|
|
|
62
|
|
|
|
63
|
|
|
} else if (isset($parameters['location']) && GeneralUtility::isValidUrl($parameters['location'])) { |
64
|
|
|
|
65
|
|
|
$doc = Doc::getInstance($parameters['location'], [], true); |
66
|
|
|
|
67
|
|
|
if ($doc->recordId) { |
68
|
|
|
$document = $this->findOneByRecordId($doc->recordId); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($document === null) { |
72
|
|
|
// create new (dummy) Document object |
73
|
|
|
$document = GeneralUtility::makeInstance(Document::class); |
74
|
|
|
$document->setLocation($parameters['location']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($document !== null && $doc === null) { |
80
|
|
|
$doc = Doc::getInstance($document->getLocation(), [], true); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($doc !== null) { |
84
|
|
|
$document->setDoc($doc); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $document; |
|
|
|
|
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
public function findByUidAndPartOf($uid, $partOf) |
93
|
|
|
{ |
94
|
|
|
$query = $this->createQuery(); |
95
|
|
|
|
96
|
|
|
$query->matching($query->equals('uid', $uid)); |
97
|
|
|
$query->matching($query->equals('partof', $partOf)); |
98
|
|
|
|
99
|
|
|
return $query->execute(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Find the oldest document |
104
|
|
|
* |
105
|
|
|
* @return \Kitodo\Dlf\Domain\Model\Document|null |
106
|
|
|
*/ |
107
|
|
|
public function findOldestDocument() |
108
|
|
|
{ |
109
|
|
|
$query = $this->createQuery(); |
110
|
|
|
|
111
|
|
|
$query->setOrderings(['tstamp' => QueryInterface::ORDER_ASCENDING]); |
112
|
|
|
$query->setLimit(1); |
113
|
|
|
|
114
|
|
|
return $query->execute()->getFirst(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param int $partOf |
119
|
|
|
* @param string $structure |
120
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
121
|
|
|
*/ |
122
|
|
|
public function getChildrenOfYearAnchor($partOf, $structure) |
123
|
|
|
{ |
124
|
|
|
$query = $this->createQuery(); |
125
|
|
|
|
126
|
|
|
$query->matching($query->equals('structure', Helper::getUidFromIndexName($structure, 'tx_dlf_structures'))); |
127
|
|
|
$query->matching($query->equals('partof', $partOf)); |
128
|
|
|
|
129
|
|
|
$query->setOrderings([ |
130
|
|
|
'mets_orderlabel' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
return $query->execute(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Finds all documents for the given settings |
138
|
|
|
* |
139
|
|
|
* @param int $uid |
140
|
|
|
* @param array $settings |
141
|
|
|
* |
142
|
|
|
* @return \Kitodo\Dlf\Domain\Model\Document|null |
143
|
|
|
*/ |
144
|
|
|
public function findOneByIdAndSettings($uid, $settings = []) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$settings = ['documentSets' => $uid]; |
147
|
|
|
|
148
|
|
|
return $this->findDocumentsBySettings($settings)->getFirst(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Finds all documents for the given settings |
153
|
|
|
* |
154
|
|
|
* @param array $settings |
155
|
|
|
* |
156
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
157
|
|
|
*/ |
158
|
|
|
public function findDocumentsBySettings($settings = []) |
159
|
|
|
{ |
160
|
|
|
$query = $this->createQuery(); |
161
|
|
|
|
162
|
|
|
$constraints = []; |
163
|
|
|
|
164
|
|
|
if ($settings['documentSets']) { |
165
|
|
|
$constraints[] = $query->in('uid', GeneralUtility::intExplode(',', $settings['documentSets'])); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (isset($settings['excludeOther']) && (int) $settings['excludeOther'] === 0) { |
169
|
|
|
$query->getQuerySettings()->setRespectStoragePage(false); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (count($constraints)) { |
173
|
|
|
$query->matching( |
174
|
|
|
$query->logicalAnd($constraints) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $query->execute(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Finds all documents for the given collections |
183
|
|
|
* |
184
|
|
|
* @param array $collections |
185
|
|
|
* @param int $limit |
186
|
|
|
* |
187
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
188
|
|
|
*/ |
189
|
|
|
public function findAllByCollectionsLimited($collections, $limit = 50) |
190
|
|
|
{ |
191
|
|
|
$query = $this->createQuery(); |
192
|
|
|
|
193
|
|
|
// order by start_date -> start_time... |
194
|
|
|
$query->setOrderings( |
195
|
|
|
['tstamp' => QueryInterface::ORDER_DESCENDING] |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$constraints = []; |
199
|
|
|
if ($collections) { |
|
|
|
|
200
|
|
|
$constraints[] = $query->in('collections.uid', $collections); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (count($constraints)) { |
204
|
|
|
$query->matching( |
205
|
|
|
$query->logicalAnd($constraints) |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if ($limit > 0) { |
210
|
|
|
$query->setLimit((int) $limit); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $query->execute(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Find all the titles |
218
|
|
|
* |
219
|
|
|
* documents with partof == 0 |
220
|
|
|
* |
221
|
|
|
* @param array $settings |
222
|
|
|
* |
223
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
224
|
|
|
*/ |
225
|
|
|
public function findAllTitles($settings = []) |
226
|
|
|
{ |
227
|
|
|
$query = $this->createQuery(); |
228
|
|
|
|
229
|
|
|
$constraints = []; |
230
|
|
|
$constraints[] = $query->equals('partof', 0); |
231
|
|
|
|
232
|
|
|
if ($settings['collections']) { |
233
|
|
|
$constraints[] = $query->in('collections.uid', GeneralUtility::intExplode(',', $settings['collections'])); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (count($constraints)) { |
237
|
|
|
$query->matching( |
238
|
|
|
$query->logicalAnd($constraints) |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $query->execute(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Count the titles |
247
|
|
|
* |
248
|
|
|
* documents with partof == 0 |
249
|
|
|
* |
250
|
|
|
* @param array $settings |
251
|
|
|
* |
252
|
|
|
* @return int |
253
|
|
|
*/ |
254
|
|
|
public function countAllTitles($settings = []) |
255
|
|
|
{ |
256
|
|
|
return $this->findAllTitles($settings)->count(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Count the volumes |
261
|
|
|
* |
262
|
|
|
* documents with partof != 0 |
263
|
|
|
* |
264
|
|
|
* @param array $settings |
265
|
|
|
* |
266
|
|
|
* @return int |
267
|
|
|
*/ |
268
|
|
|
public function countAllVolumes($settings = []) |
269
|
|
|
{ |
270
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
271
|
|
|
->getQueryBuilderForTable('tx_dlf_documents'); |
272
|
|
|
$subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
273
|
|
|
->getQueryBuilderForTable('tx_dlf_documents'); |
274
|
|
|
|
275
|
|
|
$subQuery = $subQueryBuilder |
276
|
|
|
->select('tx_dlf_documents.partof') |
277
|
|
|
->from('tx_dlf_documents') |
278
|
|
|
->where( |
279
|
|
|
$subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) |
280
|
|
|
) |
281
|
|
|
->groupBy('tx_dlf_documents.partof') |
282
|
|
|
->getSQL(); |
283
|
|
|
|
284
|
|
|
$countVolumes = $queryBuilder |
285
|
|
|
->count('tx_dlf_documents.uid') |
286
|
|
|
->from('tx_dlf_documents') |
287
|
|
|
->where( |
288
|
|
|
$queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), |
289
|
|
|
$queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery) |
290
|
|
|
) |
291
|
|
|
->execute() |
292
|
|
|
->fetchColumn(0); |
293
|
|
|
|
294
|
|
|
return $countVolumes; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function getStatisticsForSelectedCollection($settings) |
298
|
|
|
{ |
299
|
|
|
// Include only selected collections. |
300
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
301
|
|
|
->getQueryBuilderForTable('tx_dlf_documents'); |
302
|
|
|
|
303
|
|
|
$countTitles = $queryBuilder |
304
|
|
|
->count('tx_dlf_documents.uid') |
305
|
|
|
->from('tx_dlf_documents') |
306
|
|
|
->innerJoin( |
307
|
|
|
'tx_dlf_documents', |
308
|
|
|
'tx_dlf_relations', |
309
|
|
|
'tx_dlf_relations_joins', |
310
|
|
|
$queryBuilder->expr()->eq( |
311
|
|
|
'tx_dlf_relations_joins.uid_local', |
312
|
|
|
'tx_dlf_documents.uid' |
313
|
|
|
) |
314
|
|
|
) |
315
|
|
|
->innerJoin( |
316
|
|
|
'tx_dlf_relations_joins', |
317
|
|
|
'tx_dlf_collections', |
318
|
|
|
'tx_dlf_collections_join', |
319
|
|
|
$queryBuilder->expr()->eq( |
320
|
|
|
'tx_dlf_relations_joins.uid_foreign', |
321
|
|
|
'tx_dlf_collections_join.uid' |
322
|
|
|
) |
323
|
|
|
) |
324
|
|
|
->where( |
325
|
|
|
$queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), |
326
|
|
|
$queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])), |
327
|
|
|
$queryBuilder->expr()->eq('tx_dlf_documents.partof', 0), |
328
|
|
|
$queryBuilder->expr()->in('tx_dlf_collections_join.uid', |
329
|
|
|
$queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', |
330
|
|
|
$settings['collections']), Connection::PARAM_INT_ARRAY)), |
331
|
|
|
$queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', |
332
|
|
|
$queryBuilder->createNamedParameter('docs_colls')) |
333
|
|
|
) |
334
|
|
|
->execute() |
335
|
|
|
->fetchColumn(0); |
336
|
|
|
|
337
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
338
|
|
|
->getQueryBuilderForTable('tx_dlf_documents'); |
339
|
|
|
$subQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
340
|
|
|
->getQueryBuilderForTable('tx_dlf_documents'); |
341
|
|
|
|
342
|
|
|
$subQuery = $subQueryBuilder |
343
|
|
|
->select('tx_dlf_documents.partof') |
344
|
|
|
->from('tx_dlf_documents') |
345
|
|
|
->where( |
346
|
|
|
$subQueryBuilder->expr()->neq('tx_dlf_documents.partof', 0) |
347
|
|
|
) |
348
|
|
|
->groupBy('tx_dlf_documents.partof') |
349
|
|
|
->getSQL(); |
350
|
|
|
|
351
|
|
|
$countVolumes = $queryBuilder |
352
|
|
|
->count('tx_dlf_documents.uid') |
353
|
|
|
->from('tx_dlf_documents') |
354
|
|
|
->innerJoin( |
355
|
|
|
'tx_dlf_documents', |
356
|
|
|
'tx_dlf_relations', |
357
|
|
|
'tx_dlf_relations_joins', |
358
|
|
|
$queryBuilder->expr()->eq( |
359
|
|
|
'tx_dlf_relations_joins.uid_local', |
360
|
|
|
'tx_dlf_documents.uid' |
361
|
|
|
) |
362
|
|
|
) |
363
|
|
|
->innerJoin( |
364
|
|
|
'tx_dlf_relations_joins', |
365
|
|
|
'tx_dlf_collections', |
366
|
|
|
'tx_dlf_collections_join', |
367
|
|
|
$queryBuilder->expr()->eq( |
368
|
|
|
'tx_dlf_relations_joins.uid_foreign', |
369
|
|
|
'tx_dlf_collections_join.uid' |
370
|
|
|
) |
371
|
|
|
) |
372
|
|
|
->where( |
373
|
|
|
$queryBuilder->expr()->eq('tx_dlf_documents.pid', intval($settings['pages'])), |
374
|
|
|
$queryBuilder->expr()->eq('tx_dlf_collections_join.pid', intval($settings['pages'])), |
375
|
|
|
$queryBuilder->expr()->notIn('tx_dlf_documents.uid', $subQuery), |
376
|
|
|
$queryBuilder->expr()->in('tx_dlf_collections_join.uid', |
377
|
|
|
$queryBuilder->createNamedParameter(GeneralUtility::intExplode(',', |
378
|
|
|
$settings['collections']), Connection::PARAM_INT_ARRAY)), |
379
|
|
|
$queryBuilder->expr()->eq('tx_dlf_relations_joins.ident', |
380
|
|
|
$queryBuilder->createNamedParameter('docs_colls')) |
381
|
|
|
) |
382
|
|
|
->execute() |
383
|
|
|
->fetchColumn(0); |
384
|
|
|
|
385
|
|
|
return ['titles' => $countTitles, 'volumes' => $countVolumes]; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function getTableOfContentsFromDb($uid, $pid, $settings) |
389
|
|
|
{ |
390
|
|
|
// Build table of contents from database. |
391
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
392
|
|
|
->getQueryBuilderForTable('tx_dlf_documents'); |
393
|
|
|
|
394
|
|
|
$excludeOtherWhere = ''; |
395
|
|
|
if ($settings['excludeOther']) { |
396
|
|
|
$excludeOtherWhere = 'tx_dlf_documents.pid=' . intval($settings['pages']); |
397
|
|
|
} |
398
|
|
|
// Check if there are any metadata to suggest. |
399
|
|
|
$result = $queryBuilder |
400
|
|
|
->select( |
401
|
|
|
'tx_dlf_documents.uid AS uid', |
402
|
|
|
'tx_dlf_documents.title AS title', |
403
|
|
|
'tx_dlf_documents.volume AS volume', |
404
|
|
|
'tx_dlf_documents.mets_label AS mets_label', |
405
|
|
|
'tx_dlf_documents.mets_orderlabel AS mets_orderlabel', |
406
|
|
|
'tx_dlf_structures_join.index_name AS type' |
407
|
|
|
) |
408
|
|
|
->innerJoin( |
409
|
|
|
'tx_dlf_documents', |
410
|
|
|
'tx_dlf_structures', |
411
|
|
|
'tx_dlf_structures_join', |
412
|
|
|
$queryBuilder->expr()->eq( |
413
|
|
|
'tx_dlf_structures_join.uid', |
414
|
|
|
'tx_dlf_documents.structure' |
415
|
|
|
) |
416
|
|
|
) |
417
|
|
|
->from('tx_dlf_documents') |
418
|
|
|
->where( |
419
|
|
|
$queryBuilder->expr()->eq('tx_dlf_documents.partof', intval($uid)), |
420
|
|
|
$queryBuilder->expr()->eq('tx_dlf_structures_join.pid', intval($pid)), |
421
|
|
|
$excludeOtherWhere |
422
|
|
|
) |
423
|
|
|
->addOrderBy('tx_dlf_documents.volume_sorting') |
424
|
|
|
->addOrderBy('tx_dlf_documents.mets_orderlabel') |
425
|
|
|
->execute(); |
426
|
|
|
return $result; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Find one document by given settings and identifier |
431
|
|
|
* |
432
|
|
|
* @param array $settings |
433
|
|
|
* @param array $parameters |
434
|
|
|
* |
435
|
|
|
* @return array The found document object |
436
|
|
|
*/ |
437
|
|
|
public function getOaiRecord($settings, $parameters) |
438
|
|
|
{ |
439
|
|
|
$where = ''; |
440
|
|
|
|
441
|
|
|
if (!$settings['show_userdefined']) { |
442
|
|
|
$where .= 'AND tx_dlf_collections.fe_cruser_id=0 '; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
$connection = GeneralUtility::makeInstance(ConnectionPool::class) |
446
|
|
|
->getConnectionForTable('tx_dlf_documents'); |
447
|
|
|
|
448
|
|
|
$sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' . |
449
|
|
|
'FROM `tx_dlf_documents` ' . |
450
|
|
|
'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' . |
451
|
|
|
'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' . |
452
|
|
|
'WHERE `tx_dlf_documents`.`record_id` = ? ' . |
453
|
|
|
'AND `tx_dlf_relations`.`ident`="docs_colls" ' . |
454
|
|
|
$where; |
455
|
|
|
|
456
|
|
|
$values = [ |
457
|
|
|
$parameters['identifier'] |
458
|
|
|
]; |
459
|
|
|
|
460
|
|
|
$types = [ |
461
|
|
|
Connection::PARAM_STR |
462
|
|
|
]; |
463
|
|
|
|
464
|
|
|
// Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query |
465
|
|
|
$statement = $connection->executeQuery($sql, $values, $types); |
466
|
|
|
|
467
|
|
|
return $statement->fetch(); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Finds all documents for the given settings |
472
|
|
|
* |
473
|
|
|
* @param array $settings |
474
|
|
|
* @param array $documentsToProcess |
475
|
|
|
* |
476
|
|
|
* @return array The found document objects |
477
|
|
|
*/ |
478
|
|
|
public function getOaiDocumentList($settings, $documentsToProcess) |
|
|
|
|
479
|
|
|
{ |
480
|
|
|
$connection = GeneralUtility::makeInstance(ConnectionPool::class) |
481
|
|
|
->getConnectionForTable('tx_dlf_documents'); |
482
|
|
|
|
483
|
|
|
$sql = 'SELECT `tx_dlf_documents`.*, GROUP_CONCAT(DISTINCT `tx_dlf_collections`.`oai_name` ORDER BY `tx_dlf_collections`.`oai_name` SEPARATOR " ") AS `collections` ' . |
484
|
|
|
'FROM `tx_dlf_documents` ' . |
485
|
|
|
'INNER JOIN `tx_dlf_relations` ON `tx_dlf_relations`.`uid_local` = `tx_dlf_documents`.`uid` ' . |
486
|
|
|
'INNER JOIN `tx_dlf_collections` ON `tx_dlf_collections`.`uid` = `tx_dlf_relations`.`uid_foreign` ' . |
487
|
|
|
'WHERE `tx_dlf_documents`.`uid` IN ( ? ) ' . |
488
|
|
|
'AND `tx_dlf_relations`.`ident`="docs_colls" ' . |
489
|
|
|
'AND ' . Helper::whereExpression('tx_dlf_collections') . ' ' . |
490
|
|
|
'GROUP BY `tx_dlf_documents`.`uid` '; |
491
|
|
|
|
492
|
|
|
$values = [ |
493
|
|
|
$documentsToProcess, |
494
|
|
|
]; |
495
|
|
|
|
496
|
|
|
$types = [ |
497
|
|
|
Connection::PARAM_INT_ARRAY, |
498
|
|
|
]; |
499
|
|
|
|
500
|
|
|
// Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query |
501
|
|
|
$documents = $connection->executeQuery($sql, $values, $types); |
502
|
|
|
|
503
|
|
|
return $documents; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Finds all documents with given uids |
508
|
|
|
* |
509
|
|
|
* @param array $uids |
510
|
|
|
* |
511
|
|
|
* @return array |
512
|
|
|
*/ |
513
|
|
|
private function findAllByUids($uids) |
514
|
|
|
{ |
515
|
|
|
// get all documents from db we are talking about |
516
|
|
|
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
517
|
|
|
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_dlf_documents'); |
518
|
|
|
// Fetch document info for UIDs in $documentSet from DB |
519
|
|
|
$kitodoDocuments = $queryBuilder |
520
|
|
|
->select( |
521
|
|
|
'tx_dlf_documents.uid AS uid', |
522
|
|
|
'tx_dlf_documents.title AS title', |
523
|
|
|
'tx_dlf_documents.structure AS structure', |
524
|
|
|
'tx_dlf_documents.thumbnail AS thumbnail', |
525
|
|
|
'tx_dlf_documents.volume_sorting AS volumeSorting', |
526
|
|
|
'tx_dlf_documents.mets_orderlabel AS metsOrderlabel', |
527
|
|
|
'tx_dlf_documents.partof AS partOf' |
528
|
|
|
) |
529
|
|
|
->from('tx_dlf_documents') |
530
|
|
|
->where( |
531
|
|
|
$queryBuilder->expr()->in('tx_dlf_documents.pid', $this->settings['storagePid']), |
532
|
|
|
$queryBuilder->expr()->in('tx_dlf_documents.uid', $uids) |
533
|
|
|
) |
534
|
|
|
->addOrderBy('tx_dlf_documents.volume_sorting', 'asc') |
535
|
|
|
->addOrderBy('tx_dlf_documents.mets_orderlabel', 'asc') |
536
|
|
|
->execute(); |
537
|
|
|
|
538
|
|
|
$allDocuments = []; |
539
|
|
|
$documentStructures = Helper::getDocumentStructures($this->settings['storagePid']); |
540
|
|
|
// Process documents in a usable array structure |
541
|
|
|
while ($resArray = $kitodoDocuments->fetch()) { |
542
|
|
|
$resArray['structure'] = $documentStructures[$resArray['structure']]; |
543
|
|
|
$allDocuments[$resArray['uid']] = $resArray; |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
return $allDocuments; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Find all documents with given collection from Solr |
551
|
|
|
* |
552
|
|
|
* @param \Kitodo\Dlf\Domain\Model\Collection $collection |
553
|
|
|
* @param array $settings |
554
|
|
|
* @param array $searchParams |
555
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata |
556
|
|
|
* @return array |
557
|
|
|
*/ |
558
|
|
|
public function findSolrByCollection($collection, $settings, $searchParams, $listedMetadata = null) |
559
|
|
|
{ |
560
|
|
|
// set settings global inside this repository |
561
|
|
|
$this->settings = $settings; |
562
|
|
|
|
563
|
|
|
// Prepare query parameters. |
564
|
|
|
$params = []; |
565
|
|
|
$matches = []; |
566
|
|
|
|
567
|
|
|
// if a collection is given, we prepare the collection query string |
568
|
|
|
if ($collection) { |
|
|
|
|
569
|
|
|
$collecionsQueryString = $collection->getIndexName(); |
570
|
|
|
$params['filterquery'][] = ['query' => 'toplevel:true']; |
571
|
|
|
$params['filterquery'][] = ['query' => 'partof:0']; |
572
|
|
|
$params['filterquery'][] = ['query' => 'collection_faceting:("' . $collecionsQueryString .'")']; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
// Set search query. |
576
|
|
|
if ( |
577
|
|
|
(!empty($searchParams['fulltext'])) |
578
|
|
|
|| preg_match('/fulltext:\((.*)\)/', trim($searchParams['query']), $matches) |
579
|
|
|
) { |
580
|
|
|
// If the query already is a fulltext query e.g using the facets |
581
|
|
|
$searchParams['query'] = empty($matches[1]) ? $searchParams['query'] : $matches[1]; |
582
|
|
|
// Search in fulltext field if applicable. Query must not be empty! |
583
|
|
|
if (!empty($searchParams['query'])) { |
584
|
|
|
$query = 'fulltext:(' . Solr::escapeQuery(trim($searchParams['query'])) . ')'; |
585
|
|
|
} |
586
|
|
|
$params['fulltext'] = true; |
587
|
|
|
} else { |
588
|
|
|
// Retain given search field if valid. |
589
|
|
|
if (!empty($searchParams['query'])) { |
590
|
|
|
$query = Solr::escapeQueryKeepField(trim($searchParams['query']), $settings['storagePid']); |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
|
595
|
|
|
// Set some query parameters. |
596
|
|
|
$params['query'] = !empty($query) ? $query : '*'; |
597
|
|
|
$params['start'] = 0; |
598
|
|
|
$params['rows'] = 10000; |
599
|
|
|
|
600
|
|
|
// order the results as given or by title as default |
601
|
|
|
if (!empty($searchParams['orderBy'])) { |
602
|
|
|
$querySort = [ |
603
|
|
|
$searchParams['orderBy'] => $searchParams['order'] |
604
|
|
|
]; |
605
|
|
|
} else { |
606
|
|
|
$querySort = [ |
607
|
|
|
'year_sorting' => 'asc', |
608
|
|
|
'title_sorting' => 'asc' |
609
|
|
|
]; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
$params['sort'] = $querySort; |
613
|
|
|
$params['listMetadataRecords'] = []; |
614
|
|
|
|
615
|
|
|
// Restrict the fields to the required ones. |
616
|
|
|
$params['fields'] = 'uid,id,page,title,thumbnail,partof,toplevel,type'; |
617
|
|
|
|
618
|
|
|
if ($listedMetadata) { |
619
|
|
|
foreach ($listedMetadata as $metadata) { |
620
|
|
|
if ($metadata->getIndexStored() || $metadata->getIndexIndexed()) { |
621
|
|
|
$listMetadataRecord = $metadata->getIndexName() . '_' . ($metadata->getIndexTokenized() ? 't' : 'u') . ($metadata->getIndexStored() ? 's' : 'u') . ($metadata->getIndexIndexed() ? 'i' : 'u'); |
622
|
|
|
$params['fields'] .= ',' . $listMetadataRecord; |
623
|
|
|
$params['listMetadataRecords'][$metadata->getIndexName()] = $listMetadataRecord; |
624
|
|
|
} |
625
|
|
|
} |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
// Perform search. |
629
|
|
|
$result = $this->searchSolr($params, true); |
630
|
|
|
|
631
|
|
|
// Initialize values |
632
|
|
|
$numberOfToplevels = 0; |
633
|
|
|
$documents = []; |
634
|
|
|
|
635
|
|
|
if ($result['numFound'] > 0) { |
636
|
|
|
// flat array with uids from Solr search |
637
|
|
|
$documentSet = array_unique(array_column($result['documents'], 'uid')); |
638
|
|
|
|
639
|
|
|
if (empty($documentSet)) { |
640
|
|
|
// return nothing found |
641
|
|
|
return ['solrResults' => [], 'documents' => []]; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
// get the Extbase document objects for all uids |
645
|
|
|
$allDocuments = $this->findAllByUids($documentSet); |
646
|
|
|
|
647
|
|
|
foreach ($result['documents'] as $doc) { |
648
|
|
|
if (empty($documents[$doc['uid']]) && $allDocuments[$doc['uid']]) { |
649
|
|
|
$documents[$doc['uid']] = $allDocuments[$doc['uid']]; |
650
|
|
|
} |
651
|
|
|
if ($documents[$doc['uid']]) { |
652
|
|
|
if ($doc['toplevel'] === false) { |
653
|
|
|
// this maybe a chapter, article, ..., year |
654
|
|
|
if ($doc['type'] === 'year') { |
655
|
|
|
continue; |
656
|
|
|
} |
657
|
|
|
if (!empty($doc['page'])) { |
658
|
|
|
// it's probably a fulltext or metadata search |
659
|
|
|
$searchResult = []; |
660
|
|
|
$searchResult['page'] = $doc['page']; |
661
|
|
|
$searchResult['thumbnail'] = $doc['thumbnail']; |
662
|
|
|
$searchResult['structure'] = $doc['type']; |
663
|
|
|
$searchResult['title'] = $doc['title']; |
664
|
|
|
foreach ($params['listMetadataRecords'] as $indexName => $solrField) { |
665
|
|
|
if (isset($doc['metadata'][$indexName])) { |
666
|
|
|
$documents[$doc['uid']]['metadata'][$indexName] = $doc['metadata'][$indexName]; |
667
|
|
|
} |
668
|
|
|
} |
669
|
|
|
if ($searchParams['fulltext'] == '1') { |
670
|
|
|
$searchResult['snippet'] = $doc['snippet']; |
671
|
|
|
$searchResult['highlight'] = $doc['highlight']; |
672
|
|
|
$searchResult['highlight_word'] = $searchParams['query']; |
673
|
|
|
} |
674
|
|
|
$documents[$doc['uid']]['searchResults'][] = $searchResult; |
675
|
|
|
} |
676
|
|
|
} else if ($doc['toplevel'] === true) { |
677
|
|
|
$numberOfToplevels++; |
678
|
|
|
foreach ($params['listMetadataRecords'] as $indexName => $solrField) { |
679
|
|
|
if (isset($doc['metadata'][$indexName])) { |
680
|
|
|
$documents[$doc['uid']]['metadata'][$indexName] = $doc['metadata'][$indexName]; |
681
|
|
|
} |
682
|
|
|
} |
683
|
|
|
if ($searchParams['fulltext'] != '1') { |
684
|
|
|
$documents[$doc['uid']]['page'] = 1; |
685
|
|
|
$documents[$doc['uid']]['children'] = $this->findByPartof($doc['uid']); |
|
|
|
|
686
|
|
|
} |
687
|
|
|
} |
688
|
|
|
if (empty($documents[$doc['uid']]['metadata'])) { |
689
|
|
|
$documents[$doc['uid']]['metadata'] = $this->fetchMetadataFromSolr($doc['uid'], $listedMetadata); |
690
|
|
|
} |
691
|
|
|
// get title of parent if empty |
692
|
|
|
if (empty($documents[$doc['uid']]['title']) && ($documents[$doc['uid']]['partOf'] > 0)) { |
693
|
|
|
$parentDocument = $this->findByUid($documents[$doc['uid']]['partOf']); |
694
|
|
|
if ($parentDocument) { |
695
|
|
|
$documents[$doc['uid']]['title'] = $parentDocument->getTitle(); |
696
|
|
|
} |
697
|
|
|
} |
698
|
|
|
} |
699
|
|
|
} |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
return ['solrResults' => $result, 'numberOfToplevels' => $numberOfToplevels, 'documents' => $documents]; |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
/** |
706
|
|
|
* Find all listed metadata for given document |
707
|
|
|
* |
708
|
|
|
* @param int $uid the uid of the document |
709
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult $listedMetadata |
710
|
|
|
* @return array |
711
|
|
|
*/ |
712
|
|
|
public function fetchMetadataFromSolr($uid, $listedMetadata = []) |
713
|
|
|
{ |
714
|
|
|
// Prepare query parameters. |
715
|
|
|
$params = []; |
716
|
|
|
$metadataArray = []; |
717
|
|
|
|
718
|
|
|
// Set some query parameters. |
719
|
|
|
$params['query'] = 'uid:' . $uid; |
720
|
|
|
$params['start'] = 0; |
721
|
|
|
$params['rows'] = 1; |
722
|
|
|
$params['sort'] = ['score' => 'desc']; |
723
|
|
|
$params['listMetadataRecords'] = []; |
724
|
|
|
|
725
|
|
|
// Restrict the fields to the required ones. |
726
|
|
|
$params['fields'] = 'uid,toplevel'; |
727
|
|
|
|
728
|
|
|
if ($listedMetadata) { |
729
|
|
|
foreach ($listedMetadata as $metadata) { |
730
|
|
|
if ($metadata->getIndexIndexed()) { |
731
|
|
|
$listMetadataRecord = $metadata->getIndexName() . '_' . ($metadata->getIndexTokenized() ? 't' : 'u') . ($metadata->getIndexStored() ? 's' : 'u') . 'i'; |
732
|
|
|
$params['fields'] .= ',' . $listMetadataRecord; |
733
|
|
|
$params['listMetadataRecords'][$metadata->getIndexName()] = $listMetadataRecord; |
734
|
|
|
} |
735
|
|
|
} |
736
|
|
|
} |
737
|
|
|
// Set filter query to just get toplevel documents. |
738
|
|
|
$params['filterquery'][] = ['query' => 'toplevel:true']; |
739
|
|
|
|
740
|
|
|
// Perform search. |
741
|
|
|
$result = $this->searchSolr($params, true); |
742
|
|
|
|
743
|
|
|
if ($result['numFound'] > 0) { |
744
|
|
|
// There is only one result found because of toplevel:true. |
745
|
|
|
if (isset($result['documents'][0]['metadata'])) { |
746
|
|
|
$metadataArray = $result['documents'][0]['metadata']; |
747
|
|
|
} |
748
|
|
|
} |
749
|
|
|
return $metadataArray; |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
/** |
753
|
|
|
* Processes a search request |
754
|
|
|
* |
755
|
|
|
* @access public |
756
|
|
|
* |
757
|
|
|
* @param array $parameters: Additional search parameters |
758
|
|
|
* @param boolean $enableCache: Enable caching of Solr requests |
759
|
|
|
* |
760
|
|
|
* @return array The Apache Solr Documents that were fetched |
761
|
|
|
*/ |
762
|
|
|
protected function searchSolr($parameters = [], $enableCache = true) |
763
|
|
|
{ |
764
|
|
|
// Set additional query parameters. |
765
|
|
|
$parameters['start'] = 0; |
766
|
|
|
// Set query. |
767
|
|
|
$parameters['query'] = isset($parameters['query']) ? $parameters['query'] : '*'; |
768
|
|
|
$parameters['filterquery'] = isset($parameters['filterquery']) ? $parameters['filterquery'] : []; |
769
|
|
|
|
770
|
|
|
// Perform Solr query. |
771
|
|
|
// Instantiate search object. |
772
|
|
|
$solr = Solr::getInstance($this->settings['solrcore']); |
773
|
|
|
if (!$solr->ready) { |
774
|
|
|
Helper::log('Apache Solr not available', LOG_SEVERITY_ERROR); |
775
|
|
|
return []; |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
$cacheIdentifier = ''; |
779
|
|
|
$cache = null; |
780
|
|
|
// Calculate cache identifier. |
781
|
|
|
if ($enableCache === true) { |
782
|
|
|
$cacheIdentifier = Helper::digest($solr->core . print_r($parameters, true)); |
|
|
|
|
783
|
|
|
$cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('tx_dlf_solr'); |
784
|
|
|
} |
785
|
|
|
$resultSet = [ |
786
|
|
|
'documents' => [], |
787
|
|
|
'numFound' => 0, |
788
|
|
|
]; |
789
|
|
|
if ($enableCache === false || ($entry = $cache->get($cacheIdentifier)) === false) { |
790
|
|
|
$selectQuery = $solr->service->createSelect($parameters); |
791
|
|
|
|
792
|
|
|
if ($parameters['fulltext'] === true) { |
793
|
|
|
// get highlighting component and apply settings |
794
|
|
|
$selectQuery->getHighlighting(); |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
$solrRequest = $solr->service->createRequest($selectQuery); |
798
|
|
|
|
799
|
|
|
if ($parameters['fulltext'] === true) { |
800
|
|
|
// If it is a fulltext search, enable highlighting. |
801
|
|
|
// field for which highlighting is going to be performed, |
802
|
|
|
// is required if you want to have OCR highlighting |
803
|
|
|
$solrRequest->addParam('hl.ocr.fl', 'fulltext'); |
804
|
|
|
// return the coordinates of highlighted search as absolute coordinates |
805
|
|
|
$solrRequest->addParam('hl.ocr.absoluteHighlights', 'on'); |
806
|
|
|
// max amount of snippets for a single page |
807
|
|
|
$solrRequest->addParam('hl.snippets', 20); |
808
|
|
|
// we store the fulltext on page level and can disable this option |
809
|
|
|
$solrRequest->addParam('hl.ocr.trackPages', 'off'); |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
// Perform search for all documents with the same uid that either fit to the search or marked as toplevel. |
813
|
|
|
$response = $solr->service->executeRequest($solrRequest); |
814
|
|
|
$result = $solr->service->createResult($selectQuery, $response); |
815
|
|
|
|
816
|
|
|
/** @scrutinizer ignore-call */ |
817
|
|
|
$resultSet['numFound'] = $result->getNumFound(); |
818
|
|
|
$highlighting = []; |
819
|
|
|
if ($parameters['fulltext'] === true) { |
820
|
|
|
$data = $result->getData(); |
821
|
|
|
$highlighting = $data['ocrHighlighting']; |
822
|
|
|
} |
823
|
|
|
$fields = Solr::getFields(); |
824
|
|
|
|
825
|
|
|
foreach ($result as $record) { |
826
|
|
|
$resultDocument = new ResultDocument($record, $highlighting, $fields); |
827
|
|
|
|
828
|
|
|
$document = [ |
829
|
|
|
'id' => $resultDocument->getId(), |
830
|
|
|
'page' => $resultDocument->getPage(), |
831
|
|
|
'snippet' => $resultDocument->getSnippets(), |
832
|
|
|
'thumbnail' => $resultDocument->getThumbnail(), |
833
|
|
|
'title' => $resultDocument->getTitle(), |
834
|
|
|
'toplevel' => $resultDocument->getToplevel(), |
835
|
|
|
'type' => $resultDocument->getType(), |
836
|
|
|
'uid' => !empty($resultDocument->getUid()) ? $resultDocument->getUid() : $parameters['uid'], |
837
|
|
|
'highlight' => $resultDocument->getHighlightsIds(), |
838
|
|
|
]; |
839
|
|
|
foreach ($parameters['listMetadataRecords'] as $indexName => $solrField) { |
840
|
|
|
if (!empty($record->$solrField)) { |
841
|
|
|
$document['metadata'][$indexName] = $record->$solrField; |
842
|
|
|
} |
843
|
|
|
} |
844
|
|
|
$resultSet['documents'][] = $document; |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
// Save value in cache. |
848
|
|
|
if (!empty($resultSet) && $enableCache === true) { |
849
|
|
|
$cache->set($cacheIdentifier, $resultSet); |
850
|
|
|
} |
851
|
|
|
} else { |
852
|
|
|
// Return cache hit. |
853
|
|
|
$resultSet = $entry; |
854
|
|
|
} |
855
|
|
|
return $resultSet; |
856
|
|
|
} |
857
|
|
|
|
858
|
|
|
} |
859
|
|
|
|