From fcc92a9f43e75ecdb15adb0e948d45eac119306f Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind@owncloud.com>
Date: Sat, 27 Oct 2012 10:34:25 +0200
Subject: [PATCH] add searchByMime to new cache api

---
 lib/files/cache/cache.php       | 21 +++++++++++++++++++++
 lib/files/filesystem.php        |  4 ++++
 lib/files/view.php              | 25 ++++++++++++++++++++++---
 tests/lib/files/cache/cache.php |  3 +++
 tests/lib/files/view.php        |  3 +++
 5 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/lib/files/cache/cache.php b/lib/files/cache/cache.php
index 5efc7d67c4a..6604525477b 100644
--- a/lib/files/cache/cache.php
+++ b/lib/files/cache/cache.php
@@ -287,6 +287,27 @@ class Cache {
 		return $files;
 	}
 
+	/**
+	 * search for files by mimetype
+	 *
+	 * @param string $part1
+	 * @param string $part2
+	 * @return array
+	 */
+	public function searchByMime($mimetype) {
+		if (strpos($mimetype, '/')) {
+			$where = '`mimetype` = ?';
+		} else {
+			$where = '`mimepart` = ?';
+		}
+		$query = \OC_DB::prepare('
+			SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`
+			FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'
+		);
+		$result = $query->execute(array($mimetype, $this->storageId));
+		return $result->fetchAll();
+	}
+
 	/**
 	 * get all file ids on the files on the storage
 	 *
diff --git a/lib/files/filesystem.php b/lib/files/filesystem.php
index b3c92f38558..96660a8ce30 100644
--- a/lib/files/filesystem.php
+++ b/lib/files/filesystem.php
@@ -551,6 +551,10 @@ class Filesystem {
 		return self::$defaultInstance->search($query);
 	}
 
+	static public function searchByMime($query) {
+		return self::$defaultInstance->searchByMime($query);
+	}
+
 	/**
 	 * check if a file or folder has been updated since $time
 	 *
diff --git a/lib/files/view.php b/lib/files/view.php
index 82455d582eb..0a5a6436c27 100644
--- a/lib/files/view.php
+++ b/lib/files/view.php
@@ -747,7 +747,7 @@ class View {
 			$files[$i]['permissions'] = $permissions[$file['fileid']];
 		}
 
-		usort($files, "fileCmp"); //TODO: remove this once ajax is merged
+		usort($files, "fileCmp");
 		return $files;
 	}
 
@@ -784,6 +784,25 @@ class View {
 	 * @return array
 	 */
 	public function search($query) {
+		return $this->searchCommon('%' . $query . '%', 'search');
+	}
+
+	/**
+	 * search for files by mimetype
+	 *
+	 * @param string $query
+	 * @return array
+	 */
+	public function searchByMime($mimetype) {
+		return $this->searchCommon($mimetype, 'searchByMime');
+	}
+
+	/**
+	 * @param string $query
+	 * @param string $method
+	 * @return array
+	 */
+	private function searchCommon($query, $method) {
 		$files = array();
 		$rootLength = strlen($this->fakeRoot);
 
@@ -791,7 +810,7 @@ class View {
 		$storage = Filesystem::getStorage($mountPoint);
 		$cache = $storage->getCache();
 
-		$results = $cache->search('%' . $query . '%');
+		$results = $cache->$method($query);
 		foreach ($results as $result) {
 			if (substr($mountPoint . $result['path'], 0, $rootLength) === $this->fakeRoot) {
 				$result['path'] = substr($mountPoint . $result['path'], $rootLength);
@@ -805,7 +824,7 @@ class View {
 			$cache = $storage->getCache();
 
 			$relativeMountPoint = substr($mountPoint, $rootLength);
-			$results = $cache->search('%' . $query . '%');
+			$results = $cache->$method($query);
 			foreach ($results as $result) {
 				$result['path'] = $relativeMountPoint . $result['path'];
 				$files[] = $result;
diff --git a/tests/lib/files/cache/cache.php b/tests/lib/files/cache/cache.php
index 839e06e93fc..5a8d79b902c 100644
--- a/tests/lib/files/cache/cache.php
+++ b/tests/lib/files/cache/cache.php
@@ -129,6 +129,9 @@ class Cache extends \UnitTestCase {
 		$this->assertEquals(1, count($this->cache->search('%folder%')));
 		$this->assertEquals(1, count($this->cache->search('folder%')));
 		$this->assertEquals(3, count($this->cache->search('%')));
+
+		$this->assertEquals(3, count($this->cache->searchByMime('foo')));
+		$this->assertEquals(2, count($this->cache->searchByMime('foo/file')));
 	}
 
 	public function tearDown() {
diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php
index 980c5298fb4..5144fb1caad 100644
--- a/tests/lib/files/view.php
+++ b/tests/lib/files/view.php
@@ -139,6 +139,9 @@ class View extends \PHPUnit_Framework_TestCase {
 		}
 		$this->assertContains('/anotherstorage/foo.txt', $paths);
 		$this->assertContains('/anotherstorage/foo.png', $paths);
+
+		$this->assertEquals(6, count($rootView->searchByMime('text')));
+		$this->assertEquals(3, count($folderView->searchByMime('text')));
 	}
 
 	/**
-- 
GitLab