You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
5.9 KiB

  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Cache\Wrapper;
  9. use OC\Files\Cache\Cache;
  10. class CacheWrapper extends Cache {
  11. /**
  12. * @var \OC\Files\Cache\Cache
  13. */
  14. protected $cache;
  15. /**
  16. * @param \OC\Files\Cache\Cache $cache
  17. */
  18. public function __construct($cache) {
  19. $this->cache = $cache;
  20. }
  21. /**
  22. * Make it easy for wrappers to modify every returned cache entry
  23. *
  24. * @param array $entry
  25. * @return array
  26. */
  27. protected function formatCacheEntry($entry) {
  28. return $entry;
  29. }
  30. /**
  31. * get the stored metadata of a file or folder
  32. *
  33. * @param string /int $file
  34. * @return array|false
  35. */
  36. public function get($file) {
  37. $result = $this->cache->get($file);
  38. if ($result) {
  39. $result = $this->formatCacheEntry($result);
  40. }
  41. return $result;
  42. }
  43. /**
  44. * get the metadata of all files stored in $folder
  45. *
  46. * @param string $folder
  47. * @return array
  48. */
  49. public function getFolderContents($folder) {
  50. // cant do a simple $this->cache->.... call here since getFolderContentsById needs to be called on this
  51. // and not the wrapped cache
  52. $fileId = $this->getId($folder);
  53. return $this->getFolderContentsById($fileId);
  54. }
  55. /**
  56. * get the metadata of all files stored in $folder
  57. *
  58. * @param int $fileId the file id of the folder
  59. * @return array
  60. */
  61. public function getFolderContentsById($fileId) {
  62. $results = $this->cache->getFolderContentsById($fileId);
  63. return array_map(array($this, 'formatCacheEntry'), $results);
  64. }
  65. /**
  66. * store meta data for a file or folder
  67. *
  68. * @param string $file
  69. * @param array $data
  70. *
  71. * @return int file id
  72. */
  73. public function put($file, array $data) {
  74. return $this->cache->put($file, $data);
  75. }
  76. /**
  77. * update the metadata in the cache
  78. *
  79. * @param int $id
  80. * @param array $data
  81. */
  82. public function update($id, array $data) {
  83. $this->cache->update($id, $data);
  84. }
  85. /**
  86. * get the file id for a file
  87. *
  88. * @param string $file
  89. * @return int
  90. */
  91. public function getId($file) {
  92. return $this->cache->getId($file);
  93. }
  94. /**
  95. * get the id of the parent folder of a file
  96. *
  97. * @param string $file
  98. * @return int
  99. */
  100. public function getParentId($file) {
  101. return $this->cache->getParentId($file);
  102. }
  103. /**
  104. * check if a file is available in the cache
  105. *
  106. * @param string $file
  107. * @return bool
  108. */
  109. public function inCache($file) {
  110. return $this->cache->inCache($file);
  111. }
  112. /**
  113. * remove a file or folder from the cache
  114. *
  115. * @param string $file
  116. */
  117. public function remove($file) {
  118. $this->cache->remove($file);
  119. }
  120. /**
  121. * Move a file or folder in the cache
  122. *
  123. * @param string $source
  124. * @param string $target
  125. */
  126. public function move($source, $target) {
  127. $this->cache->move($source, $target);
  128. }
  129. /**
  130. * remove all entries for files that are stored on the storage from the cache
  131. */
  132. public function clear() {
  133. $this->cache->clear();
  134. }
  135. /**
  136. * @param string $file
  137. *
  138. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  139. */
  140. public function getStatus($file) {
  141. return $this->cache->getStatus($file);
  142. }
  143. /**
  144. * search for files matching $pattern
  145. *
  146. * @param string $pattern
  147. * @return array an array of file data
  148. */
  149. public function search($pattern) {
  150. $results = $this->cache->search($pattern);
  151. return array_map(array($this, 'formatCacheEntry'), $results);
  152. }
  153. /**
  154. * search for files by mimetype
  155. *
  156. * @param string $mimetype
  157. * @return array
  158. */
  159. public function searchByMime($mimetype) {
  160. $results = $this->cache->searchByMime($mimetype);
  161. return array_map(array($this, 'formatCacheEntry'), $results);
  162. }
  163. /**
  164. * search for files by tag
  165. *
  166. * @param string|int $tag name or tag id
  167. * @param string $userId owner of the tags
  168. * @return array file data
  169. */
  170. public function searchByTag($tag, $userId) {
  171. $results = $this->cache->searchByTag($tag, $userId);
  172. return array_map(array($this, 'formatCacheEntry'), $results);
  173. }
  174. /**
  175. * update the folder size and the size of all parent folders
  176. *
  177. * @param string|boolean $path
  178. * @param array $data (optional) meta data of the folder
  179. */
  180. public function correctFolderSize($path, $data = null) {
  181. $this->cache->correctFolderSize($path, $data);
  182. }
  183. /**
  184. * get the size of a folder and set it in the cache
  185. *
  186. * @param string $path
  187. * @param array $entry (optional) meta data of the folder
  188. * @return int
  189. */
  190. public function calculateFolderSize($path, $entry = null) {
  191. return $this->cache->calculateFolderSize($path, $entry);
  192. }
  193. /**
  194. * get all file ids on the files on the storage
  195. *
  196. * @return int[]
  197. */
  198. public function getAll() {
  199. return $this->cache->getAll();
  200. }
  201. /**
  202. * find a folder in the cache which has not been fully scanned
  203. *
  204. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  205. * use the one with the highest id gives the best result with the background scanner, since that is most
  206. * likely the folder where we stopped scanning previously
  207. *
  208. * @return string|bool the path of the folder or false when no folder matched
  209. */
  210. public function getIncomplete() {
  211. return $this->cache->getIncomplete();
  212. }
  213. /**
  214. * get the path of a file on this storage by it's id
  215. *
  216. * @param int $id
  217. * @return string|null
  218. */
  219. public function getPathById($id) {
  220. return $this->cache->getPathById($id);
  221. }
  222. /**
  223. * Returns the numeric storage id
  224. *
  225. * @return int
  226. */
  227. public function getNumericStorageId() {
  228. return $this->cache->getNumericStorageId();
  229. }
  230. /**
  231. * get the storage id of the storage for a file and the internal path of the file
  232. * unlike getPathById this does not limit the search to files on this storage and
  233. * instead does a global search in the cache table
  234. *
  235. * @param int $id
  236. * @return array first element holding the storage id, second the path
  237. */
  238. static public function getById($id) {
  239. return parent::getById($id);
  240. }
  241. }