|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Enables text extraction of file content via the Tika Rest Server |
|
5
|
|
|
* |
|
6
|
|
|
* {@link http://tika.apache.org/1.7/gettingstarted.html} |
|
7
|
|
|
*/ |
|
8
|
|
|
class TikaServerTextExtractor extends FileTextExtractor |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Tika server is pretty efficient so use it immediately if available |
|
12
|
|
|
* |
|
13
|
|
|
* @var integer |
|
14
|
|
|
* @config |
|
15
|
|
|
*/ |
|
16
|
|
|
private static $priority = 80; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Server endpoint |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
* @config |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $server_endpoint; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var TikaRestClient |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $client = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return TikaRestClient |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getClient() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->client ?: |
|
37
|
|
|
($this->client = |
|
38
|
|
|
Injector::inst()->createWithArgs( |
|
39
|
|
|
'TikaRestClient', |
|
40
|
|
|
array($this->getServerEndpoint()) |
|
41
|
|
|
) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getServerEndpoint() |
|
46
|
|
|
{ |
|
47
|
|
|
if (defined('SS_TIKA_ENDPOINT')) { |
|
48
|
|
|
return SS_TIKA_ENDPOINT; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (getenv('SS_TIKA_ENDPOINT')) { |
|
52
|
|
|
return getenv('SS_TIKA_ENDPOINT'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Default to configured endpoint |
|
56
|
|
|
return $this->config()->server_endpoint; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the version of tika installed, or 0 if not installed |
|
61
|
|
|
* |
|
62
|
|
|
* @return float version of tika |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getVersion() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this |
|
67
|
|
|
->getClient() |
|
68
|
|
|
->getVersion(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function isAvailable() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->getServerEndpoint() && |
|
74
|
|
|
$this->getClient()->isAvailable() && |
|
75
|
|
|
$this->getVersion() >= 1.7; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function supportsExtension($extension) |
|
79
|
|
|
{ |
|
80
|
|
|
// Determine support via mime type only |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Cache of supported mime types |
|
87
|
|
|
* |
|
88
|
|
|
* @var array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $supportedMimes = array(); |
|
91
|
|
|
|
|
92
|
|
|
public function supportsMime($mime) |
|
93
|
|
|
{ |
|
94
|
|
|
$supported = $this->supportedMimes ?: |
|
95
|
|
|
($this->supportedMimes = $this->getClient()->getSupportedMimes()); |
|
96
|
|
|
|
|
97
|
|
|
// Check if supported (most common / quickest lookup) |
|
98
|
|
|
if (isset($supported[$mime])) { |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Check aliases |
|
103
|
|
|
foreach ($supported as $info) { |
|
104
|
|
|
if (isset($info['alias']) && in_array($mime, $info['alias'])) { |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getContent($path) |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->getClient()->tika($path); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
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.