1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\FieldProcessor; |
19
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; |
21
|
|
|
use Doctrine\DBAL\Driver\Exception as DBALDriverException; |
22
|
|
|
use Doctrine\DBAL\Exception as DBALException; |
23
|
|
|
use Exception; |
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Service class that modifies fields in an Apache Solr Document, used for |
28
|
|
|
* common field processing during indexing or resolving |
29
|
|
|
* |
30
|
|
|
* @author Daniel Poetzinger <[email protected]> |
31
|
|
|
* @copyright (c) 2009-2015 Daniel Poetzinger <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class Service |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Modifies a list of documents |
37
|
|
|
* |
38
|
|
|
* @param Document[] $documents |
39
|
|
|
* @param array $processingConfiguration |
40
|
|
|
* @throws DBALDriverException |
41
|
|
|
* @throws DBALException |
42
|
89 |
|
*/ |
43
|
|
|
public function processDocuments(array $documents, array $processingConfiguration) |
44
|
89 |
|
{ |
45
|
89 |
|
foreach ($documents as $document) { |
46
|
|
|
$this->processDocument($document, $processingConfiguration); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* modifies a document according to the given configuration |
52
|
|
|
* |
53
|
|
|
* @param Document $document |
54
|
|
|
* @param array $processingConfiguration |
55
|
|
|
* @throws DBALDriverException |
56
|
|
|
* @throws DBALException |
57
|
93 |
|
*/ |
58
|
|
|
public function processDocument(Document $document, array $processingConfiguration) |
59
|
93 |
|
{ |
60
|
93 |
|
foreach ($processingConfiguration as $fieldName => $instruction) { |
61
|
93 |
|
$fieldValue = $document[$fieldName] ?? false; |
62
|
|
|
$isSingleValueField = false; |
63
|
93 |
|
|
64
|
93 |
|
if ($fieldValue !== false) { |
65
|
|
|
if (!is_array($fieldValue)) { |
66
|
91 |
|
// turn single value field into multi value field |
67
|
91 |
|
$fieldValue = [$fieldValue]; |
68
|
|
|
$isSingleValueField = true; |
69
|
|
|
} |
70
|
93 |
|
|
71
|
93 |
|
switch ($instruction) { |
72
|
|
|
case 'timestampToUtcIsoDate': |
73
|
|
|
/** @var $processor TimestampToUtcIsoDate */ |
74
|
|
|
$processor = GeneralUtility::makeInstance(TimestampToUtcIsoDate::class); |
75
|
|
|
$fieldValue = $processor->process($fieldValue); |
76
|
93 |
|
break; |
77
|
|
|
case 'timestampToIsoDate': |
78
|
91 |
|
/** @var $processor TimestampToIsoDate */ |
79
|
91 |
|
$processor = GeneralUtility::makeInstance(TimestampToIsoDate::class); |
80
|
91 |
|
$fieldValue = $processor->process($fieldValue); |
81
|
71 |
|
break; |
82
|
|
|
case 'pathToHierarchy': |
83
|
1 |
|
/** @var $processor PathToHierarchy */ |
84
|
1 |
|
$processor = GeneralUtility::makeInstance(PathToHierarchy::class); |
85
|
1 |
|
$fieldValue = $processor->process($fieldValue); |
86
|
71 |
|
break; |
87
|
|
|
case 'pageUidToHierarchy': |
88
|
69 |
|
/** @var $processor PageUidToHierarchy */ |
89
|
69 |
|
$processor = GeneralUtility::makeInstance(PageUidToHierarchy::class); |
90
|
69 |
|
$fieldValue = $processor->process($fieldValue); |
91
|
2 |
|
break; |
92
|
|
|
case 'categoryUidToHierarchy': |
93
|
|
|
/** @var $processor CategoryUidToHierarchy */ |
94
|
|
|
$processor = GeneralUtility::makeInstance(CategoryUidToHierarchy::class); |
95
|
|
|
$fieldValue = $processor->process($fieldValue); |
96
|
2 |
|
break; |
97
|
2 |
|
case 'uppercase': |
98
|
2 |
|
$fieldValue = array_map('mb_strtoupper', $fieldValue); |
99
|
|
|
break; |
100
|
|
|
default: |
101
|
93 |
|
$classReference = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['fieldProcessor'][$instruction] ?? false; |
102
|
|
|
if ($classReference) { |
103
|
91 |
|
$customFieldProcessor = GeneralUtility::makeInstance($classReference); |
104
|
|
|
if ($customFieldProcessor instanceof FieldProcessor) { |
105
|
|
|
$fieldValue = $customFieldProcessor->process($fieldValue); |
106
|
93 |
|
} else { |
107
|
|
|
throw new Exception('A FieldProcessor must implement the FieldProcessor interface', 1635082295); |
108
|
|
|
} |
109
|
|
|
} else { |
110
|
|
|
throw new Exception(sprintf('FieldProcessor %s is not implemented', $instruction), 1635082296); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($isSingleValueField) { |
115
|
|
|
// turn multi value field back into single value field |
116
|
|
|
$fieldValue = $fieldValue[0]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$document->setField($fieldName, $fieldValue); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|