From 9707df553c4e8743a2d7a087d59bdca9216ca3aa Mon Sep 17 00:00:00 2001 From: Phie Date: Sun, 29 Mar 2020 18:23:17 +0200 Subject: new search method --- lib/Command/Search.php | 75 +++++++++++++++++++++++++++------------ lib/Controller/NoteController.php | 13 +++---- 2 files changed, 58 insertions(+), 30 deletions(-) (limited to 'lib') diff --git a/lib/Command/Search.php b/lib/Command/Search.php index 7119858..245ca49 100644 --- a/lib/Command/Search.php +++ b/lib/Command/Search.php @@ -20,16 +20,29 @@ class Search extends Command { private $Config; private $rootFolder; private $searchCache; + private $current=0; + private $from; + /** * @param string $appName * @param IRootFolder $rootFolder */ -public function __construct($AppName, $RootFolder, $Config, IDBConnection $IDBConnection){ +public function __construct($AppName, $RootFolder, $Config, IDBConnection $IDBConnection, $userId){ parent::__construct(); $this->appName = $AppName; $this->Config = $Config; $this->db = $IDBConnection; $this->rootFolder = $RootFolder; + $this->userId = $userId; + $folder = $this->Config->getUserValue($this->userId , $this->appName, "note_folder"); + + if(empty($folder)) + $folder= 'Documents/QuickNote'; + try { + $this->CarnetFolder = $this->rootFolder->getUserFolder($this->userId)->get($folder); + } catch(\OCP\Files\NotFoundException $e) { + $this->CarnetFolder = $this->rootFolder->getUserFolder($this->userId)->newFolder($folder); + } } protected function removeAccents($str) { @@ -38,6 +51,29 @@ protected function removeAccents($str) { return str_replace($a, $b, $str); } + +public function startSearch($query, $from) { + $query = $this->removeAccents($query); + $query = strtolower($query); + $this->data = array(); + $this->startTime = time(); + $this->from=$from; + $searchInPath = true; + if($from==0){ + $this->searchInCache($query); + if(sizeof($this->data)>0) + $searchInPath = false; + } + $this->current = 1; + $result = array(); + $result['end'] = false; + if($searchInPath) + $result['end'] = $this->search("", $this->CarnetFolder, $query,0); + $result['next'] = $this->current; + $result['files'] = $this->data; + return $result; +} + /** * @param InputInterface $input * @param OutputInterface $output @@ -53,15 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { } $this->searchCache = $this->getCacheFolder()->newFile("carnet_search"); $this->searchCache->putContent("[]"); - $folder = $this->Config->getUserValue($this->userId , $this->appName, "note_folder"); - if(empty($folder)) - $folder= 'Documents/QuickNote'; - try { - $this->CarnetFolder = $this->rootFolder->getUserFolder($this->userId)->get($folder); - } catch(\OCP\Files\NotFoundException $e) { - $this->CarnetFolder = $this->rootFolder->getUserFolder($this->userId)->newFolder($folder); - } $output->writeln('starting '.$this->appName.' user '.$input->getArgument('user_id')); $output->writeln('searching '.$input->getArgument('query')." in ".$this->CarnetFolder->getFullPath($input->getArgument('root'))); @@ -100,7 +128,7 @@ private function searchInCache($query){ array_push($this->pathArray, $path); } - $this->searchCache->putContent(json_encode($this->data)); + // $this->searchCache->putContent(json_encode($this->data)); } } @@ -115,31 +143,30 @@ private function getCacheFolder(){ } private function writeFound($relativePath, $in){ - $this->output->writeln('found in '.$in->getPath()); - if($this->searchCache){ $inf = $in->getFileInfo(); + $file = array(); $file['name'] = $inf->getName(); - $file['path'] = $relativePath."/".$inf->getName(); + $file['path'] = $relativePath.$inf->getName(); $file['isDir'] = $inf->getType() === "dir"; $file['mtime'] = $inf->getMtime(); array_push($this->data, $file); - $this->searchCache->putContent(json_encode($this->data)); - } + //$this->searchCache->putContent(json_encode($this->data)); + } private function search($relativePath, $folder, $query, $curDepth){ $array = array(); - + $endWell = true; foreach($folder->getDirectoryListing() as $in){ + $this->current = $this->current+1; //$this->output->writeln('in '.$in->getPath()); if($in->getFileInfo()->getType() === "dir"){ if($curDepth<30) //might be a problem in nc db - $this->search(($relativePath!==""?relativePath."/":"").$in->getName(), $in, $query, $curDepth+1); - + $endWell = $this->search(($relativePath!==""?$relativePath."/":"").$in->getName()."/", $in, $query, $curDepth+1); } - else{ - if(in_array($relativePath."/".$in->getName(), $this->pathArray)){ + else if($this->current > $this->from){ + if(in_array($relativePath.$in->getName(), $this->pathArray)){ continue; } if(strstr(strtolower($this->removeAccents($in->getName())), $query)){ @@ -160,8 +187,9 @@ private function search($relativePath, $folder, $query, $curDepth){ break; } } - if($hasFound) + if($hasFound){ continue; + } } catch(Exception $e){ } @@ -173,8 +201,11 @@ private function search($relativePath, $folder, $query, $curDepth){ } catch(\PhpZip\Exception\ZipException $e){ } } + if(time() - $this->startTime>=2){ + return false; + } } - return $array; + return $endWell; } protected function configure() { diff --git a/lib/Controller/NoteController.php b/lib/Controller/NoteController.php index fdfc62f..6c131f9 100755 --- a/lib/Controller/NoteController.php +++ b/lib/Controller/NoteController.php @@ -11,6 +11,7 @@ use OCA\Carnet\Misc\NoteUtils; use OCA\Carnet\Misc\CacheManager; use OCP\IDBConnection; + use OCA\Carnet\Command\Search; //require_once 'vendor/autoload.php'; class MyZipFile extends \PhpZip\ZipFile { @@ -619,14 +620,10 @@ public function getOpusEncoder(){ * @NoAdminRequired * @NoCSRFRequired */ - public function search($from, $query){ - try { - $this->getCacheFolder()->get("carnet_search")->delete(); - } catch(\OCP\Files\NotFoundException $e) { - - } - shell_exec('php occ carnet:search '.escapeshellarg($this->userId).' '.escapeshellarg($query).' '.escapeshellarg($from).'> /dev/null 2>/dev/null &'); - } + public function search($from, $query, $path){ + $SearchEngine = new Search($this->appName, $this->rootFolder, $this->Config, $this->db, $this->userId); + return $SearchEngine->startSearch($query, $from); + } /** * @NoAdminRequired -- cgit v1.2.3