1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Guzzle\Http\Client; |
4
|
|
|
use Guzzle\Http\Exception\RequestException; |
5
|
|
|
|
6
|
|
|
class TikaRestClient extends Client |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Detect if the service is available |
10
|
|
|
* |
11
|
|
|
* @return bool |
12
|
|
|
*/ |
13
|
|
|
public function isAvailable() |
14
|
|
|
{ |
15
|
|
|
try { |
16
|
|
|
return $this |
17
|
|
|
->get()->send() |
18
|
|
|
->getStatusCode() == 200; |
19
|
|
|
} catch (RequestException $ex) { |
20
|
|
|
return false; |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get version code |
26
|
|
|
* |
27
|
|
|
* @return float |
28
|
|
|
*/ |
29
|
|
|
public function getVersion() |
30
|
|
|
{ |
31
|
|
|
$response = $this->get('version')->send(); |
32
|
|
|
// Parse output |
33
|
|
|
if ($response->getStatusCode() == 200 && |
34
|
|
|
preg_match('/Apache Tika (?<version>[\.\d]+)/', $response->getBody(), $matches) |
35
|
|
|
) { |
36
|
|
|
return (float)$matches['version']; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return 0.0; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected $mimes = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Gets supported mime data. May include aliased mime types. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getSupportedMimes() |
50
|
|
|
{ |
51
|
|
|
if ($this->mimes) { |
|
|
|
|
52
|
|
|
return $this->mimes; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$response = $this->get( |
56
|
|
|
'mime-types', |
57
|
|
|
array('Accept' => 'application/json') |
58
|
|
|
)->send(); |
59
|
|
|
|
60
|
|
|
return $this->mimes = $response->json(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Extract text content from a given file. |
65
|
|
|
* Logs a notice-level error if the document can't be parsed. |
66
|
|
|
* |
67
|
|
|
* @param string $file Full filesystem path to a file to post |
68
|
|
|
* @return string Content of the file extracted as plain text |
69
|
|
|
*/ |
70
|
|
|
public function tika($file) |
71
|
|
|
{ |
72
|
|
|
$text = null; |
73
|
|
|
try { |
74
|
|
|
$response = $this->put( |
75
|
|
|
'tika', |
76
|
|
|
array('Accept' => 'text/plain'), |
77
|
|
|
file_get_contents($file) |
78
|
|
|
)->send(); |
79
|
|
|
$text = $response->getBody(true); |
80
|
|
|
} catch (RequestException $e) { |
81
|
|
|
$msg = sprintf( |
82
|
|
|
'TikaRestClient was not able to process %s. Response: %s %s.', |
83
|
|
|
$file, |
84
|
|
|
$e->getResponse()->getStatusCode(), |
|
|
|
|
85
|
|
|
$e->getResponse()->getReasonPhrase() |
|
|
|
|
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
// Only available if tika-server was started with --includeStack |
89
|
|
|
$body = $e->getResponse()->getBody(true); |
|
|
|
|
90
|
|
|
if ($body) { |
91
|
|
|
$msg .= ' Body: ' . $body; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
SS_Log::log($msg, SS_Log::NOTICE); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $text; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.