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