Passed
Push — master ( 323f40...cdc43c )
by Roeland
13:28 queued 10s
created

CacheWrapper::searchByMime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Daniel Jagszent <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 * @author Robin McCorkell <[email protected]>
10
 * @author Vincent Petry <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OC\Files\Cache\Wrapper;
29
30
use OC\Files\Cache\Cache;
31
use OCP\Files\Cache\ICacheEntry;
32
use OCP\Files\Cache\ICache;
33
use OCP\Files\Search\ISearchQuery;
34
35
class 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
	protected function getCache() {
49
		return $this->cache;
50
	}
51
52
	/**
53
	 * Make it easy for wrappers to modify every returned cache entry
54
	 *
55
	 * @param ICacheEntry $entry
56
	 * @return ICacheEntry
57
	 */
58
	protected function formatCacheEntry($entry) {
59
		return $entry;
60
	}
61
62
	/**
63
	 * get the stored metadata of a file or folder
64
	 *
65
	 * @param string|int $file
66
	 * @return ICacheEntry|false
67
	 */
68
	public function get($file) {
69
		$result = $this->getCache()->get($file);
70
		if ($result) {
71
			$result = $this->formatCacheEntry($result);
72
		}
73
		return $result;
74
	}
75
76
	/**
77
	 * get the metadata of all files stored in $folder
78
	 *
79
	 * @param string $folder
80
	 * @return ICacheEntry[]
81
	 */
82
	public function getFolderContents($folder) {
83
		// can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this
84
		// and not the wrapped cache
85
		$fileId = $this->getId($folder);
86
		return $this->getFolderContentsById($fileId);
87
	}
88
89
	/**
90
	 * get the metadata of all files stored in $folder
91
	 *
92
	 * @param int $fileId the file id of the folder
93
	 * @return array
94
	 */
95
	public function getFolderContentsById($fileId) {
96
		$results = $this->getCache()->getFolderContentsById($fileId);
97
		return array_map(array($this, 'formatCacheEntry'), $results);
98
	}
99
100
	/**
101
	 * insert or update meta data for a file or folder
102
	 *
103
	 * @param string $file
104
	 * @param array $data
105
	 *
106
	 * @return int file id
107
	 * @throws \RuntimeException
108
	 */
109
	public function put($file, array $data) {
110
		if (($id = $this->getId($file)) > -1) {
111
			$this->update($id, $data);
112
			return $id;
113
		} else {
114
			return $this->insert($file, $data);
115
		}
116
	}
117
118
	/**
119
	 * insert meta data for a new file or folder
120
	 *
121
	 * @param string $file
122
	 * @param array $data
123
	 *
124
	 * @return int file id
125
	 * @throws \RuntimeException
126
	 */
127
	public function insert($file, array $data) {
128
		return $this->getCache()->insert($file, $data);
129
	}
130
131
	/**
132
	 * update the metadata in the cache
133
	 *
134
	 * @param int $id
135
	 * @param array $data
136
	 */
137
	public function update($id, array $data) {
138
		$this->getCache()->update($id, $data);
139
	}
140
141
	/**
142
	 * get the file id for a file
143
	 *
144
	 * @param string $file
145
	 * @return int
146
	 */
147
	public function getId($file) {
148
		return $this->getCache()->getId($file);
149
	}
150
151
	/**
152
	 * get the id of the parent folder of a file
153
	 *
154
	 * @param string $file
155
	 * @return int
156
	 */
157
	public function getParentId($file) {
158
		return $this->getCache()->getParentId($file);
159
	}
160
161
	/**
162
	 * check if a file is available in the cache
163
	 *
164
	 * @param string $file
165
	 * @return bool
166
	 */
167
	public function inCache($file) {
168
		return $this->getCache()->inCache($file);
169
	}
170
171
	/**
172
	 * remove a file or folder from the cache
173
	 *
174
	 * @param string $file
175
	 */
176
	public function remove($file) {
177
		$this->getCache()->remove($file);
178
	}
179
180
	/**
181
	 * Move a file or folder in the cache
182
	 *
183
	 * @param string $source
184
	 * @param string $target
185
	 */
186
	public function move($source, $target) {
187
		$this->getCache()->move($source, $target);
188
	}
189
190
	protected function getMoveInfo($path) {
191
		/** @var Cache $cache */
192
		$cache = $this->getCache();
193
		return $cache->getMoveInfo($path);
194
	}
195
196
	public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
197
		$this->getCache()->moveFromCache($sourceCache, $sourcePath, $targetPath);
198
	}
199
200
	/**
201
	 * remove all entries for files that are stored on the storage from the cache
202
	 */
