3 * Data class to mark notices as bookmarks
9 * @author Evan Prodromou <evan@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2009, StatusNet, Inc.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Affero General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Affero General Public License for more details.
26 * You should have received a copy of the GNU Affero General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 if (!defined('GNUSOCIAL')) { exit(1); }
33 * For storing the fact that a notice is a bookmark
37 * @author Evan Prodromou <evan@status.net>
38 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
39 * @link http://status.net/
43 class Bookmark extends Managed_DataObject
45 public $__table = 'bookmark'; // table name
46 public $id; // char(36) primary_key not_null
47 public $profile_id; // int(4) not_null
48 public $url; // varchar(191) not_null not 255 because utf8mb4 takes more space
49 public $title; // varchar(191) not 255 because utf8mb4 takes more space
50 public $uri; // varchar(191) not 255 because utf8mb4 takes more space
51 public $description; // text
52 public $created; // datetime
54 public static function schemaDef()
58 'id' => array('type' => 'char',
61 'profile_id' => array('type' => 'int', 'not null' => true),
62 'uri' => array('type' => 'varchar',
65 'url' => array('type' => 'varchar',
68 'title' => array('type' => 'varchar', 'length' => 191),
69 'description' => array('type' => 'text'),
70 'created' => array('type' => 'datetime', 'not null' => true),
72 'primary key' => array('uri'),
73 'unique keys' => array(
74 'bookmark_id_key' => array('id'),
76 'foreign keys' => array(
77 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
78 'bookmark_uri_fkey' => array('notice', array('uri' => 'uri')),
80 'indexes' => array('bookmark_created_idx' => array('created'),
81 'bookmark_url_idx' => array('url'),
82 'bookmark_profile_id_idx' => array('profile_id'),
88 * Get a bookmark based on a notice
90 * @param Notice $stored Notice activity which represents the Bookmark
92 * @return Bookmark The found bookmark object.
93 * @throws NoResultException When you don't find it after all.
95 <<<<<<< .merge_file_ZPs3Af
96 static public function fromStored(Notice $stored)
98 return self::getByPK(array('uri' => $stored->getUri()));
101 public function getStored()
103 static function getByNotice(Notice $notice)
104 >>>>>>> .merge_file_eD7Wwf
106 return Notice::getByKeys(array('uri' => $this->getUri()));
109 public function getDescription()
111 return $this->description;
114 public function getTitle()
119 public function getUri()
124 public function getUrl()
126 if (empty($this->url)) {
127 throw new InvalidUrlException($this->url);
133 * Get the bookmark that a user made for an URL
135 * @param Profile $profile Profile to check for
136 * @param string $url URL to check for
138 * @return Bookmark bookmark found or null
140 static function getByURL(Profile $profile, $url)
142 $nb = new Bookmark();
144 $nb->profile_id = $profile->getID();
147 if (!$nb->find(true)) {
148 throw new NoResultException($nb);
155 * Store a Bookmark object
157 * @param Profile $profile To save the bookmark for
158 * @param string $title Title of the bookmark
159 * @param string $url URL of the bookmark
160 * @param string $description Description of the bookmark
162 * @return Bookmark the Bookmark object
164 <<<<<<< .merge_file_ZPs3Af
165 static function saveActivityObject(ActivityObject $actobj, Notice $stored)
167 static function saveNew(Profile $profile, $title, $url, $rawtags, $description,
168 array $options=array())
169 >>>>>>> .merge_file_eD7Wwf
172 // each extra element is array('tagname', array('attr'=>'val', ...), 'content')
173 foreach ($actobj->extra as $extra) {
174 if ($extra[1]['rel'] !== 'related') {
177 if ($url===null && strlen($extra[1]['href'])>0) {
178 $url = $extra[1]['href'];
179 } elseif ($url !== null) {
180 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
181 throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
185 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
186 throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
189 <<<<<<< .merge_file_ZPs3Af
190 if (!strlen($actobj->title)) {
191 throw new ClientException(_m('You must provide a non-empty title.'));
193 if (!common_valid_http_url($url)) {
194 throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
196 if (array_key_exists('uri', $options)) {
197 $other = Bookmark::getKV('uri', $options['uri']);
198 if (!empty($other)) {
199 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
200 throw new ClientException(_m('Bookmark already exists.'));
202 >>>>>>> .merge_file_eD7Wwf
206 $object = self::getByURL($stored->getProfile(), $url);
207 throw new ClientException(_m('You have already bookmarked this URL.'));
208 } catch (NoResultException $e) {
209 // Alright, so then we have to create it.
212 $nb = new Bookmark();
214 $nb->id = UUID::gen();
215 $nb->uri = $stored->getUri();
216 $nb->profile_id = $stored->getProfile()->getID();
217 $nb->title = $actobj->title;
219 $nb->description = $actobj->summary;
220 $nb->created = $stored->created;
222 $result = $nb->insert();
223 if ($result === false) {
224 throw new ServerException('Could not insert Bookmark into database!');
230 public function asActivityObject()
232 $stored = $this->getStored();
234 $object = new ActivityObject();
235 $object->id = $this->getUri();
236 $object->type = ActivityObject::BOOKMARK;
237 $object->title = $this->getTitle();
238 $object->summary = $this->getDescription();
239 $object->link = $stored->getUrl();
240 $object->content = $stored->getRendered();
242 // Attributes of the URL
244 $attachments = $stored->attachments();
246 if (count($attachments) != 1) {
247 // TRANS: Server exception thrown when a bookmark has multiple attachments.
248 throw new ServerException(_m('Bookmark notice with the '.
249 'wrong number of attachments.'));
252 $bookmarkedurl = $attachments[0];
254 $attrs = array('rel' => 'related',
255 'href' => $bookmarkedurl->getUrl());
257 if (!strlen($bookmarkedurl->title)) {
258 $attrs['title'] = $bookmarkedurl->title;
261 $object->extra[] = array('link', $attrs, null);
263 // Attributes of the thumbnail, if any
266 $thumbnail = $bookmarkedurl->getThumbnail();
267 $tattrs = array('rel' => 'preview',
268 'href' => $thumbnail->getUrl());
270 if (!empty($thumbnail->width)) {
271 $tattrs['media:width'] = $thumbnail->width;
274 if (!empty($thumbnail->height)) {
275 $tattrs['media:height'] = $thumbnail->height;
278 $object->extra[] = array('link', $tattrs, null);
279 } catch (UnsupportedMediaException $e) {
280 // No image thumbnail metadata available
287 * Save a new notice bookmark
289 * @param Profile $profile To save the bookmark for
290 * @param string $title Title of the bookmark
291 * @param string $url URL of the bookmark
292 * @param array $rawtags array of tags
293 * @param string $description Description of the bookmark
294 * @param array $options Options for the Notice::saveNew()
296 * @return Notice saved notice
298 static function addNew(Profile $actor, $title, $url, array $rawtags, $description, array $options=array())
300 $act = new Activity();
301 $act->verb = ActivityVerb::POST;
303 $act->actor = $actor->asActivityObject();
305 $actobj = new ActivityObject();
306 $actobj->type = ActivityObject::BOOKMARK;
307 $actobj->title = $title;
308 $actobj->summary = $description;
309 $actobj->extra[] = array('link', array('rel'=>'related', 'href'=>$url), null);
310 $act->objects[] = $actobj;
312 $act->enclosures[] = $url;
317 // filter "for:nickname" tags
318 foreach ($rawtags as $tag) {
319 if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
320 // skip if done by caller
321 if (!array_key_exists('replies', $options)) {
322 $nickname = mb_substr($tag, 4);
323 $other = common_relative_profile($actor, $nickname);
324 if (!empty($other)) {
325 $replies[] = $other->getUri();
329 $tags[] = common_canonical_tag($tag);
336 foreach ($tags as $tag) {
337 $hashtags[] = '#'.$tag;
338 $attrs = array('href' => Notice_tag::url($tag),
341 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
344 // Use user's preferences for short URLs, if possible
345 // FIXME: Should be possible to with the Profile object...
347 $user = $actor->getUser();
348 $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user);
349 } catch (Exception $e) {
350 // Don't let this stop us.
354 // TRANS: Rendered bookmark content.
355 // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
356 // TRANS: %4$s is space separated list of hash tags.
357 $actobj->content = sprintf(_m('<span class="xfolkentry">'.
358 '<a class="taggedlink" href="%1$s">%2$s</a> '.
359 '<span class="description">%3$s</span> '.
360 '<span class="meta">%4$s</span>'.
362 htmlspecialchars($url),
363 htmlspecialchars($title),
364 htmlspecialchars($description),
365 implode(' ', $taglinks));
367 foreach ($tags as $term) {
368 $catEl = new AtomCategory();
369 $catEl->term = $term;
370 $activity->categories[] = $catEl;
373 $options = array_merge(array('urls' => array($url),
374 'rendered' => $rendered,
376 'replies' => $replies,
377 'object_type' => ActivityObject::BOOKMARK),
380 return Notice::saveActivity($act, $actor, $options);