|
|
|
@ -34,6 +34,9 @@ |
|
|
|
|
|
|
|
|
|
namespace OC; |
|
|
|
|
|
|
|
|
|
use \OC\Tagging\Tag, |
|
|
|
|
\OC\Tagging\TagMapper; |
|
|
|
|
|
|
|
|
|
class Tags implements \OCP\ITags { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -55,14 +58,44 @@ class Tags implements \OCP\ITags { |
|
|
|
|
* |
|
|
|
|
* @var string |
|
|
|
|
*/ |
|
|
|
|
private $type = null; |
|
|
|
|
private $type; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* User |
|
|
|
|
* |
|
|
|
|
* @var string |
|
|
|
|
*/ |
|
|
|
|
private $user = null; |
|
|
|
|
private $user; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Are we including tags for shared items? |
|
|
|
|
* |
|
|
|
|
* @var bool |
|
|
|
|
*/ |
|
|
|
|
private $includeShared = false; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The current user, plus any owners of the items shared with the current |
|
|
|
|
* user, if $this->includeShared === true. |
|
|
|
|
* |
|
|
|
|
* @var array |
|
|
|
|
*/ |
|
|
|
|
private $owners = array(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The Mapper we're using to communicate our Tag objects to the database. |
|
|
|
|
* |
|
|
|
|
* @var TagMapper |
|
|
|
|
*/ |
|
|
|
|
private $mapper; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The sharing backend for objects of $this->type. Required if |
|
|
|
|
* $this->includeShared === true to determine ownership of items. |
|
|
|
|
* |
|
|
|
|
* @var \OCP\Share_Backend |
|
|
|
|
*/ |
|
|
|
|
private $backend; |
|
|
|
|
|
|
|
|
|
const TAG_TABLE = '*PREFIX*vcategory'; |
|
|
|
|
const RELATION_TABLE = '*PREFIX*vcategory_to_object'; |
|
|
|
@ -72,47 +105,29 @@ class Tags implements \OCP\ITags { |
|
|
|
|
/** |
|
|
|
|
* Constructor. |
|
|
|
|
* |
|
|
|
|
* @param string $user The user whos data the object will operate on. |
|
|
|
|
* @param string $type |
|
|
|
|
* @param TagMapper $mapper Instance of the TagMapper abstraction layer. |
|
|
|
|
* @param string $user The user whose data the object will operate on. |
|
|
|
|
* @param string $type The type of items for which tags will be loaded. |
|
|
|
|
* @param array $defaultTags Tags that should be created at construction. |
|
|
|
|
* @param boolean $includeShared Whether to include tags for items shared with this user by others. |
|
|
|
|
*/ |
|
|
|
|
public function __construct($user, $type, $defaultTags = array()) { |
|
|
|
|
public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) { |
|
|
|
|
$this->mapper = $mapper; |
|
|
|
|
$this->user = $user; |
|
|
|
|
$this->type = $type; |
|
|
|
|
$this->loadTags($defaultTags); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Load tags from db. |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
protected function loadTags($defaultTags=array()) { |
|
|
|
|
$this->tags = array(); |
|
|
|
|
$result = null; |
|
|
|
|
$sql = 'SELECT `id`, `category` FROM `' . self::TAG_TABLE . '` ' |
|
|
|
|
. 'WHERE `uid` = ? AND `type` = ? ORDER BY `category`'; |
|
|
|
|
try { |
|
|
|
|
$stmt = \OCP\DB::prepare($sql); |
|
|
|
|
$result = $stmt->execute(array($this->user, $this->type)); |
|
|
|
|
if (\OCP\DB::isError($result)) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR); |
|
|
|
|
} |
|
|
|
|
} catch(\Exception $e) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
|
|
|
\OCP\Util::ERROR); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!is_null($result)) { |
|
|
|
|
while( $row = $result->fetchRow()) { |
|
|
|
|
$this->tags[$row['id']] = $row['category']; |
|
|
|
|
} |
|
|
|
|
$this->includeShared = $includeShared; |
|
|
|
|
$this->owners = array($this->user); |
|
|
|
|
if ($this->includeShared) { |
|
|
|
|
$this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true)); |
|
|
|
|
$this->backend = \OC\Share\Share::getBackend($this->type); |
|
|
|
|
} |
|
|
|
|
$this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
|
|
|
|
|
|
|
|
if(count($defaultTags) > 0 && count($this->tags) === 0) { |
|
|
|
|
$this->addMultiple($defaultTags, true); |
|
|
|
|
} |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true), |
|
|
|
|
\OCP\Util::DEBUG); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -124,13 +139,28 @@ class Tags implements \OCP\ITags { |
|
|
|
|
return count($this->tags) === 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns an array mapping a given tag's properties to its values: |
|
|
|
|
* ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
|
|
|
* |
|
|
|
|
* @param string $id The ID of the tag that is going to be mapped |
|
|
|
|
* @return array|false |
|
|
|
|
*/ |
|
|
|
|
public function getTag($id) { |
|
|
|
|
$key = $this->getTagById($id); |
|
|
|
|
if ($key !== false) { |
|
|
|
|
return $this->tagMap($this->tags[$key]); |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the tags for a specific user. |
|
|
|
|
* |
|
|
|
|
* This returns an array with id/name maps: |
|
|
|
|
* This returns an array with maps containing each tag's properties: |
|
|
|
|
* [ |
|
|
|
|
* ['id' => 0, 'name' = 'First tag'], |
|
|
|
|
* ['id' => 1, 'name' = 'Second tag'], |
|
|
|
|
* ['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'], |
|
|
|
|
* ['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'], |
|
|
|
|
* ] |
|
|
|
|
* |
|
|
|
|
* @return array |
|
|
|
@ -140,22 +170,35 @@ class Tags implements \OCP\ITags { |
|
|
|
|
return array(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$tags = array_values($this->tags); |
|
|
|
|
uasort($tags, 'strnatcasecmp'); |
|
|
|
|
usort($this->tags, function($a, $b) { |
|
|
|
|
return strnatcasecmp($a->getName(), $b->getName()); |
|
|
|
|
}); |
|
|
|
|
$tagMap = array(); |
|
|
|
|
|
|
|
|
|
foreach($tags as $tag) { |
|
|
|
|
if($tag !== self::TAG_FAVORITE) { |
|
|
|
|
$tagMap[] = array( |
|
|
|
|
'id' => $this->array_searchi($tag, $this->tags), |
|
|
|
|
'name' => $tag |
|
|
|
|
); |
|
|
|
|
foreach($this->tags as $tag) { |
|
|
|
|
if($tag->getName() !== self::TAG_FAVORITE) { |
|
|
|
|
$tagMap[] = $this->tagMap($tag); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return $tagMap; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Return only the tags owned by the given user, omitting any tags shared |
|
|
|
|
* by other users. |
|
|
|
|
* |
|
|
|
|
* @param string $user The user whose tags are to be checked. |
|
|
|
|
* @return array An array of Tag objects. |
|
|
|
|
*/ |
|
|
|
|
public function getTagsForUser($user) { |
|
|
|
|
return array_filter($this->tags, |
|
|
|
|
function($tag) use($user) { |
|
|
|
|
return $tag->getOwner() === $user; |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the a list if items tagged with $tag. |
|
|
|
|
* |
|
|
|
@ -174,7 +217,7 @@ class Tags implements \OCP\ITags { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
$tagId = $this->array_searchi($tag, $this->tags); |
|
|
|
|
$tagId = $this->getTagId($tag); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if($tagId === false) { |
|
|
|
@ -203,7 +246,22 @@ class Tags implements \OCP\ITags { |
|
|
|
|
|
|
|
|
|
if(!is_null($result)) { |
|
|
|
|
while( $row = $result->fetchRow()) { |
|
|
|
|
$ids[] = (int)$row['objid']; |
|
|
|
|
$id = (int)$row['objid']; |
|
|
|
|
|
|
|
|
|
if ($this->includeShared) { |
|
|
|
|
// We have to check if we are really allowed to access the |
|
|
|
|
// items that are tagged with $tag. To that end, we ask the |
|
|
|
|
// corresponding sharing backend if the item identified by $id |
|
|
|
|
// is owned by any of $this->owners. |
|
|
|
|
foreach ($this->owners as $owner) { |
|
|
|
|
if ($this->backend->isValidSource($id, $owner)) { |
|
|
|
|
$ids[] = $id; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
$ids[] = $id; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -211,13 +269,26 @@ class Tags implements \OCP\ITags { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Checks whether a tag is already saved. |
|
|
|
|
* Checks whether a tag is saved for the given user, |
|
|
|
|
* disregarding the ones shared with him or her. |
|
|
|
|
* |
|
|
|
|
* @param string $name The tag name to check for. |
|
|
|
|
* @param string $user The user whose tags are to be checked. |
|
|
|
|
* @return bool |
|
|
|
|
*/ |
|
|
|
|
public function userHasTag($name, $user) { |
|
|
|
|
$key = $this->array_searchi($name, $this->getTagsForUser($user)); |
|
|
|
|
return ($key !== false) ? $this->tags[$key]->getId() : false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Checks whether a tag is saved for or shared with the current user. |
|
|
|
|
* |
|
|
|
|
* @param string $name The name to check for. |
|
|
|
|
* @param string $name The tag name to check for. |
|
|
|
|
* @return bool |
|
|
|
|
*/ |
|
|
|
|
public function hasTag($name) { |
|
|
|
|
return $this->in_arrayi($name, $this->tags); |
|
|
|
|
return $this->getTagId($name) !== false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -233,41 +304,27 @@ class Tags implements \OCP\ITags { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if($this->hasTag($name)) { |
|
|
|
|
if($this->userHasTag($name, $this->user)) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
$result = \OCP\DB::insertIfNotExist( |
|
|
|
|
self::TAG_TABLE, |
|
|
|
|
array( |
|
|
|
|
'uid' => $this->user, |
|
|
|
|
'type' => $this->type, |
|
|
|
|
'category' => $name, |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
if (\OCP\DB::isError($result)) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR); |
|
|
|
|
return false; |
|
|
|
|
} elseif((int)$result === 0) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', Tag already exists: ' . $name, \OCP\Util::DEBUG); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
$tag = new Tag($this->user, $this->type, $name); |
|
|
|
|
$tag = $this->mapper->insert($tag); |
|
|
|
|
$this->tags[] = $tag; |
|
|
|
|
} catch(\Exception $e) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
|
|
|
\OCP\Util::ERROR); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
$id = \OCP\DB::insertid(self::TAG_TABLE); |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', id: ' . $id, \OCP\Util::DEBUG); |
|
|
|
|
$this->tags[$id] = $name; |
|
|
|
|
return $id; |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG); |
|
|
|
|
return $tag->getId(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Rename tag. |
|
|
|
|
* |
|
|
|
|
* @param string $from The name of the existing tag |
|
|
|
|
* @param string|integer $from The name or ID of the existing tag |
|
|
|
|
* @param string $to The new name of the tag. |
|
|
|
|
* @return bool |
|
|
|
|
*/ |
|
|
|
@ -280,27 +337,30 @@ class Tags implements \OCP\ITags { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$id = $this->array_searchi($from, $this->tags); |
|
|
|
|
if($id === false) { |
|
|
|
|
if (is_numeric($from)) { |
|
|
|
|
$key = $this->getTagById($from); |
|
|
|
|
} else { |
|
|
|
|
$key = $this->getTagByName($from); |
|
|
|
|
} |
|
|
|
|
if($key === false) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
$tag = $this->tags[$key]; |
|
|
|
|
|
|
|
|
|
if($this->userHasTag($to, $tag->getOwner())) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$sql = 'UPDATE `' . self::TAG_TABLE . '` SET `category` = ? ' |
|
|
|
|
. 'WHERE `uid` = ? AND `type` = ? AND `id` = ?'; |
|
|
|
|
try { |
|
|
|
|
$stmt = \OCP\DB::prepare($sql); |
|
|
|
|
$result = $stmt->execute(array($to, $this->user, $this->type, $id)); |
|
|
|
|
if (\OCP\DB::isError($result)) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
$tag->setName($to); |
|
|
|
|
$this->tags[$key] = $this->mapper->update($tag); |
|
|
|
|
} catch(\Exception $e) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
|
|
|
\OCP\Util::ERROR); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
$this->tags[$id] = $to; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -308,7 +368,7 @@ class Tags implements \OCP\ITags { |
|
|
|
|
* Add a list of new tags. |
|
|
|
|
* |
|
|
|
|
* @param string[] $names A string with a name or an array of strings containing |
|
|
|
|
* the name(s) of the to add. |
|
|
|
|
* the name(s) of the tag(s) to add. |
|
|
|
|
* @param bool $sync When true, save the tags |
|
|
|
|
* @param int|null $id int Optional object id to add to this|these tag(s) |
|
|
|
|
* @return bool Returns false on error. |
|
|
|
@ -322,9 +382,8 @@ class Tags implements \OCP\ITags { |
|
|
|
|
|
|
|
|
|
$newones = array(); |
|
|
|
|
foreach($names as $name) { |
|
|
|
|
if(($this->in_arrayi( |
|
|
|
|
$name, $this->tags) == false) && $name !== '') { |
|
|
|
|
$newones[] = $name; |
|
|
|
|
if(!$this->hasTag($name) && $name !== '') { |
|
|
|
|
$newones[] = new Tag($this->user, $this->type, $name); |
|
|
|
|
} |
|
|
|
|
if(!is_null($id) ) { |
|
|
|
|
// Insert $objectid, $categoryid pairs if not exist. |
|
|
|
@ -346,26 +405,26 @@ class Tags implements \OCP\ITags { |
|
|
|
|
if(is_array($this->tags)) { |
|
|
|
|
foreach($this->tags as $tag) { |
|
|
|
|
try { |
|
|
|
|
\OCP\DB::insertIfNotExist(self::TAG_TABLE, |
|
|
|
|
array( |
|
|
|
|
'uid' => $this->user, |
|
|
|
|
'type' => $this->type, |
|
|
|
|
'category' => $tag, |
|
|
|
|
)); |
|
|
|
|
if (!$this->mapper->tagExists($tag)) { |
|
|
|
|
$this->mapper->insert($tag); |
|
|
|
|
} |
|
|
|
|
} catch(\Exception $e) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(), |
|
|
|
|
\OCP\Util::ERROR); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// reload tags to get the proper ids. |
|
|
|
|
$this->loadTags(); |
|
|
|
|
$this->tags = $this->mapper->loadTags($this->owners, $this->type); |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true), |
|
|
|
|
\OCP\Util::DEBUG); |
|
|
|
|
// Loop through temporarily cached objectid/tagname pairs |
|
|
|
|
// and save relations. |
|
|
|
|
$tags = $this->tags; |
|
|
|
|
// For some reason this is needed or array_search(i) will return 0..? |
|
|
|
|
ksort($tags); |
|
|
|
|
foreach(self::$relations as $relation) { |
|
|
|
|
$tagId = $this->array_searchi($relation['tag'], $tags); |
|
|
|
|
$tagId = $this->getTagId($relation['tag']); |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG); |
|
|
|
|
if($tagId) { |
|
|
|
|
try { |
|
|
|
@ -493,7 +552,7 @@ class Tags implements \OCP\ITags { |
|
|
|
|
* @return boolean |
|
|
|
|
*/ |
|
|
|
|
public function addToFavorites($objid) { |
|
|
|
|
if(!$this->hasTag(self::TAG_FAVORITE)) { |
|
|
|
|
if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) { |
|
|
|
|
$this->add(self::TAG_FAVORITE); |
|
|
|
|
} |
|
|
|
|
return $this->tagAs($objid, self::TAG_FAVORITE); |
|
|
|
@ -526,7 +585,7 @@ class Tags implements \OCP\ITags { |
|
|
|
|
if(!$this->hasTag($tag)) { |
|
|
|
|
$this->add($tag); |
|
|
|
|
} |
|
|
|
|
$tagId = $this->array_searchi($tag, $this->tags); |
|
|
|
|
$tagId = $this->getTagId($tag); |
|
|
|
|
} else { |
|
|
|
|
$tagId = $tag; |
|
|
|
|
} |
|
|
|
@ -559,7 +618,7 @@ class Tags implements \OCP\ITags { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
$tagId = $this->array_searchi($tag, $this->tags); |
|
|
|
|
$tagId = $this->getTagId($tag); |
|
|
|
|
} else { |
|
|
|
|
$tagId = $tag; |
|
|
|
|
} |
|
|
|
@ -578,9 +637,9 @@ class Tags implements \OCP\ITags { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Delete tags from the |
|
|
|
|
* Delete tags from the database. |
|
|
|
|
* |
|
|
|
|
* @param string[] $names An array of tags to delete |
|
|
|
|
* @param string[]|integer[] $names An array of tags (names or IDs) to delete |
|
|
|
|
* @return bool Returns false on error |
|
|
|
|
*/ |
|
|
|
|
public function delete($names) { |
|
|
|
@ -596,21 +655,19 @@ class Tags implements \OCP\ITags { |
|
|
|
|
foreach($names as $name) { |
|
|
|
|
$id = null; |
|
|
|
|
|
|
|
|
|
if($this->hasTag($name)) { |
|
|
|
|
$id = $this->array_searchi($name, $this->tags); |
|
|
|
|
unset($this->tags[$id]); |
|
|
|
|
if (is_numeric($name)) { |
|
|
|
|
$key = $this->getTagById($name); |
|
|
|
|
} else { |
|
|
|
|
$key = $this->getTagByName($name); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
$stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` WHERE ' |
|
|
|
|
. '`uid` = ? AND `type` = ? AND `category` = ?'); |
|
|
|
|
$result = $stmt->execute(array($this->user, $this->type, $name)); |
|
|
|
|
if (\OCP\DB::isError($result)) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR); |
|
|
|
|
} |
|
|
|
|
} catch(\Exception $e) { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__ . ', exception: ' |
|
|
|
|
. $e->getMessage(), \OCP\Util::ERROR); |
|
|
|
|
return false; |
|
|
|
|
if ($key !== false) { |
|
|
|
|
$tag = $this->tags[$key]; |
|
|
|
|
$id = $tag->getId(); |
|
|
|
|
unset($this->tags[$key]); |
|
|
|
|
$this->mapper->delete($tag); |
|
|
|
|
} else { |
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name |
|
|
|
|
. ': not found.', \OCP\Util::ERROR); |
|
|
|
|
} |
|
|
|
|
if(!is_null($id) && $id !== false) { |
|
|
|
|
try { |
|
|
|
@ -634,19 +691,67 @@ class Tags implements \OCP\ITags { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// case-insensitive in_array |
|
|
|
|
private function in_arrayi($needle, $haystack) { |
|
|
|
|
// case-insensitive array_search |
|
|
|
|
protected function array_searchi($needle, $haystack, $mem='getName') { |
|
|
|
|
if(!is_array($haystack)) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return in_array(strtolower($needle), array_map('strtolower', $haystack)); |
|
|
|
|
return array_search(strtolower($needle), array_map( |
|
|
|
|
function($tag) use($mem) { |
|
|
|
|
return strtolower(call_user_func(array($tag, $mem))); |
|
|
|
|
}, $haystack) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// case-insensitive array_search |
|
|
|
|
private function array_searchi($needle, $haystack) { |
|
|
|
|
if(!is_array($haystack)) { |
|
|
|
|
return false; |
|
|
|
|
/** |
|
|
|
|
* Get a tag's ID. |
|
|
|
|
* |
|
|
|
|
* @param string $name The tag name to look for. |
|
|
|
|
* @return string|bool The tag's id or false if no matching tag is found. |
|
|
|
|
*/ |
|
|
|
|
private function getTagId($name) { |
|
|
|
|
$key = $this->array_searchi($name, $this->tags); |
|
|
|
|
if ($key !== false) { |
|
|
|
|
return $this->tags[$key]->getId(); |
|
|
|
|
} |
|
|
|
|
return array_search(strtolower($needle), array_map('strtolower', $haystack)); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get a tag by its name. |
|
|
|
|
* |
|
|
|
|
* @param string $name The tag name. |
|
|
|
|
* @return integer|bool The tag object's offset within the $this->tags |
|
|
|
|
* array or false if it doesn't exist. |
|
|
|
|
*/ |
|
|
|
|
private function getTagByName($name) { |
|
|
|
|
return $this->array_searchi($name, $this->tags, 'getName'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get a tag by its ID. |
|
|
|
|
* |
|
|
|
|
* @param string $id The tag ID to look for. |
|
|
|
|
* @return integer|bool The tag object's offset within the $this->tags |
|
|
|
|
* array or false if it doesn't exist. |
|
|
|
|
*/ |
|
|
|
|
private function getTagById($id) { |
|
|
|
|
return $this->array_searchi($id, $this->tags, 'getId'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns an array mapping a given tag's properties to its values: |
|
|
|
|
* ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype'] |
|
|
|
|
* |
|
|
|
|
* @param Tag $tag The tag that is going to be mapped |
|
|
|
|
* @return array |
|
|
|
|
*/ |
|
|
|
|
private function tagMap(Tag $tag) { |
|
|
|
|
return array( |
|
|
|
|
'id' => $tag->getId(), |
|
|
|
|
'name' => $tag->getName(), |
|
|
|
|
'owner' => $tag->getOwner(), |
|
|
|
|
'type' => $tag->getType() |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|