203
	public function clear() {
204
		$this->getCache()->clear();
0 ignored issues
show
Bug introduced by
The method clear() does not exist on OCP\Files\Cache\ICache. It seems like you code against a sub-type of said class. However, the method does not exist in OC\Lockdown\Filesystem\NullCache. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
		$this->getCache()->/** @scrutinizer ignore-call */ clear();
Loading history...
205
	}
206
207
	/**
208
	 * @param string $file
209
	 *
210
	 * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
211
	 */
212
	public function getStatus($file) {
213
		return $this->getCache()->getStatus($file);
214
	}
215
216
	/**
217
	 * search for files matching $pattern
218
	 *
219
	 * @param string $pattern
220
	 * @return ICacheEntry[] an array of file data
221
	 */
222
	public function search($pattern) {
223
		$results = $this->getCache()->search($pattern);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\Files\Cache\ICache::search() has been deprecated: 9.0.0 due to lack of pagination, not all backends might implement this ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

223
		$results = /** @scrutinizer ignore-deprecated */ $this->getCache()->search($pattern);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
224
		return array_map(array($this, 'formatCacheEntry'), $results);
225
	}
226
227
	/**
228
	 * search for files by mimetype
229
	 *
230
	 * @param string $mimetype
231
	 * @return ICacheEntry[]
232
	 */
233
	public function searchByMime($mimetype) {
234
		$results = $this->getCache()->searchByMime($mimetype);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\Files\Cache\ICache::searchByMime() has been deprecated: 9.0.0 due to lack of pagination, not all backends might implement this ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

234
		$results = /** @scrutinizer ignore-deprecated */ $this->getCache()->searchByMime($mimetype);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
235
		return array_map(array($this, 'formatCacheEntry'), $results);
236
	}
237
238
	public function searchQuery(ISearchQuery $query) {
239
		$results = $this->getCache()->searchQuery($query);
240
		return array_map(array($this, 'formatCacheEntry'), $results);
241
	}
242
243
	/**
244
	 * update the folder size and the size of all parent folders
245
	 *
246
	 * @param string|boolean $path
247
	 * @param array $data (optional) meta data of the folder
248
	 */
249
	public function correctFolderSize($path, $data = null, $isBackgroundScan = false) {
250
		if ($this->getCache() instanceof Cache) {
251
			$this->getCache()->correctFolderSize($path, $data, $isBackgroundScan);
252
		}
253
	}
254
255
	/**
256
	 * get the size of a folder and set it in the cache
257
	 *
258
	 * @param string $path
259
	 * @param array $entry (optional) meta data of the folder
260
	 * @return int
261
	 */
262
	public function calculateFolderSize($path, $entry = null) {
263
		if ($this->getCache() instanceof Cache) {
264
			return $this->getCache()->calculateFolderSize($path, $entry);
265
		} else {
266
			return 0;
267
		}
268
	}
269
270
	/**
271
	 * get all file ids on the files on the storage
272
	 *
273
	 * @return int[]
274
	 */
275
	public function getAll() {
276
		return $this->getCache()->getAll();
0 ignored issues
show
Bug introduced by
The method getAll() does not exist on OCP\Files\Cache\ICache. It seems like you code against a sub-type of said class. However, the method does not exist in OC\Lockdown\Filesystem\NullCache. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

276
		return $this->getCache()->/** @scrutinizer ignore-call */ getAll();
Loading history...
277
	}
278
279
	/**
280
	 * find a folder in the cache which has not been fully scanned
281
	 *
282
	 * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
283
	 * use the one with the highest id gives the best result with the background scanner, since that is most
284
	 * likely the folder where we stopped scanning previously
285
	 *
286
	 * @return string|bool the path of the folder or false when no folder matched
287
	 */
288
	public function getIncomplete() {
289
		return $this->getCache()->getIncomplete();
290
	}
291
292
	/**
293
	 * get the path of a file on this storage by it's id
294
	 *
295
	 * @param int $id
296
	 * @return string|null
297
	 */
298
	public function getPathById($id) {
299
		return $this->getCache()->getPathById($id);
300
	}
301
302
	/**
303
	 * Returns the numeric storage id
304
	 *
305
	 * @return int
306
	 */
307
	public function getNumericStorageId() {
308
		return $this->getCache()->getNumericStorageId();
309
	}
310
311
	/**
312
	 * get the storage id of the storage for a file and the internal path of the file
313
	 * unlike getPathById this does not limit the search to files on this storage and
314
	 * instead does a global search in the cache table
315
	 *
316
	 * @param int $id
317
	 * @return array first element holding the storage id, second the path
318
	 */
319
	static public function getById($id) {
320
		return parent::getById($id);
0 ignored issues
show
Deprecated Code introduced by
The function OC\Files\Cache\Cache::getById() has been deprecated: use getPathById() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

320
		return /** @scrutinizer ignore-deprecated */ parent::getById($id);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
321
	}
322
}
323