diff options
author | Frank Karlitschek <frank@owncloud.org> | 2013-10-30 20:42:21 +0400 |
---|---|---|
committer | Frank Karlitschek <frank@owncloud.org> | 2013-10-30 20:42:21 +0400 |
commit | c251ccfcfb336d234a6bd6e4282ba354d9a1b5ee (patch) | |
tree | d603a4b9c01ad413f6d5dafb5e71daa2f782d78e /apps | |
parent | 800d2e9564627fcccd9ccd83d1cff57eb630ecaf (diff) | |
parent | a75017bb484674e8edbb4fafe0a2a70b44c654f6 (diff) |
Merge pull request #5487 from owncloud/backport_search_in_shared_files_to_stable5
backport search in shared files to stable5
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files_sharing/lib/cache.php | 97 |
1 files changed, 83 insertions, 14 deletions
diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index a2b7e22d1b3..baa632ec9c1 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -20,6 +20,7 @@ */ namespace OC\Files\Cache; +use OCP\Share_Backend_Collection; /** * Metadata cache for shared files @@ -218,31 +219,86 @@ class Shared_Cache extends Cache { * @return array of file data */ public function search($pattern) { - // TODO + + $where = '`name` LIKE ? AND '; + + // normalize pattern + $value = $this->normalize($pattern); + + // we have one ? in our where clause + $chunksize = self::MAX_SQL_CHUNK_SIZE - 1; + + return $this->searchWithWhere($where, $value, $chunksize); + } /** * search for files by mimetype * - * @param string $part1 - * @param string $part2 + * @param string $mimetype * @return array */ public function searchByMime($mimetype) { + if (strpos($mimetype, '/')) { - $where = '`mimetype` = ?'; + $where = '`mimetype` = ? AND '; } else { - $where = '`mimepart` = ?'; + $where = '`mimepart` = ? AND '; } - $mimetype = $this->getMimetypeId($mimetype); + + $value = $this->getMimetypeId($mimetype); + + // we have one ? in our where clause + $chunksize = self::MAX_SQL_CHUNK_SIZE - 1; + + return $this->searchWithWhere($where, $value, $chunksize); + + } + + /** + * The maximum number of placeholders that can be used in an SQL query. + * Value MUST be < 1000. + * Also see ORA-01795 maximum number of expressions in a list is 1000 + */ + const MAX_SQL_CHUNK_SIZE = 999; + + /** + * search for files with a custom where clause and value + * the $wherevalue will be array_merge()d with the file id chunks + * + * @param string $sqlwhere + * @param string $wherevalue + * @return array + */ + private function searchWithWhere($sqlwhere, $wherevalue, $chunksize = self::MAX_SQL_CHUNK_SIZE) { + $ids = $this->getAll(); - $placeholders = join(',', array_fill(0, count($ids), '?')); - $query = \OC_DB::prepare(' - SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted` - FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `fileid` IN (' . $placeholders . ')' - ); - $result = $query->execute(array_merge(array($mimetype), $ids)); - return $result->fetchAll(); + + $files = array(); + + // divide into 1k chunks + $chunks = array_chunk($ids, $chunksize); + + foreach ($chunks as $chunk) { + $placeholders = join(',', array_fill(0, count($chunk), '?')); + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, + `encrypted`, `unencrypted_size`, `etag` + FROM `*PREFIX*filecache` WHERE ' . $sqlwhere . ' `fileid` IN (' . $placeholders . ')'; + + $stmt = \OC_DB::prepare($sql); + + $result = $stmt->execute(array_merge(array($wherevalue), $chunk)); + + while ($row = $result->fetchRow()) { + if (substr($row['path'], 0, 6) === 'files/') { + $row['path'] = substr($row['path'], 6); // remove 'files/' from path as it's relative to '/Shared' + } + $row['mimetype'] = $this->getMimetype($row['mimetype']); + $row['mimepart'] = $this->getMimetype($row['mimepart']); + $files[] = $row; + } + } + return $files; } /** @@ -264,7 +320,20 @@ class Shared_Cache extends Cache { * @return int[] */ public function getAll() { - return \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_ALL); + $ids = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_ALL); + $folderBackend = \OCP\Share::getBackend('folder'); + if ($folderBackend instanceof Share_Backend_Collection) { + foreach ($ids as $file) { + /** @var $folderBackend Share_Backend_Collection */ + $children = $folderBackend->getChildren($file); + foreach ($children as $child) { + $ids[] = (int)$child['source']; + } + + } + } + + return $ids; } } |