diff options
author | Sean Molenaar <sean@m2mobi.com> | 2018-11-29 20:59:46 +0100 |
---|---|---|
committer | Sean Molenaar <sean@m2mobi.com> | 2018-12-14 07:54:43 +0100 |
commit | becce6b7520912257c3d72697a3aefec9923a467 (patch) | |
tree | a2e0da34df11ade7c630e9b681fac6d3b2383918 /lib/Db | |
parent | 0f2645145ac31ad77788954c513f391bc4fbb295 (diff) |
Define an official codestyle and adhere to it.
Diffstat (limited to 'lib/Db')
-rw-r--r-- | lib/Db/EntityJSONSerializer.php | 8 | ||||
-rw-r--r-- | lib/Db/Feed.php | 1 | ||||
-rw-r--r-- | lib/Db/FeedMapper.php | 15 | ||||
-rw-r--r-- | lib/Db/FeedType.php | 3 | ||||
-rw-r--r-- | lib/Db/Folder.php | 1 | ||||
-rw-r--r-- | lib/Db/FolderMapper.php | 12 | ||||
-rw-r--r-- | lib/Db/Item.php | 4 | ||||
-rw-r--r-- | lib/Db/ItemMapper.php | 85 | ||||
-rw-r--r-- | lib/Db/MapperFactory.php | 16 | ||||
-rw-r--r-- | lib/Db/Mysql/ItemMapper.php | 9 | ||||
-rw-r--r-- | lib/Db/NewsMapper.php | 17 |
11 files changed, 87 insertions, 84 deletions
diff --git a/lib/Db/EntityJSONSerializer.php b/lib/Db/EntityJSONSerializer.php index 72fbf99ec..3afc18202 100644 --- a/lib/Db/EntityJSONSerializer.php +++ b/lib/Db/EntityJSONSerializer.php @@ -17,14 +17,12 @@ trait EntityJSONSerializer { - public function serializeFields($properties) + public function serializeFields($properties) { $result = []; - foreach($properties as $property) { + foreach ($properties as $property) { $result[$property] = $this->$property; } return $result; } - - -}
\ No newline at end of file +} diff --git a/lib/Db/Feed.php b/lib/Db/Feed.php index e6bd73481..246ae9886 100644 --- a/lib/Db/Feed.php +++ b/lib/Db/Feed.php @@ -17,7 +17,6 @@ use OCP\AppFramework\Db\Entity; class Feed extends Entity implements IAPI, \JsonSerializable { - use EntityJSONSerializer; /** @var string */ diff --git a/lib/Db/FeedMapper.php b/lib/Db/FeedMapper.php index 00f4007a9..6446e484d 100644 --- a/lib/Db/FeedMapper.php +++ b/lib/Db/FeedMapper.php @@ -17,12 +17,11 @@ use OCA\News\Utility\Time; use OCP\IDBConnection; use OCP\AppFramework\Db\Entity; - class FeedMapper extends NewsMapper { - public function __construct(IDBConnection $db, Time $time) + public function __construct(IDBConnection $db, Time $time) { parent::__construct($db, 'news_feeds', Feed::class, $time); } @@ -52,7 +51,7 @@ class FeedMapper extends NewsMapper { $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` '. + '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` ' . @@ -77,7 +76,7 @@ class FeedMapper extends NewsMapper { $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' . 'FROM `*PREFIX*news_feeds` `feeds` ' . - 'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` '. + '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` ' . @@ -134,7 +133,7 @@ class FeedMapper extends NewsMapper * @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) + public function getToDelete($deleteOlderThan = null, $userId = null) { $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' . 'WHERE `deleted_at` > 0 '; @@ -147,7 +146,7 @@ class FeedMapper extends NewsMapper } // we need to sometimes only delete feeds of a user - if($userId !== null) { + if ($userId !== null) { $sql .= 'AND `user_id` = ?'; $params[] = $userId; } @@ -162,11 +161,9 @@ class FeedMapper extends NewsMapper * * @param string $userId the name of the user */ - public function deleteUser($userId) + public function deleteUser($userId) { $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `user_id` = ?'; $this->execute($sql, [$userId]); } - - } diff --git a/lib/Db/FeedType.php b/lib/Db/FeedType.php index a8ae887f6..23adbefb3 100644 --- a/lib/Db/FeedType.php +++ b/lib/Db/FeedType.php @@ -13,7 +13,6 @@ namespace OCA\News\Db; - class FeedType { const FEED = 0; @@ -22,4 +21,4 @@ class FeedType const SUBSCRIPTIONS = 3; const SHARED = 4; const EXPLORE = 5; -}
\ No newline at end of file +} diff --git a/lib/Db/Folder.php b/lib/Db/Folder.php index f588cdacc..4f54524a8 100644 --- a/lib/Db/Folder.php +++ b/lib/Db/Folder.php @@ -17,7 +17,6 @@ use OCP\AppFramework\Db\Entity; class Folder extends Entity implements IAPI, \JsonSerializable { - use EntityJSONSerializer; /** @var int|null */ diff --git a/lib/Db/FolderMapper.php b/lib/Db/FolderMapper.php index d60864586..fe73093a9 100644 --- a/lib/Db/FolderMapper.php +++ b/lib/Db/FolderMapper.php @@ -20,7 +20,7 @@ use OCP\AppFramework\Db\Entity; class FolderMapper extends NewsMapper { - public function __construct(IDBConnection $db, Time $time) + public function __construct(IDBConnection $db, Time $time) { parent::__construct($db, 'news_folders', Folder::class, $time); } @@ -68,7 +68,7 @@ class FolderMapper extends NewsMapper $stmt = $this->execute($sql, $params); $stmt->closeCursor(); - $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '. + $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN ' . '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)'; $stmt = $this->execute($sql); @@ -82,7 +82,7 @@ class FolderMapper extends NewsMapper * @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) + public function getToDelete($deleteOlderThan = null, $userId = null) { $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `deleted_at` > 0 '; @@ -95,7 +95,7 @@ class FolderMapper extends NewsMapper } // we need to sometimes only delete feeds of a user - if($userId !== null) { + if ($userId !== null) { $sql .= 'AND `user_id` = ?'; $params[] = $userId; } @@ -109,11 +109,9 @@ class FolderMapper extends NewsMapper * * @param string $userId the name of the user */ - public function deleteUser($userId) + public function deleteUser($userId) { $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?'; $this->execute($sql, [$userId]); } - - } diff --git a/lib/Db/Item.php b/lib/Db/Item.php index cd7606f22..1a8d284a2 100644 --- a/lib/Db/Item.php +++ b/lib/Db/Item.php @@ -17,7 +17,6 @@ use OCP\AppFramework\Db\Entity; class Item extends Entity implements IAPI, \JsonSerializable { - use EntityJSONSerializer; /** @var string|null */ @@ -425,8 +424,7 @@ class Item extends Entity implements IAPI, \JsonSerializable public function setUrl(string $url = null) { $url = trim($url); - if ( - (strpos($url, 'http') === 0 || strpos($url, 'magnet') === 0) + if ((strpos($url, 'http') === 0 || strpos($url, 'magnet') === 0) && $this->url !== $url ) { $this->url = $url; diff --git a/lib/Db/ItemMapper.php b/lib/Db/ItemMapper.php index 57561928a..82a38d67d 100644 --- a/lib/Db/ItemMapper.php +++ b/lib/Db/ItemMapper.php @@ -18,16 +18,17 @@ use OCA\News\Utility\Time; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; - class ItemMapper extends NewsMapper { - public function __construct(IDBConnection $db, Time $time) + public function __construct(IDBConnection $db, Time $time) { parent::__construct($db, 'news_items', Item::class, $time); } - private function makeSelectQuery($prependTo = '', $oldestFirst = false, + private function makeSelectQuery( + $prependTo = '', + $oldestFirst = false, $distinctFingerprint = false ) { if ($oldestFirst) { @@ -56,7 +57,7 @@ class ItemMapper extends NewsMapper * @param int|null $type * @return string */ - private function buildStatusQueryPart($showAll, $type = null) + private function buildStatusQueryPart($showAll, $type = null) { $sql = ''; @@ -71,7 +72,7 @@ class ItemMapper extends NewsMapper return $sql; } - private function buildSearchQueryPart(array $search = []) + private function buildSearchQueryPart(array $search = []) { return str_repeat('AND `items`.`search_index` LIKE ? ', count($search)); } @@ -82,13 +83,14 @@ class ItemMapper extends NewsMapper * @param string[] $search an array of strings that should be searched * @return array with like parameters */ - private function buildLikeParameters($search = []) + private function buildLikeParameters($search = []) { return array_map( function ($param) { $param = addcslashes($param, '\\_%'); return '%' . mb_strtolower($param, 'UTF-8') . '%'; - }, $search + }, + $search ); } @@ -97,13 +99,13 @@ class ItemMapper extends NewsMapper * @param string $userId * @return \OCA\News\Db\Item */ - public function find($id, $userId) + public function find($id, $userId) { $sql = $this->makeSelectQuery('AND `items`.`id` = ? '); return $this->findEntity($sql, [$userId, $id]); } - public function starredCount($userId) + public function starredCount($userId) { $sql = 'SELECT COUNT(*) AS size FROM `*PREFIX*news_items` `items` ' . 'JOIN `*PREFIX*news_feeds` `feeds` ' . @@ -124,7 +126,7 @@ class ItemMapper extends NewsMapper } - public function readAll($highestItemId, $time, $userId) + public function readAll($highestItemId, $time, $userId) { $sql = 'UPDATE `*PREFIX*news_items` ' . 'SET unread = ? ' . @@ -139,7 +141,7 @@ class ItemMapper extends NewsMapper } - public function readFolder($folderId, $highestItemId, $time, $userId) + public function readFolder($folderId, $highestItemId, $time, $userId) { $sql = 'UPDATE `*PREFIX*news_items` ' . 'SET unread = ? ' . @@ -156,7 +158,7 @@ class ItemMapper extends NewsMapper } - public function readFeed($feedId, $highestItemId, $time, $userId) + public function readFeed($feedId, $highestItemId, $time, $userId) { $sql = 'UPDATE `*PREFIX*news_items` ' . 'SET unread = ? ' . @@ -174,7 +176,7 @@ class ItemMapper extends NewsMapper } - private function getOperator($oldestFirst) + private function getOperator($oldestFirst) { if ($oldestFirst) { return '>'; @@ -184,7 +186,7 @@ class ItemMapper extends NewsMapper } - public function findAllNew($updatedSince, $type, $showAll, $userId) + public function findAllNew($updatedSince, $type, $showAll, $userId) { $sql = $this->buildStatusQueryPart($showAll, $type); @@ -195,7 +197,7 @@ class ItemMapper extends NewsMapper } - public function findAllNewFolder($id, $updatedSince, $showAll, $userId) + public function findAllNewFolder($id, $updatedSince, $showAll, $userId) { $sql = $this->buildStatusQueryPart($showAll); @@ -207,7 +209,7 @@ class ItemMapper extends NewsMapper } - public function findAllNewFeed($id, $updatedSince, $showAll, $userId) + public function findAllNewFeed($id, $updatedSince, $showAll, $userId) { $sql = $this->buildStatusQueryPart($showAll); @@ -219,7 +221,7 @@ class ItemMapper extends NewsMapper } - private function findEntitiesIgnoringNegativeLimit($sql, $params, $limit) + private function findEntitiesIgnoringNegativeLimit($sql, $params, $limit) { // ignore limit if negative to offer a way to return all feeds if ($limit >= 0) { @@ -230,8 +232,14 @@ class ItemMapper extends NewsMapper } - public function findAllFeed($id, $limit, $offset, $showAll, $oldestFirst, - $userId, $search = [] + public function findAllFeed( + $id, + $limit, + $offset, + $showAll, + $oldestFirst, + $userId, + $search = [] ) { $params = [$userId]; $params = array_merge($params, $this->buildLikeParameters($search)); @@ -251,8 +259,14 @@ class ItemMapper extends NewsMapper } - public function findAllFolder($id, $limit, $offset, $showAll, $oldestFirst, - $userId, $search = [] + public function findAllFolder( + $id, + $limit, + $offset, + $showAll, + $oldestFirst, + $userId, + $search = [] ) { $params = [$userId]; $params = array_merge($params, $this->buildLikeParameters($search)); @@ -272,7 +286,13 @@ class ItemMapper extends NewsMapper } - public function findAll($limit, $offset, $type, $showAll, $oldestFirst, $userId, + public function findAll( + $limit, + $offset, + $type, + $showAll, + $oldestFirst, + $userId, $search = [] ) { $params = [$userId]; @@ -292,7 +312,7 @@ class ItemMapper extends NewsMapper } - public function findAllUnreadOrStarred($userId) + public function findAllUnreadOrStarred($userId) { $params = [$userId, true, true]; $sql = 'AND (`items`.`unread` = ? OR `items`.`starred` = ?) '; @@ -301,7 +321,7 @@ class ItemMapper extends NewsMapper } - public function findByGuidHash($guidHash, $feedId, $userId) + public function findByGuidHash($guidHash, $feedId, $userId) { $sql = $this->makeSelectQuery( 'AND `items`.`guid_hash` = ? ' . @@ -318,7 +338,7 @@ class ItemMapper extends NewsMapper * * @param int $threshold the number of items that should be deleted */ - public function deleteReadOlderThanThreshold($threshold) + public function deleteReadOlderThanThreshold($threshold) { $params = [false, false, $threshold]; @@ -335,7 +355,6 @@ class ItemMapper extends NewsMapper $result = $this->execute($sql, $params); while ($row = $result->fetch()) { - $size = (int)$row['size']; $limit = $size - $threshold; @@ -355,11 +374,10 @@ class ItemMapper extends NewsMapper $this->execute($sql, $params); } } - } - public function getNewestItemId($userId) + public function getNewestItemId($userId) { $sql = 'SELECT MAX(`items`.`id`) AS `max_id` ' . 'FROM `*PREFIX*news_items` `items` ' . @@ -379,7 +397,7 @@ class ItemMapper extends NewsMapper * * @param string $userId the name of the user */ - public function deleteUser($userId) + public function deleteUser($userId) { $sql = 'DELETE FROM `*PREFIX*news_items` ' . 'WHERE `feed_id` IN (' . @@ -394,7 +412,7 @@ class ItemMapper extends NewsMapper /** * Returns a list of ids and userid of all items */ - public function findAllIds($limit = null, $offset = null) + public function findAllIds($limit = null, $offset = null) { $sql = 'SELECT `id` FROM `*PREFIX*news_items`'; return $this->execute($sql, [], $limit, $offset)->fetchAll(); @@ -403,7 +421,7 @@ class ItemMapper extends NewsMapper /** * Update search indices of all items */ - public function updateSearchIndices() + public function updateSearchIndices() { // update indices in steps to prevent memory issues on larger systems $step = 1000; // update 1000 items at a time @@ -419,7 +437,7 @@ class ItemMapper extends NewsMapper } } - private function updateSearchIndex(array $items = []) + private function updateSearchIndex(array $items = []) { foreach ($items as $row) { $sql = 'SELECT * FROM `*PREFIX*news_items` WHERE `id` = ?'; @@ -430,7 +448,7 @@ class ItemMapper extends NewsMapper } } - public function readItem($itemId, $isRead, $lastModified, $userId) + public function readItem($itemId, $isRead, $lastModified, $userId) { $item = $this->find($itemId, $userId); @@ -454,5 +472,4 @@ class ItemMapper extends NewsMapper $this->update($item); } } - } diff --git a/lib/Db/MapperFactory.php b/lib/Db/MapperFactory.php index 679d2a934..635003c64 100644 --- a/lib/Db/MapperFactory.php +++ b/lib/Db/MapperFactory.php @@ -19,7 +19,6 @@ use OCP\IDBConnection; use OCA\News\Db\Mysql\ItemMapper as MysqlItemMapper; use OCA\News\DependencyInjection\IFactory; - class MapperFactory implements IFactory { @@ -30,21 +29,20 @@ class MapperFactory implements IFactory */ private $time; - public function __construct(IDBConnection $db, $databaseType, Time $time) + public function __construct(IDBConnection $db, $databaseType, Time $time) { $this->dbType = $databaseType; $this->db = $db; $this->time = $time; } - public function build() + public function build() { - switch($this->dbType) { - case 'mysql': - return new MysqlItemMapper($this->db, $this->time); - default: - return new ItemMapper($this->db, $this->time); + switch ($this->dbType) { + case 'mysql': + return new MysqlItemMapper($this->db, $this->time); + default: + return new ItemMapper($this->db, $this->time); } } - } diff --git a/lib/Db/Mysql/ItemMapper.php b/lib/Db/Mysql/ItemMapper.php index 4244faedb..42c3b44e1 100644 --- a/lib/Db/Mysql/ItemMapper.php +++ b/lib/Db/Mysql/ItemMapper.php @@ -45,12 +45,11 @@ class ItemMapper extends \OCA\News\Db\ItemMapper $params = [false, false, $threshold]; $result = $this->execute($sql, $params); - while($row = $result->fetch()) { - + while ($row = $result->fetch()) { $size = (int) $row['size']; $limit = $size - $threshold; - if($limit > 0) { + if ($limit > 0) { $params = [false, false, $row['feed_id'], $limit]; $sql = 'DELETE FROM `*PREFIX*news_items` ' . @@ -63,10 +62,9 @@ class ItemMapper extends \OCA\News\Db\ItemMapper $this->execute($sql, $params); } } - } - public function readItem($itemId, $isRead, $lastModified, $userId) + public function readItem($itemId, $isRead, $lastModified, $userId) { $item = $this->find($itemId, $userId); @@ -86,5 +84,4 @@ class ItemMapper extends \OCA\News\Db\ItemMapper $this->update($item); } } - } diff --git a/lib/Db/NewsMapper.php b/lib/Db/NewsMapper.php index a913147b1..4fa001f31 100644 --- a/lib/Db/NewsMapper.php +++ b/lib/Db/NewsMapper.php @@ -26,20 +26,23 @@ abstract class NewsMapper extends Mapper */ private $time; - public function __construct(IDBConnection $db, $table, $entity, + public function __construct( + IDBConnection $db, + $table, + $entity, Time $time ) { parent::__construct($db, $table, $entity); $this->time = $time; } - public function update(Entity $entity) + public function update(Entity $entity) { $entity->setLastModified($this->time->getMicroTime()); return parent::update($entity); } - public function insert(Entity $entity) + public function insert(Entity $entity) { $entity->setLastModified($this->time->getMicroTime()); return parent::insert($entity); @@ -66,9 +69,9 @@ abstract class NewsMapper extends Mapper * @paran int $offset * @return array */ - public function where(array $search = [], $limit = null, $offset = null) + public function where(array $search = [], $limit = null, $offset = null) { - $entity = new $this->entityClass; + $entity = new $this->entityClass(); // turn keys into sql query filter, e.g. feedId -> feed_id = :feedId $filter = array_map( @@ -83,7 +86,8 @@ abstract class NewsMapper extends Mapper $column = $entity->propertyToColumn($property); return $column . ' = :' . $property; - }, array_keys($search) + }, + array_keys($search) ); $andStatement = implode(' AND ', $filter); @@ -96,5 +100,4 @@ abstract class NewsMapper extends Mapper return $this->findEntities($sql, $search, $limit, $offset); } - } |