Stricter typing for Bookmark plugin

This commit is contained in:
Mikael Nordfeldth 2015-10-10 23:15:51 +02:00
parent 5726459629
commit b209276e72
3 changed files with 14 additions and 26 deletions

View File

@ -93,7 +93,7 @@ class NewbookmarkAction extends Action
$this->title = $this->trimmed('title'); $this->title = $this->trimmed('title');
$this->url = $this->trimmed('url'); $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'); $this->description = $this->trimmed('description');
return true; return true;

View File

@ -112,11 +112,11 @@ class Bookmark extends Managed_DataObject
$nb->profile_id = $profile->id; $nb->profile_id = $profile->id;
$nb->url = $url; $nb->url = $url;
if ($nb->find(true)) { if (!$nb->find(true)) {
return $nb; throw new NoResultException($nb);
} else {
return null;
} }
return $nb;
} }
/** /**
@ -125,28 +125,24 @@ class Bookmark extends Managed_DataObject
* @param Profile $profile To save the bookmark for * @param Profile $profile To save the bookmark for
* @param string $title Title of the bookmark * @param string $title Title of the bookmark
* @param string $url URL 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 string $description Description of the bookmark
* @param array $options Options for the Notice::saveNew() * @param array $options Options for the Notice::saveNew()
* *
* @return Notice saved notice * @return Notice saved notice
*/ */
static function saveNew($profile, $title, $url, $rawtags, $description, static function saveNew(Profile $profile, $title, $url, $rawtags, $description,
$options=null) array $options=array())
{ {
if (!common_valid_http_url($url)) { if (!common_valid_http_url($url)) {
throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).')); throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
} }
$nb = self::getByURL($profile, $url); try {
$object = self::getByURL($profile, $url);
if (!empty($nb)) { return $object;
// TRANS: Client exception thrown when trying to save a new bookmark that already exists. } catch (NoResultException $e) {
throw new ClientException(_m('Bookmark already exists.')); // Alright, so then we have to create it.
}
if (empty($options)) {
$options = array();
} }
if (array_key_exists('uri', $options)) { 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 = new Bookmark();
$nb->id = UUID::gen(); $nb->id = UUID::gen();

View File

@ -202,7 +202,7 @@ class DeliciousBackupImporter extends QueueHandler
'title' => $a->nodeValue, 'title' => $a->nodeValue,
'description' => $description, 'description' => $description,
'url' => $a->getAttribute('href'), '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)) 'created' => common_sql_date(intval($addDate))
); );