diff options
author | Phie <phie@phie.ovh> | 2019-04-09 03:13:45 +0300 |
---|---|---|
committer | Phie <phie@phie.ovh> | 2019-04-09 03:13:45 +0300 |
commit | 8081be9f755d6f356fe74f15469db763f0edd56d (patch) | |
tree | ceed107b56874c087ae22b820811ddab99d62022 /lib | |
parent | 0fd5ee3074a6938e9e87f8df24c9d9659cc3cbd4 (diff) |
cache builder
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Command/Cache.php | 62 | ||||
-rwxr-xr-x | lib/Controller/NoteController.php | 2 | ||||
-rw-r--r-- | lib/Misc/CacheManager.php | 61 | ||||
-rw-r--r-- | lib/Misc/NoteUtils.php | 3 |
4 files changed, 123 insertions, 5 deletions
diff --git a/lib/Command/Cache.php b/lib/Command/Cache.php new file mode 100644 index 0000000..dc5ee06 --- /dev/null +++ b/lib/Command/Cache.php @@ -0,0 +1,62 @@ +<?php + +namespace OCA\Carnet\Command; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +use OCP\Files\File; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCA\Carnet\Misc\CacheManager; +use OCP\IDBConnection; + +class Cache extends Command { + + + /** + * @param string $appName + * @param IRootFolder $rootFolder + */ + public function __construct($AppName, $RootFolder, $Config, IDBConnection $IDBConnection, $UserManager){ + parent::__construct(); + $this->appName = $AppName; + $this->Config = $Config; + $this->rootFolder = $RootFolder; + $this->db = $IDBConnection; + $this->userManager = $UserManager; + + } + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output) { + $this->output = $output; + $this->action = $input->getArgument('action'); + if($this->action === "rebuild"){ + $users = $this->userManager->search("",20000, 0); + $arrayId = array(); + echo count($users); + foreach($users as $user){ + array_push($arrayId,$user->getUID()); + } + $cache = new CacheManager($this->db, null); + $cache->buildCache($this->Config, $this->appName, $this->rootFolder, $arrayId); + } + } + protected function configure() { + $this->setName('carnet:cache') + ->setDescription('Cache') + ->addArgument( + 'action', + InputArgument::REQUIRED, + 'Action: rebuild, clear' + ); + } +} + +?>
\ No newline at end of file diff --git a/lib/Controller/NoteController.php b/lib/Controller/NoteController.php index 752f4e3..615422b 100755 --- a/lib/Controller/NoteController.php +++ b/lib/Controller/NoteController.php @@ -35,7 +35,7 @@ $folder = $this->Config->getUserValue($this->userId, $this->appName, "note_folder"); //$this->Config->setUserValue($this->userId, $this->appName, "note_folder", 'Documents/QuickNote'); if(empty($folder)) - $folder= 'Documents/QuickNote'; + $folder= NoteUtils::$defaultCarnetNotePath; try { $this->CarnetFolder = $RootFolder->getUserFolder($this->userId)->get($folder); } catch(\OCP\Files\NotFoundException $e) { diff --git a/lib/Misc/CacheManager.php b/lib/Misc/CacheManager.php index 398bbdb..0585872 100644 --- a/lib/Misc/CacheManager.php +++ b/lib/Misc/CacheManager.php @@ -1,6 +1,7 @@ <?php namespace OCA\Carnet\Misc; use OCP\IDBConnection; +use OCA\Carnet\Misc\NoteUtils; class CacheManager{ private $db; @@ -11,9 +12,13 @@ class CacheManager{ } public function addToCache($relativePath, $metadata, $lastmodfile){ + $this->addToCacheFullPath($this->carnetFolder->getFullPath($relativePath), $metadata, $lastmodfile); + } + + public function addToCacheFullPath($fullPath, $metadata, $lastmodfile){ $sql = 'INSERT into `*PREFIX*carnet_metadata` VALUES(?, ?, ?)'; $stmt = $this->db->prepare($sql); - $stmt->bindParam(1, $this->carnetFolder->getFullPath($relativePath), \PDO::PARAM_STR); + $stmt->bindParam(1, $fullPath, \PDO::PARAM_STR); if(!is_string($metadata)) $metadata = json_encode($metadata); $stmt->bindParam(2, $metadata, \PDO::PARAM_STR); @@ -25,7 +30,7 @@ class CacheManager{ $sql = 'UPDATE `*PREFIX*carnet_metadata` SET `metadata` = ?, `last_modification_file` = ? WHERE `path` = ?'; $stmt = $this->db->prepare($sql); $stmt->bindParam(1, $metadata, \PDO::PARAM_STR); - $stmt->bindParam(3, $this->carnetFolder->getFullPath($relativePath), \PDO::PARAM_STR); + $stmt->bindParam(3, $fullPath, \PDO::PARAM_STR); $stmt->bindParam(2, $lastmodfile, \PDO::PARAM_INT); $stmt->execute(); @@ -33,6 +38,58 @@ class CacheManager{ } + public function buildCache($config, $appName, $rootFolder, $users){ + $arrayFolder = array(); + $sql = 'SELECT path, last_modification_file FROM `*PREFIX*carnet_metadata`'; + $stmt = $this->db->prepare($sql); + $stmt->execute(); + $cache = array(); + $fetched = $stmt->fetchAll(); + foreach ($fetched as $row){ + $cache[$row['path']] = $row['last_modification_file']; + } + + $stmt->closeCursor(); + foreach($users as $user){ + $notePath = $config->getUserValue($user, $appName, "note_folder"); + if(empty($notePath)) + $notePath= NoteUtils::$defaultCarnetNotePath; + try { + echo $notePath."pet"; + $carnetFolder = $rootFolder->getUserFolder($user)->get($notePath); + $this->recursiveAddToCache($carnetFolder, $carnetFolder, $cache); + } catch(\OCP\Files\NotFoundException $e) { + + } + + } + } + + private function recursiveAddToCache($carnetFolder, $node, $cache){ + if($node instanceof \OCP\Files\Folder){ + foreach($node->get($path)->getDirectoryListing() as $inNode){ + echo $inNode->getPath(); + $this->recursiveAddToCache($carnetFolder, $inNode, $cache); + } + } else if(substr($node->getName(), -3) === "sqd"){ + $inf = $node->getFileInfo(); + if($cache[$node->getPath()] != null && $inf->getMtime() == $cache[$node->getPath()]){ + return; + } + $utils = new NoteUtils(); + try{ + $relativePath = substr($node->getPath(), strlen($carnetFolder->getPath())); + if(substr($relativePath, 0, 1) === "/") + $relativePath = substr($relativePath, 1); + $meta = $utils->getMetadata($carnetFolder, $relativePath); + $this->addToCacheFullPath($node->getPath(), $meta, $meta['lastmodfile']); + } catch(\PhpZip\Exception\ZipException $e){ + + } + } + + } + public function deleteFromCache($relativePath){ $sql = 'DELETE FROM `*PREFIX*carnet_metadata` WHERE `path` = ?'; $stmt = $this->db->prepare($sql); diff --git a/lib/Misc/NoteUtils.php b/lib/Misc/NoteUtils.php index 1fcac21..7f191bb 100644 --- a/lib/Misc/NoteUtils.php +++ b/lib/Misc/NoteUtils.php @@ -1,8 +1,7 @@ <?php namespace OCA\Carnet\Misc; - class NoteUtils{ - + public static $defaultCarnetNotePath = "Documents/QuickNote"; public function getMetadata($carnetFolder, $path){ $meta = array(); $tmppath = tempnam(sys_get_temp_dir(), uniqid().".zip"); |