Added searchByTags to view, storage and cache

This commit is contained in:
Vincent Petry 2014-12-04 14:01:15 +01:00
parent 745d8706b9
commit 25dde7e93b
10 changed files with 230 additions and 0 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 = null) {
// 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,39 @@ class Test_Files_Sharing_Cache extends TestCase {
$this->verifyFiles($check, $results);
}
/**
* Test searching by tag
*/
function testSearchByTag() {
$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');
$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');
$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,54 @@ 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 = null) {
if (is_null($userId)) {
$userId = \OC::$server->getUserSession()->getUser()->getUID();
}
$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

@ -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 = null) {
$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,14 @@ class Filesystem {
return self::$defaultInstance->searchByMime($query);
}
/**
* @param string|int $tag name or tag id
* @return FileInfo[] array or file info
*/
static public function searchByTag($tag) {
return self::$defaultInstance->searchByTag($tag);
}
/**
* check if a file or folder has been updated since $time
*

View File

@ -236,6 +236,16 @@ class Folder extends Node implements \OCP\Files\Folder {
return $this->searchCommon($mimetype, 'searchByMime');
}
/**
* search for files by tag
*
* @param string $tag
* @return Node[]
*/
public function searchByTag($tag) {
return $this->searchCommon($tag, 'searchByTag');
}
/**
* @param string $query
* @param string $method

View File

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

View File

@ -1134,6 +1134,16 @@ class View {
return $this->searchCommon($mimetype, 'searchByMime');
}
/**
* search for files by tag
*
* @param string|int $tag name or tag id
* @return FileInfo[]
*/
public function searchByTag($tag) {
return $this->searchCommon($tag, 'searchByTag');
}
/**
* @param string $query
* @param string $method

View File

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

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';