aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/systemtag
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/systemtag')
-rw-r--r--lib/private/systemtag/managerfactory.php80
-rw-r--r--lib/private/systemtag/systemtag.php91
-rw-r--r--lib/private/systemtag/systemtagmanager.php322
-rw-r--r--lib/private/systemtag/systemtagobjectmapper.php262
4 files changed, 0 insertions, 755 deletions
diff --git a/lib/private/systemtag/managerfactory.php b/lib/private/systemtag/managerfactory.php
deleted file mode 100644
index d9acf327f8a..00000000000
--- a/lib/private/systemtag/managerfactory.php
+++ /dev/null
@@ -1,80 +0,0 @@
-<?php
-/**
- * @author Joas Schilling <nickvergessen@owncloud.com>
- * @author Vincent Petry <pvince81@owncloud.com>
- *
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- * @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,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
-namespace OC\SystemTag;
-
-use OCP\IServerContainer;
-use OCP\SystemTag\ISystemTagManager;
-use OCP\SystemTag\ISystemTagManagerFactory;
-use OCP\SystemTag\ISystemTagObjectMapper;
-
-/**
- * Default factory class for system tag managers
- *
- * @package OCP\SystemTag
- * @since 9.0.0
- */
-class ManagerFactory implements ISystemTagManagerFactory {
-
- /**
- * Server container
- *
- * @var IServerContainer
- */
- private $serverContainer;
-
- /**
- * Constructor for the system tag manager factory
- *
- * @param IServerContainer $serverContainer server container
- */
- public function __construct(IServerContainer $serverContainer) {
- $this->serverContainer = $serverContainer;
- }
-
- /**
- * Creates and returns an instance of the system tag manager
- *
- * @return ISystemTagManager
- * @since 9.0.0
- */
- public function getManager() {
- return new SystemTagManager(
- $this->serverContainer->getDatabaseConnection(),
- $this->serverContainer->getEventDispatcher()
- );
- }
-
- /**
- * Creates and returns an instance of the system tag object
- * mapper
- *
- * @return ISystemTagObjectMapper
- * @since 9.0.0
- */
- public function getObjectMapper() {
- return new SystemTagObjectMapper(
- $this->serverContainer->getDatabaseConnection(),
- $this->getManager(),
- $this->serverContainer->getEventDispatcher()
- );
- }
-}
diff --git a/lib/private/systemtag/systemtag.php b/lib/private/systemtag/systemtag.php
deleted file mode 100644
index 559b6fdefa8..00000000000
--- a/lib/private/systemtag/systemtag.php
+++ /dev/null
@@ -1,91 +0,0 @@
-<?php
-/**
- * @author Joas Schilling <nickvergessen@owncloud.com>
- * @author Vincent Petry <pvince81@owncloud.com>
- *
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- * @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,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
-
-namespace OC\SystemTag;
-
-use OCP\SystemTag\ISystemTag;
-
-class SystemTag implements ISystemTag {
-
- /**
- * @var string
- */
- private $id;
-
- /**
- * @var string
- */
- private $name;
-
- /**
- * @var bool
- */
- private $userVisible;
-
- /**
- * @var bool
- */
- private $userAssignable;
-
- /**
- * Constructor.
- *
- * @param string $id tag id
- * @param string $name tag name
- * @param bool $userVisible whether the tag is user visible
- * @param bool $userAssignable whether the tag is user assignable
- */
- public function __construct($id, $name, $userVisible, $userAssignable) {
- $this->id = $id;
- $this->name = $name;
- $this->userVisible = $userVisible;
- $this->userAssignable = $userAssignable;
- }
-
- /**
- * {@inheritdoc}
- */
- public function getId() {
- return $this->id;
- }
-
- /**
- * {@inheritdoc}
- */
- public function getName() {
- return $this->name;
- }
-
- /**
- * {@inheritdoc}
- */
- public function isUserVisible() {
- return $this->userVisible;
- }
-
- /**
- * {@inheritdoc}
- */
- public function isUserAssignable() {
- return $this->userAssignable;
- }
-}
diff --git a/lib/private/systemtag/systemtagmanager.php b/lib/private/systemtag/systemtagmanager.php
deleted file mode 100644
index 76a60a91328..00000000000
--- a/lib/private/systemtag/systemtagmanager.php
+++ /dev/null
@@ -1,322 +0,0 @@
-<?php
-/**
- * @author Joas Schilling <nickvergessen@owncloud.com>
- * @author Vincent Petry <pvince81@owncloud.com>
- *
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- * @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,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
-
-namespace OC\SystemTag;
-
-use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
-use OCP\DB\QueryBuilder\IQueryBuilder;
-use OCP\IDBConnection;
-use OCP\SystemTag\ISystemTagManager;
-use OCP\SystemTag\ManagerEvent;
-use OCP\SystemTag\TagAlreadyExistsException;
-use OCP\SystemTag\TagNotFoundException;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-
-class SystemTagManager implements ISystemTagManager {
-
- const TAG_TABLE = 'systemtag';
-
- /** @var IDBConnection */
- protected $connection;
-
- /** @var EventDispatcherInterface */
- protected $dispatcher;
-
- /**
- * Prepared query for selecting tags directly
- *
- * @var \OCP\DB\QueryBuilder\IQueryBuilder
- */
- private $selectTagQuery;
-
- /**
- * Constructor.
- *
- * @param IDBConnection $connection database connection
- * @param EventDispatcherInterface $dispatcher
- */
- public function __construct(IDBConnection $connection, EventDispatcherInterface $dispatcher) {
- $this->connection = $connection;
- $this->dispatcher = $dispatcher;
-
- $query = $this->connection->getQueryBuilder();
- $this->selectTagQuery = $query->select('*')
- ->from(self::TAG_TABLE)
- ->where($query->expr()->eq('name', $query->createParameter('name')))
- ->andWhere($query->expr()->eq('visibility', $query->createParameter('visibility')))
- ->andWhere($query->expr()->eq('editable', $query->createParameter('editable')));
- }
-
- /**
- * {@inheritdoc}
- */
- public function getTagsByIds($tagIds) {
- if (!is_array($tagIds)) {
- $tagIds = [$tagIds];
- }
-
- $tags = [];
-
- // note: not all databases will fail if it's a string or starts with a number
- foreach ($tagIds as $tagId) {
- if (!is_numeric($tagId)) {
- throw new \InvalidArgumentException('Tag id must be integer');
- }
- }
-
- $query = $this->connection->getQueryBuilder();
- $query->select('*')
- ->from(self::TAG_TABLE)
- ->where($query->expr()->in('id', $query->createParameter('tagids')))
- ->addOrderBy('name', 'ASC')
- ->addOrderBy('visibility', 'ASC')
- ->addOrderBy('editable', 'ASC')
- ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY);
-
- $result = $query->execute();
- while ($row = $result->fetch()) {
- $tags[$row['id']] = $this->createSystemTagFromRow($row);
- }
-
- $result->closeCursor();
-
- if (count($tags) !== count($tagIds)) {
- throw new TagNotFoundException(
- 'Tag id(s) not found', 0, null, array_diff($tagIds, array_keys($tags))
- );
- }
-
- return $tags;
- }
-
- /**
- * {@inheritdoc}
- */
- public function getAllTags($visibilityFilter = null, $nameSearchPattern = null) {
- $tags = [];
-
- $query = $this->connection->getQueryBuilder();
- $query->select('*')
- ->from(self::TAG_TABLE);
-
- if (!is_null($visibilityFilter)) {
- $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter)));
- }
-
- if (!empty($nameSearchPattern)) {
- $query->andWhere(
- $query->expr()->like(
- 'name',
- $query->expr()->literal('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%')
- )
- );
- }
-
- $query
- ->addOrderBy('name', 'ASC')
- ->addOrderBy('visibility', 'ASC')
- ->addOrderBy('editable', 'ASC');
-
- $result = $query->execute();
- while ($row = $result->fetch()) {
- $tags[$row['id']] = $this->createSystemTagFromRow($row);
- }
-
- $result->closeCursor();
-
- return $tags;
- }
-
- /**
- * {@inheritdoc}
- */
- public function getTag($tagName, $userVisible, $userAssignable) {
- $userVisible = (int)$userVisible;
- $userAssignable = (int)$userAssignable;
-
- $result = $this->selectTagQuery
- ->setParameter('name', $tagName)
- ->setParameter('visibility', $userVisible)
- ->setParameter('editable', $userAssignable)
- ->execute();
-
- $row = $result->fetch();
- $result->closeCursor();
- if (!$row) {
- throw new TagNotFoundException(
- 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist'
- );
- }
-
- return $this->createSystemTagFromRow($row);
- }
-
- /**
- * {@inheritdoc}
- */
- public function createTag($tagName, $userVisible, $userAssignable) {
- $userVisible = (int)$userVisible;
- $userAssignable = (int)$userAssignable;
-
- $query = $this->connection->getQueryBuilder();
- $query->insert(self::TAG_TABLE)
- ->values([
- 'name' => $query->createNamedParameter($tagName),
- 'visibility' => $query->createNamedParameter($userVisible),
- 'editable' => $query->createNamedParameter($userAssignable),
- ]);
-
- try {
- $query->execute();
- } catch (UniqueConstraintViolationException $e) {
- throw new TagAlreadyExistsException(
- 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
- 0,
- $e
- );
- }
-
- $tagId = $query->getLastInsertId();
-
- $tag = new SystemTag(
- (int)$tagId,
- $tagName,
- (bool)$userVisible,
- (bool)$userAssignable
- );
-
- $this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent(
- ManagerEvent::EVENT_CREATE, $tag
- ));
-
- return $tag;
- }
-
- /**
- * {@inheritdoc}
- */
- public function updateTag($tagId, $tagName, $userVisible, $userAssignable) {
- $userVisible = (int)$userVisible;
- $userAssignable = (int)$userAssignable;
-
- try {
- $tags = $this->getTagsByIds($tagId);
- } catch (TagNotFoundException $e) {
- throw new TagNotFoundException(
- 'Tag does not exist', 0, null, [$tagId]
- );
- }
-
- $beforeUpdate = array_shift($tags);
- $afterUpdate = new SystemTag(
- (int) $tagId,
- $tagName,
- (bool) $userVisible,
- (bool) $userAssignable
- );
-
- $query = $this->connection->getQueryBuilder();
- $query->update(self::TAG_TABLE)
- ->set('name', $query->createParameter('name'))
- ->set('visibility', $query->createParameter('visibility'))
- ->set('editable', $query->createParameter('editable'))
- ->where($query->expr()->eq('id', $query->createParameter('tagid')))
- ->setParameter('name', $tagName)
- ->setParameter('visibility', $userVisible)
- ->setParameter('editable', $userAssignable)
- ->setParameter('tagid', $tagId);
-
- try {
- if ($query->execute() === 0) {
- throw new TagNotFoundException(
- 'Tag does not exist', 0, null, [$tagId]
- );
- }
- } catch (UniqueConstraintViolationException $e) {
- throw new TagAlreadyExistsException(
- 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
- 0,
- $e
- );
- }
-
- $this->dispatcher->dispatch(ManagerEvent::EVENT_UPDATE, new ManagerEvent(
- ManagerEvent::EVENT_UPDATE, $afterUpdate, $beforeUpdate
- ));
- }
-
- /**
- * {@inheritdoc}
- */
- public function deleteTags($tagIds) {
- if (!is_array($tagIds)) {
- $tagIds = [$tagIds];
- }
-
- $tagNotFoundException = null;
- $tags = [];
- try {
- $tags = $this->getTagsByIds($tagIds);
- } catch (TagNotFoundException $e) {
- $tagNotFoundException = $e;
-
- // Get existing tag objects for the hooks later
- $existingTags = array_diff($tagIds, $tagNotFoundException->getMissingTags());
- if (!empty($existingTags)) {
- try {
- $tags = $this->getTagsByIds($existingTags);
- } catch (TagNotFoundException $e) {
- // Ignore further errors...
- }
- }
- }
-
- // delete relationships first
- $query = $this->connection->getQueryBuilder();
- $query->delete(SystemTagObjectMapper::RELATION_TABLE)
- ->where($query->expr()->in('systemtagid', $query->createParameter('tagids')))
- ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
- ->execute();
-
- $query = $this->connection->getQueryBuilder();
- $query->delete(self::TAG_TABLE)
- ->where($query->expr()->in('id', $query->createParameter('tagids')))
- ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
- ->execute();
-
- foreach ($tags as $tag) {
- $this->dispatcher->dispatch(ManagerEvent::EVENT_DELETE, new ManagerEvent(
- ManagerEvent::EVENT_DELETE, $tag
- ));
- }
-
- if ($tagNotFoundException !== null) {
- throw new TagNotFoundException(
- 'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags()
- );
- }
- }
-
- private function createSystemTagFromRow($row) {
- return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']);
- }
-}
diff --git a/lib/private/systemtag/systemtagobjectmapper.php b/lib/private/systemtag/systemtagobjectmapper.php
deleted file mode 100644
index 412842b69e3..00000000000
--- a/lib/private/systemtag/systemtagobjectmapper.php
+++ /dev/null
@@ -1,262 +0,0 @@
-<?php
-/**
- * @author Joas Schilling <nickvergessen@owncloud.com>
- * @author Vincent Petry <pvince81@owncloud.com>
- *
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- * @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,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
-
-namespace OC\SystemTag;
-
-use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
-use OCP\DB\QueryBuilder\IQueryBuilder;
-use OCP\IDBConnection;
-use OCP\SystemTag\ISystemTag;
-use OCP\SystemTag\ISystemTagManager;
-use OCP\SystemTag\ISystemTagObjectMapper;
-use OCP\SystemTag\MapperEvent;
-use OCP\SystemTag\TagNotFoundException;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-
-class SystemTagObjectMapper implements ISystemTagObjectMapper {
-
- const RELATION_TABLE = 'systemtag_object_mapping';
-
- /** @var ISystemTagManager */
- protected $tagManager;
-
- /** @var IDBConnection */
- protected $connection;
-
- /** @var EventDispatcherInterface */
- protected $dispatcher;
-
- /**
- * Constructor.
- *
- * @param IDBConnection $connection database connection
- * @param ISystemTagManager $tagManager system tag manager
- * @param EventDispatcherInterface $dispatcher
- */
- public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) {
- $this->connection = $connection;
- $this->tagManager = $tagManager;
- $this->dispatcher = $dispatcher;
- }
-
- /**
- * {@inheritdoc}
- */
- public function getTagIdsForObjects($objIds, $objectType) {
- if (!is_array($objIds)) {
- $objIds = [$objIds];
- } else if (empty($objIds)) {
- return [];
- }
-
- $query = $this->connection->getQueryBuilder();
- $query->select(['systemtagid', 'objectid'])
- ->from(self::RELATION_TABLE)
- ->where($query->expr()->in('objectid', $query->createParameter('objectids')))
- ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
- ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_INT_ARRAY)
- ->setParameter('objecttype', $objectType)
- ->addOrderBy('objectid', 'ASC')
- ->addOrderBy('systemtagid', 'ASC');
-
- $mapping = [];
- foreach ($objIds as $objId) {
- $mapping[$objId] = [];
- }
-
- $result = $query->execute();
- while ($row = $result->fetch()) {
- $objectId = $row['objectid'];
- $mapping[$objectId][] = $row['systemtagid'];
- }
-
- $result->closeCursor();
-
- return $mapping;
- }
-
- /**
- * {@inheritdoc}
- */
- public function getObjectIdsForTags($tagIds, $objectType, $limit = 0, $offset = '') {
- if (!is_array($tagIds)) {
- $tagIds = [$tagIds];
- }
-
- $this->assertTagsExist($tagIds);
-
- $query = $this->connection->getQueryBuilder();
- $query->selectDistinct('objectid')
- ->from(self::RELATION_TABLE)
- ->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY)))
- ->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType)));
-
- if ($limit) {
- if (sizeof($tagIds) !== 1) {
- throw new \InvalidArgumentException('Limit is only allowed with a single tag');
- }
-
- $query->setMaxResults($limit)
- ->orderBy('objectid', 'ASC');
-
- if ($offset !== '') {
- $query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset)));
- }
- }
-
- $objectIds = [];
-
- $result = $query->execute();
- while ($row = $result->fetch()) {
- $objectIds[] = $row['objectid'];
- }
-
- return $objectIds;
- }
-
- /**
- * {@inheritdoc}
- */
- public function assignTags($objId, $objectType, $tagIds) {
- if (!is_array($tagIds)) {
- $tagIds = [$tagIds];
- }
-
- $this->assertTagsExist($tagIds);
-
- $query = $this->connection->getQueryBuilder();
- $query->insert(self::RELATION_TABLE)
- ->values([
- 'objectid' => $query->createNamedParameter($objId),
- 'objecttype' => $query->createNamedParameter($objectType),
- 'systemtagid' => $query->createParameter('tagid'),
- ]);
-
- foreach ($tagIds as $tagId) {
- try {
- $query->setParameter('tagid', $tagId);
- $query->execute();
- } catch (UniqueConstraintViolationException $e) {
- // ignore existing relations
- }
- }
-
- $this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent(
- MapperEvent::EVENT_ASSIGN,
- $objectType,
- $objId,
- $tagIds
- ));
- }
-
- /**
- * {@inheritdoc}
- */
- public function unassignTags($objId, $objectType, $tagIds) {
- if (!is_array($tagIds)) {
- $tagIds = [$tagIds];
- }
-
- $this->assertTagsExist($tagIds);
-
- $query = $this->connection->getQueryBuilder();
- $query->delete(self::RELATION_TABLE)
- ->where($query->expr()->eq('objectid', $query->createParameter('objectid')))
- ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
- ->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids')))
- ->setParameter('objectid', $objId)
- ->setParameter('objecttype', $objectType)
- ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
- ->execute();
-
- $this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent(
- MapperEvent::EVENT_UNASSIGN,
- $objectType,
- $objId,
- $tagIds
- ));
- }
-
- /**
- * {@inheritdoc}
- */
- public function haveTag($objIds, $objectType, $tagId, $all = true) {
- $this->assertTagsExist([$tagId]);
-
- if (!is_array($objIds)) {
- $objIds = [$objIds];
- }
-
- $query = $this->connection->getQueryBuilder();
-
- if (!$all) {
- // If we only need one entry, we make the query lighter, by not
- // counting the elements
- $query->select('*')
- ->setMaxResults(1);
- } else {
- $query->select($query->createFunction('COUNT(1)'));
- }
-
- $query->from(self::RELATION_TABLE)
- ->where($query->expr()->in('objectid', $query->createParameter('objectids')))
- ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
- ->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid')))
- ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY)
- ->setParameter('tagid', $tagId)
- ->setParameter('objecttype', $objectType);
-
- $result = $query->execute();
- $row = $result->fetch(\PDO::FETCH_NUM);
- $result->closeCursor();
-
- if ($all) {
- return ((int)$row[0] === count($objIds));
- } else {
- return (bool) $row;
- }
- }
-
- /**
- * Asserts that all the given tag ids exist.
- *
- * @param string[] $tagIds tag ids to check
- *
- * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist
- */
- private function assertTagsExist($tagIds) {
- $tags = $this->tagManager->getTagsByIds($tagIds);
- if (count($tags) !== count($tagIds)) {
- // at least one tag missing, bail out
- $foundTagIds = array_map(
- function(ISystemTag $tag) {
- return $tag->getId();
- },
- $tags
- );
- $missingTagIds = array_diff($tagIds, $foundTagIds);
- throw new TagNotFoundException(
- 'Tags not found', 0, null, $missingTagIds
- );
- }
- }
-}