2013-09-18 02:37:00 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bernhard Reiter <ockham@raz.or.at>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author derkostka <sebastian.kostka@gmail.com>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for easily tagging objects by their id
|
2013-09-18 02:37:00 +04:00
|
|
|
*
|
2015-02-26 13:37:37 +03:00
|
|
|
* A tag can be e.g. 'Family', 'Work', 'Chore', 'Special Occation' or
|
|
|
|
* anything else that is either parsed from a vobject or that the user chooses
|
|
|
|
* to add.
|
|
|
|
* Tag names are not case-sensitive, but will be saved with the case they
|
|
|
|
* are entered in. If a user already has a tag 'family' for a type, and
|
|
|
|
* tries to add a tag named 'Family' it will be silently ignored.
|
2013-09-18 02:37:00 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-09-18 02:37:00 +04:00
|
|
|
namespace OC;
|
|
|
|
|
2016-01-25 19:17:36 +03:00
|
|
|
use OC\Tagging\Tag;
|
|
|
|
use OC\Tagging\TagMapper;
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2018-04-25 16:22:28 +03:00
|
|
|
use OCP\ILogger;
|
2020-02-11 23:40:01 +03:00
|
|
|
use OCP\ITags;
|
2014-09-08 21:58:43 +04:00
|
|
|
|
2020-02-11 23:40:01 +03:00
|
|
|
class Tags implements ITags {
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tags
|
2013-09-24 19:10:01 +04:00
|
|
|
*
|
|
|
|
* @var array
|
2013-09-18 02:37:00 +04:00
|
|
|
*/
|
2020-03-26 11:30:18 +03:00
|
|
|
private $tags = [];
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for storing objectid/categoryname pairs while rescanning.
|
2013-09-24 19:10:01 +04:00
|
|
|
*
|
|
|
|
* @var array
|
2013-09-18 02:37:00 +04:00
|
|
|
*/
|
2020-03-26 11:30:18 +03:00
|
|
|
private static $relations = [];
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2013-09-24 19:10:01 +04:00
|
|
|
/**
|
|
|
|
* Type
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-10-02 18:08:24 +04:00
|
|
|
private $type;
|
2013-09-24 19:10:01 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-10-02 18:08:24 +04:00
|
|
|
private $user;
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2014-09-16 02:20:52 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-03-26 11:30:18 +03:00
|
|
|
private $owners = [];
|
2014-09-16 02:20:52 +04:00
|
|
|
|
2014-09-08 21:58:43 +04:00
|
|
|
/**
|
|
|
|
* The Mapper we're using to communicate our Tag objects to the database.
|
|
|
|
*
|
|
|
|
* @var TagMapper
|
|
|
|
*/
|
|
|
|
private $mapper;
|
|
|
|
|
2014-09-16 02:20:52 +04:00
|
|
|
/**
|
|
|
|
* The sharing backend for objects of $this->type. Required if
|
|
|
|
* $this->includeShared === true to determine ownership of items.
|
|
|
|
*
|
|
|
|
* @var \OCP\Share_Backend
|
|
|
|
*/
|
|
|
|
private $backend;
|
|
|
|
|
2020-04-10 17:54:27 +03:00
|
|
|
public const TAG_TABLE = '*PREFIX*vcategory';
|
|
|
|
public const RELATION_TABLE = '*PREFIX*vcategory_to_object';
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @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.
|
2020-04-29 21:40:45 +03:00
|
|
|
*
|
|
|
|
* since 20.0.0 $includeShared isn't used anymore
|
2020-04-08 23:24:54 +03:00
|
|
|
*/
|
2020-04-29 21:40:45 +03:00
|
|
|
public function __construct(TagMapper $mapper, $user, $type, $defaultTags = []) {
|
2014-09-08 21:58:43 +04:00
|
|
|
$this->mapper = $mapper;
|
2013-09-18 02:37:00 +04:00
|
|
|
$this->user = $user;
|
2013-09-24 19:10:01 +04:00
|
|
|
$this->type = $type;
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->owners = [$this->user];
|
2014-09-16 02:20:52 +04:00
|
|
|
$this->tags = $this->mapper->loadTags($this->owners, $this->type);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (count($defaultTags) > 0 && count($this->tags) === 0) {
|
2013-09-19 13:27:13 +04:00
|
|
|
$this->addMultiple($defaultTags, true);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Check if any tags are saved for this type and user.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function isEmpty() {
|
2014-03-05 03:26:06 +04:00
|
|
|
return count($this->tags) === 0;
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:30:36 +04:00
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* 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
|
|
|
|
*/
|
2014-10-14 00:30:36 +04:00
|
|
|
public function getTag($id) {
|
|
|
|
$key = $this->getTagById($id);
|
|
|
|
if ($key !== false) {
|
|
|
|
return $this->tagMap($this->tags[$key]);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-18 02:37:00 +04:00
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Get the tags for a specific user.
|
|
|
|
*
|
|
|
|
* This returns an array with maps containing each tag's properties:
|
|
|
|
* [
|
|
|
|
* ['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'],
|
|
|
|
* ['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'],
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-09-19 13:27:13 +04:00
|
|
|
public function getTags() {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!count($this->tags)) {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [];
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
usort($this->tags, function ($a, $b) {
|
2014-09-08 21:58:43 +04:00
|
|
|
return strnatcasecmp($a->getName(), $b->getName());
|
|
|
|
});
|
2020-03-26 11:30:18 +03:00
|
|
|
$tagMap = [];
|
2013-09-18 02:37:00 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($this->tags as $tag) {
|
|
|
|
if ($tag->getName() !== ITags::TAG_FAVORITE) {
|
2014-10-14 00:30:36 +04:00
|
|
|
$tagMap[] = $this->tagMap($tag);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tagMap;
|
|
|
|
}
|
|
|
|
|
2014-09-16 02:20:52 +04:00
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-09-16 02:20:52 +04:00
|
|
|
public function getTagsForUser($user) {
|
|
|
|
return array_filter($this->tags,
|
2020-04-09 14:53:40 +03:00
|
|
|
function ($tag) use ($user) {
|
2014-09-16 02:20:52 +04:00
|
|
|
return $tag->getOwner() === $user;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-11-24 23:51:45 +03:00
|
|
|
/**
|
|
|
|
* Get the list of tags for the given ids.
|
|
|
|
*
|
|
|
|
* @param array $objIds array of object ids
|
|
|
|
* @return array|boolean of tags id as key to array of tag names
|
|
|
|
* or false if an error occurred
|
|
|
|
*/
|
|
|
|
public function getTagsForObjects(array $objIds) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$entries = [];
|
2014-11-24 23:51:45 +03:00
|
|
|
|
|
|
|
try {
|
2016-01-07 12:26:00 +03:00
|
|
|
$conn = \OC::$server->getDatabaseConnection();
|
2015-01-19 19:05:44 +03:00
|
|
|
$chunks = array_chunk($objIds, 900, false);
|
2014-11-24 23:51:45 +03:00
|
|
|
foreach ($chunks as $chunk) {
|
|
|
|
$result = $conn->executeQuery(
|
|
|
|
'SELECT `category`, `categoryid`, `objid` ' .
|
|
|
|
'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' .
|
|
|
|
'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)',
|
2020-03-26 11:30:18 +03:00
|
|
|
[$this->user, $this->type, $chunk],
|
|
|
|
[null, null, IQueryBuilder::PARAM_INT_ARRAY]
|
2014-11-24 23:51:45 +03:00
|
|
|
);
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$objId = (int)$row['objid'];
|
|
|
|
if (!isset($entries[$objId])) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$entries[$objId] = [];
|
2014-11-24 23:51:45 +03:00
|
|
|
}
|
2015-04-18 17:17:15 +03:00
|
|
|
$entries[$objId][] = $row['category'];
|
2014-11-24 23:51:45 +03:00
|
|
|
}
|
2018-03-12 17:13:37 +03:00
|
|
|
if ($result === null) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR);
|
2014-12-19 14:09:46 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-24 23:51:45 +03:00
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2014-11-24 23:51:45 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entries;
|
|
|
|
}
|
|
|
|
|
2013-09-18 02:37:00 +04:00
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Get the a list if items tagged with $tag.
|
|
|
|
*
|
|
|
|
* Throws an exception if the tag could not be found.
|
|
|
|
*
|
|
|
|
* @param string $tag Tag id or name.
|
|
|
|
* @return array|false An array of object ids or false on error.
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2013-09-19 13:27:13 +04:00
|
|
|
public function getIdsForTag($tag) {
|
2013-09-18 02:37:00 +04:00
|
|
|
$result = null;
|
2015-05-04 15:46:18 +03:00
|
|
|
$tagId = false;
|
2020-04-10 15:19:56 +03:00
|
|
|
if (is_numeric($tag)) {
|
2013-09-18 02:37:00 +04:00
|
|
|
$tagId = $tag;
|
2020-04-10 15:19:56 +03:00
|
|
|
} elseif (is_string($tag)) {
|
2013-09-18 02:37:00 +04:00
|
|
|
$tag = trim($tag);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($tag === '') {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', ILogger::DEBUG);
|
2014-03-05 03:26:06 +04:00
|
|
|
return false;
|
|
|
|
}
|
2014-09-08 21:58:43 +04:00
|
|
|
$tagId = $this->getTagId($tag);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($tagId === false) {
|
2014-08-31 12:05:59 +04:00
|
|
|
$l10n = \OC::$server->getL10N('core');
|
2013-09-18 02:37:00 +04:00
|
|
|
throw new \Exception(
|
2017-07-24 12:36:20 +03:00
|
|
|
$l10n->t('Could not find category "%s"', [$tag])
|
2013-09-18 02:37:00 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$ids = [];
|
2013-09-18 02:37:00 +04:00
|
|
|
$sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
|
|
|
|
. '` WHERE `categoryid` = ?';
|
|
|
|
|
|
|
|
try {
|
2018-03-26 14:07:12 +03:00
|
|
|
$stmt = \OC_DB::prepare($sql);
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = $stmt->execute([$tagId]);
|
2018-03-12 17:13:37 +03:00
|
|
|
if ($result === null) {
|
2020-11-05 12:50:53 +03:00
|
|
|
$stmt->closeCursor();
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($result)) {
|
|
|
|
while ($row = $result->fetchRow()) {
|
2020-04-29 21:40:45 +03:00
|
|
|
$ids[] = (int)$row['objid'];
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
2020-11-05 12:50:53 +03:00
|
|
|
$result->closeCursor();
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* 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
|
|
|
|
*/
|
2014-09-16 02:20:52 +04:00
|
|
|
public function userHasTag($name, $user) {
|
|
|
|
$key = $this->array_searchi($name, $this->getTagsForUser($user));
|
|
|
|
return ($key !== false) ? $this->tags[$key]->getId() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Checks whether a tag is saved for or shared with the current user.
|
|
|
|
*
|
|
|
|
* @param string $name The tag name to check for.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function hasTag($name) {
|
2014-09-08 21:58:43 +04:00
|
|
|
return $this->getTagId($name) !== false;
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Add a new tag.
|
|
|
|
*
|
|
|
|
* @param string $name A string with a name of the tag
|
|
|
|
* @return false|int the id of the added tag or false on error.
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function add($name) {
|
|
|
|
$name = trim($name);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($name === '') {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', ILogger::DEBUG);
|
2014-03-05 03:26:06 +04:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->userHasTag($name, $this->user)) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', ILogger::DEBUG);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
2014-09-08 21:58:43 +04:00
|
|
|
$tag = new Tag($this->user, $this->type, $name);
|
|
|
|
$tag = $this->mapper->insert($tag);
|
|
|
|
$this->tags[] = $tag;
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-23 15:27:43 +04:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), ILogger::DEBUG);
|
2014-09-08 21:58:43 +04:00
|
|
|
return $tag->getId();
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Rename tag.
|
|
|
|
*
|
|
|
|
* @param string|integer $from The name or ID of the existing tag
|
|
|
|
* @param string $to The new name of the tag.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function rename($from, $to) {
|
|
|
|
$from = trim($from);
|
|
|
|
$to = trim($to);
|
2014-03-05 03:26:06 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($to === '' || $from === '') {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', ILogger::DEBUG);
|
2014-03-05 03:26:06 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-16 02:20:52 +04:00
|
|
|
if (is_numeric($from)) {
|
|
|
|
$key = $this->getTagById($from);
|
|
|
|
} else {
|
|
|
|
$key = $this->getTagByName($from);
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($key === false) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', ILogger::DEBUG);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-03 21:32:39 +04:00
|
|
|
$tag = $this->tags[$key];
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->userHasTag($to, $tag->getOwner())) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', ILogger::DEBUG);
|
2014-10-03 21:32:39 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
try {
|
2014-09-08 21:58:43 +04:00
|
|
|
$tag->setName($to);
|
|
|
|
$this->tags[$key] = $this->mapper->update($tag);
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* 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 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.
|
|
|
|
*/
|
2020-10-05 16:12:57 +03:00
|
|
|
public function addMultiple($names, $sync = false, $id = null) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_array($names)) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$names = [$names];
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
$names = array_map('trim', $names);
|
2014-03-05 03:26:06 +04:00
|
|
|
array_filter($names);
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$newones = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($names as $name) {
|
|
|
|
if (!$this->hasTag($name) && $name !== '') {
|
2014-09-08 21:58:43 +04:00
|
|
|
$newones[] = new Tag($this->user, $this->type, $name);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($id)) {
|
2013-09-18 02:37:00 +04:00
|
|
|
// Insert $objectid, $categoryid pairs if not exist.
|
2020-03-26 11:30:18 +03:00
|
|
|
self::$relations[] = ['objid' => $id, 'tag' => $name];
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->tags = array_merge($this->tags, $newones);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($sync === true) {
|
2013-09-18 02:37:00 +04:00
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the list of tags and their object relations
|
|
|
|
*/
|
|
|
|
protected function save() {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (is_array($this->tags)) {
|
|
|
|
foreach ($this->tags as $tag) {
|
2013-09-18 02:37:00 +04:00
|
|
|
try {
|
2014-09-08 21:58:43 +04:00
|
|
|
if (!$this->mapper->tagExists($tag)) {
|
|
|
|
$this->mapper->insert($tag);
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
2014-09-30 14:19:08 +04:00
|
|
|
|
2013-09-18 02:37:00 +04:00
|
|
|
// reload tags to get the proper ids.
|
2014-10-11 01:18:43 +04:00
|
|
|
$this->tags = $this->mapper->loadTags($this->owners, $this->type);
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true),
|
2018-04-25 16:22:28 +03:00
|
|
|
ILogger::DEBUG);
|
2013-09-18 02:37:00 +04:00
|
|
|
// 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);
|
2018-03-12 17:09:55 +03:00
|
|
|
$dbConnection = \OC::$server->getDatabaseConnection();
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach (self::$relations as $relation) {
|
2014-09-08 21:58:43 +04:00
|
|
|
$tagId = $this->getTagId($relation['tag']);
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, ILogger::DEBUG);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($tagId) {
|
2013-09-18 02:37:00 +04:00
|
|
|
try {
|
2018-03-12 17:09:55 +03:00
|
|
|
$dbConnection->insertIfNotExist(self::RELATION_TABLE,
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2013-09-18 02:37:00 +04:00
|
|
|
'objid' => $relation['objid'],
|
|
|
|
'categoryid' => $tagId,
|
|
|
|
'type' => $this->type,
|
2020-04-09 10:22:29 +03:00
|
|
|
]);
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 11:30:18 +03:00
|
|
|
self::$relations = []; // reset
|
2013-09-18 02:37:00 +04:00
|
|
|
} else {
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! '
|
2018-04-25 16:22:28 +03:00
|
|
|
. print_r($this->tags, true), ILogger::ERROR);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Delete tags and tag/object relations for a user.
|
|
|
|
*
|
|
|
|
* For hooking up on post_deleteUser
|
|
|
|
*
|
|
|
|
* @param array $arguments
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public static function post_deleteUser($arguments) {
|
|
|
|
// Find all objectid/tagId pairs.
|
|
|
|
$result = null;
|
|
|
|
try {
|
2018-03-26 14:07:12 +03:00
|
|
|
$stmt = \OC_DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` '
|
2013-09-18 02:37:00 +04:00
|
|
|
. 'WHERE `uid` = ?');
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = $stmt->execute([$arguments['uid']]);
|
2018-03-12 17:13:37 +03:00
|
|
|
if ($result === null) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($result)) {
|
2013-09-18 02:37:00 +04:00
|
|
|
try {
|
2018-03-26 14:07:12 +03:00
|
|
|
$stmt = \OC_DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
|
2013-09-18 02:37:00 +04:00
|
|
|
. 'WHERE `categoryid` = ?');
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($row = $result->fetchRow()) {
|
2013-09-18 02:37:00 +04:00
|
|
|
try {
|
2020-03-26 11:30:18 +03:00
|
|
|
$stmt->execute([$row['id']]);
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
2020-11-05 12:50:53 +03:00
|
|
|
$result->closeCursor();
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
2018-03-26 14:07:12 +03:00
|
|
|
$stmt = \OC_DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` '
|
2013-09-18 02:37:00 +04:00
|
|
|
. 'WHERE `uid` = ?');
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = $stmt->execute([$arguments['uid']]);
|
2018-03-12 17:13:37 +03:00
|
|
|
if ($result === null) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Delete tag/object relations from the db
|
|
|
|
*
|
|
|
|
* @param array $ids The ids of the objects
|
|
|
|
* @return boolean Returns false on error.
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function purgeObjects(array $ids) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (count($ids) === 0) {
|
2013-09-18 02:37:00 +04:00
|
|
|
// job done ;)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$updates = $ids;
|
|
|
|
try {
|
|
|
|
$query = 'DELETE FROM `' . self::RELATION_TABLE . '` ';
|
2020-10-05 16:12:57 +03:00
|
|
|
$query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids) - 1) . '?) ';
|
2013-09-18 02:37:00 +04:00
|
|
|
$query .= 'AND `type`= ?';
|
|
|
|
$updates[] = $this->type;
|
2018-03-26 14:07:12 +03:00
|
|
|
$stmt = \OC_DB::prepare($query);
|
2013-09-18 02:37:00 +04:00
|
|
|
$result = $stmt->execute($updates);
|
2018-03-12 17:13:37 +03:00
|
|
|
if ($result === null) {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(), ILogger::ERROR);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Get favorites for an object type
|
|
|
|
*
|
|
|
|
* @return array|false An array of object ids.
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function getFavorites() {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$this->userHasTag(ITags::TAG_FAVORITE, $this->user)) {
|
2018-07-26 16:33:26 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2013-09-18 02:37:00 +04:00
|
|
|
try {
|
2020-02-11 23:40:01 +03:00
|
|
|
return $this->getIdsForTag(ITags::TAG_FAVORITE);
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2020-03-26 11:30:18 +03:00
|
|
|
return [];
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Add an object to favorites
|
|
|
|
*
|
|
|
|
* @param int $objid The id of the object
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function addToFavorites($objid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$this->userHasTag(ITags::TAG_FAVORITE, $this->user)) {
|
2020-02-11 23:40:01 +03:00
|
|
|
$this->add(ITags::TAG_FAVORITE);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
2020-02-11 23:40:01 +03:00
|
|
|
return $this->tagAs($objid, ITags::TAG_FAVORITE);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Remove an object from favorites
|
|
|
|
*
|
|
|
|
* @param int $objid The id of the object
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function removeFromFavorites($objid) {
|
2020-02-11 23:40:01 +03:00
|
|
|
return $this->unTag($objid, ITags::TAG_FAVORITE);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Creates a tag/object relation.
|
|
|
|
*
|
|
|
|
* @param int $objid The id of the object
|
|
|
|
* @param string $tag The id or name of the tag
|
|
|
|
* @return boolean Returns false on error.
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function tagAs($objid, $tag) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (is_string($tag) && !is_numeric($tag)) {
|
2013-09-18 02:37:00 +04:00
|
|
|
$tag = trim($tag);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($tag === '') {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', ILogger::DEBUG);
|
2014-03-05 03:26:06 +04:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$this->hasTag($tag)) {
|
2014-03-05 03:26:06 +04:00
|
|
|
$this->add($tag);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
2020-10-05 16:12:57 +03:00
|
|
|
$tagId = $this->getTagId($tag);
|
2013-09-18 02:37:00 +04:00
|
|
|
} else {
|
|
|
|
$tagId = $tag;
|
|
|
|
}
|
|
|
|
try {
|
2018-03-12 17:09:55 +03:00
|
|
|
\OC::$server->getDatabaseConnection()->insertIfNotExist(self::RELATION_TABLE,
|
2020-03-26 11:30:18 +03:00
|
|
|
[
|
2013-09-18 02:37:00 +04:00
|
|
|
'objid' => $objid,
|
|
|
|
'categoryid' => $tagId,
|
|
|
|
'type' => $this->type,
|
2020-03-26 11:30:18 +03:00
|
|
|
]);
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Delete single tag/object relation from the db
|
|
|
|
*
|
|
|
|
* @param int $objid The id of the object
|
|
|
|
* @param string $tag The id or name of the tag
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function unTag($objid, $tag) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (is_string($tag) && !is_numeric($tag)) {
|
2013-09-18 02:37:00 +04:00
|
|
|
$tag = trim($tag);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($tag === '') {
|
2018-04-25 16:22:28 +03:00
|
|
|
\OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', ILogger::DEBUG);
|
2014-03-05 03:26:06 +04:00
|
|
|
return false;
|
|
|
|
}
|
2020-10-05 16:12:57 +03:00
|
|
|
$tagId = $this->getTagId($tag);
|
2013-09-18 02:37:00 +04:00
|
|
|
} else {
|
|
|
|
$tagId = $tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
|
|
|
|
. 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
|
2018-03-26 14:07:12 +03:00
|
|
|
$stmt = \OC_DB::prepare($sql);
|
2020-03-26 11:30:18 +03:00
|
|
|
$stmt->execute([$objid, $tagId, $this->type]);
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* Delete tags from the database.
|
|
|
|
*
|
|
|
|
* @param string[]|integer[] $names An array of tags (names or IDs) to delete
|
|
|
|
* @return bool Returns false on error
|
|
|
|
*/
|
2013-09-18 02:37:00 +04:00
|
|
|
public function delete($names) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_array($names)) {
|
2020-03-26 11:30:18 +03:00
|
|
|
$names = [$names];
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$names = array_map('trim', $names);
|
2014-03-05 03:26:06 +04:00
|
|
|
array_filter($names);
|
2013-09-18 02:37:00 +04:00
|
|
|
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__ . ', before: '
|
2018-04-25 16:22:28 +03:00
|
|
|
. print_r($this->tags, true), ILogger::DEBUG);
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($names as $name) {
|
2013-09-18 02:37:00 +04:00
|
|
|
$id = null;
|
|
|
|
|
2014-09-16 02:20:52 +04:00
|
|
|
if (is_numeric($name)) {
|
|
|
|
$key = $this->getTagById($name);
|
|
|
|
} else {
|
|
|
|
$key = $this->getTagByName($name);
|
|
|
|
}
|
|
|
|
if ($key !== false) {
|
2014-09-08 21:58:43 +04:00
|
|
|
$tag = $this->tags[$key];
|
|
|
|
$id = $tag->getId();
|
|
|
|
unset($this->tags[$key]);
|
2014-10-04 16:08:20 +04:00
|
|
|
$this->mapper->delete($tag);
|
|
|
|
} else {
|
|
|
|
\OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name
|
2018-04-25 16:22:28 +03:00
|
|
|
. ': not found.', ILogger::ERROR);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($id) && $id !== false) {
|
2013-09-18 02:37:00 +04:00
|
|
|
try {
|
|
|
|
$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
|
|
|
|
. 'WHERE `categoryid` = ?';
|
2018-03-26 14:07:12 +03:00
|
|
|
$stmt = \OC_DB::prepare($sql);
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = $stmt->execute([$id]);
|
2018-03-12 17:13:37 +03:00
|
|
|
if ($result === null) {
|
2013-09-18 02:37:00 +04:00
|
|
|
\OCP\Util::writeLog('core',
|
2018-03-21 14:17:55 +03:00
|
|
|
__METHOD__. 'DB error: ' . \OC::$server->getDatabaseConnection()->getError(),
|
2018-04-25 16:22:28 +03:00
|
|
|
ILogger::ERROR);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => __METHOD__,
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-08 21:58:43 +04:00
|
|
|
// case-insensitive array_search
|
2020-10-05 16:12:57 +03:00
|
|
|
protected function array_searchi($needle, $haystack, $mem = 'getName') {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_array($haystack)) {
|
2013-09-18 02:37:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
2014-09-08 21:58:43 +04:00
|
|
|
return array_search(strtolower($needle), array_map(
|
2020-04-09 14:53:40 +03:00
|
|
|
function ($tag) use ($mem) {
|
2020-03-26 11:30:18 +03:00
|
|
|
return strtolower(call_user_func([$tag, $mem]));
|
2014-09-08 21:58:43 +04:00
|
|
|
}, $haystack)
|
|
|
|
);
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
|
|
|
|
2014-09-08 21:58:43 +04:00
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-09-08 21:58:43 +04:00
|
|
|
private function getTagId($name) {
|
2014-09-16 02:20:52 +04:00
|
|
|
$key = $this->array_searchi($name, $this->tags);
|
|
|
|
if ($key !== false) {
|
|
|
|
return $this->tags[$key]->getId();
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
2014-09-16 02:20:52 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-09-16 02:20:52 +04:00
|
|
|
private function getTagByName($name) {
|
|
|
|
return $this->array_searchi($name, $this->tags, 'getName');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-09-16 02:20:52 +04:00
|
|
|
private function getTagById($id) {
|
|
|
|
return $this->array_searchi($id, $this->tags, 'getId');
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|
2014-10-14 00:30:36 +04:00
|
|
|
|
|
|
|
/**
|
2020-04-08 23:24:54 +03:00
|
|
|
* 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
|
|
|
|
*/
|
2014-10-14 00:30:36 +04:00
|
|
|
private function tagMap(Tag $tag) {
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
2020-10-05 16:12:57 +03:00
|
|
|
'id' => $tag->getId(),
|
|
|
|
'name' => $tag->getName(),
|
2014-10-14 00:30:36 +04:00
|
|
|
'owner' => $tag->getOwner(),
|
2020-10-05 16:12:57 +03:00
|
|
|
'type' => $tag->getType()
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
2014-10-14 00:30:36 +04:00
|
|
|
}
|
2013-09-18 02:37:00 +04:00
|
|
|
}
|