indexer.php
8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
namespace OCA\Search_Lucene;
use \OC\Files\Filesystem;
use OCA\Search_Lucene\Document\Ods;
use OCA\Search_Lucene\Document\Odt;
use OCA\Search_Lucene\Document\Pdf;
use \OCP\Util;
/**
* @author Jörn Dreyer <jfd@butonic.de>
*/
class Indexer {
/**
* classname which used for hooks handling
* used as signalclass in OC_Hooks::emit()
*/
const CLASSNAME = 'Indexer';
/**
* index a file
*
* @author Jörn Dreyer <jfd@butonic.de>
*
* @param string $path the path of the file
*
* @return bool
*/
static public function indexFile($path = '', $user = null) {
if (!Filesystem::isValidPath($path)) {
return;
}
if ($path === '') {
//ignore the empty path element
return false;
}
if (is_null($user)) {
$view = Filesystem::getView();
$user = \OCP\User::getUser();
} else {
$view = new \OC\Files\View('/' . $user . '/files');
}
if ( ! $view ) {
Util::writeLog('search_lucene',
'could not resolve filesystem view',
Util::WARN);
return false;
}
if(!$view->file_exists($path)) {
Util::writeLog('search_lucene',
'file vanished, ignoring',
Util::DEBUG);
return true;
}
$root = $view->getRoot();
$pk = md5($root . $path);
// the cache already knows mime and other basic stuff
$data = $view->getFileInfo($path);
if (isset($data['mimetype'])) {
$mimeType = $data['mimetype'];
// initialize plain lucene document
$doc = new \Zend_Search_Lucene_Document();
// index content for local files only
$localFile = $view->getLocalFile($path);
if ( $localFile ) {
//try to use special lucene document types
if ('text/plain' === $mimeType) {
$body = $view->file_get_contents($path);
if ($body != '') {
$doc->addField(\Zend_Search_Lucene_Field::UnStored('body', $body));
}
} else if ('text/html' === $mimeType) {
//TODO could be indexed, even if not local
$doc = \Zend_Search_Lucene_Document_Html::loadHTML($view->file_get_contents($path));
} else if ('application/pdf' === $mimeType) {
$doc = Pdf::loadPdf($view->file_get_contents($path));
// commented the mimetype checks, as the zend classes only understand docx and not doc files.
// FIXME distinguish doc and docx, xls and xlsx, ppt and pptx, in oc core mimetype helper ...
//} else if ('application/msword' === $mimeType) {
} else if (strtolower(substr($data['name'], -5)) === '.docx') {
$doc = \Zend_Search_Lucene_Document_Docx::loadDocxFile($localFile);
//} else if ('application/msexcel' === $mimeType) {
} else if (strtolower(substr($data['name'], -5)) === '.xlsx') {
$doc = \Zend_Search_Lucene_Document_Xlsx::loadXlsxFile($localFile);
//} else if ('application/mspowerpoint' === $mimeType) {
} else if (strtolower(substr($data['name'], -5)) === '.pptx') {
$doc = \Zend_Search_Lucene_Document_Pptx::loadPptxFile($localFile);
} else if (strtolower(substr($data['name'], -4)) === '.odt') {
$doc = Odt::loadOdtFile($localFile);
} else if (strtolower(substr($data['name'], -4)) === '.ods') {
$doc = Ods::loadOdsFile($localFile);
}
}
// Store filecache id as unique id to lookup by when deleting
$doc->addField(\Zend_Search_Lucene_Field::Keyword('pk', $pk));
// Store filename
$doc->addField(\Zend_Search_Lucene_Field::Text('filename', $data['name'], 'UTF-8'));
// Store document path to identify it in the search results
$doc->addField(\Zend_Search_Lucene_Field::Text('path', $path, 'UTF-8'));
$doc->addField(\Zend_Search_Lucene_Field::unIndexed('size', $data['size']));
$doc->addField(\Zend_Search_Lucene_Field::unIndexed('mimetype', $mimeType));
//self::extractMetadata($doc, $path, $view, $mimeType);
Lucene::updateFile($doc, $path, $user);
return true;
} else {
Util::writeLog(
'search_lucene',
'need mimetype for content extraction',
Util::ERROR
);
return false;
}
}
/**
* extract the metadata from a file
*
* uses getid3 to extract metadata.
* if possible also adds content (currently only for plain text files)
* hint: use OC\Files\Filesystem::getFileInfo($path) to get metadata for the last param
*
* @author Jörn Dreyer <jfd@butonic.de>
*
* @param Zend_Search_Lucene_Document $doc to add the metadata to
* @param string $path path of the file to extract metadata from
* @param string $mimetype depending on the mimetype different extractions are performed
*
* @return void
*/
private static function extractMetadata(
\Zend_Search_Lucene_Document $doc,
$path,
\OC\Files\View $view,
$mimetype
) {
$file = $view->getLocalFile($path);
if (is_dir($file)) {
// Don't lose time analizing a directory for file-specific metadata
return;
}
$getID3 = new \getID3();
$getID3->encoding = 'UTF-8';
$data = $getID3->analyze($file);
// TODO index meta information from media files?
//show me what you got
/*foreach ($data as $key => $value) {
Util::writeLog('search_lucene',
'getid3 extracted '.$key.': '.$value,
Util::DEBUG);
if (is_array($value)) {
foreach ($value as $k => $v) {
Util::writeLog('search_lucene',
' ' . $value .'-' .$k.': '.$v,
Util::DEBUG);
}
}
}*/
if ('application/pdf' === $mimetype) {
try {
$zendpdf = \Zend_Pdf::parse($view->file_get_contents($path));
//we currently only display the filename, so we only index metadata here
if (isset($zendpdf->properties['Title'])) {
$doc->addField(\Zend_Search_Lucene_Field::UnStored('title', $zendpdf->properties['Title']));
}
if (isset($zendpdf->properties['Author'])) {
$doc->addField(\Zend_Search_Lucene_Field::UnStored('author', $zendpdf->properties['Author']));
}
if (isset($zendpdf->properties['Subject'])) {
$doc->addField(\Zend_Search_Lucene_Field::UnStored('subject', $zendpdf->properties['Subject']));
}
if (isset($zendpdf->properties['Keywords'])) {
$doc->addField(\Zend_Search_Lucene_Field::UnStored('keywords', $zendpdf->properties['Keywords']));
}
//TODO handle PDF 1.6 metadata Zend_Pdf::getMetadata()
//do the content extraction
$pdfParse = new \App_Search_Helper_PdfParser();
$body = $pdfParse->pdf2txt($zendpdf->render());
} catch (Exception $e) {
Util::writeLog('search_lucene',
$e->getMessage() . ' Trace:\n' . $e->getTraceAsString(),
Util::ERROR);
}
}
if ($body != '') {
$doc->addField(\Zend_Search_Lucene_Field::UnStored('body', $body));
}
if (isset($data['error'])) {
Util::writeLog(
'search_lucene',
'failed to extract meta information for ' . $view->getAbsolutePath($path) . ': ' . $data['error']['0'],
Util::WARN
);
return;
}
}
/**
* get the list of all unindexed files for the user
*
* @return array
*/
static public function getUnindexed() {
$files = array();
$absoluteRoot = Filesystem::getView()->getAbsolutePath('/');
$mounts = Filesystem::getMountPoints($absoluteRoot);
$mount = Filesystem::getMountPoint($absoluteRoot);
if (!in_array($mount, $mounts)) {
$mounts[] = $mount;
}
$query = \OC_DB::prepare('
SELECT `*PREFIX*filecache`.`fileid`
FROM `*PREFIX*filecache`
LEFT JOIN `*PREFIX*lucene_status`
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*lucene_status`.`fileid`
WHERE `storage` = ?
AND `status` IS NULL OR `status` = ?
');
foreach ($mounts as $mount) {
if (is_string($mount)) {
$storage = Filesystem::getStorage($mount);
} else if ($mount instanceof \OC\Files\Mount\Mount) {
$storage = $mount->getStorage();
} else {
$storage = null;
Util::writeLog('search_lucene',
'expected string or instance of \OC\Files\Mount\Mount got ' . json_encode($mount),
Util::DEBUG);
}
//only index local files for now
if ($storage instanceof \OC\Files\Storage\Local) {
$cache = $storage->getCache();
$numericId = $cache->getNumericStorageId();
$result = $query->execute(array($numericId, 'N'));
if (\OC_DB::isError($result)) {
Util::writeLog(
'search_lucene',
'failed to find unindexed files: '.\OC_DB::getErrorMessage($result),
Util::WARN
);
return false;
}
while ($row = $result->fetchRow()) {
$files[] = $row['fileid'];
}
}
}
return $files;
}
}