summaryrefslogtreecommitdiffstats
path: root/lib/Db
diff options
context:
space:
mode:
authorSean Molenaar <sean@seanmolenaar.eu>2020-12-26 13:09:41 +0100
committerSean Molenaar <SMillerDev@users.noreply.github.com>2020-12-30 11:21:44 +0100
commit05377d023ef4d43818a4b42039c8143cb0f907e4 (patch)
treec01fc681c42671dad774de7dc7c46524eb9ae015 /lib/Db
parent27bd54058050a70bd1c9ec8cfcdf42d38541f1b0 (diff)
Remove PHPunit integration tests
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'lib/Db')
-rw-r--r--lib/Db/Feed.php10
-rw-r--r--lib/Db/FeedMapper.php202
-rw-r--r--lib/Db/FeedMapperV2.php16
-rw-r--r--lib/Db/ItemMapper.php121
-rw-r--r--lib/Db/ItemMapperV2.php5
-rw-r--r--lib/Db/NewsMapper.php158
6 files changed, 108 insertions, 404 deletions
diff --git a/lib/Db/Feed.php b/lib/Db/Feed.php
index 473912acc..1d5721d3b 100644
--- a/lib/Db/Feed.php
+++ b/lib/Db/Feed.php
@@ -25,6 +25,16 @@ class Feed extends Entity implements IAPI, \JsonSerializable
{
use EntityJSONSerializer;
+ /**
+ * Silently import new items
+ */
+ const UPDATE_MODE_SILENT = 0;
+
+ /**
+ * Mark new items as unread.
+ */
+ const UPDATE_MODE_NORMAL = 1;
+
/** @var string */
protected $userId = '';
/** @var string */
diff --git a/lib/Db/FeedMapper.php b/lib/Db/FeedMapper.php
deleted file mode 100644
index cf12c4dfa..000000000
--- a/lib/Db/FeedMapper.php
+++ /dev/null
@@ -1,202 +0,0 @@
-<?php
-/**
- * Nextcloud - News
- *
- * This file is licensed under the Affero General Public License version 3 or
- * later. See the COPYING file.
- *
- * @author Alessandro Cosentino <cosenal@gmail.com>
- * @author Bernhard Posselt <dev@bernhard-posselt.com>
- * @copyright 2012 Alessandro Cosentino
- * @copyright 2012-2014 Bernhard Posselt
- */
-
-namespace OCA\News\Db;
-
-use OCA\News\Utility\Time;
-use OCP\AppFramework\Db\DoesNotExistException;
-use OCP\AppFramework\Db\MultipleObjectsReturnedException;
-use OCP\IDBConnection;
-use OCP\AppFramework\Db\Entity;
-
-/**
- * Class LegacyFeedMapper
- *
- * @package OCA\News\Db
- * @deprecated use FeedMapper
- */
-class FeedMapper extends NewsMapper
-{
- const TABLE_NAME = 'news_feeds';
-
- public function __construct(IDBConnection $db, Time $time)
- {
- parent::__construct($db, $time, Feed::class);
- }
-
-
- public function find(string $userId, int $id)
- {
- $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' .
- 'FROM `*PREFIX*news_feeds` `feeds` ' .
- 'JOIN ( ' .
- 'SELECT `feeds`.`id`, COUNT(`items`.`id`) AS `unread_count` ' .
- 'FROM `*PREFIX*news_feeds` `feeds` ' .
- 'LEFT JOIN `*PREFIX*news_items` `items` ' .
- 'ON `feeds`.`id` = `items`.`feed_id` ' .
- // WARNING: this is a desperate attempt at making this query
- // work because prepared statements dont work. This is a
- // POSSIBLE SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT.
- // think twice when changing this
- 'AND `items`.`unread` = ? ' .
- 'WHERE `feeds`.`id` = ? ' .
- 'AND `feeds`.`user_id` = ? ' .
- 'GROUP BY `feeds`.`id` ' .
- ') `item_numbers` ' .
- 'ON `item_numbers`.`id` = `feeds`.`id` ';
- $params = [true, $id, $userId];
-
- return $this->findEntity($sql, $params);
- }
-
-
- public function findAllFromUser(string $userId): array
- {
- $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' .
- 'FROM `*PREFIX*news_feeds` `feeds` ' .
- 'JOIN ( ' .
- 'SELECT `feeds`.`id`, COUNT(`items`.`id`) AS `unread_count` ' .
- 'FROM `*PREFIX*news_feeds` `feeds` ' .
- 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` ' .
- 'ON `feeds`.`folder_id` = `folders`.`id` ' .
- 'LEFT JOIN `*PREFIX*news_items` `items` ' .
- 'ON `feeds`.`id` = `items`.`feed_id` ' .
- // WARNING: this is a desperate attempt at making this query
- // work because prepared statements dont work. This is a
- // POSSIBLE SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT.
- // think twice when changing this
- 'AND `items`.`unread` = ? ' .
- 'WHERE `feeds`.`user_id` = ? ' .
- 'AND (`feeds`.`folder_id` IS NULL ' .
- 'OR `folders`.`deleted_at` = 0 ' .
- ') ' .
- 'AND `feeds`.`deleted_at` = 0 ' .
- 'GROUP BY `feeds`.`id` ' .
- ') `item_numbers` ' .
- 'ON `item_numbers`.`id` = `feeds`.`id` ';
- $params = [true, $userId];
-
- return $this->findEntities($sql, $params);
- }
-
-
- public function findAll(): array
- {
- $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' .
- 'FROM `*PREFIX*news_feeds` `feeds` ' .
- 'JOIN ( ' .
- 'SELECT `feeds`.`id`, COUNT(`items`.`id`) AS `unread_count` ' .
- 'FROM `*PREFIX*news_feeds` `feeds` ' .
- 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` ' .
- 'ON `feeds`.`folder_id` = `folders`.`id` ' .
- 'LEFT JOIN `*PREFIX*news_items` `items` ' .
- 'ON `feeds`.`id` = `items`.`feed_id` ' .
- // WARNING: this is a desperate attempt at making this query
- // work because prepared statements dont work. This is a
- // POSSIBLE SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT.
- // think twice when changing this
- 'AND `items`.`unread` = ? ' .
- 'WHERE (`feeds`.`folder_id` IS NULL ' .
- 'OR `folders`.`deleted_at` = 0 ' .
- ') ' .
- 'AND `feeds`.`deleted_at` = 0 ' .
- 'GROUP BY `feeds`.`id` ' .
- ') `item_numbers` ' .
- 'ON `item_numbers`.`id` = `feeds`.`id` ';
-
- return $this->findEntities($sql, [true]);
- }
-
-
- public function findByUrlHash($hash, $userId)
- {
- $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' .
- 'FROM `*PREFIX*news_feeds` `feeds` ' .
- 'JOIN ( ' .
- 'SELECT `feeds`.`id`, COUNT(`items`.`id`) AS `unread_count` ' .
- 'FROM `*PREFIX*news_feeds` `feeds` ' .
- 'LEFT JOIN `*PREFIX*news_items` `items` ' .
- 'ON `feeds`.`id` = `items`.`feed_id` ' .
- // WARNING: this is a desperate attempt at making this query
- // work because prepared statements dont work. This is a
- // POSSIBLE SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT.
- // think twice when changing this
- 'AND `items`.`unread` = ? ' .
- 'WHERE `feeds`.`url_hash` = ? ' .
- 'AND `feeds`.`user_id` = ? ' .
- 'GROUP BY `feeds`.`id` ' .
- ') `item_numbers` ' .
- 'ON `item_numbers`.`id` = `feeds`.`id` ';
- $params = [true, $hash, $userId];
-
- return $this->findEntity($sql, $params);
- }
-
-
- public function delete(Entity $entity): Entity
- {
- // someone please slap me for doing this manually :P
- // we needz CASCADE + FKs please
- $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` = ?';
- $params = [$entity->getId()];
- $this->execute($sql, $params);
-
- return parent::delete($entity);
- }
-
-
- /**
- * @param int $deleteOlderThan if given gets all entries with a delete date
- * older than that timestamp
- * @param string $userId if given returns only entries from the given user
- * @return array with the database rows
- */
- public function getToDelete($deleteOlderThan = null, $userId = null)
- {
- $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' .
- 'WHERE `deleted_at` > 0 ';
- $params = [];
-
- // sometimes we want to delete all entries
- if ($deleteOlderThan !== null) {
- $sql .= 'AND `deleted_at` < ? ';
- $params[] = $deleteOlderThan;
- }
-
- // we need to sometimes only delete feeds of a user
- if ($userId !== null) {
- $sql .= 'AND `user_id` = ?';
- $params[] = $userId;
- }
-
- return $this->findEntities($sql, $params);
- }
-
-
- /**
- * Deletes all feeds of a user, delete items first since the user_id
- * is not defined in there
- *
- * @param string $userId the name of the user
- */
- public function deleteUser($userId)
- {
- $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `user_id` = ?';
- $this->execute($sql, [$userId]);
- }
-
- public function findFromUser(string $userId, int $id): Entity
- {
- return $this->find($userId, $id);
- }
-}
diff --git a/lib/Db/FeedMapperV2.php b/lib/Db/FeedMapperV2.php
index 0ecb5ba4f..85a1bd3f3 100644
--- a/lib/Db/FeedMapperV2.php
+++ b/lib/Db/FeedMapperV2.php
@@ -52,7 +52,7 @@ class FeedMapperV2 extends NewsMapperV2
{
$builder = $this->db->getQueryBuilder();
$builder->select('feeds.*', $builder->func()->count('items.id', 'unreadCount'))
- ->from($this->tableName, 'feeds')
+ ->from(static::TABLE_NAME, 'feeds')
->leftJoin(
'feeds',
ItemMapperV2::TABLE_NAME,
@@ -82,8 +82,8 @@ class FeedMapperV2 extends NewsMapperV2
public function findFromUser(string $userId, int $id): Entity
{
$builder = $this->db->getQueryBuilder();
- $builder->addSelect('*')
- ->from($this->tableName)
+ $builder->select('*')
+ ->from(static::TABLE_NAME)
->where('user_id = :user_id')
->andWhere('id = :id')
->setParameter(':user_id', $userId)
@@ -101,7 +101,7 @@ class FeedMapperV2 extends NewsMapperV2
{
$builder = $this->db->getQueryBuilder();
$builder->select('*')
- ->from($this->tableName)
+ ->from(static::TABLE_NAME)
->where('deleted_at = 0');
return $this->findEntities($builder);
@@ -121,8 +121,8 @@ class FeedMapperV2 extends NewsMapperV2
public function findByURL(string $userId, string $url): Entity
{
$builder = $this->db->getQueryBuilder();
- $builder->addSelect('*')
- ->from($this->tableName)
+ $builder->select('*')
+ ->from(static::TABLE_NAME)
->where('user_id = :user_id')
->andWhere('url = :url')
->setParameter(':user_id', $userId)
@@ -141,8 +141,8 @@ class FeedMapperV2 extends NewsMapperV2
public function findAllFromFolder(?int $id): array
{
$builder = $this->db->getQueryBuilder();
- $builder->addSelect('*')
- ->from($this->tableName);
+ $builder->select('*')
+ ->from(static::TABLE_NAME);
if (is_null($id)) {
$builder->where('folder_id IS NULL');
diff --git a/lib/Db/ItemMapper.php b/lib/Db/ItemMapper.php
index 65c6e0b15..71e31dbc4 100644
--- a/lib/Db/ItemMapper.php
+++ b/lib/Db/ItemMapper.php
@@ -17,6 +17,7 @@ use Exception;
use OCA\News\Utility\Time;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
+use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
@@ -27,13 +28,25 @@ use OCP\IDBConnection;
* @package OCA\News\Db
* @deprecated use ItemMapper
*/
-class ItemMapper extends NewsMapper
+class ItemMapper extends Mapper
{
const TABLE_NAME = 'news_items';
+ /**
+ * @var Time
+ */
+ private $time;
+
+ /**
+ * NewsMapper constructor.
+ *
+ * @param IDBConnection $db Database connection
+ * @param Time $time Time class
+ */
public function __construct(IDBConnection $db, Time $time)
{
- parent::__construct($db, $time, Item::class);
+ parent::__construct($db, static::TABLE_NAME, Item::class);
+ $this->time = $time;
}
private function makeSelectQuery(
@@ -107,7 +120,7 @@ class ItemMapper extends NewsMapper
/**
* @param int $id
* @param string $userId
- * @return \OCA\News\Db\Item
+ * @return \OCA\News\Db\Item|Entity
*/
public function find(string $userId, int $id)
{
@@ -332,7 +345,15 @@ class ItemMapper extends NewsMapper
return $this->findEntities($sql, $params);
}
-
+ /**
+ * @param $guidHash
+ * @param $feedId
+ * @param $userId
+ *
+ * @return Entity|Item
+ * @throws DoesNotExistException
+ * @throws MultipleObjectsReturnedException
+ */
public function findByGuidHash($guidHash, $feedId, $userId)
{
$sql = $this->makeSelectQuery(
@@ -412,23 +433,6 @@ class ItemMapper extends NewsMapper
/**
- * Deletes all items of a user
- *
- * @param string $userId the name of the user
- */
- public function deleteUser($userId)
- {
- $sql = 'DELETE FROM `*PREFIX*news_items` ' .
- 'WHERE `feed_id` IN (' .
- 'SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds` ' .
- 'WHERE `feeds`.`user_id` = ?' .
- ')';
-
- $this->execute($sql, [$userId]);
- }
-
-
- /**
* Returns a list of ids and userid of all items
*/
public function findAllIds($limit = null, $offset = null)
@@ -492,29 +496,74 @@ class ItemMapper extends NewsMapper
}
}
- /**
- * NO-OP
- *
- * @param string $userId
- *
- * @return array
- */
- public function findAllFromUser(string $userId): array
+ public function update(Entity $entity): Entity
{
- return [];
+ $entity->setLastModified($this->time->getMicroTime());
+ return parent::update($entity);
}
- public function findFromUser(string $userId, int $id): Entity
+ public function insert(Entity $entity): Entity
{
- return $this->find($id, $userId);
+ $entity->setLastModified($this->time->getMicroTime());
+ return parent::insert($entity);
}
/**
- * NO-OP
- * @return array
+ * Remove deleted items.
+ *
+ * @return void
+ */
+ public function purgeDeleted(): void
+ {
+ $builder = $this->db->getQueryBuilder();
+ $builder->delete($this->tableName)
+ ->where('deleted_at != 0')
+ ->execute();
+ }
+ /**
+ * Performs a SELECT query with all arguments appened to the WHERE clause
+ * The SELECT will be performed on the current table and take the entity
+ * that is related for transforming the properties into column names
+ *
+ * Important: This method does not filter marked as deleted rows!
+ *
+ * @param array $search an assoc array from property to filter value
+ * @param int|null $limit Output limit
+ * @param int|null $offset Output offset
+ *
+ * @depreacted Legacy function
+ *
+ * @return Entity[]
*/
- public function findAll(): array
+ public function where(array $search = [], ?int $limit = null, ?int $offset = null)
{
- return [];
+ $entity = new $this->entityClass();
+
+ // turn keys into sql query filter, e.g. feedId -> feed_id = :feedId
+ $filter = array_map(
+ function ($property) use ($entity) {
+ // check if the property actually exists on the entity to prevent
+ // accidental Sql injection
+ if (!property_exists($entity, $property)) {
+ $msg = 'Property ' . $property . ' does not exist on '
+ . $this->entityClass;
+ throw new \BadFunctionCallException($msg);
+ }
+
+ $column = $entity->propertyToColumn($property);
+ return $column . ' = :' . $property;
+ },
+ array_keys($search)
+ );
+
+ $andStatement = implode(' AND ', $filter);
+
+ $sql = 'SELECT * FROM `' . $this->getTableName() . '`';
+
+ if (count($search) > 0) {
+ $sql .= 'WHERE ' . $andStatement;
+ }
+
+ return $this->findEntities($sql, $search, $limit, $offset);
}
}
diff --git a/lib/Db/ItemMapperV2.php b/lib/Db/ItemMapperV2.php
index 71a6ba043..10ebe056b 100644
--- a/lib/Db/ItemMapperV2.php
+++ b/lib/Db/ItemMapperV2.php
@@ -120,6 +120,11 @@ class ItemMapperV2 extends NewsMapperV2
return $this->findEntity($builder);
}
+ /**
+ * @param int $feedId
+ *
+ * @return array
+ */
public function findAllForFeed(int $feedId): array
{
$builder = $this->db->getQueryBuilder();
diff --git a/lib/Db/NewsMapper.php b/lib/Db/NewsMapper.php
deleted file mode 100644
index b03c42c91..000000000
--- a/lib/Db/NewsMapper.php
+++ /dev/null
@@ -1,158 +0,0 @@
-<?php
-/**
- * Nextcloud - News
- *
- * This file is licensed under the Affero General Public License version 3 or
- * later. See the COPYING file.
- *
- * @author Alessandro Cosentino <cosenal@gmail.com>
- * @author Bernhard Posselt <dev@bernhard-posselt.com>
- * @copyright 2012 Alessandro Cosentino
- * @copyright 2012-2014 Bernhard Posselt
- */
-
-namespace OCA\News\Db;
-
-use OCA\News\Utility\Time;
-use OCP\AppFramework\Db\DoesNotExistException;
-use OCP\AppFramework\Db\MultipleObjectsReturnedException;
-use OCP\AppFramework\Db\QBMapper;
-use OCP\IDBConnection;
-use OCP\AppFramework\Db\Mapper;
-use OCP\AppFramework\Db\Entity;
-
-/**
- * Class NewsMapper
- *
- * @package OCA\News\Db
- */
-abstract class NewsMapper extends Mapper
-{
- const TABLE_NAME = '';
-
- /**
- * @var Time
- */
- private $time;
-
- /**
- * NewsMapper constructor.
- *
- * @param IDBConnection $db Database connection
- * @param Time $time Time class
- * @param string $entity Entity class
- */
- public function __construct(
- IDBConnection $db,
- Time $time,
- string $entity
- ) {
- parent::__construct($db, static::TABLE_NAME, $entity);
- $this->time = $time;
- }
-
- public function update(Entity $entity): Entity
- {
- $entity->setLastModified($this->time->getMicroTime());
- return parent::update($entity);
- }
-
- public function insert(Entity $entity): Entity
- {
- $entity->setLastModified($this->time->getMicroTime());
- return parent::insert($entity);
- }
-
- /**
- * Remove deleted items.
- *
- * @return void
- */
- public function purgeDeleted(): void
- {
- $builder = $this->db->getQueryBuilder();
- $builder->delete($this->tableName)
- ->where('deleted_at != 0')
- ->execute();
- }
-
- abstract public function find(string $userId, int $id);
-
- /**
- * Find all items.
- *
- * @return Entity[]
- */
- abstract public function findAll(): array;
-
- /**
- * Find all items for a user.
- *
- * @param string $userId ID of the user
- *
- * @return Entity[]
- */
- abstract public function findAllFromUser(string $userId): array;
-
- /**
- * Find item for a user.
- *
- * @param string $userId ID of the user
- * @param int $id ID of the item
- *
- * @return Feed
- *
- * @throws DoesNotExistException The item is not found
- * @throws MultipleObjectsReturnedException Multiple items found
- */
- abstract public function findFromUser(string $userId, int $id): Entity;
-
-
-
- /**
- * Performs a SELECT query with all arguments appened to the WHERE clause
- * The SELECT will be performed on the current table and take the entity
- * that is related for transforming the properties into column names
- *
- * Important: This method does not filter marked as deleted rows!
- *
- * @param array $search an assoc array from property to filter value
- * @param int|null $limit Output limit
- * @param int|null $offset Output offset
- *
- * @depreacted Legacy function
- *
- * @return array
- */
- public function where(array $search = [], ?int $limit = null, ?int $offset = null)
- {
- $entity = new $this->entityClass();
-
- // turn keys into sql query filter, e.g. feedId -> feed_id = :feedId
- $filter = array_map(
- function ($property) use ($entity) {
- // check if the property actually exists on the entity to prevent
- // accidental Sql injection
- if (!property_exists($entity, $property)) {
- $msg = 'Property ' . $property . ' does not exist on '
- . $this->entityClass;
- throw new \BadFunctionCallException($msg);
- }
-
- $column = $entity->propertyToColumn($property);
- return $column . ' = :' . $property;
- },
- array_keys($search)
- );
-
- $andStatement = implode(' AND ', $filter);
-
- $sql = 'SELECT * FROM `' . $this->getTableName() . '`';
-
- if (count($search) > 0) {
- $sql .= 'WHERE ' . $andStatement;
- }
-
- return $this->findEntities($sql, $search, $limit, $offset);
- }
-}