diff options
author | Maxence Lange <maxence@pontapreta.net> | 2016-08-31 20:08:54 +0300 |
---|---|---|
committer | Maxence Lange <maxence@pontapreta.net> | 2016-08-31 20:08:54 +0300 |
commit | 63b6061cdfdb6a19295414d9bd7cad82739d6ee7 (patch) | |
tree | 44cb88c385f7cd2ef28a4b892c897f079d29a536 | |
parent | f80c354c23531022d60b4b344f3f3f2ac3e6f42f (diff) |
delete/restore -> remove documents
- Documents are removed from Solr Server when deleted from the Cloud.
- deleting directories remove all its files/documents from the Solr
Server
- restoring directories re-extract all its files/documents.
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | lib/AppInfo/Application.php | 4 | ||||
-rw-r--r-- | lib/Events/FilesEvents.php | 17 | ||||
-rw-r--r-- | lib/Provider/SearchProvider.php | 1 | ||||
-rw-r--r-- | lib/Service/FileService.php | 54 | ||||
-rw-r--r-- | lib/Service/SolrService.php | 14 |
6 files changed, 75 insertions, 16 deletions
@@ -15,7 +15,6 @@ What it does right now and should not: - display results behind an element so it can't be clicked. What it should do in the future: -- remove documents from the Solr Server when they are deleted from the cloud. - documents should be kept on the Solr Server while the original file is still in the trashbin. - testing your setup from the administration page - extract files with an ./occ command diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 3feed3f..9043768 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -65,7 +65,7 @@ class Application extends App }); $container->registerService('FileService', function ($c) { - return new FileService($c->query('Root')); + return new FileService($c->query('Root'), $c->query('SolrService'), $c->query('MiscService')); }); $container->registerService('SolrService', function ($c) { @@ -132,7 +132,7 @@ class Application extends App Util::connectHook('OC_Filesystem', 'post_create', '\OCA\Nextant\Hooks\FilesHooks', 'fileCreated'); Util::connectHook('OC_Filesystem', 'post_update', '\OCA\Nextant\Hooks\FilesHooks', 'fileUpdated'); // Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Nextant\Hooks\FilesHooks', 'fileRenamed'); - Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Nextant\Hooks\FilesHooks', 'fileDeleted'); + Util::connectHook('OC_Filesystem', 'delete', '\OCA\Nextant\Hooks\FilesHooks', 'fileDeleted'); Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', '\OCA\Nextant\Hooks\FilesHooks', 'fileRestored'); // Util::connectHook('OCP\Share', 'post_shared', '\OCA\Nextant\Hooks\FilesHooks', 'fileShared'); // Util::connectHook('OCP\Share', 'post_unshare', '\OCA\Nextant\Hooks\FilesHooks', 'fileUnshared'); diff --git a/lib/Events/FilesEvents.php b/lib/Events/FilesEvents.php index 8d2589c..19ef32a 100644 --- a/lib/Events/FilesEvents.php +++ b/lib/Events/FilesEvents.php @@ -55,6 +55,8 @@ class FilesEvents */ public function onFileCreate($path) { +// $this->fileService->createFile($path); + $absolutePath = $this->fileService->getRoot() . FileService::getAbsolutePath($path); $fileInfos = FileService::getFileInfo($path); $this->miscService->log('>> ' . $fileInfos->getMimeType()); @@ -69,8 +71,8 @@ class FilesEvents */ public function onFileUpdate($path) { - $absolutePath = $this->fileService->getRoot() . FileService::getAbsolutePath($path); - $fileInfos = FileService::getFileInfo($path); + $absolutePath = $this->fileService->getRoot() . FileService::getAbsolutePath($path); + $fileInfos = FileService::getFileInfo($path); $solrResult = $this->solrService->extractFile($absolutePath, FileService::getId($path), $fileInfos->getMimeType()); } @@ -78,11 +80,11 @@ class FilesEvents /** * onFileRename() * - * @param string $path + * @param string $source + * @param string $target */ public function onFileRename($source, $target) - { - } + {} /** * onFileDelete() @@ -91,8 +93,7 @@ class FilesEvents */ public function onFileDelete($path) { - // $path incorrect - $this->miscService->log('A file has been deleted: ' . FileService::getId($path), 2); + $this->fileService->deleteFiles($path); } /** @@ -102,7 +103,7 @@ class FilesEvents */ public function onFileRestore($path) { - $this->miscService->log('A file has been restored: ' . FileService::getId($path), 2); + $this->fileService->restoreFiles($path); } /** diff --git a/lib/Provider/SearchProvider.php b/lib/Provider/SearchProvider.php index 0dc2e2c..5fea360 100644 --- a/lib/Provider/SearchProvider.php +++ b/lib/Provider/SearchProvider.php @@ -71,6 +71,7 @@ class SearchProvider extends \OCP\Search\Provider if (sizeof($solrResult) > 0) $topScore = $solrResult[0]['score']; + foreach ($solrResult as $data) { // This is not clean, but right now it is the only descent way I found to display result diff --git a/lib/Service/FileService.php b/lib/Service/FileService.php index 4f180ff..b3e1361 100644 --- a/lib/Service/FileService.php +++ b/lib/Service/FileService.php @@ -28,15 +28,26 @@ namespace OCA\Nextant\Service; use OC\Files\Filesystem; use OC\Files\View; +use OCP\Files\NotFoundException; class FileService { private $root; - public function __construct($root) + private $solrService; + + private $miscService; + + private $view; + + public function __construct($root, $solrService, $miscService) { $this->root = $root; + $this->solrService = $solrService; + $this->miscService = $miscService; + + $this->view = Filesystem::getView(); } public function getRoot() @@ -44,6 +55,41 @@ class FileService return $this->root; } + /** + * delete file or files if directory + * + * @param string $path + */ + public function deleteFiles($path) + { + $fileInfos = $this->view->getFileInfo($path); + if ($fileInfos->getMimeType() == 'httpd/unix-directory') { + $files = $this->view->getDirectoryContent($path); + foreach ($files as $file) + $this->deleteFiles($this->view->getPath($file->getId())); + } else { + $solrResult = $this->solrService->removeDocument($fileInfos->getId()); + } + } + + /** + * restore file or files if directory + * + * @param string $path + */ + public function restoreFiles($path) + { + $fileInfos = $this->view->getFileInfo($path); + if ($fileInfos->getMimeType() == 'httpd/unix-directory') { + $files = $this->view->getDirectoryContent($path); + foreach ($files as $file) + $this->restoreFiles($this->view->getPath($file->getId())); + } else { + $absolutePath = $this->getRoot() . $this->view->getAbsolutePath($path); + $solrResult = $this->solrService->extractFile($absolutePath, $fileInfos->getId(), $fileInfos->getMimeType()); + } + } + public static function getId($path) { $fileId = 0; @@ -62,7 +108,7 @@ class FileService else return $view->getPath($id); } catch (NotFoundException $e) { - throw new NotFoundException('File with id ' . $id . ' not found'); + return false; } } @@ -74,10 +120,10 @@ class FileService $path = $view->getPath($pathorid); else $path = $pathorid; - + return $view->getFileInfo($path); } catch (NotFoundException $e) { - throw new NotFoundException('File with id ' . $pathorid . ' not found'); + return false; } } diff --git a/lib/Service/SolrService.php b/lib/Service/SolrService.php index 5c38c54..b257492 100644 --- a/lib/Service/SolrService.php +++ b/lib/Service/SolrService.php @@ -68,7 +68,7 @@ class SolrService /** * extract a simple text file. * - * @param string $path + * @param string $path * @param int $docid */ public function extractSimpleTextFile($path, $docid) @@ -94,6 +94,18 @@ class SolrService return $client->extract($query); } + public function removeDocument($docid) + { + $client = $this->solariumClient; + $update = $client->createUpdate(); + + $update->addDeleteById($docid); + $update->addCommit(); + + // this executes the query and returns the result + return $client->update($update); + } + public function ping() { // We should ping and test the new setup |