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 static public function fromStored(Notice $stored)
97 return self::getByPK(array('uri' => $stored->getUri()));
100 public function getStored()
102 return Notice::getByKeys(array('uri' => $this->getUri()));
105 public function getDescription()
107 return $this->description;
110 public function getTitle()
115 public function getUri()
120 public function getUrl()
122 if (empty($this->url)) {
123 throw new InvalidUrlException($this->url);
129 * Get the bookmark that a user made for an URL
131 * @param Profile $profile Profile to check for
132 * @param string $url URL to check for
134 * @return Bookmark bookmark found or null
136 static function getByURL(Profile $profile, $url)
138 $nb = new Bookmark();
140 $nb->profile_id = $profile->getID();
143 if (!$nb->find(true)) {
144 throw new NoResultException($nb);
151 * Store a Bookmark object
153 * @param Profile $profile To save the bookmark for
154 * @param string $title Title of the bookmark
155 * @param string $url URL of the bookmark
156 * @param string $description Description of the bookmark
158 * @return Bookmark the Bookmark object
160 static function saveActivityObject(ActivityObject $actobj, Notice $stored)
162 common_debug('saving bookmark activity object: '.var_export($actobj,true));
164 // each extra element is array('tagname', array('attr'=>'val', ...), 'content')
165 foreach ($actobj->extra as $extra) {
166 common_debug('bookmark extra: '.var_export($extra,true));
167 if ($extra[1]['rel'] !== 'related') {
170 if ($url===null && strlen($extra[1]['href'])>0) {
171 $url = $extra[1]['href'];
172 } elseif ($url !== null) {
173 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
174 throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
178 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
179 throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
182 if (!strlen($actobj->title)) {
183 throw new ClientException(_m('You must provide a non-empty title.'));
185 if (!common_valid_http_url($url)) {
186 throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
190 $object = self::getByURL($stored->getProfile(), $url);
191 throw new ClientException(_m('You have already bookmarked this URL.'));
192 } catch (NoResultException $e) {
193 // Alright, so then we have to create it.
196 $nb = new Bookmark();
198 $nb->id = UUID::gen();
199 $nb->uri = $stored->getUri();
200 $nb->profile_id = $stored->getProfile()->getID();
201 $nb->title = $actobj->title;
203 $nb->description = $actobj->summary;
204 $nb->created = $stored->created;
206 $result = $nb->insert();
207 if ($result === false) {
208 throw new ServerException('Could not insert Bookmark into database!');
214 public function asActivityObject()
216 $stored = $this->getStored();
218 $object = new ActivityObject();
219 $object->id = $this->getUri();
220 $object->type = ActivityObject::BOOKMARK;
221 $object->title = $this->getTitle();
222 $object->summary = $this->getDescription();
223 $object->link = $stored->getUrl();
224 $object->content = $stored->getRendered();
226 // Attributes of the URL
228 $attachments = $stored->attachments();
230 if (count($attachments) != 1) {
231 // TRANS: Server exception thrown when a bookmark has multiple attachments.
232 throw new ServerException(_m('Bookmark notice with the '.
233 'wrong number of attachments.'));
236 $bookmarkedurl = $attachments[0];
238 $attrs = array('rel' => 'related',
239 'href' => $bookmarkedurl->getUrl());
241 if (!strlen($bookmarkedurl->title)) {
242 $attrs['title'] = $bookmarkedurl->title;
245 $object->extra[] = array('link', $attrs, null);
247 // Attributes of the thumbnail, if any
250 $thumbnail = $bookmarkedurl->getThumbnail();
251 $tattrs = array('rel' => 'preview',
252 'href' => $thumbnail->getUrl());
254 if (!empty($thumbnail->width)) {
255 $tattrs['media:width'] = $thumbnail->width;
258 if (!empty($thumbnail->height)) {
259 $tattrs['media:height'] = $thumbnail->height;
262 $object->extra[] = array('link', $tattrs, null);
263 } catch (UnsupportedMediaException $e) {
264 // No image thumbnail metadata available
271 * Save a new notice bookmark
273 * @param Profile $profile To save the bookmark for
274 * @param string $title Title of the bookmark
275 * @param string $url URL of the bookmark
276 * @param array $rawtags array of tags
277 * @param string $description Description of the bookmark
278 * @param array $options Options for the Notice::saveNew()
280 * @return Notice saved notice
282 static function addNew(Profile $actor, $title, $url, array $rawtags, $description, array $options=array())
284 $act = new Activity();
285 $act->verb = ActivityVerb::POST;
287 $act->actor = $actor->asActivityObject();
289 $actobj = new ActivityObject();
290 $actobj->type = ActivityObject::BOOKMARK;
291 $actobj->title = $title;
292 $actobj->summary = $description;
293 $actobj->extra[] = array('link', array('rel'=>'related', 'href'=>$url), null);
294 $act->objects[] = $actobj;
296 $act->enclosures[] = $url;
301 // filter "for:nickname" tags
302 foreach ($rawtags as $tag) {
303 if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
304 // skip if done by caller
305 if (!array_key_exists('replies', $options)) {
306 $nickname = mb_substr($tag, 4);
307 $other = common_relative_profile($actor, $nickname);
308 if (!empty($other)) {
309 $replies[] = $other->getUri();
313 $tags[] = common_canonical_tag($tag);
320 foreach ($tags as $tag) {
321 $hashtags[] = '#'.$tag;
322 $attrs = array('href' => Notice_tag::url($tag),
325 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
328 // Use user's preferences for short URLs, if possible
329 // FIXME: Should be possible to with the Profile object...
331 $user = $actor->getUser();
332 $shortUrl = File_redirection::makeShort($url, empty($user) ? null : $user);
333 } catch (Exception $e) {
334 // Don't let this stop us.
338 // TRANS: Rendered bookmark content.
339 // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
340 // TRANS: %4$s is space separated list of hash tags.
341 $actobj->content = sprintf(_m('<span class="xfolkentry">'.
342 '<a class="taggedlink" href="%1$s">%2$s</a> '.
343 '<span class="description">%3$s</span> '.
344 '<span class="meta">%4$s</span>'.
346 htmlspecialchars($url),
347 htmlspecialchars($title),
348 htmlspecialchars($description),
349 implode(' ', $taglinks));
351 foreach ($tags as $term) {
352 $catEl = new AtomCategory();
353 $catEl->term = $term;
354 $activity->categories[] = $catEl;
357 $options = array_merge(array('urls' => array($url),
358 'rendered' => $rendered,
360 'replies' => $replies,
361 'object_type' => ActivityObject::BOOKMARK),
364 return Notice::saveActivity($act, $actor, $options);