From: Mikael Nordfeldth Date: Sat, 10 Oct 2015 21:15:51 +0000 (+0200) Subject: Stricter typing for Bookmark plugin X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b209276e724d3b8a7780857a2d522eba6811999d;p=quix0rs-gnu-social.git Stricter typing for Bookmark plugin --- diff --git a/plugins/Bookmark/actions/newbookmark.php b/plugins/Bookmark/actions/newbookmark.php index 7551994b7c..823b7cfbb6 100644 --- a/plugins/Bookmark/actions/newbookmark.php +++ b/plugins/Bookmark/actions/newbookmark.php @@ -93,7 +93,7 @@ class NewbookmarkAction extends Action $this->title = $this->trimmed('title'); $this->url = $this->trimmed('url'); - $this->tags = $this->trimmed('tags'); + $this->tags = preg_split('/[\s,]+/', $this->trimmed('tags'), null, PREG_SPLIT_NO_EMPTY); $this->description = $this->trimmed('description'); return true; diff --git a/plugins/Bookmark/classes/Bookmark.php b/plugins/Bookmark/classes/Bookmark.php index cdbdf526ca..a3a608ce5e 100644 --- a/plugins/Bookmark/classes/Bookmark.php +++ b/plugins/Bookmark/classes/Bookmark.php @@ -112,11 +112,11 @@ class Bookmark extends Managed_DataObject $nb->profile_id = $profile->id; $nb->url = $url; - if ($nb->find(true)) { - return $nb; - } else { - return null; + if (!$nb->find(true)) { + throw new NoResultException($nb); } + + return $nb; } /** @@ -125,28 +125,24 @@ class Bookmark extends Managed_DataObject * @param Profile $profile To save the bookmark for * @param string $title Title of the bookmark * @param string $url URL of the bookmark - * @param mixed $rawtags array of tags or string + * @param array $rawtags array of tags * @param string $description Description of the bookmark * @param array $options Options for the Notice::saveNew() * * @return Notice saved notice */ - static function saveNew($profile, $title, $url, $rawtags, $description, - $options=null) + static function saveNew(Profile $profile, $title, $url, $rawtags, $description, + array $options=array()) { if (!common_valid_http_url($url)) { throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).')); } - $nb = self::getByURL($profile, $url); - - if (!empty($nb)) { - // TRANS: Client exception thrown when trying to save a new bookmark that already exists. - throw new ClientException(_m('Bookmark already exists.')); - } - - if (empty($options)) { - $options = array(); + try { + $object = self::getByURL($profile, $url); + return $object; + } catch (NoResultException $e) { + // Alright, so then we have to create it. } if (array_key_exists('uri', $options)) { @@ -157,14 +153,6 @@ class Bookmark extends Managed_DataObject } } - if (is_string($rawtags)) { - if (empty($rawtags)) { - $rawtags = array(); - } else { - $rawtags = preg_split('/[\s,]+/', $rawtags); - } - } - $nb = new Bookmark(); $nb->id = UUID::gen(); diff --git a/plugins/Bookmark/lib/deliciousbackupimporter.php b/plugins/Bookmark/lib/deliciousbackupimporter.php index 0ceba61d89..c8caef689a 100644 --- a/plugins/Bookmark/lib/deliciousbackupimporter.php +++ b/plugins/Bookmark/lib/deliciousbackupimporter.php @@ -202,7 +202,7 @@ class DeliciousBackupImporter extends QueueHandler 'title' => $a->nodeValue, 'description' => $description, 'url' => $a->getAttribute('href'), - 'tags' => $a->getAttribute('tags'), + 'tags' => preg_split('/[\s,]+/', $a->getAttribute('tags'), null, PREG_SPLIT_NO_EMPTY), 'created' => common_sql_date(intval($addDate)) );