Merge pull request #12778 from owncloud/searchbytags2

Added searchByTags to view, storage and cache
This commit is contained in:
Vincent Petry 2014-12-12 14:27:19 +01:00
commit 4b57892c4e
17 changed files with 371 additions and 49 deletions

View File

@ -344,6 +344,46 @@ class Shared_Cache extends Cache {
return $result;
}
/**
* search for files by tag
*
* @param string|int $tag tag to search for
* @param string $userId owner of the tags
* @return array file data
*/
public function searchByTag($tag, $userId) {
// TODO: inject this
$tagger = \OC::$server->getTagManager()->load('files', null, null, $userId);
$result = array();
$exploreDirs = array('');
// FIXME: this is so wrong and unefficient, need to replace with actual DB queries
while (count($exploreDirs) > 0) {
$dir = array_pop($exploreDirs);
$files = $this->getFolderContents($dir);
// no results?
if (!$files) {
// maybe it's a single shared file
$file = $this->get('');
$tags = $tagger->getTagsForObjects(array((int)$file['fileid']));
if (!empty($tags) && in_array($tag, current($tags))) {
$result[] = $file;
}
continue;
}
foreach ($files as $file) {
if ($file['mimetype'] === 'httpd/unix-directory') {
$exploreDirs[] = ltrim($dir . '/' . $file['name'], '/');
} else {
$tags = $tagger->getTagsForObjects(array((int)$file['fileid']));
if (!empty($tags) && in_array($tag, current($tags))) {
$result[] = $file;
}
}
}
}
return $result;
}
/**
* get the size of a folder and set it in the cache
*

View File

@ -204,6 +204,40 @@ class Test_Files_Sharing_Cache extends TestCase {
$this->verifyFiles($check, $results);
}
/**
* Test searching by tag
*/
function testSearchByTag() {
$userId = \OC::$server->getUserSession()->getUser()->getUId();
$id1 = $this->sharedCache->get('bar.txt')['fileid'];
$id2 = $this->sharedCache->get('subdir/another too.txt')['fileid'];
$id3 = $this->sharedCache->get('subdir/not a text file.xml')['fileid'];
$id4 = $this->sharedCache->get('subdir/another.txt')['fileid'];
$tagManager = \OC::$server->getTagManager()->load('files', null, null, $userId);
$tagManager->tagAs($id1, 'tag1');
$tagManager->tagAs($id1, 'tag2');
$tagManager->tagAs($id2, 'tag1');
$tagManager->tagAs($id3, 'tag1');
$tagManager->tagAs($id4, 'tag2');
$results = $this->sharedStorage->getCache()->searchByTag('tag1', $userId);
$check = array(
array(
'name' => 'bar.txt',
'path' => 'bar.txt'
),
array(
'name' => 'another too.txt',
'path' => 'subdir/another too.txt'
),
array(
'name' => 'not a text file.xml',
'path' => 'subdir/not a text file.xml'
),
);
$this->verifyFiles($check, $results);
$tagManager->delete(array('tag1', 'tag2'));
}
function testGetFolderContentsInRoot() {
$results = $this->user2View->getDirectoryContent('/');

View File

@ -503,6 +503,51 @@ class Cache {
return $files;
}
/**
* Search for files by tag of a given users.
*
* Note that every user can tag files differently.
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return array file data
*/
public function searchByTag($tag, $userId) {
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' .
'`mimetype`, `mimepart`, `size`, `mtime`, ' .
'`encrypted`, `unencrypted_size`, `etag`, `permissions` ' .
'FROM `*PREFIX*filecache` `file`, ' .
'`*PREFIX*vcategory_to_object` `tagmap`, ' .
'`*PREFIX*vcategory` `tag` ' .
// JOIN filecache to vcategory_to_object
'WHERE `file`.`fileid` = `tagmap`.`objid` '.
// JOIN vcategory_to_object to vcategory
'AND `tagmap`.`type` = `tag`.`type` ' .
'AND `tagmap`.`categoryid` = `tag`.`id` ' .
// conditions
'AND `file`.`storage` = ? '.
'AND `tag`.`type` = \'files\' ' .
'AND `tag`.`uid` = ? ';
if (is_int($tag)) {
$sql .= 'AND `tag`.`id` = ? ';
} else {
$sql .= 'AND `tag`.`category` = ? ';
}
$result = \OC_DB::executeAudited(
$sql,
array(
$this->getNumericStorageId(),
$userId,
$tag
)
);
$files = array();
while ($row = $result->fetchRow()) {
$files[] = $row;
}
return $files;
}
/**
* update the folder size and the size of all parent folders
*

View File

@ -197,6 +197,18 @@ class CacheJail extends CacheWrapper {
return $this->formatSearchResults($results);
}
/**
* search for files by mimetype
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return array
*/
public function searchByTag($tag, $userId) {
$results = $this->cache->searchByTag($tag, $userId);
return $this->formatSearchResults($results);
}
/**
* update the folder size and the size of all parent folders
*

View File

@ -180,6 +180,18 @@ class CacheWrapper extends Cache {
return array_map(array($this, 'formatCacheEntry'), $results);
}
/**
* search for files by tag
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return array file data
*/
public function searchByTag($tag, $userId) {
$results = $this->cache->searchByTag($tag, $userId);
return array_map(array($this, 'formatCacheEntry'), $results);
}
/**
* update the folder size and the size of all parent folders
*

View File

@ -686,6 +686,15 @@ class Filesystem {
return self::$defaultInstance->searchByMime($query);
}
/**
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return FileInfo[] array or file info
*/
static public function searchByTag($tag, $userId) {
return self::$defaultInstance->searchByTag($tag, $userId);
}
/**
* check if a file or folder has been updated since $time
*

View File

@ -223,7 +223,7 @@ class Folder extends Node implements \OCP\Files\Folder {
* @return \OC\Files\Node\Node[]
*/
public function search($query) {
return $this->searchCommon('%' . $query . '%', 'search');
return $this->searchCommon('search', array('%' . $query . '%'));
}
/**
@ -233,15 +233,26 @@ class Folder extends Node implements \OCP\Files\Folder {
* @return Node[]
*/
public function searchByMime($mimetype) {
return $this->searchCommon($mimetype, 'searchByMime');
return $this->searchCommon('searchByMime', array($mimetype));
}
/**
* @param string $query
* @param string $method
* search for files by tag
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return Node[]
*/
public function searchByTag($tag, $userId) {
return $this->searchCommon('searchByTag', array($tag, $userId));
}
/**
* @param string $method cache method
* @param array $args call args
* @return \OC\Files\Node\Node[]
*/
private function searchCommon($query, $method) {
private function searchCommon($method, $args) {
$files = array();
$rootLength = strlen($this->path);
/**
@ -252,7 +263,7 @@ class Folder extends Node implements \OCP\Files\Folder {
$cache = $storage->getCache('');
$results = $cache->$method($query);
$results = call_user_func_array(array($cache, $method), $args);
foreach ($results as $result) {
if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
$result['internalPath'] = $result['path'];
@ -269,7 +280,7 @@ class Folder extends Node implements \OCP\Files\Folder {
$cache = $storage->getCache('');
$relativeMountPoint = substr($mount->getMountPoint(), $rootLength);
$results = $cache->$method($query);
$results = call_user_func_array(array($cache, $method), $args);
foreach ($results as $result) {
$result['internalPath'] = $result['path'];
$result['path'] = $relativeMountPoint . $result['path'];

View File

@ -99,6 +99,10 @@ class NonExistingFolder extends Folder {
throw new NotFoundException();
}
public function searchByTag($tag, $userId) {
throw new NotFoundException();
}
public function getById($id) {
throw new NotFoundException();
}

View File

@ -1111,7 +1111,7 @@ class View {
* @return FileInfo[]
*/
public function search($query) {
return $this->searchCommon('%' . $query . '%', 'search');
return $this->searchCommon('search', array('%' . $query . '%'));
}
/**
@ -1121,7 +1121,7 @@ class View {
* @return FileInfo[]
*/
public function searchRaw($query) {
return $this->searchCommon($query, 'search');
return $this->searchCommon('search', array($query));
}
/**
@ -1131,15 +1131,26 @@ class View {
* @return FileInfo[]
*/
public function searchByMime($mimetype) {
return $this->searchCommon($mimetype, 'searchByMime');
return $this->searchCommon('searchByMime', array($mimetype));
}
/**
* @param string $query
* @param string $method
* search for files by tag
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return FileInfo[]
*/
private function searchCommon($query, $method) {
public function searchByTag($tag, $userId) {
return $this->searchCommon('searchByTag', array($tag, $userId));
}
/**
* @param string $method cache method
* @param array $args
* @return FileInfo[]
*/
private function searchCommon($method, $args) {
$files = array();
$rootLength = strlen($this->fakeRoot);
@ -1148,7 +1159,7 @@ class View {
if ($storage) {
$cache = $storage->getCache('');
$results = $cache->$method($query);
$results = call_user_func_array(array($cache, $method), $args);
foreach ($results as $result) {
if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') {
$internalPath = $result['path'];
@ -1165,7 +1176,7 @@ class View {
$cache = $storage->getCache('');
$relativeMountPoint = substr($mountPoint, $rootLength);
$results = $cache->$method($query);
$results = call_user_func_array(array($cache, $method), $args);
if ($results) {
foreach ($results as $result) {
$internalPath = $result['path'];

View File

@ -87,8 +87,7 @@ class Server extends SimpleContainer implements IServerContainer {
});
$this->registerService('TagManager', function (Server $c) {
$tagMapper = $c->query('TagMapper');
$user = \OC_User::getUser();
return new TagManager($tagMapper, $user);
return new TagManager($tagMapper, $c->getUserSession());
});
$this->registerService('RootFolder', function (Server $c) {
// TODO: get user and user manager from container as well

View File

@ -38,11 +38,11 @@ use OC\Tagging\TagMapper;
class TagManager implements \OCP\ITagManager {
/**
* User
* User session
*
* @var string
* @var \OCP\IUserSession
*/
private $user;
private $userSession;
/**
* TagMapper
@ -55,12 +55,11 @@ class TagManager implements \OCP\ITagManager {
* Constructor.
*
* @param TagMapper $mapper Instance of the TagMapper abstraction layer.
* @param string $user The user whose data the object will operate on.
* @param \OCP\IUserSession $userSession the user session
*/
public function __construct(TagMapper $mapper, $user) {
public function __construct(TagMapper $mapper, \OCP\IUserSession $userSession) {
$this->mapper = $mapper;
$this->user = $user;
$this->userSession = $userSession;
}
@ -71,10 +70,15 @@ class TagManager implements \OCP\ITagManager {
* @param string $type The type identifier e.g. 'contact' or 'event'.
* @param array $defaultTags An array of default tags to be used if none are stored.
* @param boolean $includeShared Whether to include tags for items shared with this user by others.
* @param string $userId user for which to retrieve the tags, defaults to the currently
* logged in user
* @return \OCP\ITags
*/
public function load($type, $defaultTags=array(), $includeShared=false) {
return new Tags($this->mapper, $this->user, $type, $defaultTags, $includeShared);
public function load($type, $defaultTags = array(), $includeShared = false, $userId = null) {
if (is_null($userId)) {
$userId = $this->userSession->getUser()->getUId();
}
return new Tags($this->mapper, $userId, $type, $defaultTags, $includeShared);
}
}

View File

@ -116,6 +116,15 @@ interface Folder extends Node {
*/
public function searchByMime($mimetype);
/**
* search for files by tag
*
* @param string|int $tag tag name or tag id
* @param string $userId owner of the tags
* @return \OCP\Files\Node[]
*/
public function searchByTag($tag, $userId);
/**
* get a file or folder inside the folder by it's internal id
*

View File

@ -43,14 +43,15 @@ namespace OCP;
interface ITagManager {
/**
* Create a new \OCP\ITags instance and load tags from db.
* Create a new \OCP\ITags instance and load tags from db for the current user.
*
* @see \OCP\ITags
* @param string $type The type identifier e.g. 'contact' or 'event'.
* @param array $defaultTags An array of default tags to be used if none are stored.
* @param boolean $includeShared Whether to include tags for items shared with this user by others.
* @param string $userId user for which to retrieve the tags, defaults to the currently
* logged in user
* @return \OCP\ITags
*/
public function load($type, $defaultTags=array(), $includeShared=false);
public function load($type, $defaultTags = array(), $includeShared = false, $userId = null);
}

View File

@ -270,6 +270,63 @@ class Cache extends \Test\TestCase {
$this->assertEquals(2, count($this->cache->searchByMime('foo/file')));
}
function testSearchByTag() {
$userId = $this->getUniqueId('user');
\OC_User::createUser($userId, $userId);
$this->loginAsUser($userId);
$user = new \OC\User\User($userId, null);
$file1 = 'folder';
$file2 = 'folder/foobar';
$file3 = 'folder/foo';
$file4 = 'folder/foo2';
$file5 = 'folder/foo3';
$data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder');
$fileData = array();
$fileData['foobar'] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
$fileData['foo'] = array('size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file');
$fileData['foo2'] = array('size' => 25, 'mtime' => 28, 'mimetype' => 'foo/file');
$fileData['foo3'] = array('size' => 88, 'mtime' => 34, 'mimetype' => 'foo/file');
$id1 = $this->cache->put($file1, $data1);
$id2 = $this->cache->put($file2, $fileData['foobar']);
$id3 = $this->cache->put($file3, $fileData['foo']);
$id4 = $this->cache->put($file4, $fileData['foo2']);
$id5 = $this->cache->put($file5, $fileData['foo3']);
$tagManager = \OC::$server->getTagManager()->load('files', null, null, $userId);
$this->assertTrue($tagManager->tagAs($id1, 'tag1'));
$this->assertTrue($tagManager->tagAs($id1, 'tag2'));
$this->assertTrue($tagManager->tagAs($id2, 'tag2'));
$this->assertTrue($tagManager->tagAs($id3, 'tag1'));
$this->assertTrue($tagManager->tagAs($id4, 'tag2'));
// use tag name
$results = $this->cache->searchByTag('tag1', $userId);
$this->assertEquals(2, count($results));
$this->assertEquals('folder', $results[0]['name']);
$this->assertEquals('foo', $results[1]['name']);
// use tag id
$tags = $tagManager->getTagsForUser($userId);
$this->assertNotEmpty($tags);
$tags = array_filter($tags, function($tag) { return $tag->getName() === 'tag2'; });
$results = $this->cache->searchByTag(current($tags)->getId(), $userId);
$this->assertEquals(3, count($results));
$this->assertEquals('folder', $results[0]['name']);
$this->assertEquals('foobar', $results[1]['name']);
$this->assertEquals('foo2', $results[2]['name']);
$tagManager->delete('tag1');
$tagManager->delete('tag2');
$this->logout();
\OC_User::deleteUser($userId);
}
function testMove() {
$file1 = 'folder';
$file2 = 'folder/bar';

View File

@ -405,6 +405,45 @@ class Folder extends \Test\TestCase {
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
}
public function testSearchByTag() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->getMock('\OC\Files\View');
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
$root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$storage = $this->getMock('\OC\Files\Storage\Storage');
$cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
$storage->expects($this->once())
->method('getCache')
->will($this->returnValue($cache));
$cache->expects($this->once())
->method('searchByTag')
->with('tag1', 'user1')
->will($this->returnValue(array(
array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain')
)));
$root->expects($this->once())
->method('getMountsIn')
->with('/bar/foo')
->will($this->returnValue(array()));
$view->expects($this->once())
->method('resolvePath')
->will($this->returnValue(array($storage, 'foo')));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$result = $node->searchByTag('tag1', 'user1');
$this->assertEquals(1, count($result));
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
}
public function testSearchSubStorages() {
$manager = $this->getMock('\OC\Files\Mount\Manager');
/**

View File

@ -23,7 +23,10 @@
class Test_Tags extends \Test\TestCase {
protected $objectType;
/** @var \OC\IUser */
protected $user;
/** @var \OC\IUserSession */
protected $userSession;
protected $backupGlobals = FALSE;
/** @var \OC\Tagging\TagMapper */
protected $tagMapper;
@ -35,12 +38,19 @@ class Test_Tags extends \Test\TestCase {
OC_User::clearBackends();
OC_User::useBackend('dummy');
$this->user = $this->getUniqueID('user_');
$userId = $this->getUniqueID('user_');
OC_User::createUser($userId, 'pass');
OC_User::setUserId($userId);
$this->user = new OC\User\User($userId, null);
$this->userSession = $this->getMock('\OCP\IUserSession');
$this->userSession
->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$this->objectType = $this->getUniqueID('type_');
OC_User::createUser($this->user, 'pass');
OC_User::setUserId($this->user);
$this->tagMapper = new OC\Tagging\TagMapper(\OC::$server->getDb());
$this->tagMgr = new OC\TagManager($this->tagMapper, $this->user);
$this->tagMgr = new OC\TagManager($this->tagMapper, $this->userSession);
}
@ -166,7 +176,7 @@ class Test_Tags extends \Test\TestCase {
);
}
public function testdeleteTags() {
public function testDeleteTags() {
$defaultTags = array('Friends', 'Family', 'Work', 'Other');
$tagger = $this->tagMgr->load($this->objectType, $defaultTags);
@ -177,7 +187,6 @@ class Test_Tags extends \Test\TestCase {
$tagger->delete(array('Friends', 'Work', 'Other'));
$this->assertEquals(0, count($tagger->getTags()));
}
public function testRenameTag() {
@ -233,27 +242,32 @@ class Test_Tags extends \Test\TestCase {
}
public function testShareTags() {
$test_tag = 'TestTag';
$testTag = 'TestTag';
OCP\Share::registerBackend('test', 'Test_Share_Backend');
$tagger = $this->tagMgr->load('test');
$tagger->tagAs(1, $test_tag);
$tagger->tagAs(1, $testTag);
$other_user = $this->getUniqueID('user2_');
OC_User::createUser($other_user, 'pass');
$otherUserId = $this->getUniqueID('user2_');
OC_User::createUser($otherUserId, 'pass');
OC_User::setUserId($otherUserId);
$otherUserSession = $this->getMock('\OCP\IUserSession');
$otherUserSession
->expects($this->any())
->method('getUser')
->will($this->returnValue(new OC\User\User($otherUserId, null)));
OC_User::setUserId($other_user);
$other_tagMgr = new OC\TagManager($this->tagMapper, $other_user);
$other_tagger = $other_tagMgr->load('test');
$this->assertFalse($other_tagger->hasTag($test_tag));
$otherTagMgr = new OC\TagManager($this->tagMapper, $otherUserSession);
$otherTagger = $otherTagMgr->load('test');
$this->assertFalse($otherTagger->hasTag($testTag));
OC_User::setUserId($this->user);
OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $other_user, \OCP\Constants::PERMISSION_READ);
OC_User::setUserId($this->user->getUID());
OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $otherUserId, \OCP\Constants::PERMISSION_READ);
OC_User::setUserId($other_user);
$other_tagger = $other_tagMgr->load('test', array(), true); // Update tags, load shared ones.
$this->assertTrue($other_tagger->hasTag($test_tag));
$this->assertContains(1, $other_tagger->getIdsForTag($test_tag));
OC_User::setUserId($otherUserId);
$otherTagger = $otherTagMgr->load('test', array(), true); // Update tags, load shared ones.
$this->assertTrue($otherTagger->hasTag($testTag));
$this->assertContains(1, $otherTagger->getIdsForTag($testTag));
}
}

View File

@ -148,4 +148,25 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
\OC_FileProxy::$enabled = true;
\OC_FileProxy::clearProxies();
}
/**
* Login and setup FS as a given user,
* sets the given user as the current user.
*
* @param string $user user id
*/
static protected function loginAsUser($user) {
self::logout();
\OC\Files\Filesystem::tearDown();
\OC_User::setUserId($user);
\OC_Util::setupFS($user);
}
/**
* Logout the current user and tear down the filesystem.
*/
static protected function logout() {
\OC_Util::tearDownFS();
\OC_User::setUserId('');
}
}