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('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō', 'Ŏ', 'ŏ', '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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' ); } } ?>