1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\TextExtraction\Rest; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use SilverStripe\Core\Environment; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
|
11
|
|
|
class TikaRestClient extends Client |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Authentication options to be sent to the Tika server |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $options = ['username' => null, 'password' => null]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $mimes = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @param string $baseUrl |
28
|
|
|
* @param array $config |
29
|
|
|
*/ |
30
|
|
|
public function __construct($baseUrl = '', $config = []) |
31
|
|
|
{ |
32
|
|
|
$password = Environment::getEnv('SS_TIKA_PASSWORD'); |
33
|
|
|
|
34
|
|
|
if (!empty($password)) { |
35
|
|
|
$this->options = [ |
36
|
|
|
'username' => Environment::getEnv('SS_TIKA_USERNAME'), |
37
|
|
|
'password' => $password, |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
parent::__construct($config); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Detect if the service is available |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function isAvailable() |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
$result = $this->get(null); |
53
|
|
|
$result->setAuth($this->options['username'], $this->options['password']); |
|
|
|
|
54
|
|
|
$result->send(); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if ($result->getResponse()->getStatusCode() == 200) { |
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
} catch (RequestException $ex) { |
60
|
|
|
$msg = sprintf("Tika unavailable - %s", $ex->getMessage()); |
61
|
|
|
Injector::inst()->get(LoggerInterface::class)->info($msg); |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get version code |
69
|
|
|
* |
70
|
|
|
* @return float |
71
|
|
|
*/ |
72
|
|
|
public function getVersion() |
73
|
|
|
{ |
74
|
|
|
$response = $this->get('version'); |
75
|
|
|
$response->setAuth($this->options['username'], $this->options['password']); |
76
|
|
|
$response->send(); |
77
|
|
|
$version = 0.0; |
78
|
|
|
|
79
|
|
|
// Parse output |
80
|
|
|
if ($response->getResponse()->getStatusCode() == 200 && |
81
|
|
|
preg_match('/Apache Tika (?<version>[\.\d]+)/', $response->getResponse()->getBody(), $matches) |
82
|
|
|
) { |
83
|
|
|
$version = (float)$matches['version']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $version; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets supported mime data. May include aliased mime types. |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getSupportedMimes() |
95
|
|
|
{ |
96
|
|
|
if ($this->mimes) { |
|
|
|
|
97
|
|
|
return $this->mimes; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$response = $this->get( |
101
|
|
|
'mime-types', |
102
|
|
|
array('Accept' => 'application/json') |
103
|
|
|
); |
104
|
|
|
$response->setAuth($this->options['username'], $this->options['password']); |
105
|
|
|
$response->send(); |
106
|
|
|
|
107
|
|
|
return $this->mimes = $response->getResponse()->json(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Extract text content from a given file. |
112
|
|
|
* Logs a notice-level error if the document can't be parsed. |
113
|
|
|
* |
114
|
|
|
* @param string $file Full filesystem path to a file to post |
115
|
|
|
* @return string Content of the file extracted as plain text |
116
|
|
|
*/ |
117
|
|
|
public function tika($file) |
118
|
|
|
{ |
119
|
|
|
$text = null; |
120
|
|
|
try { |
121
|
|
|
$response = $this->put( |
122
|
|
|
'tika', |
123
|
|
|
['Accept' => 'text/plain'], |
124
|
|
|
file_get_contents($file) |
|
|
|
|
125
|
|
|
); |
126
|
|
|
$response->setAuth($this->options['username'], $this->options['password']); |
127
|
|
|
$response->send(); |
128
|
|
|
$text = $response->getResponse()->getBody(true); |
129
|
|
|
} catch (RequestException $e) { |
130
|
|
|
$msg = sprintf( |
131
|
|
|
'TikaRestClient was not able to process %s. Response: %s %s.', |
132
|
|
|
$file, |
133
|
|
|
$e->getResponse()->getStatusCode(), |
134
|
|
|
$e->getResponse()->getReasonPhrase() |
135
|
|
|
); |
136
|
|
|
// Only available if tika-server was started with --includeStack |
137
|
|
|
$body = $e->getResponse()->getBody(true); |
|
|
|
|
138
|
|
|
if ($body) { |
|
|
|
|
139
|
|
|
$msg .= ' Body: ' . $body; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
Injector::inst()->get(LoggerInterface::class)->info($msg); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $text; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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.