documentProcessor.inc.php
10.8 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* $Id:$
*
* KnowledgeTree Community Edition
* Document Management Made Simple
* Copyright (C) 2008, 2009, 2010 KnowledgeTree Inc.
*
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
* California 94120-7775, or email info@knowledgetree.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* KnowledgeTree" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by KnowledgeTree" and retain the original
* copyright notice.
* Contributor( s): ______________________________________
*
*/
require_once(realpath(dirname(__FILE__) . '/../../config/dmsDefaults.php'));
function orderProcessors($a, $b)
{
if ($a->order == $b->order) {
return 0;
}
return ($a->order < $b->order) ? -1 : 1;
}
/**
* The processor runs all document processing tasks in the background.
* New tasks can be added using the plugin architecture and creating a trigger that the Document Processor picks up and calls.
*
*/
class DocumentProcessor
{
/**
* The indexer class
*/
private $indexer = false;
/**
* Document processors
*/
private $processors = false;
/**
* Number of documents per batch to be processed
*/
private $limit = 20;
/**
* Initialise the indexer and processors
*
*/
public function __construct()
{
global $default;
// Set the number of documents in a batch (config setting: indexer/batchDocuments)
$max = $default->batchDocuments;
$this->limit = (is_numeric($max)) ? $max : $this->limit;
// Load processors
$this->processors = $this->loadProcessors();
// Initialise the indexer
$this->indexer = Indexer::get();
}
/**
* Returns a reference to the main class
*
* @return DocumentProcessor
*/
public static function get()
{
static $singleton = null;
if (is_null($singleton))
{
$singleton = new DocumentProcessor();
}
return $singleton;
}
/**
* Load the processors that will get run on the documents, eg pdf generation
*
* @return array
*/
private function loadProcessors()
{
// Get list of registered processors (plugins)
$query = 'SELECT h.* FROM plugin_helper h
INNER JOIN plugins p ON (p.namespace = h.plugin)
WHERE p.disabled = 0 AND h.classtype = "processor"';
$results = DBUtil::getResultArray($query);
if(PEAR::isError($results)){
global $default;
$default->log->error('documentProcessor: error loading processors').' - '.$results->getMessage();
return false;
}
if(empty($results)){
return false;
}
$processors = array();
foreach ($results as $item){
$path = KTUtil::isAbsolutePath($item['pathname']) ? $item['pathname'] : KT_DIR . DIRECTORY_SEPARATOR . $item['pathname'];
require_once($path);
$processors[] = new $item['classname'];
}
usort($processors, 'orderProcessors');
return $processors;
}
/**
* Fetch the documents in the indexing queue and start the indexer
*
*/
public function processIndexQueue()
{
global $default;
if(!$default->enableIndexing){
$default->log->debug('documentProcessor: indexer disabled');
return ;
}
$default->log->debug('documentProcessor: starting indexer');
// Check for lock file to ensure processor is not currently running
$cacheDir = $default->cacheDirectory;
$lockFile = $cacheDir . DIRECTORY_SEPARATOR . 'document_processor.lock';
if(file_exists($lockFile)){
// If something causes the document processor to stop part way through processing, the lock
// file will remain stopping the document processor from resuming. To workaround this problem
// we check the creation date of the lockfile and remove it if it is older than 24 hours or
// 48 hours if the batch size is greater than 1000 documents.
$stat = stat($lockFile);
$created = $stat['mtime'];
$gap = 24;
if($this->limit > 1000){
$gap = 48;
$default->log->warn('documentProcessor: batch size of documents to index is set to '.$this->limit.', this could cause problems.');
}
$check = time() - ($gap*60*60);
if($check > $created){
$default->log->error('documentProcessor: lock file is older than '.$gap.' hours, deleting it to restart indexing - '.$lockFile);
@unlink($lockFile);
}else{
// lock file exists, exit
// through a warning if the lock file is older than half an hour
$small_gap = time() - (30*60);
if($small_gap > $created){
$default->log->warn('documentProcessor: stopping, lock file in place since '. date('Y-m-d H:i:s', $created) .' - '.$lockFile);
}
return ;
}
}
// Setup indexing - load extractors, run diagnostics
if($this->indexer->preIndexingSetup() === false){
$default->log->error('documentProcessor: stopping - indexer setup failed.');
return;
}
// Get document queue
$queue = $this->indexer->getDocumentsQueue($this->limit);
if(empty($queue)){
$default->log->debug('documentProcessor: stopping - no documents in indexing queue');
return ;
}
// indexing starting - create lock file
touch($lockFile);
// Process queue
foreach($queue as $item){
// Get the document object
$docId = $item['document_id'];
$document = Document::get($docId);
if (PEAR::isError($document))
{
Indexer::unqueueDocument($docId, sprintf(_kt("indexDocuments: Cannot resolve document id %d: %s."),$docId, $document->getMessage()), 'error');
continue;
}
// index document
$this->indexer->processDocument($document, $item);
}
// update the indexer statistics
$this->indexer->updateIndexStats();
// Remove lock file to indicate processing has completed
if(file_exists($lockFile)){
@unlink($lockFile);
}
$default->log->debug('documentProcessor: stopping indexer, batch completed');
}
/**
* Fetch the process queue for running the processors on
*
*/
public function processQueue()
{
global $default;
$default->log->debug('documentProcessor: starting processing');
if($this->processors === false){
$default->log->info('documentProcessor: stopping - no processors enabled');
return ;
}
// Get processing queue
// Use the same batch size as the indexer (for now)
// If the batch size is huge then reset it to a smaller number
// Open office leaks memory, so we don't want to do too many documents at once
$batch = ($this->limit > 500) ? 500 : $this->limit;
$queue = $this->indexer->getDocumentProcessingQueue($batch);
if(empty($queue)){
$default->log->debug('documentProcessor: stopping - no documents in processing queue');
return ;
}
// Process queue
foreach($queue as $item){
// Get the document object
$docId = $item['document_id'];
$document = Document::get($docId);
if (PEAR::isError($document))
{
Indexer::unqueueDocFromProcessing($docId, "Cannot resolve document id: {$document->getMessage()}", 'error');
continue;
}
// loop through processors
if($this->processors !== false){
foreach($this->processors as $processor){
$default->log->debug('documentProcessor: running processor: '.$processor->getNamespace());
// Check document mime type against supported types
if(!$this->isSupportedMimeType($item['mimetypes'], $processor->getSupportedMimeTypes())){
$default->log->debug('documentProcessor: not a supported mimetype: '.$item['mimetypes']);
continue;
}
// Process document
$processor->setDocument($document);
$processor->processDocument();
}
Indexer::unqueueDocFromProcessing($docId, "Document processed", 'debug');
}
}
$default->log->debug('documentProcessor: stopping processing, batch completed');
}
/**
* Determines whether the document is a supported mime type
*
* @param string $mimeType
* @param array $processorTypes
* @return boolean
*/
private function isSupportedMimeType($mimeType, $processorTypes){
// Check $processorTypes is an array
if(is_array($processorTypes)){
if(!in_array($mimeType, $processorTypes)){
return false;
}
return true;
}
// True if it supports all types, false if it supports none.
return $processorTypes;
}
}
abstract class BaseProcessor
{
public $order;
protected $document;
protected $namespace;
public function BaseProcessor()
{
// Constructor
}
/**
* Returns the namespace of the processor
*
* @return string
*/
public function getNamespace()
{
return $this->namespace;
}
/**
* Set the document object
*
* @param unknown_type $document
*/
public function setDocument($document)
{
$this->document = $document;
}
abstract public function processDocument();
abstract public function getSupportedMimeTypes();
}
?>