1<?php
2/**
3 * @author Daniel Jagszent <daniel@jagszent.de>
4 * @author Joas Schilling <coding@schilljs.com>
5 * @author Morris Jobke <hey@morrisjobke.de>
6 * @author Robin Appelman <icewind@owncloud.com>
7 * @author Robin McCorkell <robin@mccorkell.me.uk>
8 * @author Stefan Weil <sw@weilnetz.de>
9 * @author Thomas Müller <thomas.mueller@tmit.eu>
10 * @author Vincent Petry <pvince81@owncloud.com>
11 *
12 * @copyright Copyright (c) 2018, ownCloud GmbH
13 * @license AGPL-3.0
14 *
15 * This code is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Affero General Public License, version 3,
17 * as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Affero General Public License for more details.
23 *
24 * You should have received a copy of the GNU Affero General Public License, version 3,
25 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26 *
27 */
28
29namespace OC\Files\Cache\Wrapper;
30
31use OC\Files\Cache\Cache;
32use OCP\Files\Cache\ICacheEntry;
33use OCP\Files\Cache\ICache;
34
35class CacheWrapper extends Cache {
36	/**
37	 * @var \OCP\Files\Cache\ICache
38	 */
39	protected $cache;
40
41	/**
42	 * @param \OCP\Files\Cache\ICache $cache
43	 */
44	public function __construct($cache) {
45		$this->cache = $cache;
46	}
47
48	/**
49	 * Make it easy for wrappers to modify every returned cache entry
50	 *
51	 * @param ICacheEntry $entry
52	 * @return ICacheEntry
53	 */
54	protected function formatCacheEntry($entry) {
55		return $entry;
56	}
57
58	/**
59	 * get the stored metadata of a file or folder
60	 *
61	 * @param string|int $file
62	 * @return ICacheEntry|false
63	 */
64	public function get($file) {
65		$result = $this->cache->get($file);
66		if ($result) {
67			$result = $this->formatCacheEntry($result);
68		}
69		return $result;
70	}
71
72	/**
73	 * get the metadata of all files stored in $folder
74	 *
75	 * @param string $folder
76	 * @return ICacheEntry[]
77	 */
78	public function getFolderContents($folder) {
79		// can't do a simple $this->cache->.... call here since getFolderContentsById needs to be called on this
80		// and not the wrapped cache
81		$fileId = $this->getId($folder);
82		return $this->getFolderContentsById($fileId);
83	}
84
85	/**
86	 * get the metadata of all files stored in $folder
87	 *
88	 * @param int $fileId the file id of the folder
89	 * @return array
90	 */
91	public function getFolderContentsById($fileId) {
92		$results = $this->cache->getFolderContentsById($fileId);
93		return \array_map([$this, 'formatCacheEntry'], $results);
94	}
95
96	/**
97	 * insert or update meta data for a file or folder
98	 *
99	 * @param string $file
100	 * @param array $data
101	 *
102	 * @return int file id
103	 * @throws \RuntimeException
104	 */
105	public function put($file, array $data) {
106		if (($id = $this->getId($file)) > -1) {
107			$this->update($id, $data);
108			return $id;
109		} else {
110			return $this->insert($file, $data);
111		}
112	}
113
114	/**
115	 * insert meta data for a new file or folder
116	 *
117	 * @param string $file
118	 * @param array $data
119	 *
120	 * @return int file id
121	 * @throws \RuntimeException
122	 */
123	public function insert($file, array $data) {
124		return $this->cache->insert($file, $data);
125	}
126
127	/**
128	 * update the metadata in the cache
129	 *
130	 * @param int $id
131	 * @param array $data
132	 */
133	public function update($id, array $data) {
134		$this->cache->update($id, $data);
135	}
136
137	/**
138	 * get the file id for a file
139	 *
140	 * @param string $file
141	 * @return int
142	 */
143	public function getId($file) {
144		return $this->cache->getId($file);
145	}
146
147	/**
148	 * get the id of the parent folder of a file
149	 *
150	 * @param string $file
151	 * @return int
152	 */
153	public function getParentId($file) {
154		return $this->cache->getParentId($file);
155	}
156
157	/**
158	 * check if a file is available in the cache
159	 *
160	 * @param string $file
161	 * @return bool
162	 */
163	public function inCache($file) {
164		return $this->cache->inCache($file);
165	}
166
167	/**
168	 * remove a file or folder from the cache
169	 *
170	 * @param string $file
171	 */
172	public function remove($file) {
173		$this->cache->remove($file);
174	}
175
176	/**
177	 * Move a file or folder in the cache
178	 *
179	 * @param string $source
180	 * @param string $target
181	 */
182	public function move($source, $target) {
183		$this->cache->move($source, $target);
184	}
185
186	public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
187		$this->cache->moveFromCache($sourceCache, $sourcePath, $targetPath);
188	}
189
190	/**
191	 * remove all entries for files that are stored on the storage from the cache
192	 */
193	public function clear() {
194		'@phan-var \OC\Files\Cache\Cache $this->cache';
195		$this->cache->clear();
196	}
197
198	/**
199	 * @param string $file
200	 *
201	 * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW, Cache::COMPLETE or Cache::NOT_SCANNED
202	 */
203	public function getStatus($file) {
204		return $this->cache->getStatus($file);
205	}
206
207	/**
208	 * search for files matching $pattern
209	 *
210	 * @param string $pattern
211	 * @return ICacheEntry[] an array of file data
212	 */
213	public function search($pattern) {
214		$results = $this->cache->search($pattern);
215		return \array_map([$this, 'formatCacheEntry'], $results);
216	}
217
218	/**
219	 * search for files by mimetype
220	 *
221	 * @param string $mimetype
222	 * @return ICacheEntry[]
223	 */
224	public function searchByMime($mimetype) {
225		$results = $this->cache->searchByMime($mimetype);
226		return \array_map([$this, 'formatCacheEntry'], $results);
227	}
228
229	/**
230	 * search for files by tag
231	 *
232	 * @param string|int $tag name or tag id
233	 * @param string $userId owner of the tags
234	 * @return ICacheEntry[] file data
235	 */
236	public function searchByTag($tag, $userId) {
237		$results = $this->cache->searchByTag($tag, $userId);
238		return \array_map([$this, 'formatCacheEntry'], $results);
239	}
240
241	/**
242	 * update the folder size and the size of all parent folders
243	 *
244	 * @param string|boolean $path
245	 * @param array $data (optional) meta data of the folder
246	 */
247	public function correctFolderSize($path, $data = null) {
248		if ($this->cache instanceof Cache) {
249			$this->cache->correctFolderSize($path, $data);
250		}
251	}
252
253	/**
254	 * get the size of a folder and set it in the cache
255	 *
256	 * @param string $path
257	 * @param array $entry (optional) meta data of the folder
258	 * @return int
259	 */
260	public function calculateFolderSize($path, $entry = null) {
261		if ($this->cache instanceof Cache) {
262			return $this->cache->calculateFolderSize($path, $entry);
263		} else {
264			return 0;
265		}
266	}
267
268	/**
269	 * get all file ids on the files on the storage
270	 *
271	 * @return int[]
272	 */
273	public function getAll() {
274		'@phan-var \OC\Files\Cache\Cache $this->cache';
275		return $this->cache->getAll();
276	}
277
278	/**
279	 * find a folder in the cache which has not been fully scanned
280	 *
281	 * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
282	 * use the one with the highest id gives the best result with the background scanner, since that is most
283	 * likely the folder where we stopped scanning previously
284	 *
285	 * @return string|bool the path of the folder or false when no folder matched
286	 */
287	public function getIncomplete() {
288		return $this->cache->getIncomplete();
289	}
290
291	/**
292	 * get the path of a file on this storage by it's id
293	 *
294	 * @param int $id
295	 * @return string|null
296	 */
297	public function getPathById($id) {
298		return $this->cache->getPathById($id);
299	}
300
301	/**
302	 * Returns the numeric storage id
303	 *
304	 * @return int
305	 */
306	public function getNumericStorageId() {
307		return $this->cache->getNumericStorageId();
308	}
309}
310