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\Hooks; |
14
|
|
|
|
15
|
|
|
use Kitodo\Dlf\Common\Document; |
16
|
|
|
use Kitodo\Dlf\Common\Helper; |
17
|
|
|
use Kitodo\Dlf\Common\Indexer; |
18
|
|
|
use Kitodo\Dlf\Common\Solr; |
19
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
20
|
|
|
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction; |
21
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Hooks and helper for \TYPO3\CMS\Core\DataHandling\DataHandler |
25
|
|
|
* |
26
|
|
|
* @author Sebastian Meyer <[email protected]> |
27
|
|
|
* @package TYPO3 |
28
|
|
|
* @subpackage dlf |
29
|
|
|
* @access public |
30
|
|
|
*/ |
31
|
|
|
class DataHandler |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Field post-processing hook for the process_datamap() method. |
35
|
|
|
* |
36
|
|
|
* @access public |
37
|
|
|
* |
38
|
|
|
* @param string $status: 'new' or 'update' |
39
|
|
|
* @param string $table: The destination table |
40
|
|
|
* @param int $id: The uid of the record |
41
|
|
|
* @param array &$fieldArray: Array of field values |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray) |
46
|
|
|
{ |
47
|
|
|
if ($status == 'new') { |
48
|
|
|
switch ($table) { |
49
|
|
|
// Field post-processing for table "tx_dlf_documents". |
50
|
|
|
case 'tx_dlf_documents': |
51
|
|
|
// Set sorting field if empty. |
52
|
|
|
if ( |
53
|
|
|
empty($fieldArray['title_sorting']) |
54
|
|
|
&& !empty($fieldArray['title']) |
55
|
|
|
) { |
56
|
|
|
$fieldArray['title_sorting'] = $fieldArray['title']; |
57
|
|
|
} |
58
|
|
|
break; |
59
|
|
|
// Field post-processing for table "tx_dlf_metadata". |
60
|
|
|
case 'tx_dlf_metadata': |
61
|
|
|
// Store field in index if it should appear in lists. |
62
|
|
|
if (!empty($fieldArray['is_listed'])) { |
63
|
|
|
$fieldArray['index_stored'] = 1; |
64
|
|
|
} |
65
|
|
|
// Index field in index if it should be used for auto-completion. |
66
|
|
|
if (!empty($fieldArray['index_autocomplete'])) { |
67
|
|
|
$fieldArray['index_indexed'] = 1; |
68
|
|
|
} |
69
|
|
|
// Field post-processing for tables "tx_dlf_metadata", "tx_dlf_collections", "tx_dlf_libraries" and "tx_dlf_structures". |
70
|
|
|
case 'tx_dlf_collections': |
71
|
|
|
case 'tx_dlf_libraries': |
72
|
|
|
case 'tx_dlf_structures': |
73
|
|
|
// Set label as index name if empty. |
74
|
|
|
if ( |
75
|
|
|
empty($fieldArray['index_name']) |
76
|
|
|
&& !empty($fieldArray['label']) |
77
|
|
|
) { |
78
|
|
|
$fieldArray['index_name'] = $fieldArray['label']; |
79
|
|
|
} |
80
|
|
|
// Set index name as label if empty. |
81
|
|
|
if ( |
82
|
|
|
empty($fieldArray['label']) |
83
|
|
|
&& !empty($fieldArray['index_name']) |
84
|
|
|
) { |
85
|
|
|
$fieldArray['label'] = $fieldArray['index_name']; |
86
|
|
|
} |
87
|
|
|
// Ensure that index names don't get mixed up with sorting values. |
88
|
|
|
if (substr($fieldArray['index_name'], -8) == '_sorting') { |
89
|
|
|
$fieldArray['index_name'] .= '0'; |
90
|
|
|
} |
91
|
|
|
break; |
92
|
|
|
// Field post-processing for table "tx_dlf_solrcores". |
93
|
|
|
case 'tx_dlf_solrcores': |
94
|
|
|
// Create new Solr core. |
95
|
|
|
$fieldArray['index_name'] = Solr::createCore(); |
96
|
|
|
if (empty($fieldArray['index_name'])) { |
97
|
|
|
Helper::devLog('Could not create new Apache Solr core', DEVLOG_SEVERITY_ERROR); |
98
|
|
|
// Solr core could not be created, thus unset field array. |
99
|
|
|
$fieldArray = []; |
100
|
|
|
} |
101
|
|
|
break; |
102
|
|
|
} |
103
|
|
|
} elseif ($status == 'update') { |
104
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
105
|
|
|
->getQueryBuilderForTable($table); |
106
|
|
|
|
107
|
|
|
switch ($table) { |
108
|
|
|
// Field post-processing for table "tx_dlf_metadata". |
109
|
|
|
case 'tx_dlf_metadata': |
110
|
|
|
// Store field in index if it should appear in lists. |
111
|
|
|
if (!empty($fieldArray['is_listed'])) { |
112
|
|
|
$fieldArray['index_stored'] = 1; |
113
|
|
|
} |
114
|
|
|
if ( |
115
|
|
|
isset($fieldArray['index_stored']) |
116
|
|
|
&& $fieldArray['index_stored'] == 0 |
117
|
|
|
&& !isset($fieldArray['is_listed']) |
118
|
|
|
) { |
119
|
|
|
// Get current configuration. |
120
|
|
|
$result = $queryBuilder |
121
|
|
|
->select($table . '.is_listed AS is_listed') |
122
|
|
|
->from($table) |
123
|
|
|
->where( |
124
|
|
|
$queryBuilder->expr()->eq($table . '.uid', intval($id)), |
125
|
|
|
Helper::whereExpression($table) |
126
|
|
|
) |
127
|
|
|
->setMaxResults(1) |
128
|
|
|
->execute(); |
129
|
|
|
|
130
|
|
|
if ($resArray = $result->fetch()) { |
131
|
|
|
// Reset storing to current. |
132
|
|
|
$fieldArray['index_stored'] = $resArray['is_listed']; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
// Index field in index if it should be used for auto-completion. |
136
|
|
|
if (!empty($fieldArray['index_autocomplete'])) { |
137
|
|
|
$fieldArray['index_indexed'] = 1; |
138
|
|
|
} |
139
|
|
|
if ( |
140
|
|
|
isset($fieldArray['index_indexed']) |
141
|
|
|
&& $fieldArray['index_indexed'] == 0 |
142
|
|
|
&& !isset($fieldArray['index_autocomplete']) |
143
|
|
|
) { |
144
|
|
|
// Get current configuration. |
145
|
|
|
$result = $queryBuilder |
146
|
|
|
->select($table . '.index_autocomplete AS index_autocomplete') |
147
|
|
|
->from($table) |
148
|
|
|
->where( |
149
|
|
|
$queryBuilder->expr()->eq($table . '.uid', intval($id)), |
150
|
|
|
Helper::whereExpression($table) |
151
|
|
|
) |
152
|
|
|
->setMaxResults(1) |
153
|
|
|
->execute(); |
154
|
|
|
|
155
|
|
|
if ($resArray = $result->fetch()) { |
156
|
|
|
// Reset indexing to current. |
157
|
|
|
$fieldArray['index_indexed'] = $resArray['index_autocomplete']; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
// Field post-processing for tables "tx_dlf_metadata" and "tx_dlf_structures". |
161
|
|
|
case 'tx_dlf_structures': |
162
|
|
|
// The index name should not be changed in production. |
163
|
|
|
if (isset($fieldArray['index_name'])) { |
164
|
|
|
if (count($fieldArray) < 2) { |
165
|
|
|
// Unset the whole field array. |
166
|
|
|
$fieldArray = []; |
167
|
|
|
} else { |
168
|
|
|
// Get current index name. |
169
|
|
|
$result = $queryBuilder |
170
|
|
|
->select($table . '.index_autocomplete AS index_autocomplete') |
171
|
|
|
->from($table) |
172
|
|
|
->where( |
173
|
|
|
$queryBuilder->expr()->eq($table . '.uid', intval($id)), |
174
|
|
|
Helper::whereExpression($table) |
175
|
|
|
) |
176
|
|
|
->setMaxResults(1) |
177
|
|
|
->execute(); |
178
|
|
|
|
179
|
|
|
if ($resArray = $result->fetch()) { |
180
|
|
|
// Reset indexing to current. |
181
|
|
|
$fieldArray['index_indexed'] = $resArray['index_autocomplete']; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
Helper::devLog('Prevented change of index_name for UID ' . $id . ' in table "' . $table . '"', DEVLOG_SEVERITY_NOTICE); |
185
|
|
|
} |
186
|
|
|
break; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* After database operations hook for the process_datamap() method. |
193
|
|
|
* |
194
|
|
|
* @access public |
195
|
|
|
* |
196
|
|
|
* @param string $status: 'new' or 'update' |
197
|
|
|
* @param string $table: The destination table |
198
|
|
|
* @param int $id: The uid of the record |
199
|
|
|
* @param array &$fieldArray: Array of field values |
200
|
|
|
* |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
|
|
public function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray) |
204
|
|
|
{ |
205
|
|
|
if ($status == 'update') { |
206
|
|
|
switch ($table) { |
207
|
|
|
// After database operations for table "tx_dlf_documents". |
208
|
|
|
case 'tx_dlf_documents': |
209
|
|
|
// Delete/reindex document in Solr if "hidden" status or collections have changed. |
210
|
|
|
if ( |
211
|
|
|
isset($fieldArray['hidden']) |
212
|
|
|
|| isset($fieldArray['collections']) |
213
|
|
|
) { |
214
|
|
|
// Get Solr-Core. |
215
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
216
|
|
|
->getQueryBuilderForTable('tx_dlf_solrcores'); |
217
|
|
|
|
218
|
|
|
$result = $queryBuilder |
219
|
|
|
->select( |
220
|
|
|
'tx_dlf_solrcores.uid AS core', |
221
|
|
|
'tx_dlf_solrcores.index_name', |
222
|
|
|
'tx_dlf_documents_join.hidden AS hidden' |
223
|
|
|
) |
224
|
|
|
->innerJoin( |
225
|
|
|
'tx_dlf_solrcores', |
226
|
|
|
'tx_dlf_documents', |
227
|
|
|
'tx_dlf_documents_join', |
228
|
|
|
$queryBuilder->expr()->eq( |
229
|
|
|
'tx_dlf_documents_join.solrcore', |
230
|
|
|
'tx_dlf_solrcores.uid' |
231
|
|
|
) |
232
|
|
|
) |
233
|
|
|
->from('tx_dlf_solrcores') |
234
|
|
|
->where( |
235
|
|
|
$queryBuilder->expr()->eq( |
236
|
|
|
'tx_dlf_documents_join.uid', |
237
|
|
|
intval($id) |
238
|
|
|
) |
239
|
|
|
) |
240
|
|
|
->setMaxResults(1) |
241
|
|
|
->execute(); |
242
|
|
|
|
243
|
|
|
$allResults = $result->fetchAll(); |
244
|
|
|
|
245
|
|
|
if (count($allResults) == 1) { |
246
|
|
|
$resArray = $allResults[0]; |
247
|
|
|
if ($resArray['hidden']) { |
248
|
|
|
// Establish Solr connection. |
249
|
|
|
$solr = Solr::getInstance($resArray['core']); |
250
|
|
|
if ($solr->ready) { |
251
|
|
|
// Delete Solr document. |
252
|
|
|
$updateQuery = $solr->service->createUpdate(); |
253
|
|
|
$updateQuery->addDeleteQuery('uid:' . $id); |
254
|
|
|
$updateQuery->addCommit(); |
255
|
|
|
$solr->service->update($updateQuery); |
256
|
|
|
} |
257
|
|
|
} else { |
258
|
|
|
// Reindex document. |
259
|
|
|
$doc = Document::getInstance($id); |
260
|
|
|
if ($doc->ready) { |
261
|
|
|
Indexer::add($doc, $resArray['core']); |
262
|
|
|
} else { |
263
|
|
|
Helper::devLog('Failed to re-index document with UID ' . $id, DEVLOG_SEVERITY_ERROR); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
break; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Post-processing hook for the process_cmdmap() method. |
275
|
|
|
* |
276
|
|
|
* @access public |
277
|
|
|
* |
278
|
|
|
* @param string $command: 'move', 'copy', 'localize', 'inlineLocalizeSynchronize', 'delete' or 'undelete' |
279
|
|
|
* @param string $table: The destination table |
280
|
|
|
* @param int $id: The uid of the record |
281
|
|
|
* |
282
|
|
|
* @return void |
283
|
|
|
*/ |
284
|
|
|
public function processCmdmap_postProcess($command, $table, $id) |
285
|
|
|
{ |
286
|
|
|
if ( |
287
|
|
|
in_array($command, ['move', 'delete', 'undelete']) |
288
|
|
|
&& $table == 'tx_dlf_documents' |
289
|
|
|
) { |
290
|
|
|
// Get Solr core. |
291
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
292
|
|
|
->getQueryBuilderForTable('tx_dlf_solrcores'); |
293
|
|
|
// Record in "tx_dlf_documents" is already deleted at this point. |
294
|
|
|
$queryBuilder |
295
|
|
|
->getRestrictions() |
296
|
|
|
->removeByType(DeletedRestriction::class); |
297
|
|
|
|
298
|
|
|
$result = $queryBuilder |
299
|
|
|
->select( |
300
|
|
|
'tx_dlf_solrcores.uid AS core' |
301
|
|
|
) |
302
|
|
|
->innerJoin( |
303
|
|
|
'tx_dlf_solrcores', |
304
|
|
|
'tx_dlf_documents', |
305
|
|
|
'tx_dlf_documents_join', |
306
|
|
|
$queryBuilder->expr()->eq( |
307
|
|
|
'tx_dlf_documents_join.solrcore', |
308
|
|
|
'tx_dlf_solrcores.uid' |
309
|
|
|
) |
310
|
|
|
) |
311
|
|
|
->from('tx_dlf_solrcores') |
312
|
|
|
->where( |
313
|
|
|
$queryBuilder->expr()->eq( |
314
|
|
|
'tx_dlf_documents_join.uid', |
315
|
|
|
intval($id) |
316
|
|
|
) |
317
|
|
|
) |
318
|
|
|
->setMaxResults(1) |
319
|
|
|
->execute(); |
320
|
|
|
|
321
|
|
|
$allResults = $result->fetchAll(); |
322
|
|
|
|
323
|
|
|
if (count($allResults) == 1) { |
324
|
|
|
$resArray = $allResults[0]; |
325
|
|
|
switch ($command) { |
326
|
|
|
case 'move': |
327
|
|
|
case 'delete': |
328
|
|
|
// Establish Solr connection. |
329
|
|
|
$solr = Solr::getInstance($resArray['core']); |
330
|
|
|
if ($solr->ready) { |
331
|
|
|
// Delete Solr document. |
332
|
|
|
$updateQuery = $solr->service->createUpdate(); |
333
|
|
|
$updateQuery->addDeleteQuery('uid:' . $id); |
334
|
|
|
$updateQuery->addCommit(); |
335
|
|
|
$solr->service->update($updateQuery); |
336
|
|
|
if ($command == 'delete') { |
337
|
|
|
break; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
case 'undelete': |
341
|
|
|
// Reindex document. |
342
|
|
|
$doc = Document::getInstance($id); |
343
|
|
|
if ($doc->ready) { |
344
|
|
|
Indexer::add($doc, $resArray['core']); |
345
|
|
|
} else { |
346
|
|
|
Helper::devLog('Failed to re-index document with UID ' . $id, DEVLOG_SEVERITY_ERROR); |
347
|
|
|
} |
348
|
|
|
break; |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
if ( |
353
|
|
|
$command === 'delete' |
354
|
|
|
&& $table == 'tx_dlf_solrcores' |
355
|
|
|
) { |
356
|
|
|
// Is core deletion allowed in extension configuration? |
357
|
|
|
$extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['dlf']); |
358
|
|
|
if (!empty($extConf['solrAllowCoreDelete'])) { |
359
|
|
|
// Delete core from Apache Solr as well. |
360
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
361
|
|
|
->getQueryBuilderForTable('tx_dlf_solrcores'); |
362
|
|
|
// Record in "tx_dlf_solrcores" is already deleted at this point. |
363
|
|
|
$queryBuilder |
364
|
|
|
->getRestrictions() |
365
|
|
|
->removeByType(DeletedRestriction::class); |
366
|
|
|
|
367
|
|
|
$result = $queryBuilder |
368
|
|
|
->select( |
369
|
|
|
'tx_dlf_solrcores.index_name AS core' |
370
|
|
|
) |
371
|
|
|
->from('tx_dlf_solrcores') |
372
|
|
|
->where($queryBuilder->expr()->eq('tx_dlf_solrcores.uid', intval($id))) |
373
|
|
|
->setMaxResults(1) |
374
|
|
|
->execute(); |
375
|
|
|
|
376
|
|
|
$allResults = $result->fetchAll(); |
377
|
|
|
|
378
|
|
|
if (count($allResults) == 1) { |
379
|
|
|
$resArray = $allResults[0]; |
380
|
|
|
// Establish Solr connection. |
381
|
|
|
$solr = Solr::getInstance(); |
382
|
|
|
if ($solr->ready) { |
383
|
|
|
// Delete Solr core. |
384
|
|
|
$query = $solr->service->createCoreAdmin(); |
385
|
|
|
$action = $query->createUnload(); |
386
|
|
|
$action->setCore($resArray['core']); |
387
|
|
|
$action->setDeleteDataDir(true); |
388
|
|
|
$action->setDeleteIndex(true); |
389
|
|
|
$action->setDeleteInstanceDir(true); |
390
|
|
|
$query->setAction($action); |
391
|
|
|
try { |
392
|
|
|
$response = $solr->service->coreAdmin($query); |
393
|
|
|
if ($response->getWasSuccessful()) { |
394
|
|
|
return; |
395
|
|
|
} |
396
|
|
|
} catch (\Exception $e) { |
397
|
|
|
// Nothing to do here. |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
Helper::devLog('Core ' . $resArray['core'] . ' could not be deleted from Apache Solr', DEVLOG_SEVERITY_WARNING); |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|