/
TikaStatus.php
271 lines (243 loc) · 7.43 KB
/
TikaStatus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
namespace ApacheSolrForTypo3\Tika\Report;
/***************************************************************
* Copyright notice
*
* (c) 2010-2015 Ingo Renner <ingo@typo3.org>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use ApacheSolrForTypo3\Solr\ConnectionManager;
use ApacheSolrForTypo3\Tika\Service\Tika\ServerService;
use ApacheSolrForTypo3\Tika\Utility\FileUtility;
use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Reports\Status;
use TYPO3\CMS\Reports\StatusProviderInterface;
/**
* Provides a status report about whether Tika is properly configured
*
* @author Ingo Renner <ingo@typo3.org>
* @package TYPO3
* @subpackage tika
*/
class TikaStatus implements StatusProviderInterface
{
/**
* EXT:tika configuration.
*
* @var array
*/
protected $tikaConfiguration = [];
/**
* Constructor, reads the extension's configuration
*
*/
public function __construct()
{
$this->tikaConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['tika']);
}
/**
* Checks whether Tika is properly configured
*
* TODO Check whether EXT:tika is installed AFTER EXT:solr
*/
public function getStatus()
{
$checks = [];
if ($this->tikaConfiguration['extractor'] != 'solr') {
$checks[] = $this->getJavaInstalledStatus();
}
switch ($this->tikaConfiguration['extractor']) {
case 'jar':
case 'tika': // backwards compatibility only
$checks[] = $this->getAppConfigurationStatus();
break;
case 'server':
$checks[] = $this->getServerConfigurationStatus();
break;
case 'solr':
$checks[] = $this->getSolrCellConfigurationStatus();
break;
}
return $checks;
}
/**
* Creates a configuration OK status.
*
* @return Status
*/
protected function getOkStatus()
{
return GeneralUtility::makeInstance(Status::class,
'Apache Tika',
'Configuration OK'
);
}
/**
* Creates a system status report status checking whether Java is installed.
*
* @return \TYPO3\CMS\Reports\Status
*/
protected function getJavaInstalledStatus()
{
$status = GeneralUtility::makeInstance(Status::class,
'Apache Tika',
'Java OK'
);
if (!$this->isJavaInstalled()) {
$status = GeneralUtility::makeInstance(Status::class,
'Apache Tika',
'Java Not Found',
'<p>Please install Java.</p>',
Status::ERROR
);
}
return $status;
}
/**
* Checks configuration for use with Tika app jar
*
* @return Status
*/
protected function getAppConfigurationStatus()
{
$status = $this->getOkStatus();
if (!$this->isFilePresent($this->tikaConfiguration['tikaPath'])) {
$status = GeneralUtility::makeInstance(Status::class,
'Apache Tika',
'Configuration Incomplete',
'<p>Could not find Tika app jar.</p>',
Status::ERROR
);
}
return $status;
}
/**
* Checks configuration for use with Tika server jar
*
* @return Status
*/
protected function getServerConfigurationStatus()
{
$status = $this->getOkStatus();
$tikaServer = $this->getTikaServiceFromTikaConfiguration();
if (!$tikaServer->isAvailable()) {
$status = GeneralUtility::makeInstance(Status::class,
'Apache Tika',
'Configuration Incomplete',
'<p>Could not connect to Tika server.</p>',
Status::ERROR
);
}
return $status;
}
/**
* Checks configuration for use with Solr
*
* @return Status
*/
protected function getSolrCellConfigurationStatus()
{
$status = $this->getOkStatus();
$solrCellConfigurationOk = false;
try {
$solr = $this->getSolrServiceFromTikaConfiguration();
$solr->ping();
$plugins = $solr->getPluginsInformation();
if (array_key_exists(
'/update/extract',
$plugins->plugins->QUERYHANDLER
)) {
$solrCellConfigurationOk = true;
}
} catch (\Exception $e) {
$this->writeDevLog(
'Exception while retrieving Solr plugin list',
'tika',
3,
[
'configuration' => $this->tikaConfiguration,
'exception' => $e,
]
);
}
if (!$solrCellConfigurationOk) {
$status = GeneralUtility::makeInstance(Status::class,
'Apache Tika',
'Configuration Incomplete',
'<p>Could not connect to Solr server.</p>',
Status::ERROR
);
}
return $status;
}
/**
* @return \ApacheSolrForTypo3\Solr\SolrService
*/
protected function getSolrServiceFromTikaConfiguration()
{
return GeneralUtility::makeInstance(ConnectionManager::class)->getConnection(
$this->tikaConfiguration['solrHost'],
$this->tikaConfiguration['solrPort'],
$this->tikaConfiguration['solrPath'],
$this->tikaConfiguration['solrScheme']
);
}
/**
* @return \ApacheSolrForTypo3\Tika\Service\Tika\ServerService
*/
protected function getTikaServiceFromTikaConfiguration()
{
return $tikaServer = GeneralUtility::makeInstance(
ServerService::class,
$this->tikaConfiguration
);
}
/**
* Checks if java is installed.
*
* @return bool
*/
protected function isJavaInstalled()
{
return CommandUtility::checkCommand('java');
}
/**
* Checks if a certain file name is present.
*
* @param string $fileName
* @return bool
*/
protected function isFilePresent($fileName)
{
return is_file(FileUtility::getAbsoluteFilePath($fileName));
}
/**
* Wrapper for GeneralUtility::devLog, used during testing to mock logging.
*
* @param string $message message
* @param string $extKey extension key
* @param int $severity severity
* @param array $data data
*/
protected function writeDevLog($message, $extKey, $severity = 0, $data = [])
{
GeneralUtility::devLog($message, $extKey, $severity, $data);
}
}