From 38f9fc794237fe6c6fdb2fd728d569120acd151c Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Sun, 16 Oct 2016 00:41:47 +0200 Subject: [PATCH] Make Bookmarks class non-static --- appinfo/application.php | 17 +- controller/lib/bookmarks.php | 303 +++++++++++++------------ controller/lib/search.php | 8 +- controller/rest/bookmarkcontroller.php | 34 +-- controller/rest/publiccontroller.php | 14 +- controller/rest/tagscontroller.php | 15 +- controller/webviewcontroller.php | 15 +- 7 files changed, 209 insertions(+), 197 deletions(-) diff --git a/appinfo/application.php b/appinfo/application.php index 1515c03a..b6ffac8c 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -15,6 +15,7 @@ namespace OCA\Bookmarks\AppInfo; +use OCA\Bookmarks\Controller\Lib\Bookmarks; use \OCP\AppFramework\App; use \OCP\IContainer; use \OCA\Bookmarks\Controller\WebViewController; @@ -45,25 +46,19 @@ class Application extends App { $c->query('Request'), $uid, $c->query('ServerContainer')->getURLGenerator(), - $c->query('ServerContainer')->getDb() + $c->query('ServerContainer')->query(Bookmarks::class) ); }); $container->registerService('BookmarkController', function($c) { - if(method_exists($c->query('ServerContainer'), 'getL10NFactory')) { - $l = $c->query('ServerContainer')->getL10NFactory()->get('bookmarks'); - } else { - // OC 8.1 compatibility - $l = new \OC_L10N('bookmarks'); - } - /** @var IContainer $c */ return new BookmarkController( $c->query('AppName'), $c->query('Request'), $c->query('ServerContainer')->getUserSession()->getUser()->getUID(), $c->query('ServerContainer')->getDb(), - $l + $c->query('ServerContainer')->getL10NFactory()->get('bookmarks'), + $c->query('ServerContainer')->query(Bookmarks::class) ); }); @@ -73,7 +68,7 @@ class Application extends App { $c->query('AppName'), $c->query('Request'), $c->query('ServerContainer')->getUserSession()->getUser()->getUID(), - $c->query('ServerContainer')->getDb() + $c->query('ServerContainer')->query(Bookmarks::class) ); }); @@ -82,7 +77,7 @@ class Application extends App { return new PublicController( $c->query('AppName'), $c->query('Request'), - $c->query('ServerContainer')->getDb(), + $c->query('ServerContainer')->query(Bookmarks::class), $c->query('ServerContainer')->getUserManager() ); }); diff --git a/controller/lib/bookmarks.php b/controller/lib/bookmarks.php index 58eeef26..cb4bc50c 100644 --- a/controller/lib/bookmarks.php +++ b/controller/lib/bookmarks.php @@ -27,36 +27,69 @@ namespace OCA\Bookmarks\Controller\Lib; use GuzzleHttp\Exception\ClientException; -use \OCP\IDb; +use OCP\Http\Client\IClientService; +use OCP\IConfig; +use OCP\IDb; +use OCP\IL10N; +use OCP\ILogger; class Bookmarks { + /** @var IDb */ + private $db; + + /** @var IConfig */ + private $config; + + /** @var IL10N */ + private $l; + + /** @var IClientService */ + private $httpClientService; + + /** @var ILogger */ + private $logger; + + public function __construct( + IDb $db, + IConfig $config, + IL10N $l, + IClientService $httpClientService, + ILogger $logger + ) { + $this->db = $db; + $this->config = $config; + $this->l = $l; + $this->httpClientService = $httpClientService; + $this->logger = $logger; + } + /** * @brief Finds all tags for bookmarks * @param string $userId UserId - * @param IDb $db Database Interface - * @param filterTags array of tag to look for if empty then every tag - * @param offset integer offset - * @param limit integer of item to return - * @return Found Tags + * @param array $filterTags of tag to look for if empty then every tag + * @param int $offset + * @param int $limit + * @return array Found Tags */ - public static function findTags($userId, IDb $db, $filterTags = array(), $offset = 0, $limit = -1) { + public function findTags($userId, $filterTags = [], $offset = 0, $limit = -1) { $params = array_merge($filterTags, $filterTags); array_unshift($params, $userId); - $not_in = ''; + $notIn = ''; if (!empty($filterTags)) { - $exist_clause = " AND exists (select 1 from `*PREFIX*bookmarks_tags` + $existClause = " AND exists (select 1 from `*PREFIX*bookmarks_tags` `t2` where `t2`.`bookmark_id` = `t`.`bookmark_id` and `tag` = ?) "; - $not_in = ' AND `tag` not in (' . implode(',', array_fill(0, count($filterTags), '?')) . ')' . - str_repeat($exist_clause, count($filterTags)); + $notIn = ' AND `tag` not in (' . implode(',', array_fill(0, count($filterTags), '?')) . ')' . + str_repeat($existClause, count($filterTags)); } - $sql = 'SELECT tag, count(*) as nbr from *PREFIX*bookmarks_tags t ' . - ' WHERE EXISTS( SELECT 1 from *PREFIX*bookmarks bm where t.bookmark_id = bm.id and user_id = ?) ' . - $not_in . + $sql = 'SELECT tag, count(*) AS nbr FROM *PREFIX*bookmarks_tags t ' . + ' WHERE EXISTS( SELECT 1 FROM *PREFIX*bookmarks bm ' . + ' WHERE t.bookmark_id = bm.id AND user_id = ?) ' . + $notIn . ' GROUP BY `tag` ORDER BY `nbr` DESC '; - $query = $db->prepareQuery($sql, $limit, $offset); + $query = $this->db->prepareQuery($sql, $limit, $offset); $tags = $query->execute($params)->fetchAll(); return $tags; } @@ -65,20 +98,20 @@ class Bookmarks { * @brief Finds Bookmark with certain ID * @param int $id BookmarkId * @param string $userId UserId - * @param IDb $db Database Interface * @return array Specific Bookmark */ - public static function findUniqueBookmark($id, $userId, IDb $db) { - $CONFIG_DBTYPE = \OCP\Config::getSystemValue('dbtype', 'sqlite'); - if ($CONFIG_DBTYPE == 'pgsql') { - $group_fct = 'array_agg(`tag`)'; + public function findUniqueBookmark($id, $userId) { + $dbType = $this->config->getSystemValue('dbtype', 'sqlite'); + if ($dbType == 'pgsql') { + $groupFunction = 'array_agg(`tag`)'; } else { - $group_fct = 'GROUP_CONCAT(`tag`)'; + $groupFunction = 'GROUP_CONCAT(`tag`)'; } - $sql = "SELECT *, (select $group_fct from `*PREFIX*bookmarks_tags` where `bookmark_id` = `b`.`id`) as `tags` + $sql = "SELECT *, (SELECT $groupFunction FROM `*PREFIX*bookmarks_tags` + WHERE `bookmark_id` = `b`.`id`) AS `tags` FROM `*PREFIX*bookmarks` `b` WHERE `user_id` = ? AND `id` = ?"; - $query = $db->prepareQuery($sql); + $query = $this->db->prepareQuery($sql); $result = $query->execute(array($userId, $id))->fetchRow(); $result['tags'] = explode(',', $result['tags']); return $result; @@ -88,14 +121,13 @@ class Bookmarks { * @brief Check if an URL is bookmarked * @param string $url Url of a possible bookmark * @param string $userId UserId - * @param IDb $db Database Interface * @return boolean if the url is already bookmarked */ - public static function bookmarkExists($url, $userId, IDb $db) { - $enc_url = htmlspecialchars_decode($url); - $sql = "SELECT id from `*PREFIX*bookmarks` where `url` = ? and `user_id` = ?"; - $query = $db->prepareQuery($sql); - $result = $query->execute(array($enc_url, $userId))->fetchRow(); + public function bookmarkExists($url, $userId) { + $encodedUrl = htmlspecialchars_decode($url); + $sql = "SELECT id FROM `*PREFIX*bookmarks` WHERE `url` = ? AND `user_id` = ?"; + $query = $this->db->prepareQuery($sql); + $result = $query->execute(array($encodedUrl, $userId))->fetchRow(); if ($result) { return $result['id']; } else { @@ -106,7 +138,6 @@ class Bookmarks { /** * @brief Finds all bookmarks, matching the filter * @param string $userid UserId - * @param IDb $db Database Interface * @param int $offset offset * @param string $sqlSortColumn result with this column * @param string|array $filters filters can be: empty -> no filter, a string -> filter this, a string array -> filter for all strings @@ -115,12 +146,20 @@ class Bookmarks { * @param bool $public check if only public bookmarks should be returned * @param array $requestedAttributes select all the attributes that should be returned. default is * + tags * @param string $tagFilterConjunction select wether the filterTagOnly should filter with an AND or an OR conjunction - * @return Collection of specified bookmarks + * @return array Collection of specified bookmarks */ - public static function findBookmarks( - $userid, IDb $db, $offset, $sqlSortColumn, $filters, $filterTagOnly, $limit = 10, $public = false, $requestedAttributes = null, $tagFilterConjunction = "and") { - - $CONFIG_DBTYPE = \OCP\Config::getSystemValue('dbtype', 'sqlite'); + public function findBookmarks( + $userid, + $offset, + $sqlSortColumn, + $filters, + $filterTagOnly, + $limit = 10, + $public = false, + $requestedAttributes = null, + $tagFilterConjunction = "and" + ) { + $dbType = $this->config->getSystemValue('dbtype', 'sqlite'); if (is_string($filters)) { $filters = array($filters); } @@ -143,14 +182,14 @@ class Bookmarks { $toSelect = implode(",", array_intersect($tableAttributes, $requestedAttributes)); } - if ($CONFIG_DBTYPE == 'pgsql') { + if ($dbType == 'pgsql') { $sql = "SELECT " . $toSelect . " FROM (SELECT *, (select array_to_string(array_agg(`tag`),',') - from `*PREFIX*bookmarks_tags` where `bookmark_id` = `b2`.`id`) as `tags` + FROM `*PREFIX*bookmarks_tags` WHERE `bookmark_id` = `b2`.`id`) AS `tags` FROM `*PREFIX*bookmarks` `b2` WHERE `user_id` = ? ) as `b` WHERE true "; } else { - $sql = "SELECT " . $toSelect . ", (SELECT GROUP_CONCAT(`tag`) from `*PREFIX*bookmarks_tags` - WHERE `bookmark_id` = `b`.`id`) as `tags` + $sql = "SELECT " . $toSelect . ", (SELECT GROUP_CONCAT(`tag`) FROM `*PREFIX*bookmarks_tags` + WHERE `bookmark_id` = `b`.`id`) AS `tags` FROM `*PREFIX*bookmarks` `b` WHERE `user_id` = ? "; } @@ -162,7 +201,7 @@ class Bookmarks { } if (count($filters) > 0) { - Bookmarks::findBookmarksBuildFilter($sql, $params, $filters, $filterTagOnly, $tagFilterConjunction, $CONFIG_DBTYPE); + $this->findBookmarksBuildFilter($sql, $params, $filters, $filterTagOnly, $tagFilterConjunction, $dbType); } if (!in_array($sqlSortColumn, $tableAttributes)) { @@ -174,7 +213,7 @@ class Bookmarks { $offset = null; } - $query = $db->prepareQuery($sql, $limit, $offset); + $query = $this->db->prepareQuery($sql, $limit, $offset); $results = $query->execute($params)->fetchAll(); $bookmarks = array(); foreach ($results as $result) { @@ -188,7 +227,7 @@ class Bookmarks { return $bookmarks; } - private static function findBookmarksBuildFilter(&$sql, &$params, $filters, $filterTagOnly, $tagFilterConjunction, $CONFIG_DBTYPE) { + private function findBookmarksBuildFilter(&$sql, &$params, $filters, $filterTagOnly, $tagFilterConjunction, $dbType) { $tagOrSearch = false; $connectWord = 'AND'; @@ -203,9 +242,9 @@ class Bookmarks { } else { $sql .= 'AND'; } - $exist_clause = " exists (SELECT `id` FROM `*PREFIX*bookmarks_tags` + $existClause = " exists (SELECT `id` FROM `*PREFIX*bookmarks_tags` `t2` WHERE `t2`.`bookmark_id` = `b`.`id` AND `tag` = ?) "; - $sql .= str_repeat($exist_clause . $connectWord, count($filters)); + $sql .= str_repeat($existClause . $connectWord, count($filters)); if ($tagOrSearch) { $sql = rtrim($sql, 'OR'); $sql .= ')'; @@ -214,14 +253,14 @@ class Bookmarks { } $params = array_merge($params, $filters); } else { - if ($CONFIG_DBTYPE == 'mysql') { //Dirty hack to allow usage of alias in where + if ($dbType == 'mysql') { //Dirty hack to allow usage of alias in where $sql .= ' HAVING true '; } foreach ($filters as $filter) { - if ($CONFIG_DBTYPE == 'mysql') { + if ($dbType == 'mysql') { $sql .= ' AND lower( concat(url,title,description,IFNULL(tags,\'\') )) like ? '; } else { - $sql .= ' AND lower(url || title || description || ifnull(tags,\'\') ) like ? '; + $sql .= ' AND lower(url || title || description || IFNULL(tags,\'\') ) like ? '; } $params[] = '%' . strtolower($filter) . '%'; } @@ -231,14 +270,13 @@ class Bookmarks { /** * @brief Delete bookmark with specific id * @param string $userId UserId - * @param IDb $db Database Interface * @param int $id Bookmark ID to delete * @return boolean Success of operation */ - public static function deleteUrl($userId, IDb $db, $id) { + public function deleteUrl($userId, $id) { $user = $userId; - $query = $db->prepareQuery(" + $query = $this->db->prepareQuery(" SELECT `id` FROM `*PREFIX*bookmarks` WHERE `id` = ? AND `user_id` = ? @@ -250,14 +288,14 @@ class Bookmarks { return false; } - $query = $db->prepareQuery(" + $query = $this->db->prepareQuery(" DELETE FROM `*PREFIX*bookmarks` WHERE `id` = ? "); $query->execute(array($id)); - $query = $db->prepareQuery(" + $query = $this->db->prepareQuery(" DELETE FROM `*PREFIX*bookmarks_tags` WHERE `bookmark_id` = ? "); @@ -269,19 +307,17 @@ class Bookmarks { /** * @brief Rename a tag * @param string $userId UserId - * @param IDb $db Database Interface * @param string $old Old Tag Name * @param string $new New Tag Name * @return boolean Success of operation */ - public static function renameTag($userId, IDb $db, $old, $new) { - $user_id = $userId; - $CONFIG_DBTYPE = \OCP\Config::getSystemValue('dbtype', 'sqlite'); + public function renameTag($userId, $old, $new) { + $dbType = $this->config->getSystemValue('dbtype', 'sqlite'); - if ($CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3') { + if ($dbType == 'sqlite' or $dbType == 'sqlite3') { // Update tags to the new label unless it already exists a tag like this - $query = $db->prepareQuery(" + $query = $this->db->prepareQuery(" UPDATE OR REPLACE `*PREFIX*bookmarks_tags` SET `tag` = ? WHERE `tag` = ? @@ -289,34 +325,24 @@ class Bookmarks { WHERE `b`.`user_id` = ? AND `bookmark_id` = `b`.`id`) "); - $params = array( - $new, - $old, - $user_id, - ); + $params = [$new, $old, $userId]; $query->execute($params); } else { - // Remove potentialy duplicated tags - $query = $db->prepareQuery(" + // Remove potentially duplicated tags + $query = $this->db->prepareQuery(" DELETE FROM `*PREFIX*bookmarks_tags` as `tgs` WHERE `tgs`.`tag` = ? AND exists( SELECT `id` FROM `*PREFIX*bookmarks` WHERE `user_id` = ? AND `tgs`.`bookmark_id` = `id`) AND exists( SELECT `t`.`tag` FROM `*PREFIX*bookmarks_tags` `t` where `t`.`tag` = ? AND `tgs`.`bookmark_id` = `t`.`bookmark_id`)"); - $params = array( - $new, - $user_id, - $new - ); - + $params = [$new, $userId, $new]; $query->execute($params); - // Update tags to the new label unless it already exists a tag like this - $query = $db->prepareQuery(" + $query = $this->db->prepareQuery(" UPDATE `*PREFIX*bookmarks_tags` SET `tag` = ? WHERE `tag` = ? @@ -324,63 +350,51 @@ class Bookmarks { WHERE `b`.`user_id` = ? AND `bookmark_id` = `b`.`id`) "); - $params = array( - $new, - $old, - $user_id - ); - + $params = [$new, $old, $userId]; $query->execute($params); } - return true; } /** * @brief Delete a tag * @param string $userid UserId - * @param IDb $db Database Interface * @param string $old Tag Name to delete * @return boolean Success of operation */ - public static function deleteTag($userid, IDb $db, $old) { + public function deleteTag($userid, $old) { // Update the record - $query = $db->prepareQuery(" + $query = $this->db->prepareQuery(" DELETE FROM `*PREFIX*bookmarks_tags` WHERE `tag` = ? AND exists( SELECT `id` FROM `*PREFIX*bookmarks` WHERE `user_id` = ? AND `bookmark_id` = `id`) "); - $params = array( - $old, - $userid, - ); - + $params = [$old, $userid]; $result = $query->execute($params); return $result; } /** * Edit a bookmark + * * @param string $userid UserId - * @param IDb $db Database Interface * @param int $id The id of the bookmark to edit * @param string $url The url to set * @param string $title Name of the bookmark * @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values) * @param string $description A longer description about the bookmark - * @param boolean $is_public True if the bookmark is publishable to not registered users + * @param boolean $isPublic True if the bookmark is publishable to not registered users * @return null */ - public static function editBookmark($userid, IDb $db, $id, $url, $title, $tags = array(), $description = '', $is_public = false) { + public function editBookmark($userid, $id, $url, $title, $tags = [], $description = '', $isPublic = false) { - $is_public = $is_public ? 1 : 0; - $user_id = $userid; + $isPublic = $isPublic ? 1 : 0; // Update the record - $query = $db->prepareQuery(" + $query = $this->db->prepareQuery(" UPDATE `*PREFIX*bookmarks` SET `url` = ?, `title` = ?, `public` = ?, `description` = ?, `lastmodified` = UNIX_TIMESTAMP() @@ -391,87 +405,87 @@ class Bookmarks { $params = array( htmlspecialchars_decode($url), htmlspecialchars_decode($title), - $is_public, + $isPublic, htmlspecialchars_decode($description), $id, - $user_id, + $userid, ); $result = $query->execute($params); // Abort the operation if bookmark couldn't be set // (probably because the user is not allowed to edit this bookmark) - if ($result == 0) + if ($result == 0) { exit(); - + } // Remove old tags $sql = "DELETE FROM `*PREFIX*bookmarks_tags` WHERE `bookmark_id` = ?"; - $query = $db->prepareQuery($sql); + $query = $this->db->prepareQuery($sql); $query->execute(array($id)); // Add New Tags - self::addTags($db, $id, $tags); + $this->addTags($id, $tags); return $id; } /** * Add a bookmark + * * @param string $userid UserId - * @param IDb $db Database Interface * @param string $url * @param string $title Name of the bookmark * @param array $tags Simple array of tags to qualify the bookmark (different tags are taken from values) * @param string $description A longer description about the bookmark - * @param boolean $public True if the bookmark is publishable to not registered users + * @param boolean $isPublic True if the bookmark is publishable to not registered users * @return int The id of the bookmark created */ - public static function addBookmark($userid, IDb $db, $url, $title, $tags = array(), $description = '', $is_public = false) { - $public = $is_public ? 1 : 0; - $url_without_prefix = trim(substr($url, strpos($url, "://") + 3)); // Removes everything from the url before the "://" pattern (included) - if($url_without_prefix === '') { + public function addBookmark($userid, $url, $title, $tags = array(), $description = '', $isPublic = false) { + $public = $isPublic ? 1 : 0; + $urlWithoutPrefix = trim(substr($url, strpos($url, "://") + 3)); // Removes everything from the url before the "://" pattern (included) + if($urlWithoutPrefix === '') { throw new \InvalidArgumentException('Bookmark URL is missing'); } - $enc_url_noprefix = htmlspecialchars_decode($url_without_prefix); - $enc_url = htmlspecialchars_decode($url); + $decodedUrlNoPrefix = htmlspecialchars_decode($urlWithoutPrefix); + $decodedUrl = htmlspecialchars_decode($url); $title = mb_substr($title, 0, 4096); $description = mb_substr($description, 0, 4096); // Change lastmodified date if the record if already exists $sql = "SELECT * from `*PREFIX*bookmarks` WHERE `url` like ? AND `user_id` = ?"; - $query = $db->prepareQuery($sql, 1); - $result = $query->execute(array('%'.$enc_url_noprefix, $userid)); // Find url in the db independantly from its protocol + $query = $this->db->prepareQuery($sql, 1); + $result = $query->execute(array('%'.$decodedUrlNoPrefix, $userid)); // Find url in the db independantly from its protocol if ($row = $result->fetchRow()) { $params = array(); - $title_str = ''; + $titleStr = ''; if (trim($title) != '') { // Do we replace the old title - $title_str = ' , title = ?'; + $titleStr = ' , title = ?'; $params[] = $title; } - $desc_str = ''; + $descriptionStr = ''; if (trim($description) != '') { // Do we replace the old description - $desc_str = ' , description = ?'; + $descriptionStr = ' , description = ?'; $params[] = $description; } $sql = "UPDATE `*PREFIX*bookmarks` SET `lastmodified` = " - . "UNIX_TIMESTAMP() $title_str $desc_str , `url` = ? WHERE `url` like ? and `user_id` = ?"; - $params[] = $enc_url; - $params[] = '%'.$enc_url_noprefix; + . "UNIX_TIMESTAMP() $titleStr $descriptionStr , `url` = ? WHERE `url` like ? and `user_id` = ?"; + $params[] = $decodedUrl; + $params[] = '%'.$decodedUrlNoPrefix; $params[] = $userid; - $query = $db->prepareQuery($sql); + $query = $this->db->prepareQuery($sql); $query->execute($params); return $row['id']; } else { - $query = $db->prepareQuery(" + $query = $this->db->prepareQuery(" INSERT INTO `*PREFIX*bookmarks` (`url`, `title`, `user_id`, `public`, `added`, `lastmodified`, `description`) VALUES (?, ?, ?, ?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), ?) "); $params = array( - $enc_url, + $decodedUrl, htmlspecialchars_decode($title), $userid, $public, @@ -479,32 +493,31 @@ class Bookmarks { ); $query->execute($params); - $b_id = $db->getInsertId('*PREFIX*bookmarks'); + $insertId = $this->db->getInsertId('*PREFIX*bookmarks'); - if ($b_id !== false) { - self::addTags($db, $b_id, $tags); - return $b_id; + if ($insertId !== false) { + $this->addTags($insertId, $tags); + return $insertId; } } + return -1; } /** * @brief Add a set of tags for a bookmark - * @param IDb $db Database Interface * @param int $bookmarkID The bookmark reference * @param array $tags Set of tags to add to the bookmark - * @return null * */ - private static function addTags(IDb $db, $bookmarkID, $tags) { + private function addTags($bookmarkID, $tags) { $sql = 'INSERT INTO `*PREFIX*bookmarks_tags` (`bookmark_id`, `tag`) select ?, ? '; - $dbtype = \OCP\Config::getSystemValue('dbtype', 'sqlite'); + $dbType = $this->config->getSystemValue('dbtype', 'sqlite'); - if ($dbtype === 'mysql') { + if ($dbType === 'mysql') { $sql .= 'from dual '; } $sql .= 'where not exists(select * from `*PREFIX*bookmarks_tags` where `bookmark_id` = ? and `tag` = ?)'; - $query = $db->prepareQuery($sql); + $query = $this->db->prepareQuery($sql); foreach ($tags as $tag) { $tag = trim($tag); if (empty($tag)) { @@ -519,18 +532,16 @@ class Bookmarks { /** * @brief Import Bookmarks from html formatted file * @param string $user User imported Bookmarks should belong to - * @param IDb $db Database Interface * @param string $file Content to import * @return null * */ - public static function importFile($user, IDb $db, $file) { + public function importFile($user, $file) { libxml_use_internal_errors(true); $dom = new \domDocument(); $dom->loadHTMLFile($file); $links = $dom->getElementsByTagName('a'); - $l = \OC::$server->getL10NFactory()->get('bookmarks'); $errors = []; // Reintroduce transaction here!? @@ -538,19 +549,19 @@ class Bookmarks { /* @var \DOMElement $link */ $title = $link->nodeValue; $ref = $link->getAttribute("href"); - $tag_str = ''; + $tagStr = ''; if ($link->hasAttribute("tags")) - $tag_str = $link->getAttribute("tags"); - $tags = explode(',', $tag_str); + $tagStr = $link->getAttribute("tags"); + $tags = explode(',', $tagStr); - $desc_str = ''; + $descriptionStr = ''; if ($link->hasAttribute("description")) - $desc_str = $link->getAttribute("description"); + $descriptionStr = $link->getAttribute("description"); try { - self::addBookmark($user, $db, $ref, $title, $tags, $desc_str); + $this->addBookmark($user, $ref, $title, $tags, $descriptionStr); } catch (\InvalidArgumentException $e) { - \OC::$server->getLogger()->logException($e, ['app' => 'bookmarks']); - $errors[] = $l->t('Failed to import one bookmark, because: ') . $e->getMessage(); + $this->logger->logException($e, ['app' => 'bookmarks']); + $errors[] = $this->l->t('Failed to import one bookmark, because: ') . $e->getMessage(); } } @@ -563,14 +574,14 @@ class Bookmarks { * @return array Metadata for url; * @throws \Exception|ClientException */ - public static function getURLMetadata($url) { + public function getURLMetadata($url) { $metadata = array(); $metadata['url'] = $url; - $page = ""; + $page = $contentType = ''; try { - $request = \OC::$server->getHTTPClientService()->newClient()->get($url); + $request = $this->httpClientService->newClient()->get($url); $page = $request->getBody(); $contentType = $request->getHeader('Content-Type'); } catch (ClientException $e) { @@ -614,11 +625,11 @@ class Bookmarks { } /** - * @brief Seperate Url String at comma charachter + * @brief Separate Url String at comma character * @param $line String of Tags * @return array Array of Tags * */ - public static function analyzeTagRequest($line) { + public function analyzeTagRequest($line) { $tags = explode(',', $line); $filterTag = array(); foreach ($tags as $tag) { diff --git a/controller/lib/search.php b/controller/lib/search.php index aaaf9193..dcba38d0 100644 --- a/controller/lib/search.php +++ b/controller/lib/search.php @@ -23,7 +23,7 @@ namespace OCA\Bookmarks\Controller\Lib; -use \OCA\Bookmarks\Controller\Lib\Bookmarks; +use OCA\Bookmarks\AppInfo\Application; class Search extends \OCP\Search\Provider{ @@ -36,10 +36,12 @@ class Search extends \OCP\Search\Provider{ $search_words = $query; } - $db = \OC::$server->getDb(); $user = \OCP\User::getUser(); - $bookmarks = Bookmarks::findBookmarks($user, $db, 0, 'id', $search_words, false); + $app = new Application(); + $libBookmarks = $app->getContainer()->query(Bookmarks::class); + + $bookmarks = $libBookmarks->findBookmarks($user, 0, 'id', $search_words, false); $l = new \OC_l10n('bookmarks'); //resulttype can't be localized, javascript relies on that type foreach ($bookmarks as $bookmark) { $results[] = new \OC_Search_Result($bookmark['title'], $bookmark['title'], $bookmark['url'], (string) $l->t('Bookm.')); diff --git a/controller/rest/bookmarkcontroller.php b/controller/rest/bookmarkcontroller.php index d0a2ff18..e52acfff 100644 --- a/controller/rest/bookmarkcontroller.php +++ b/controller/rest/bookmarkcontroller.php @@ -29,12 +29,16 @@ class BookmarkController extends ApiController { private $db; private $l10n; - public function __construct($appName, IRequest $request, $userId, IDb $db, IL10N $l10n) { + /** @var Bookmarks */ + private $bookmarks; + + public function __construct($appName, IRequest $request, $userId, IDb $db, IL10N $l10n, Bookmarks $bookmarks) { parent::__construct($appName, $request); $this->userId = $userId; $this->db = $db; $this->request = $request; $this->l10n = $l10n; + $this->bookmarks = $bookmarks; } /** @@ -62,11 +66,11 @@ class BookmarkController extends ApiController { public function getBookmarks($type = "bookmark", $tag = '', $page = 0, $sort = "bookmarks_sorting_recent") { if ($type == 'rel_tags') { - $tags = Bookmarks::analyzeTagRequest($tag); - $qtags = Bookmarks::findTags($this->userId, $this->db, $tags); + $tags = $this->bookmarks->analyzeTagRequest($tag); + $qtags = $this->bookmarks->findTags($this->userId, $tags); return new JSONResponse(array('data' => $qtags, 'status' => 'success')); } else { // type == bookmark - $filterTag = Bookmarks::analyzeTagRequest($tag); + $filterTag = $this->bookmarks->analyzeTagRequest($tag); $offset = $page * 10; @@ -75,7 +79,7 @@ class BookmarkController extends ApiController { } else { $sqlSortColumn = 'lastmodified'; } - $bookmarks = Bookmarks::findBookmarks($this->userId, $this->db, $offset, $sqlSortColumn, $filterTag, true); + $bookmarks = $this->bookmarks->findBookmarks($this->userId, $offset, $sqlSortColumn, $filterTag, true); return new JSONResponse(array('data' => $bookmarks, 'status' => 'success')); } } @@ -98,15 +102,15 @@ class BookmarkController extends ApiController { $protocols = '/^(https?|s?ftp)\:\/\//i'; try { if (preg_match($protocols, $url)) { - $data = Bookmarks::getURLMetadata($url); + $data = $this->bookmarks->getURLMetadata($url); // if not (allowed) protocol is given, assume http and https (and fetch both) } else { // append https to url and fetch it $url_https = 'https://' . $url; - $data_https = Bookmarks::getURLMetadata($url_https); + $data_https = $this->bookmarks->getURLMetadata($url_https); // append http to url and fetch it $url_http = 'http://' . $url; - $data_http = Bookmarks::getURLMetadata($url_http); + $data_http = $this->bookmarks->getURLMetadata($url_http); } } catch (\Exception $e) { return new JSONResponse(array('status' => 'error'), Http::STATUS_BAD_REQUEST); @@ -132,8 +136,8 @@ class BookmarkController extends ApiController { $tags = isset($item['tags']) ? $item['tags'] : array(); - $id = Bookmarks::addBookmark($this->userId, $this->db, $url, $title, $tags, $description, $is_public); - $bm = Bookmarks::findUniqueBookmark($id, $this->userId, $this->db); + $id = $this->bookmarks->addBookmark($this->userId, $url, $title, $tags, $description, $is_public); + $bm = $this->bookmarks->findUniqueBookmark($id, $this->userId); return new JSONResponse(array('item' => $bm, 'status' => 'success')); } @@ -185,10 +189,10 @@ class BookmarkController extends ApiController { $tags = isset($item['tags']) ? $item['tags'] : array(); if (is_numeric($record_id)) { - $id = Bookmarks::editBookmark($this->userId, $this->db, $record_id, $url, $title, $tags, $description, $is_public = false); + $id = $this->bookmarks->editBookmark($this->userId, $record_id, $url, $title, $tags, $description, $is_public = false); } - $bm = Bookmarks::findUniqueBookmark($id, $this->userId, $this->db); + $bm = $this->bookmarks->findUniqueBookmark($id, $this->userId); return new JSONResponse(array('item' => $bm, 'status' => 'success')); } @@ -213,7 +217,7 @@ class BookmarkController extends ApiController { return new JSONResponse(array(), Http::STATUS_BAD_REQUEST); } - if (!Bookmarks::deleteUrl($this->userId, $this->db, $id)) { + if (!$this->bookmarks->deleteUrl($this->userId, $id)) { return new JSONResponse(array(), Http::STATUS_BAD_REQUEST); } else { return new JSONResponse(array('status' => 'success'), Http::STATUS_OK); @@ -263,7 +267,7 @@ class BookmarkController extends ApiController { $error = array(); $file = $full_input['tmp_name']; if ($full_input['type'] == 'text/html') { - $error = Bookmarks::importFile($this->userId, $this->db, $file); + $error = $this->bookmarks->importFile($this->userId, $file); if (empty($error)) { return new JSONResponse(array('status' => 'success')); } @@ -292,7 +296,7 @@ Do Not Edit! -->

Bookmarks

EOT; - $bookmarks = Bookmarks::findBookmarks($this->userId, $this->db, 0, 'id', array(), true, -1); + $bookmarks = $this->bookmarks->findBookmarks($this->userId, 0, 'id', [], true, -1); foreach ($bookmarks as $bm) { $title = $bm['title']; if (trim($title) === '') { diff --git a/controller/rest/publiccontroller.php b/controller/rest/publiccontroller.php index c644d174..95442d6a 100644 --- a/controller/rest/publiccontroller.php +++ b/controller/rest/publiccontroller.php @@ -4,7 +4,6 @@ namespace OCA\Bookmarks\Controller\Rest; use \OCP\AppFramework\ApiController; use \OCP\IRequest; -use \OCP\IDb; use \OCP\AppFramework\Http\JSONResponse; use \OC\User\Manager; use OCA\Bookmarks\Controller\Lib\Bookmarks; @@ -12,14 +11,15 @@ use OCP\Util; class PublicController extends ApiController { - private $db; private $userManager; - public function __construct($appName, IRequest $request, IDb $db, Manager $userManager) { - parent::__construct( - $appName, $request); + /** @var Bookmarks */ + protected $bookmarks; - $this->db = $db; + public function __construct($appName, IRequest $request, Bookmarks $bookmarks, Manager $userManager) { + parent::__construct($appName, $request); + + $this->bookmarks = $bookmarks; $this->userManager = $userManager; } @@ -73,7 +73,7 @@ class PublicController extends ApiController { $attributesToSelect = array_unique($attributesToSelect); } - $output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction); + $output = $this->bookmarks->findBookmarks($user, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction); if (count($output) == 0) { $output["status"] = 'error'; diff --git a/controller/rest/tagscontroller.php b/controller/rest/tagscontroller.php index 1aae9fbf..a74f8ab9 100644 --- a/controller/rest/tagscontroller.php +++ b/controller/rest/tagscontroller.php @@ -7,17 +7,18 @@ use \OCP\AppFramework\Http\JSONResponse; use \OCP\AppFramework\Http; use \OCP\AppFramework\ApiController; use \OCP\IRequest; -use \OCP\IDb; class TagsController extends ApiController { private $userId; - private $db; - public function __construct($appName, IRequest $request, $userId, IDb $db) { + /** @var Bookmarks */ + private $bookmarks; + + public function __construct($appName, IRequest $request, $userId, Bookmarks $bookmarks) { parent::__construct($appName, $request); $this->userId = $userId; - $this->db = $db; + $this->bookmarks = $bookmarks; } /** @@ -32,7 +33,7 @@ class TagsController extends ApiController { return new JSONResponse(array(), Http::STATUS_BAD_REQUEST); } - Bookmarks::deleteTag($this->userId, $this->db, $old_name); + $this->bookmarks->deleteTag($this->userId, $old_name); return new JSONResponse(array('status' => 'success')); } @@ -49,7 +50,7 @@ class TagsController extends ApiController { return new JSONResponse(array(), Http::STATUS_BAD_REQUEST); } - Bookmarks::renameTag($this->userId, $this->db, $old_name, $new_name); + $this->bookmarks->renameTag($this->userId, $old_name, $new_name); return new JSONResponse(array('status' => 'success')); } @@ -61,7 +62,7 @@ class TagsController extends ApiController { header("Cache-Control: no-cache, must-revalidate"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); - $qtags = Bookmarks::findTags($this->userId, $this->db, array(), 0, 400); + $qtags = $this->bookmarks->findTags($this->userId, array(), 0, 400); $tags = array(); foreach ($qtags as $tag) { $tags[] = $tag['tag']; diff --git a/controller/webviewcontroller.php b/controller/webviewcontroller.php index 13213b2b..93e94b60 100644 --- a/controller/webviewcontroller.php +++ b/controller/webviewcontroller.php @@ -16,7 +16,6 @@ use OCP\AppFramework\Http\ContentSecurityPolicy; use \OCP\IRequest; use \OCP\AppFramework\Http\TemplateResponse; use \OCP\AppFramework\Controller; -use \OCP\IDb; use \OCA\Bookmarks\Controller\Lib\Bookmarks; use OCP\IURLGenerator; @@ -28,8 +27,8 @@ class WebViewController extends Controller { /** @var IURLGenerator */ private $urlgenerator; - /** @var IDb */ - private $db; + /** @var Bookmarks */ + private $bookmarks; /** * WebViewController constructor. @@ -38,13 +37,13 @@ class WebViewController extends Controller { * @param IRequest $request * @param $userId * @param IURLGenerator $urlgenerator - * @param IDb $db + * @param Bookmarks $bookmarks */ - public function __construct($appName, IRequest $request, $userId, IURLGenerator $urlgenerator, IDb $db) { + public function __construct($appName, IRequest $request, $userId, IURLGenerator $urlgenerator, Bookmarks $bookmarks) { parent::__construct($appName, $request); $this->userId = $userId; $this->urlgenerator = $urlgenerator; - $this->db = $db; + $this->bookmarks = $bookmarks; } /** @@ -72,11 +71,11 @@ class WebViewController extends Controller { * @NoCSRFRequired */ public function bookmarklet($url = "", $title = "") { - $bookmarkExists = Bookmarks::bookmarkExists($url, $this->userId, $this->db); + $bookmarkExists = $this->bookmarks->bookmarkExists($url, $this->userId); $description = ""; $tags = []; if ($bookmarkExists !== false){ - $bookmark = Bookmarks::findUniqueBookmark($bookmarkExists, $this->userId, $this->db); + $bookmark = $this->bookmarks->findUniqueBookmark($bookmarkExists, $this->userId); $description = $bookmark['description']; $tags = $bookmark['tags']; }