From ae581b9425c1df1f754e800f408f1e09f970067f Mon Sep 17 00:00:00 2001 From: Phie Date: Sun, 29 Mar 2020 18:39:26 +0200 Subject: moving Search out of commands --- lib/Command/Search.php | 239 -------------------------------------- lib/Controller/NoteController.php | 2 +- lib/Misc/Search.php | 196 +++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+), 240 deletions(-) delete mode 100644 lib/Command/Search.php create mode 100644 lib/Misc/Search.php (limited to 'lib') diff --git a/lib/Command/Search.php b/lib/Command/Search.php deleted file mode 100644 index 245ca49..0000000 --- a/lib/Command/Search.php +++ /dev/null @@ -1,239 +0,0 @@ -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) { - $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', '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'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o', 'Α', 'α', 'Ε', 'ε', 'Ο', 'ο', 'Ω', 'ω', 'Ι', 'ι', 'ι', 'ι', 'Υ', 'υ', 'υ', 'υ', 'Η', 'η'); - 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 -* @return int -*/ -protected function execute(InputInterface $input, OutputInterface $output) { - $this->output = $output; - $this->userId = $input->getArgument('user_id'); - try { - $this->getCacheFolder()->get("carnet_search")->delete(); - } catch(\OCP\Files\NotFoundException $e) { - - } - $this->searchCache = $this->getCacheFolder()->newFile("carnet_search"); - $this->searchCache->putContent("[]"); - - $output->writeln('starting '.$this->appName.' user '.$input->getArgument('user_id')); - - $output->writeln('searching '.$input->getArgument('query')." in ".$this->CarnetFolder->getFullPath($input->getArgument('root'))); - $path = $input->getArgument('root'); - if (!empty($path) && substr($path, -1) !== '/' || $path !== ".") - $path = substr($path, -1); - - $query = $input->getArgument('query'); - $query = $this->removeAccents($query); - $query = strtolower($query); - - echo "searching ".$query; - $this->data = array(); - $this->pathArray = array(); - $this->searchInCache($query); - $this->search($path, $this->CarnetFolder->get($path), $query,0); - //$data = json_decode( $this->searchCache->getContent()); - array_push($this->data, "end_of_search"); - $this->searchCache->putContent(json_encode($this->data)); -} - -private function searchInCache($query){ - $cache = new CacheManager($this->db, $this->CarnetFolder); - $metadataFromCache = $cache->search($query); - if($this->searchCache){ - foreach($metadataFromCache as $path => $mTime){ - $this->output->writeln('found in '.$path); - - $file = array(); - $file['name'] = "none"; - $file['path'] = $path; - $file['isDir'] = false; - $file['mtime'] = $mTime; - - array_push($this->data, $file); - array_push($this->pathArray, $path); - - } - // $this->searchCache->putContent(json_encode($this->data)); - - } -} - -private function getCacheFolder(){ - try { - return $this->rootFolder->getUserFolder($this->userId)->get(".carnet/cache/".$this->userId); - } catch(\OCP\Files\NotFoundException $e) { - $folder = $this->rootFolder->getUserFolder($this->userId)->newFolder(".carnet/cache/".$this->userId, 0777, true); - return $folder; - } -} - -private function writeFound($relativePath, $in){ - $inf = $in->getFileInfo(); - - $file = array(); - $file['name'] = $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)); - -} -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 - $endWell = $this->search(($relativePath!==""?$relativePath."/":"").$in->getName()."/", $in, $query, $curDepth+1); - } - else if($this->current > $this->from){ - if(in_array($relativePath.$in->getName(), $this->pathArray)){ - continue; - } - if(strstr(strtolower($this->removeAccents($in->getName())), $query)){ - $this->writeFound($relativePath, $in); - continue; - } - - try { - $zipFile = new \PhpZip\ZipFile(); - $zipFile->openFromStream($in->fopen("r")); - try { - $metadata = json_decode($zipFile->getEntryContents("metadata.json")); - $hasFound = false; - foreach($metadata->keywords as $keyword){ - if(strstr($this->removeAccents(strtolower($keyword)), $query)){ - $this->writeFound($relativePath,$in); - $hasFound = true; - break; - } - } - if($hasFound){ - continue; - } - - } catch(Exception $e){ - } - $index = $zipFile->getEntryContents("index.html"); - if(strstr(strtolower($this->removeAccents($index)), $query)){ - $this->writeFound($relativePath,$in); - } - } catch(\OCP\Files\NotFoundException $e) { - } catch(\PhpZip\Exception\ZipException $e){ - } - } - if(time() - $this->startTime>=2){ - return false; - } - } - return $endWell; -} - -protected function configure() { -$this->setName('carnet:search') -->setDescription('Search') -->addArgument( - 'user_id', - InputArgument::REQUIRED, - 'Search with specified user' -) -->addArgument( - 'query', - InputArgument::REQUIRED, - 'Search query' -) -->addArgument( - 'root', - InputArgument::OPTIONAL, - 'starting in path' -) -->addArgument( - 'search_id', - InputArgument::OPTIONAL, - 'search id' -); - -} - - -} -?> \ No newline at end of file diff --git a/lib/Controller/NoteController.php b/lib/Controller/NoteController.php index 6c131f9..7a6ed5e 100755 --- a/lib/Controller/NoteController.php +++ b/lib/Controller/NoteController.php @@ -11,7 +11,7 @@ use OCA\Carnet\Misc\NoteUtils; use OCA\Carnet\Misc\CacheManager; use OCP\IDBConnection; - use OCA\Carnet\Command\Search; + use OCA\Carnet\Misc\Search; //require_once 'vendor/autoload.php'; class MyZipFile extends \PhpZip\ZipFile { diff --git a/lib/Misc/Search.php b/lib/Misc/Search.php new file mode 100644 index 0000000..e6d272d --- /dev/null +++ b/lib/Misc/Search.php @@ -0,0 +1,196 @@ +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) { + $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ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', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o', 'Α', 'α', 'Ε', 'ε', 'Ο', 'ο', 'Ω', 'ω', 'Ι', 'ι', 'ι', 'ι', 'Υ', 'υ', 'υ', 'υ', 'Η', 'η'); + 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; +} + +private function searchInCache($query){ + $cache = new CacheManager($this->db, $this->CarnetFolder); + $metadataFromCache = $cache->search($query); + if($this->searchCache){ + foreach($metadataFromCache as $path => $mTime){ + $this->output->writeln('found in '.$path); + + $file = array(); + $file['name'] = "none"; + $file['path'] = $path; + $file['isDir'] = false; + $file['mtime'] = $mTime; + + array_push($this->data, $file); + array_push($this->pathArray, $path); + + } + // $this->searchCache->putContent(json_encode($this->data)); + + } +} + +private function getCacheFolder(){ + try { + return $this->rootFolder->getUserFolder($this->userId)->get(".carnet/cache/".$this->userId); + } catch(\OCP\Files\NotFoundException $e) { + $folder = $this->rootFolder->getUserFolder($this->userId)->newFolder(".carnet/cache/".$this->userId, 0777, true); + return $folder; + } +} + +private function writeFound($relativePath, $in){ + $inf = $in->getFileInfo(); + + $file = array(); + $file['name'] = $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)); + +} +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 + $endWell = $this->search(($relativePath!==""?$relativePath."/":"").$in->getName()."/", $in, $query, $curDepth+1); + } + else if($this->current > $this->from){ + if(in_array($relativePath.$in->getName(), $this->pathArray)){ + continue; + } + if(strstr(strtolower($this->removeAccents($in->getName())), $query)){ + $this->writeFound($relativePath, $in); + continue; + } + + try { + $zipFile = new \PhpZip\ZipFile(); + $zipFile->openFromStream($in->fopen("r")); + try { + $metadata = json_decode($zipFile->getEntryContents("metadata.json")); + $hasFound = false; + foreach($metadata->keywords as $keyword){ + if(strstr($this->removeAccents(strtolower($keyword)), $query)){ + $this->writeFound($relativePath,$in); + $hasFound = true; + break; + } + } + if($hasFound){ + continue; + } + + } catch(Exception $e){ + } + $index = $zipFile->getEntryContents("index.html"); + if(strstr(strtolower($this->removeAccents($index)), $query)){ + $this->writeFound($relativePath,$in); + } + } catch(\OCP\Files\NotFoundException $e) { + } catch(\PhpZip\Exception\ZipException $e){ + } + } + if(time() - $this->startTime>=2){ + return false; + } + } + return $endWell; +} + +protected function configure() { +$this->setName('carnet:search') +->setDescription('Search') +->addArgument( + 'user_id', + InputArgument::REQUIRED, + 'Search with specified user' +) +->addArgument( + 'query', + InputArgument::REQUIRED, + 'Search query' +) +->addArgument( + 'root', + InputArgument::OPTIONAL, + 'starting in path' +) +->addArgument( + 'search_id', + InputArgument::OPTIONAL, + 'search id' +); + +} + + +} +?> \ No newline at end of file -- cgit v1.2.3