diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2020-08-29 23:39:35 +0200 |
---|---|---|
committer | Benjamin Brahmer <info@b-brahmer.de> | 2020-09-27 15:35:31 +0200 |
commit | d00d1ab2a28f428223e52b17052c072c64784016 (patch) | |
tree | c019f85fb7ac67147dd43ca64b4ac3cda99832f7 /lib/Db | |
parent | 5687baca75d47dbdffd3de74e865ad2f71ef0cb7 (diff) |
Create V2 mapper, Service and management commands
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'lib/Db')
-rw-r--r-- | lib/Db/Feed.php | 115 | ||||
-rw-r--r-- | lib/Db/FeedMapper.php | 29 | ||||
-rw-r--r-- | lib/Db/FeedMapperV2.php | 141 | ||||
-rw-r--r-- | lib/Db/Folder.php | 7 | ||||
-rw-r--r-- | lib/Db/FolderMapper.php | 38 | ||||
-rw-r--r-- | lib/Db/FolderMapperV2.php | 87 | ||||
-rw-r--r-- | lib/Db/Item.php | 6 | ||||
-rw-r--r-- | lib/Db/ItemMapper.php | 52 | ||||
-rw-r--r-- | lib/Db/ItemMapperV2.php | 124 | ||||
-rw-r--r-- | lib/Db/MapperFactory.php | 6 | ||||
-rw-r--r-- | lib/Db/Mysql/ItemMapper.php | 6 | ||||
-rw-r--r-- | lib/Db/NewsMapper.php | 84 | ||||
-rw-r--r-- | lib/Db/NewsMapperV2.php | 108 |
13 files changed, 737 insertions, 66 deletions
diff --git a/lib/Db/Feed.php b/lib/Db/Feed.php index 246ae9886..ae01c3d6c 100644 --- a/lib/Db/Feed.php +++ b/lib/Db/Feed.php @@ -15,6 +15,12 @@ namespace OCA\News\Db; use OCP\AppFramework\Db\Entity; +/** + * Class Feed + * + * @package OCA\News\Db + * @Embeddable + */ class Feed extends Entity implements IAPI, \JsonSerializable { use EntityJSONSerializer; @@ -67,6 +73,8 @@ class Feed extends Entity implements IAPI, \JsonSerializable protected $basicAuthUser = ''; /** @var string|null */ protected $basicAuthPassword = ''; + /** @var Item[] */ + public $items = []; /** * @return int|null @@ -135,7 +143,7 @@ class Feed extends Entity implements IAPI, \JsonSerializable /** * @return string|null */ - public function getHttpEtag() + public function getHttpEtag(): ?string { return $this->httpEtag; } @@ -143,7 +151,7 @@ class Feed extends Entity implements IAPI, \JsonSerializable /** * @return string|null */ - public function getHttpLastModified() + public function getHttpLastModified(): ?string { return $this->httpLastModified; } @@ -313,250 +321,294 @@ class Feed extends Entity implements IAPI, \JsonSerializable /** * @param int|null $added */ - public function setAdded(int $added = null) + public function setAdded(int $added = null): Feed { if ($this->added !== $added) { $this->added = $added; $this->markFieldUpdated('added'); } + + return $this; } /** * @param int $articlesPerUpdate */ - public function setArticlesPerUpdate(int $articlesPerUpdate) + public function setArticlesPerUpdate(int $articlesPerUpdate): Feed { if ($this->articlesPerUpdate !== $articlesPerUpdate) { $this->articlesPerUpdate = $articlesPerUpdate; $this->markFieldUpdated('articlesPerUpdate'); } + + return $this; } /** * @param string|null $basicAuthPassword */ - public function setBasicAuthPassword(string $basicAuthPassword = null) + public function setBasicAuthPassword(string $basicAuthPassword = null): Feed { if ($this->basicAuthPassword !== $basicAuthPassword) { $this->basicAuthPassword = $basicAuthPassword; $this->markFieldUpdated('basicAuthPassword'); } + + return $this; } /** * @param string|null $basicAuthUser */ - public function setBasicAuthUser(string $basicAuthUser = null) + public function setBasicAuthUser(string $basicAuthUser = null): Feed { if ($this->basicAuthUser !== $basicAuthUser) { $this->basicAuthUser = $basicAuthUser; $this->markFieldUpdated('basicAuthUser'); } + + return $this; } /** * @param int|null $deletedAt */ - public function setDeletedAt(int $deletedAt = null) + public function setDeletedAt(int $deletedAt = null): Feed { if ($this->deletedAt !== $deletedAt) { $this->deletedAt = $deletedAt; $this->markFieldUpdated('deletedAt'); } + + return $this; } /** * @param string|null $faviconLink */ - public function setFaviconLink(string $faviconLink = null) + public function setFaviconLink(string $faviconLink = null): Feed { if ($this->faviconLink !== $faviconLink) { $this->faviconLink = $faviconLink; $this->markFieldUpdated('faviconLink'); } + + return $this; } /** * @param int $folderId */ - public function setFolderId(int $folderId) + public function setFolderId(int $folderId): Feed { if ($this->folderId !== $folderId) { $this->folderId = $folderId; $this->markFieldUpdated('folderId'); } + + return $this; } /** * @param bool $fullTextEnabled */ - public function setFullTextEnabled(bool $fullTextEnabled) + public function setFullTextEnabled(bool $fullTextEnabled): Feed { if ($this->fullTextEnabled !== $fullTextEnabled) { $this->fullTextEnabled = $fullTextEnabled; $this->markFieldUpdated('fullTextEnabled'); } + + return $this; } /** * @param string|null $httpEtag */ - public function setHttpEtag(string $httpEtag = null) + public function setHttpEtag(string $httpEtag = null): Feed { if ($this->httpEtag !== $httpEtag) { $this->httpEtag = $httpEtag; $this->markFieldUpdated('httpEtag'); } + + return $this; } /** * @param string|null $httpLastModified */ - public function setHttpLastModified(string $httpLastModified = null) + public function setHttpLastModified(string $httpLastModified = null): Feed { if ($this->httpLastModified !== $httpLastModified) { $this->httpLastModified = $httpLastModified; $this->markFieldUpdated('httpLastModified'); } + + return $this; } /** * @param int $id */ - public function setId(int $id) + public function setId(int $id): Feed { if ($this->id !== $id) { $this->id = $id; $this->markFieldUpdated('id'); } + + return $this; } /** * @param string|null $lastModified */ - public function setLastModified(string $lastModified = null) + public function setLastModified(string $lastModified = null): Feed { if ($this->lastModified !== $lastModified) { $this->lastModified = $lastModified; $this->markFieldUpdated('lastModified'); } + + return $this; } /** * @param string|null $lastUpdateError */ - public function setLastUpdateError(string $lastUpdateError = null) + public function setLastUpdateError(string $lastUpdateError = null): Feed { if ($this->lastUpdateError !== $lastUpdateError) { $this->lastUpdateError = $lastUpdateError; $this->markFieldUpdated('lastUpdateError'); } + + return $this; } /** * @param string|null $link */ - public function setLink(string $link = null) + public function setLink(string $link = null): Feed { $link = trim($link); if (strpos($link, 'http') === 0 && $this->link !== $link) { $this->link = $link; $this->markFieldUpdated('link'); } + + return $this; } /** * @param string|null $location */ - public function setLocation(string $location = null) + public function setLocation(string $location = null): Feed { if ($this->location !== $location) { $this->location = $location; $this->markFieldUpdated('location'); } + + return $this; } /** * @param int $ordering */ - public function setOrdering(int $ordering) + public function setOrdering(int $ordering): Feed { if ($this->ordering !== $ordering) { $this->ordering = $ordering; $this->markFieldUpdated('ordering'); } + + return $this; } /** * @param bool $pinned */ - public function setPinned(bool $pinned) + public function setPinned(bool $pinned): Feed { if ($this->pinned !== $pinned) { $this->pinned = $pinned; $this->markFieldUpdated('pinned'); } + + return $this; } /** * @param bool $preventUpdate */ - public function setPreventUpdate(bool $preventUpdate) + public function setPreventUpdate(bool $preventUpdate): Feed { if ($this->preventUpdate !== $preventUpdate) { $this->preventUpdate = $preventUpdate; $this->markFieldUpdated('preventUpdate'); } + + return $this; } /** * @param string $title */ - public function setTitle(string $title) + public function setTitle(string $title): Feed { if ($this->title !== $title) { $this->title = $title; $this->markFieldUpdated('title'); } + + return $this; } /** * @param int $unreadCount */ - public function setUnreadCount(int $unreadCount) + public function setUnreadCount(int $unreadCount): Feed { if ($this->unreadCount !== $unreadCount) { $this->unreadCount = $unreadCount; $this->markFieldUpdated('unreadCount'); } + + return $this; } /** * @param int $updateErrorCount */ - public function setUpdateErrorCount(int $updateErrorCount) + public function setUpdateErrorCount(int $updateErrorCount): Feed { if ($this->updateErrorCount !== $updateErrorCount) { $this->updateErrorCount = $updateErrorCount; $this->markFieldUpdated('updateErrorCount'); } + + return $this; } /** * @param int $updateMode */ - public function setUpdateMode(int $updateMode) + public function setUpdateMode(int $updateMode): Feed { if ($this->updateMode !== $updateMode) { $this->updateMode = $updateMode; $this->markFieldUpdated('updateMode'); } + + return $this; } /** * @param string $url */ - public function setUrl(string $url) + public function setUrl(string $url): Feed { $url = trim($url); if (strpos($url, 'http') === 0 && $this->url !== $url) { @@ -564,28 +616,34 @@ class Feed extends Entity implements IAPI, \JsonSerializable $this->setUrlHash(md5($url)); $this->markFieldUpdated('url'); } + + return $this; } /** * @param string $urlHash */ - public function setUrlHash(string $urlHash) + public function setUrlHash(string $urlHash): Feed { if ($this->urlHash !== $urlHash) { $this->urlHash = $urlHash; $this->markFieldUpdated('urlHash'); } + + return $this; } /** * @param string $userId */ - public function setUserId(string $userId) + public function setUserId(string $userId): Feed { if ($this->userId !== $userId) { $this->userId = $userId; $this->markFieldUpdated('userId'); } + + return $this; } public function toAPI(): array @@ -603,7 +661,8 @@ class Feed extends Entity implements IAPI, \JsonSerializable 'link', 'pinned', 'updateErrorCount', - 'lastUpdateError' + 'lastUpdateError', + 'items' ] ); } diff --git a/lib/Db/FeedMapper.php b/lib/Db/FeedMapper.php index b23ced239..867ba982d 100644 --- a/lib/Db/FeedMapper.php +++ b/lib/Db/FeedMapper.php @@ -14,20 +14,28 @@ 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, 'news_feeds', Feed::class, $time); + parent::__construct($db, $time, Feed::class); } - public function find($id, $userId) + public function find(string $userId, int $id) { $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' . 'FROM `*PREFIX*news_feeds` `feeds` ' . @@ -52,7 +60,7 @@ class FeedMapper extends NewsMapper } - public function findAllFromUser($userId) + public function findAllFromUser(string $userId): array { $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' . 'FROM `*PREFIX*news_feeds` `feeds` ' . @@ -82,7 +90,7 @@ class FeedMapper extends NewsMapper } - public function findAll() + public function findAll(): array { $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' . 'FROM `*PREFIX*news_feeds` `feeds` ' . @@ -135,15 +143,15 @@ class FeedMapper extends NewsMapper } - public function delete(Entity $entity) + public function delete(Entity $entity): Entity { - parent::delete($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); } @@ -186,4 +194,9 @@ class FeedMapper extends NewsMapper $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `user_id` = ?'; $this->execute($sql, [$userId]); } + + public function findFromUser(string $userId, int $id): Entity + { + return $this->find($id, $userId); + } } diff --git a/lib/Db/FeedMapperV2.php b/lib/Db/FeedMapperV2.php new file mode 100644 index 000000000..2b4ff8f10 --- /dev/null +++ b/lib/Db/FeedMapperV2.php @@ -0,0 +1,141 @@ +<?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 FeedMapper + * + * @package OCA\News\Db + */ +class FeedMapperV2 extends NewsMapperV2 +{ + const TABLE_NAME = 'news_feeds'; + + /** + * FeedMapper constructor. + * + * @param IDBConnection $db + * @param Time $time + */ + public function __construct(IDBConnection $db, Time $time) + { + parent::__construct($db, $time, Feed::class); + } + + /** + * Find all feeds for a user. + * + * @param string $userId The user identifier + * + * @return Entity[] + */ + public function findAllFromUser(string $userId): array + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->where('user_id = :user_id') + ->andWhere('deleted_at = 0') + ->setParameter(':user_id', $userId); + + return $this->findEntities($builder); + } + + /** + * Find all feeds for a user. + * + * @param string $userId The user identifier + * @param int $id The feed identifier + * + * @return Entity + * + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + */ + public function findFromUser(string $userId, int $id): Entity + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->where('user_id = :user_id') + ->where('id = :id') + ->setParameter(':user_id', $userId) + ->setParameter(':id', $id); + + return $this->findEntity($builder); + } + + /** + * Find all items + * + * @return Entity[] + */ + public function findAll(): array + { + $builder = $this->db->getQueryBuilder(); + $builder->select('*') + ->from($this->tableName) + ->where('deleted_at = 0'); + + return $this->findEntities($builder); + } + + /** + * Find feed by URL + * + * @param string $userId The user to find in. + * @param string $url The URL to find + * + * @return Entity + * + * @throws DoesNotExistException If not found + * @throws MultipleObjectsReturnedException If multiple found + */ + public function findByURL(string $userId, string $url): Entity + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->where('user_id = :user_id') + ->andWhere('url = :url') + ->setParameter(':user_id', $userId) + ->setParameter(':url', $url); + + return $this->findEntity($builder); + } + + /** + * Find all feeds in a folder + * + * @param int $id ID of the folder + * + * @return Feed[] + */ + public function findAllFromFolder(int $id): array + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->where('folder_id = :folder_id') + ->setParameter(':folder_id', $id); + + return $this->findEntities($builder); + } +} diff --git a/lib/Db/Folder.php b/lib/Db/Folder.php index 4f54524a8..674c9fabc 100644 --- a/lib/Db/Folder.php +++ b/lib/Db/Folder.php @@ -31,6 +31,8 @@ class Folder extends Entity implements IAPI, \JsonSerializable protected $deletedAt = 0; /** @var string|null */ protected $lastModified = '0'; + /** @var Feed[] */ + public $feeds = []; /** * @return int|null @@ -134,7 +136,7 @@ class Folder extends Entity implements IAPI, \JsonSerializable } } - public function setParentId(int $parentId = null) + public function setParentId(int $parentId = 0) { if ($this->parentId !== $parentId) { $this->parentId = $parentId; @@ -155,7 +157,8 @@ class Folder extends Entity implements IAPI, \JsonSerializable return $this->serializeFields( [ 'id', - 'name' + 'name', + 'feeds' ] ); } diff --git a/lib/Db/FolderMapper.php b/lib/Db/FolderMapper.php index fe73093a9..75b749974 100644 --- a/lib/Db/FolderMapper.php +++ b/lib/Db/FolderMapper.php @@ -14,18 +14,28 @@ 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 LegacyFolderMapper + * + * @package OCA\News\Db + * @deprecated use FolderMapper + */ class FolderMapper extends NewsMapper { + const TABLE_NAME = 'news_folders'; + public function __construct(IDBConnection $db, Time $time) { - parent::__construct($db, 'news_folders', Folder::class, $time); + parent::__construct($db, $time, Folder::class); } - public function find($id, $userId) + public function find(string $userId, int $id) { $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `id` = ? ' . @@ -35,7 +45,7 @@ class FolderMapper extends NewsMapper } - public function findAllFromUser($userId) + public function findAllFromUser(string $userId): array { $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `user_id` = ? ' . @@ -46,7 +56,7 @@ class FolderMapper extends NewsMapper } - public function findByName($folderName, $userId) + public function findByName(string $folderName, string $userId) { $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `name` = ? ' . @@ -57,7 +67,7 @@ class FolderMapper extends NewsMapper } - public function delete(Entity $entity) + public function delete(Entity $entity): Entity { parent::delete($entity); @@ -73,6 +83,8 @@ class FolderMapper extends NewsMapper $stmt = $this->execute($sql); $stmt->closeCursor(); + + return $entity; } @@ -109,9 +121,23 @@ class FolderMapper extends NewsMapper * * @param string $userId the name of the user */ - public function deleteUser($userId) + public function deleteUser(string $userId) { $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?'; $this->execute($sql, [$userId]); } + + /** + * NO-OP + * @return array + */ + public function findAll(): array + { + return []; + } + + public function findFromUser(string $userId, int $id): Entity + { + return $this->find($id, $userId); + } } diff --git a/lib/Db/FolderMapperV2.php b/lib/Db/FolderMapperV2.php new file mode 100644 index 000000000..d684e5af2 --- /dev/null +++ b/lib/Db/FolderMapperV2.php @@ -0,0 +1,87 @@ +<?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\Entity; +use OCP\IDBConnection; + +/** + * Class FolderMapper + * + * @package OCA\News\Db + */ +class FolderMapperV2 extends NewsMapperV2 +{ + const TABLE_NAME = 'news_folders'; + + /** + * FolderMapper constructor. + * + * @param IDBConnection $db + * @param Time $time + */ + public function __construct(IDBConnection $db, Time $time) + { + parent::__construct($db, $time, Folder::class); + } + + /** + * Find all feeds for a user. + * + * @param string $userId The user identifier + * + * @return Entity[] + */ + public function findAllFromUser($userId): array + { + $builder = $this->db->getQueryBuilder(); + $builder->select('*') + ->from($this->tableName) + ->where('user_id = :user_id') + ->where('deleted_at = 0') + ->setParameter(':user_id', $userId); + + return $this->findEntities($builder); + } + + /** + * Find all items + * + * @return Entity[] + */ + public function findAll(): array + { + $builder = $this->db->getQueryBuilder(); + $builder->select('*') + ->from($this->tableName) + ->where('deleted_at = 0'); + + return $this->findEntities($builder); + } + + public function findFromUser(string $userId, int $id): Entity + { + $builder = $this->db->getQueryBuilder(); + $builder->select('*') + ->from($this->tableName) + ->where('user_id = :user_id') + ->where('id = :id') + ->where('deleted_at = 0') + ->setParameter(':user_id', $userId) + ->setParameter(':id', $id); + + return $this->findEntity($builder); + } +} diff --git a/lib/Db/Item.php b/lib/Db/Item.php index 4cf376366..7d3924597 100644 --- a/lib/Db/Item.php +++ b/lib/Db/Item.php @@ -15,6 +15,12 @@ namespace OCA\News\Db; use OCP\AppFramework\Db\Entity; +/** + * Class Item + * + * @package OCA\News\Db + * @Embeddable + */ class Item extends Entity implements IAPI, \JsonSerializable { use EntityJSONSerializer; diff --git a/lib/Db/ItemMapper.php b/lib/Db/ItemMapper.php index 2e08471db..270919b44 100644 --- a/lib/Db/ItemMapper.php +++ b/lib/Db/ItemMapper.php @@ -15,15 +15,25 @@ namespace OCA\News\Db; use Exception; use OCA\News\Utility\Time; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\Entity; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +/** + * Class LegacyItemMapper + * + * @package OCA\News\Db + * @deprecated use ItemMapper + */ class ItemMapper extends NewsMapper { + const TABLE_NAME = 'news_items'; public function __construct(IDBConnection $db, Time $time) { - parent::__construct($db, 'news_items', Item::class, $time); + parent::__construct($db, $time, Item::class); } private function makeSelectQuery( @@ -99,13 +109,13 @@ class ItemMapper extends NewsMapper * @param string $userId * @return \OCA\News\Db\Item */ - public function find($id, $userId) + public function find(string $userId, int $id) { $sql = $this->makeSelectQuery('AND `items`.`id` = ? '); return $this->findEntity($sql, [$userId, $id]); } |