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\ExpressionLanguage; |
14
|
|
|
|
15
|
|
|
use Kitodo\Dlf\Common\AbstractDocument; |
16
|
|
|
use Kitodo\Dlf\Common\IiifManifest; |
17
|
|
|
use Kitodo\Dlf\Domain\Model\Document; |
18
|
|
|
use Kitodo\Dlf\Domain\Repository\DocumentRepository; |
19
|
|
|
use Psr\Log\LoggerAwareInterface; |
20
|
|
|
use Psr\Log\LoggerAwareTrait; |
21
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunction; |
22
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; |
23
|
|
|
use TYPO3\CMS\Core\ExpressionLanguage\RequestWrapper; |
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
25
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
26
|
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Provider class for additional "getDocumentType" function to the ExpressionLanguage. |
30
|
|
|
* |
31
|
|
|
* @package TYPO3 |
32
|
|
|
* @subpackage dlf |
33
|
|
|
* |
34
|
|
|
* @access public |
35
|
|
|
*/ |
36
|
|
|
class DocumentTypeFunctionProvider implements ExpressionFunctionProviderInterface, LoggerAwareInterface |
37
|
|
|
{ |
38
|
|
|
use LoggerAwareTrait; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @access public |
42
|
|
|
* |
43
|
|
|
* @return ExpressionFunction[] An array of Function instances |
44
|
|
|
*/ |
45
|
|
|
public function getFunctions(): array |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
$this->getDocumentTypeFunction(), |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* This holds the current document |
54
|
|
|
* |
55
|
|
|
* @var Document|null |
56
|
|
|
* @access protected |
57
|
|
|
*/ |
58
|
|
|
protected ?Document $document; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var DocumentRepository |
62
|
|
|
*/ |
63
|
|
|
protected $documentRepository; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param DocumentRepository $documentRepository |
67
|
|
|
*/ |
68
|
|
|
public function injectDocumentRepository(DocumentRepository $documentRepository): void |
69
|
|
|
{ |
70
|
|
|
$this->documentRepository = $documentRepository; |
71
|
|
|
} |
72
|
|
|
/** |
73
|
|
|
* Initialize the extbase repositories |
74
|
|
|
* |
75
|
|
|
* @access protected |
76
|
|
|
* |
77
|
|
|
* @param int $storagePid The storage pid |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
protected function initializeRepositories(int $storagePid): void |
82
|
|
|
{ |
83
|
|
|
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class); |
84
|
|
|
$frameworkConfiguration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
85
|
|
|
$frameworkConfiguration['persistence']['storagePid'] = MathUtility::forceIntegerInRange($storagePid, 0); |
86
|
|
|
$configurationManager->setConfiguration($frameworkConfiguration); |
87
|
|
|
$this->documentRepository = GeneralUtility::makeInstance(DocumentRepository::class); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Shortcut function to access field values |
92
|
|
|
* |
93
|
|
|
* @access protected |
94
|
|
|
* |
95
|
|
|
* @return ExpressionFunction |
96
|
|
|
*/ |
97
|
|
|
protected function getDocumentTypeFunction(): ExpressionFunction |
98
|
|
|
{ |
99
|
|
|
return new ExpressionFunction( |
100
|
|
|
'getDocumentType', |
101
|
|
|
function() |
102
|
|
|
{ |
103
|
|
|
// Not implemented, we only use the evaluator |
104
|
|
|
}, |
105
|
|
|
function($arguments, $cPid) |
106
|
|
|
{ |
107
|
|
|
/** @var RequestWrapper $requestWrapper */ |
108
|
|
|
$requestWrapper = $arguments['request']; |
109
|
|
|
$queryParams = $requestWrapper->getQueryParams(); |
110
|
|
|
|
111
|
|
|
$type = 'undefined'; |
112
|
|
|
|
113
|
|
|
// It happens that $queryParams is an empty array or does not contain a key 'tx_dlf' |
114
|
|
|
// in case of other contexts. In this case we have to return here to avoid log messages. |
115
|
|
|
if (empty($queryParams) || !isset($queryParams['tx_dlf'])) { |
116
|
|
|
return $type; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// object type if model parameter is not empty so we assume that it is a 3d object |
120
|
|
|
if (!empty($queryParams['tx_dlf']['model'])) { |
121
|
|
|
return 'object'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// It happens that $queryParams does not contain a key 'tx_dlf[id]' |
125
|
|
|
if (!isset($queryParams['tx_dlf']['id'])) { |
126
|
|
|
return $type; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Load document with current plugin parameters. |
130
|
|
|
$this->loadDocument($queryParams['tx_dlf'], $cPid); |
131
|
|
|
if (!isset($this->document) || $this->document->getCurrentDocument() === null) { |
|
|
|
|
132
|
|
|
return $type; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Set PID for metadata definitions. |
136
|
|
|
$this->document->getCurrentDocument()->cPid = $cPid; |
137
|
|
|
|
138
|
|
|
$metadata = $this->document->getCurrentDocument()->getToplevelMetadata($cPid); |
139
|
|
|
|
140
|
|
|
if (!empty($metadata['type'][0]) |
141
|
|
|
&& !$this->isIiifManifestWithNewspaperRelatedType($metadata['type'][0])) { |
142
|
|
|
$type = $metadata['type'][0]; |
143
|
|
|
} |
144
|
|
|
return $type; |
145
|
|
|
}); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Loads the current document into $this->document |
150
|
|
|
* |
151
|
|
|
* @access protected |
152
|
|
|
* |
153
|
|
|
* @param array $requestData The request data |
154
|
|
|
* @param int $pid Storage Pid |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
protected function loadDocument(array $requestData, int $pid): void |
159
|
|
|
{ |
160
|
|
|
// Try to get document format from database |
161
|
|
|
if (!empty($requestData['id'])) { |
162
|
|
|
$this->initializeRepositories($pid); |
163
|
|
|
$doc = null; |
164
|
|
|
$this->document = null; |
165
|
|
|
if (MathUtility::canBeInterpretedAsInteger($requestData['id'])) { |
166
|
|
|
// find document from repository by uid |
167
|
|
|
$this->document = $this->documentRepository->findOneByIdAndSettings((int) $requestData['id'], ['storagePid' => $pid]); |
168
|
|
|
if ($this->document) { |
169
|
|
|
$doc = AbstractDocument::getInstance($this->document->getLocation(), ['storagePid' => $pid], true); |
170
|
|
|
} else { |
171
|
|
|
$this->logger->error('Invalid UID "' . $requestData['id'] . '" or PID "' . $pid . '" for document loading'); |
172
|
|
|
} |
173
|
|
|
} elseif (GeneralUtility::isValidUrl($requestData['id'])) { |
174
|
|
|
$doc = AbstractDocument::getInstance($requestData['id'], ['storagePid' => $pid], true); |
175
|
|
|
|
176
|
|
|
if ($doc !== null) { |
177
|
|
|
if ($doc->recordId) { |
178
|
|
|
$this->document = $this->documentRepository->findOneByRecordId($doc->recordId); |
179
|
|
|
} |
180
|
|
|
if (!isset($this->document)) { |
181
|
|
|
// create new dummy Document object |
182
|
|
|
$this->document = GeneralUtility::makeInstance(Document::class); |
183
|
|
|
} |
184
|
|
|
$this->document->setLocation($requestData['id']); |
185
|
|
|
} else { |
186
|
|
|
$this->logger->error('Invalid location given "' . $requestData['id'] . '" for document loading'); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
if ($this->document !== null && $doc !== null) { |
190
|
|
|
$this->document->setCurrentDocument($doc); |
191
|
|
|
} |
192
|
|
|
} elseif (!empty($requestData['recordId'])) { |
193
|
|
|
$this->document = $this->documentRepository->findOneByRecordId($requestData['recordId']); |
|
|
|
|
194
|
|
|
if ($this->document !== null) { |
195
|
|
|
$doc = AbstractDocument::getInstance($this->document->getLocation(), ['storagePid' => $pid], true); |
196
|
|
|
if ($doc !== null) { |
197
|
|
|
$this->document->setCurrentDocument($doc); |
198
|
|
|
} else { |
199
|
|
|
$this->logger->error('Failed to load document with record ID "' . $requestData['recordId'] . '"'); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} else { |
203
|
|
|
$this->logger->error('Empty UID or invalid PID "' . $pid . '" for document loading'); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Check if is IIIF Manifest with newspaper related type. |
209
|
|
|
* |
210
|
|
|
* Calendar plugin does not support IIIF (yet). Abort for all newspaper related types. |
211
|
|
|
* |
212
|
|
|
* @access private |
213
|
|
|
* |
214
|
|
|
* @param string $type The metadata type |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
private function isIiifManifestWithNewspaperRelatedType(string $type): bool |
218
|
|
|
{ |
219
|
|
|
return ($this->document->getCurrentDocument() instanceof IiifManifest |
220
|
|
|
&& in_array($type, ['newspaper', 'ephemera', 'year', 'issue'])); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.