1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vaites\ApacheTika\Clients; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
use Vaites\ApacheTika\Client; |
8
|
|
|
use Vaites\ApacheTika\Metadata\Metadata; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Apache Tika command line interface client |
12
|
|
|
* |
13
|
|
|
* @author David Martínez <[email protected]> |
14
|
|
|
* @link http://wiki.apache.org/tika/TikaJAXRS |
15
|
|
|
* @link https://tika.apache.org/1.12/formats.html |
16
|
|
|
*/ |
17
|
|
|
class CLIClient extends Client |
18
|
|
|
{ |
19
|
|
|
const MODE = 'cli'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Apache Tika app path |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $path = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Java binary path |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $java = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Configure client |
37
|
|
|
* |
38
|
|
|
* @param string $path |
39
|
|
|
* @param string $java |
40
|
|
|
* |
41
|
|
|
* @throws Exception |
42
|
|
|
*/ |
43
|
|
|
public function __construct($path = null, $java = null) |
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
|
47
|
|
|
if($path) |
48
|
|
|
{ |
49
|
|
|
$this->setPath($path); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if($java) |
53
|
|
|
{ |
54
|
|
|
$this->setJava($java); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if(self::$check === true) |
58
|
|
|
{ |
59
|
|
|
$this->check(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the path |
65
|
|
|
* |
66
|
|
|
* @return null|string |
67
|
|
|
*/ |
68
|
|
|
public function getPath() |
69
|
|
|
{ |
70
|
|
|
return $this->path; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the path |
75
|
|
|
* |
76
|
|
|
* @param string $path |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function setPath($path) |
80
|
|
|
{ |
81
|
|
|
$this->path = $path; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the Java path |
88
|
|
|
* |
89
|
|
|
* @return null|int |
90
|
|
|
*/ |
91
|
|
|
public function getJava() |
92
|
|
|
{ |
93
|
|
|
return $this->java; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the Java path |
98
|
|
|
* |
99
|
|
|
* @param string $java |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setJava($java) |
103
|
|
|
{ |
104
|
|
|
$this->java = $java; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check Java binary, JAR path or server connection |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
|
|
public function check() |
116
|
|
|
{ |
117
|
|
|
if(self::isChecked() === false) |
118
|
|
|
{ |
119
|
|
|
// Java command must not return an error |
120
|
|
|
try |
121
|
|
|
{ |
122
|
|
|
$this->exec(($this->java ?: 'java') . ' -version'); |
123
|
|
|
} |
124
|
|
|
catch(Exception $exception) |
125
|
|
|
{ |
126
|
|
|
throw new Exception('Java command not found'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// JAR path must exists |
130
|
|
|
if(file_exists($this->path) === false) |
131
|
|
|
{ |
132
|
|
|
throw new Exception('Apache Tika app JAR not found'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
self::setChecked(true); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Configure and make a request and return its results |
141
|
|
|
* |
142
|
|
|
* @param string $type |
143
|
|
|
* @param string $file |
144
|
|
|
* @return string|\Vaites\ApacheTika\Metadata\Metadata |
145
|
|
|
* @throws \Exception |
146
|
|
|
*/ |
147
|
|
|
public function request($type, $file = null) |
148
|
|
|
{ |
149
|
|
|
// check if not checked |
150
|
|
|
$this->check(); |
151
|
|
|
|
152
|
|
|
// check if is cached |
153
|
|
|
if($this->isCached($type, $file)) |
154
|
|
|
{ |
155
|
|
|
return $this->getCachedResponse($type, $file); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// command arguments |
159
|
|
|
$arguments = $this->getArguments($type, $file); |
160
|
|
|
|
161
|
|
|
// check the request |
162
|
|
|
$file = parent::checkRequest($type, $file); |
163
|
|
|
|
164
|
|
|
// add last argument |
165
|
|
|
if($file) |
166
|
|
|
{ |
167
|
|
|
$arguments[] = escapeshellarg($file); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// build command |
171
|
|
|
$jar = escapeshellarg($this->path); |
172
|
|
|
$command = ($this->java ?: 'java') . " -jar $jar " . implode(' ', $arguments); |
173
|
|
|
|
174
|
|
|
// run command |
175
|
|
|
$response = $this->exec($command); |
176
|
|
|
|
177
|
|
|
// metadata response |
178
|
|
|
if($type == 'meta') |
179
|
|
|
{ |
180
|
|
|
// fix for invalid? json returned only with images |
181
|
|
|
$response = str_replace(basename($file) . '"}{', '", ', $response); |
182
|
|
|
|
183
|
|
|
// on Windows, response comes in another charset |
184
|
|
|
if($this->platform == 'win') |
185
|
|
|
{ |
186
|
|
|
$response = utf8_encode($response); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$response = Metadata::make($response, $file); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// cache certain responses |
193
|
|
|
if($this->isCacheable($type)) |
194
|
|
|
{ |
195
|
|
|
$this->cacheResponse($type, $response, $file); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $response; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Run the command and return its results |
203
|
|
|
* |
204
|
|
|
* @param string $command |
205
|
|
|
* @return null|string |
206
|
|
|
* @throws \Exception |
207
|
|
|
*/ |
208
|
|
|
public function exec($command) |
209
|
|
|
{ |
210
|
|
|
// run command |
211
|
|
|
$exit = -1; |
212
|
|
|
$logfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tika-error.log'; |
213
|
|
|
$descriptors = [['pipe', 'r'], ['pipe', 'w'], ['file', $logfile, 'a']]; |
214
|
|
|
$process = proc_open($command, $descriptors, $pipes); |
215
|
|
|
$callback = $this->callback; |
216
|
|
|
|
217
|
|
|
// get output if command runs ok |
218
|
|
|
if(is_resource($process)) |
219
|
|
|
{ |
220
|
|
|
fclose($pipes[0]); |
221
|
|
|
$this->response = ''; |
222
|
|
|
while($chunk = stream_get_line($pipes[1], $this->chunkSize)) |
223
|
|
|
{ |
224
|
|
|
if(!is_null($callback)) |
225
|
|
|
{ |
226
|
|
|
$callback($chunk); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$this->response .= $chunk; |
230
|
|
|
} |
231
|
|
|
fclose($pipes[1]); |
232
|
|
|
$exit = proc_close($process); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// exception if exit value is not zero |
236
|
|
|
if($exit > 0) |
237
|
|
|
{ |
238
|
|
|
throw new Exception("Unexpected exit value ($exit) for command $command"); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return trim($this->response); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get the arguments to run the command |
246
|
|
|
* |
247
|
|
|
* @param string $type |
248
|
|
|
* @param string $file |
249
|
|
|
* @return array |
250
|
|
|
* @throws Exception |
251
|
|
|
*/ |
252
|
|
|
protected function getArguments($type, $file = null) |
|
|
|
|
253
|
|
|
{ |
254
|
|
|
// parameters for command |
255
|
|
|
$arguments = []; |
256
|
|
|
switch($type) |
257
|
|
|
{ |
258
|
|
|
case 'html': |
259
|
|
|
$arguments[] = '--html'; |
260
|
|
|
break; |
261
|
|
|
|
262
|
|
|
case 'lang': |
263
|
|
|
$arguments[] = '--language'; |
264
|
|
|
break; |
265
|
|
|
|
266
|
|
|
case 'mime': |
267
|
|
|
$arguments[] = '--detect'; |
268
|
|
|
break; |
269
|
|
|
|
270
|
|
|
case 'meta': |
271
|
|
|
$arguments[] = '--metadata --json'; |
272
|
|
|
break; |
273
|
|
|
|
274
|
|
|
case 'rmeta/ignore': |
275
|
|
|
case 'rmeta/html': |
276
|
|
|
case 'rmeta/text': |
277
|
|
|
throw new Exception('Recursive metadata is not supported in command line mode'); |
278
|
|
|
break; |
279
|
|
|
|
280
|
|
|
case 'text': |
281
|
|
|
$arguments[] = '--text'; |
282
|
|
|
break; |
283
|
|
|
|
284
|
|
|
case 'text-main': |
285
|
|
|
$arguments[] = '--text-main'; |
286
|
|
|
break; |
287
|
|
|
|
288
|
|
|
case 'mime-types': |
289
|
|
|
$arguments[] = '--list-supported-types'; |
290
|
|
|
break; |
291
|
|
|
|
292
|
|
|
case 'detectors': |
293
|
|
|
$arguments[] = '--list-detectors'; |
294
|
|
|
break; |
295
|
|
|
|
296
|
|
|
case 'parsers': |
297
|
|
|
$arguments[] = '--list-parsers'; |
298
|
|
|
break; |
299
|
|
|
|
300
|
|
|
case 'version': |
301
|
|
|
$arguments[] = '--version'; |
302
|
|
|
break; |
303
|
|
|
|
304
|
|
|
default: |
305
|
|
|
throw new Exception("Unknown type $type"); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return $arguments; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|