3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A plugin to enable social-bookmarking functionality
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @category SocialBookmark
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('GNUSOCIAL')) { exit(1); }
34 * Bookmark plugin main class
38 * @author Brion Vibber <brionv@status.net>
39 * @author Evan Prodromou <evan@status.net>
40 * @copyright 2010 StatusNet, Inc.
41 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
42 * @link http://status.net/
44 class BookmarkPlugin extends MicroAppPlugin
46 const VERSION = '0.1';
47 const IMPORTDELICIOUS = 'BookmarkPlugin:IMPORTDELICIOUS';
50 * Authorization for importing delicious bookmarks
52 * By default, everyone can import bookmarks except silenced people.
54 * @param Profile $profile Person whose rights to check
55 * @param string $right Right to check; const value
56 * @param boolean &$result Result of the check, writeable
58 * @return boolean hook value
60 function onUserRightsCheck($profile, $right, &$result)
62 if ($right == self::IMPORTDELICIOUS) {
63 $result = !$profile->isSilenced();
70 * Database schema setup
75 * @return boolean hook value; true means continue processing, false means stop.
77 function onCheckSchema()
79 $schema = Schema::get();
81 $schema->ensureTable('bookmark', Bookmark::schemaDef());
87 * Show the CSS necessary for this plugin
89 * @param Action $action the action being run
91 * @return boolean hook value
93 function onEndShowStyles($action)
95 $action->cssLink($this->path('css/bookmark.css'));
99 function onEndShowScripts($action)
101 $action->script($this->path('js/bookmark.js'));
106 * Map URLs to actions
108 * @param URLMapper $m path-to-action mapper
110 * @return boolean hook value; true means continue processing, false means stop.
112 public function onRouterInitialized(URLMapper $m)
114 if (common_config('singleuser', 'enabled')) {
115 $nickname = User::singleUserNickname();
116 $m->connect('bookmarks',
117 array('action' => 'bookmarks', 'nickname' => $nickname));
118 $m->connect('bookmarks/rss',
119 array('action' => 'bookmarksrss', 'nickname' => $nickname));
121 $m->connect(':nickname/bookmarks',
122 array('action' => 'bookmarks'),
123 array('nickname' => Nickname::DISPLAY_FMT));
124 $m->connect(':nickname/bookmarks/rss',
125 array('action' => 'bookmarksrss'),
126 array('nickname' => Nickname::DISPLAY_FMT));
129 $m->connect('api/bookmarks/:id.:format',
130 array('action' => 'ApiTimelineBookmarks',
131 'id' => Nickname::INPUT_FMT,
132 'format' => '(xml|json|rss|atom|as)'));
134 $m->connect('main/bookmark/new',
135 array('action' => 'newbookmark'),
136 array('id' => '[0-9]+'));
138 $m->connect('main/bookmark/popup',
139 array('action' => 'bookmarkpopup'));
141 $m->connect('main/bookmark/import',
142 array('action' => 'importdelicious'));
144 $m->connect('main/bookmark/forurl',
145 array('action' => 'bookmarkforurl'));
147 $m->connect('bookmark/:id',
148 array('action' => 'showbookmark'),
149 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
151 $m->connect('notice/by-url/:id',
152 array('action' => 'noticebyurl'),
153 array('id' => '[0-9]+'));
160 * Add our two queue handlers to the queue manager
162 * @param QueueManager $qm current queue manager
164 * @return boolean hook value
166 function onEndInitializeQueueManager($qm)
168 $qm->connect('dlcsback', 'DeliciousBackupImporter');
169 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
174 * Plugin version data
176 * @param array &$versions array of version data
180 function onPluginVersion(array &$versions)
182 $versions[] = array('name' => 'Bookmark',
183 'version' => self::VERSION,
184 'author' => 'Evan Prodromou, Stephane Berube, Jean Baptiste Favre',
185 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
187 // TRANS: Plugin description.
188 _m('Simple extension for supporting bookmarks. ') .
189 'BookmarkList feature has been developped by Stephane Berube. ' .
190 'Integration has been done by Jean Baptiste Favre.');
195 * Load our document if requested
197 * @param string &$title Title to fetch
198 * @param string &$output HTML to output
200 * @return boolean hook value
202 function onStartLoadDoc(&$title, &$output)
204 if ($title == 'bookmarklet') {
205 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
207 $c = file_get_contents($filename);
208 $output = common_markup_to_html($c);
209 return false; // success!
216 * Show a link to our delicious import page on profile settings form
218 * @param Action $action Profile settings action being shown
220 * @return boolean hook value
222 function onEndProfileSettingsActions($action)
224 $user = common_current_user();
226 if (!empty($user) && $user->hasRight(self::IMPORTDELICIOUS)) {
227 $action->elementStart('li');
228 $action->element('a',
229 array('href' => common_local_url('importdelicious')),
230 // TRANS: Link text in proile leading to import form.
231 _m('Import del.icio.us bookmarks'));
232 $action->elementEnd('li');
239 * Modify the default menu to link to our custom action
241 * Using event handlers, it's possible to modify the default UI for pages
242 * almost without limit. In this method, we add a menu item to the default
243 * primary menu for the interface to link to our action.
245 * The Action class provides a rich set of events to hook, as well as output
248 * @param Action $action The current action handler. Use this to
251 * @return boolean hook value; true means continue processing, false means stop.
255 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
257 $menu->menuItem(common_local_url('bookmarks', array('nickname' => $target->getNickname())),
258 // TRANS: Menu item in sample plugin.
260 // TRANS: Menu item title in sample plugin.
261 _m('A list of your bookmarks'), false, 'nav_timeline_bookmarks');
267 return array(ActivityObject::BOOKMARK);
271 * When a notice is deleted, delete the related Bookmark
273 * @param Notice $notice Notice being deleted
275 * @return boolean hook value
277 function deleteRelated(Notice $notice)
280 $nb = Bookmark::fromStored($notice);
281 } catch (NoResultException $e) {
282 throw new AlreadyFulfilledException('Bookmark already gone when deleting: '.$e->getMessage());
290 * Save a bookmark from an activity
292 * @param Activity $activity Activity to save
293 * @param Profile $actor Profile to use as author
294 * @param array $options Options to pass to bookmark-saving code
296 * @return Notice resulting notice
298 protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
300 $actobj = $activity->objects[0];
302 $relLinkEls = ActivityUtils::getLinks($actobj->element, 'related');
304 if (count($relLinkEls) !== 1) {
305 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
306 throw new ClientException(sprintf(_m('Expected exactly 1 link rel=related in a Bookmark, got %1$d.'), count($relLinkEls)));
309 return Bookmark::addNew($stored,
311 $relLinkEls[0]->getAttribute('href'),
315 function activityObjectFromNotice(Notice $notice)
317 assert($this->isMyNotice($notice));
320 "Formatting notice {$notice->uri} as a bookmark.");
322 $object = new ActivityObject();
323 $nb = Bookmark::fromStored($notice);
325 $object->id = $notice->uri;
326 $object->type = ActivityObject::BOOKMARK;
327 $object->title = $nb->getTitle();
328 $object->summary = $nb->getDescription();
329 $object->link = $notice->getUrl();
331 // Attributes of the URL
333 $attachments = $notice->attachments();
335 if (count($attachments) != 1) {
336 // TRANS: Server exception thrown when a bookmark has multiple attachments.
337 throw new ServerException(_m('Bookmark notice with the '.
338 'wrong number of attachments.'));
341 $target = $attachments[0];
343 $attrs = array('rel' => 'related',
344 'href' => $target->getUrl());
346 if (!empty($target->title)) {
347 $attrs['title'] = $target->title;
350 $object->extra[] = array('link', $attrs, null);
352 // Attributes of the thumbnail, if any
355 $thumbnail = $target->getThumbnail();
356 $tattrs = array('rel' => 'preview',
357 'href' => $thumbnail->getUrl());
359 if (!empty($thumbnail->width)) {
360 $tattrs['media:width'] = $thumbnail->width;
363 if (!empty($thumbnail->height)) {
364 $tattrs['media:height'] = $thumbnail->height;
367 $object->extra[] = array('link', $tattrs, null);
368 } catch (UnsupportedMediaException $e) {
369 // No image thumbnail metadata available
375 function entryForm($out)
377 return new InitialBookmarkForm($out);
387 // TRANS: Application title.
388 return _m('TITLE','Bookmark');
391 function onEndUpgrade()
393 // Version 0.9.x of the plugin didn't stamp notices
394 // with verb and object-type (for obvious reasons). Update
395 // those notices here.
397 $notice = new Notice();
399 $notice->whereAdd('exists (select uri from bookmark where bookmark.uri = notice.uri)');
400 $notice->whereAdd('((object_type is null) or (object_type = "' .ActivityObject::NOTE.'"))');
404 while ($notice->fetch()) {
405 $original = clone($notice);
406 $notice->verb = ActivityVerb::POST;
407 $notice->object_type = ActivityObject::BOOKMARK;
408 $notice->update($original);
412 public function activityObjectOutputJson(ActivityObject $obj, array &$out)
414 assert($obj->type == ActivityObject::BOOKMARK);
416 $bm = Bookmark::getByPK(array('uri' => $obj->id));
418 $out['displayName'] = $bm->getTitle();
419 $out['targetUrl'] = $bm->getUrl();
424 protected function showNoticeItemNotice(NoticeListItem $nli)
426 $nli->out->elementStart('div', 'entry-title');
429 $nli->out->elementEnd('div');
432 public function getDescription()
434 return $this->description;
437 public function getTitle()
442 public function getUrl()
444 if (empty($this->url)) {
445 throw new InvalidUrlException($this->url);
450 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
452 $nb = Bookmark::fromStored($stored);
454 $profile = $stored->getProfile();
456 // Whether to nofollow
457 $attrs = array('href' => $nb->getUrl(), 'class' => 'bookmark-title');
459 $nf = common_config('nofollow', 'external');
461 if ($nf == 'never' || ($nf == 'sometimes' and $out instanceof ShowstreamAction)) {
462 $attrs['rel'] = 'external';
464 $attrs['rel'] = 'nofollow external';
467 $out->elementStart('h3');
468 $out->element('a', $attrs, $nb->title);
469 $out->elementEnd('h3');
471 // Replies look like "for:" tags
472 $replies = $stored->getReplies();
473 $tags = $stored->getTags();
475 if (!empty($nb->description)) {
477 array('class' => 'bookmark-description'),
481 if (!empty($replies) || !empty($tags)) {
483 $out->elementStart('ul', array('class' => 'bookmark-tags'));
485 foreach ($replies as $reply) {
486 $other = Profile::getKV('id', $reply);
487 if (!empty($other)) {
488 $out->elementStart('li');
489 $out->element('a', array('rel' => 'tag',
490 'href' => $other->profileurl,
491 'title' => $other->getBestName()),
492 sprintf('for:%s', $other->nickname));
493 $out->elementEnd('li');
498 foreach ($tags as $tag) {
501 $out->elementStart('li');
503 array('rel' => 'tag',
504 'href' => Notice_tag::url($tag)),
506 $out->elementEnd('li');
511 $out->elementEnd('ul');