Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CacheManager.php « Misc « lib - github.com/CarnetApp/CarnetNextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a619e0ca97afe7d83983cbcc0f1f9c2a27f2f818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace OCA\Carnet\Misc;
use OCP\IDBConnection;
use OCA\Carnet\Misc\NoteUtils;

class CacheManager{
    private $db;
    private $carnetFolder;
    public function __construct(IDBConnection $db, $carnetFolder) {
        $this->db = $db;
        $this->carnetFolder = $carnetFolder;
    }

    public function addToCache($relativePath, $metadata, $lastmodfile, $text){
        $this->addToCacheFullPath($this->carnetFolder->getFullPath($relativePath), $metadata, $lastmodfile, $text);
    }

    public function addToCacheFullPath($fullPath, $metadata, $lastmodfile, $text){
        if(isset($metadata['text']))
            unset($metadata['text']);
        $low_case_text = strtolower(NoteUtils::removeAccents($text));
        $sql = 'INSERT into `*PREFIX*carnet_metadata` VALUES(?, ?, ?, ?)';
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(1, $fullPath, \PDO::PARAM_STR);
        if(!is_string($metadata))
            $metadata = json_encode($metadata);
        $stmt->bindParam(2, $metadata, \PDO::PARAM_STR);
        $stmt->bindParam(3, $lastmodfile, \PDO::PARAM_INT);
        $stmt->bindParam(4, $low_case_text, \PDO::PARAM_STR);

        try{
            $stmt->execute();
        }
        catch(\Doctrine\DBAL\Exception\UniqueConstraintViolationException $ex){
            $sql = 'UPDATE `*PREFIX*carnet_metadata` SET `metadata` = ?, `last_modification_file` = ?, `low_case_text` = ? WHERE `path` = ?';
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(1, $metadata, \PDO::PARAM_STR);
            $stmt->bindParam(2, $lastmodfile, \PDO::PARAM_INT);
            $stmt->bindParam(3, $low_case_text, \PDO::PARAM_STR);
            $stmt->bindParam(4, $fullPath, \PDO::PARAM_STR);

            $stmt->execute();
        }

    }

    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->getDirectoryListing() as $inNode){
                echo $inNode->getPath()."\n";
                $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);
                $text = $meta['text'];
                $this->addToCacheFullPath($node->getPath(), $meta, $meta['lastmodfile'], $text);
            } catch(\PhpZip\Exception\ZipException $e){

            } catch(\OCP\Encryption\Exceptions\GenericEncryptionException $e){
                
            }
        }

    }

    public function deleteFromCache($relativePath){
        $sql = 'DELETE FROM `*PREFIX*carnet_metadata` WHERE `path` = ?';
        $stmt = $this->db->prepare($sql);
        $param = $this->carnetFolder->getFullPath($relativePath);
        $stmt->bindParam(1, $param, \PDO::PARAM_STR);
        $stmt->execute();

    }
    public function getFromCache($arrayRelativePath){
        return $this->getFromCache2($arrayRelativePath, false);
    }

    public function getFromCache2($arrayRelativePath, $withLowCaseText){
        $total = 0;
        $array = array();

        while($total < sizeof($arrayRelativePath)){
            $arrayFullPath = array();
            $sql = 'SELECT * FROM `*PREFIX*carnet_metadata` ' . 
            'WHERE ';
            for($i = $total; $i < sizeof($arrayRelativePath); $i++){
                $sql .= "`path` = ? ";
                
                array_push($arrayFullPath, $this->carnetFolder->getFullPath($arrayRelativePath[$i]));
                if($i < sizeof($arrayRelativePath)-1 && $i - $total < 100)
                    $sql .= "OR ";
                if($i - $total == 100)
                    break;
            }
            $total = $total + 100;
        
            $stmt = $this->db->prepare($sql);
            
            $stmt->execute($arrayFullPath);
            $fetched = $stmt->fetchAll();
            foreach ($fetched as $row){
                $metadata = json_decode($row['metadata'], true);
                $item = array();
                if(!$withLowCaseText){
                    $item = $metadata;
                } else {
                    $item['metadata'] = $metadata;
                    $item['low_case_text'] = $row['low_case_text'];
                }
                $array[substr($row['path'], strlen($this->carnetFolder->getPath())+1)] = $item;
            }

            $stmt->closeCursor();
        }
        return $array;
    }

    public function search($query){
        $arrayFullPath = array();
        $sql = 'SELECT * FROM `*PREFIX*carnet_metadata` ' . 
        'WHERE path LIKE ? AND (metadata LIKE ? OR low_case_text LIKE ?)';
        $args = array();
        array_push($args, $this->carnetFolder->getPath()."/%");
        array_push($args, "%".$query."%");
        array_push($args, "%".$query."%");

        $stmt = $this->db->prepare($sql);
        $stmt->execute($args);
        $array = array();
        $fetched = $stmt->fetchAll();
        foreach ($fetched as $row){
            $array[substr($row['path'], strlen($this->carnetFolder->getPath())+1)] = json_decode($row['last_modification_file']);
        }

        $stmt->closeCursor();
        return $array;
    }
}
?>