Blame view
sources/apps/search_lucene/lib/hooks.php
3.08 KB
|
42e4f8d60
|
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 |
<?php
namespace OCA\Search_Lucene;
use \OCP\BackgroundJob;
use \OCP\Util;
/**
*
* @author Jörn Dreyer <jfd@butonic.de>
*/
class Hooks {
/**
* classname which used for hooks handling
* used as signalclass in OC_Hooks::emit()
*/
const CLASSNAME = 'Hooks';
/**
* handle for indexing file
*
* @param string $path
*/
const handle_post_write = 'indexFile';
/**
* handle for renaming file
*
* @param string $path
*/
const handle_post_rename = 'renameFile';
/**
* handle for removing file
*
* @param string $path
*/
const handle_delete = 'deleteFile';
/**
* handle file writes (triggers reindexing)
*
* the file indexing is queued as a background job
*
* @author Jörn Dreyer <jfd@butonic.de>
*
* @param $param array from postWriteFile-Hook
*/
public static function indexFile(array $param) {
if (isset($param['path'])) {
$param['user'] = \OCP\User::getUser();
//Add Background Job:
BackgroundJob::addQueuedTask(
'search_lucene',
'OCA\Search_Lucene\Hooks',
'doIndexFile',
json_encode($param) );
} else {
Util::writeLog('search_lucene',
'missing path parameter',
Util::WARN);
}
}
static public function doIndexFile($param) {
$data = json_decode($param);
if ( ! isset($data->path) ) {
Util::writeLog('search_lucene',
'missing path parameter',
Util::WARN);
return false;
}
if ( ! isset($data->user) ) {
Util::writeLog('search_lucene',
'missing user parameter',
Util::WARN);
return false;
}
Indexer::indexFile($data->path, $data->user);
}
/**
* handle file renames (triggers indexing and deletion)
*
* @author Jörn Dreyer <jfd@butonic.de>
*
* @param $param array from postRenameFile-Hook
*/
public static function renameFile(array $param) {
if (isset($param['newpath'])) {
self::indexFile(array('path'=>$param['newpath']));
}
if (isset($param['oldpath'])) {
self::deleteFile(array('path'=>$param['oldpath']));
}
}
/**
* remove a file from the lucene index when deleting a file
*
* file deletion from the index is queued as a background job
*
* @author Jörn Dreyer <jfd@butonic.de>
*
* @param $param array from postDeleteFile-Hook
*/
static public function deleteFile(array $param) {
// we cannot use post_delete as $param would not contain the id
// of the deleted file and we could not fetch it with getId
if (isset($param['path'])) {
$param['user'] = \OCP\User::getUser();
//Add Background Job:
BackgroundJob::addQueuedTask(
'search_lucene',
'OCA\Search_Lucene\Hooks',
'doDeleteFile',
json_encode($param) );
} else {
Util::writeLog('search_lucene',
'missing path parameter',
Util::WARN);
}
}
static public function doDeleteFile($param) {
$data = json_decode($param);
if ( ! isset($data->path) ) {
Util::writeLog('search_lucene',
'missing path parameter',
Util::WARN);
return false;
}
if ( ! isset($data->user) ) {
Util::writeLog('search_lucene',
'missing user parameter',
Util::WARN);
return false;
}
Lucene::deleteFile($data->path, $data->user);
}
}
|