indexer.php
6.94 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
<?php
namespace OCA\Search_Lucene;
use \OC\Files\Filesystem;
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;
}
$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'];
if ('text/html' === $mimetype) {
$doc = \Zend_Search_Lucene_Document_Html::loadHTML($view->file_get_contents($path));
} else if ('application/msword' === $mimetype) {
// FIXME uses ZipArchive ... make compatible with OC\Files\Filesystem
//$doc = Zend_Search_Lucene_Document_Docx::loadDocxFile(OC\Files\Filesystem::file_get_contents($path));
//no special treatment yet
$doc = new \Zend_Search_Lucene_Document();
} else {
$doc = new \Zend_Search_Lucene_Document();
}
// store fscacheid as unique id to lookup by when deleting
$doc->addField(\Zend_Search_Lucene_Field::Keyword('pk', $pk));
// Store document URL to identify it in the search results
$doc->addField(\Zend_Search_Lucene_Field::Text('path', $path));
$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);
$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);
}
}
}*/
// filename _should_ always work, so log if it does not
if (isset($data['filename'])) {
$doc->addField(\Zend_Search_Lucene_Field::Text('filename', $data['filename']));
} else {
Util::writeLog('search_lucene',
'failed to extract meta information for ' . $view->getAbsolutePath($path) . ': ' . $data['error']['0'],
Util::WARN);
}
//content
Util::writeLog('search_lucene',
'indexer extracting content for ' . $view->getAbsolutePath($path) . ' (' . $mimetype . ')',
Util::DEBUG);
$body = '';
if ('text/plain' === $mimetype) {
$body = $view->file_get_contents($path);
} else if ('text/html' === $mimetype) {
// getid3 does not handle html, but that's not a real error
unset($data['error']);
} else if ('httpd/unix-directory' === $mimetype) {
// getid3 does not handle directories, ignore the error
unset($data['error']);
} else 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 = \OC\Files\Mount::findIn($absoluteRoot);
$mount = \OC\Files\Mount::find($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` = "N"');
foreach ($mounts as $mount) {
$storage = $mount->getStorage();
//only index local files for now
if ($storage instanceof \OC\Files\Storage\Local) {
$cache = $storage->getCache();
$numericId = $cache->getNumericStorageId();
$result = $query->execute(array($numericId));
if (!$result) {
return false;
}
while ($row = $result->fetchRow()) {
$files[] = $row['fileid'];
}
}
}
return $files;
}
}