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('STATUSNET')) {
36 * Bookmark plugin main class
40 * @author Brion Vibber <brionv@status.net>
41 * @author Evan Prodromou <evan@status.net>
42 * @copyright 2010 StatusNet, Inc.
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
44 * @link http://status.net/
46 class BookmarkPlugin extends MicroAppPlugin
48 const VERSION = '0.1';
49 const IMPORTDELICIOUS = 'BookmarkPlugin:IMPORTDELICIOUS';
52 * Authorization for importing delicious bookmarks
54 * By default, everyone can import bookmarks except silenced people.
56 * @param Profile $profile Person whose rights to check
57 * @param string $right Right to check; const value
58 * @param boolean &$result Result of the check, writeable
60 * @return boolean hook value
62 function onUserRightsCheck($profile, $right, &$result)
64 if ($right == self::IMPORTDELICIOUS) {
65 $result = !$profile->isSilenced();
72 * Database schema setup
77 * @return boolean hook value; true means continue processing, false means stop.
79 function onCheckSchema()
81 $schema = Schema::get();
83 // For storing user-submitted flags on profiles
85 $schema->ensureTable('bookmark',
86 array(new ColumnDef('id',
91 new ColumnDef('profile_id',
101 new ColumnDef('title',
104 new ColumnDef('description',
111 new ColumnDef('created',
121 * Show the CSS necessary for this plugin
123 * @param Action $action the action being run
125 * @return boolean hook value
127 function onEndShowStyles($action)
129 $action->cssLink($this->path('bookmark.css'));
133 function onEndShowScripts($action)
135 $action->script($this->path('js/bookmark.js'));
139 * Load related modules when needed
141 * @param string $cls Name of the class to be loaded
143 * @return boolean hook value; true means continue processing, false means stop.
145 function onAutoload($cls)
147 $dir = dirname(__FILE__);
151 case 'BookmarksAction':
152 case 'BookmarksrssAction':
153 case 'ApiTimelineBookmarksAction':
154 case 'ShowbookmarkAction':
155 case 'NewbookmarkAction':
156 case 'BookmarkpopupAction':
157 case 'NoticebyurlAction':
158 case 'BookmarkforurlAction':
159 case 'ImportdeliciousAction':
160 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
163 include_once $dir.'/'.$cls.'.php';
165 case 'BookmarkListItem':
167 case 'InitialBookmarkForm':
168 case 'DeliciousBackupImporter':
169 case 'DeliciousBookmarkImporter':
170 include_once $dir.'/'.strtolower($cls).'.php';
178 * Map URLs to actions
180 * @param Net_URL_Mapper $m path-to-action mapper
182 * @return boolean hook value; true means continue processing, false means stop.
184 function onRouterInitialized($m)
186 if (common_config('singleuser', 'enabled')) {
187 $nickname = User::singleUserNickname();
188 $m->connect('bookmarks',
189 array('action' => 'bookmarks', 'nickname' => $nickname));
190 $m->connect('bookmarks/rss',
191 array('action' => 'bookmarksrss', 'nickname' => $nickname));
193 $m->connect(':nickname/bookmarks',
194 array('action' => 'bookmarks'),
195 array('nickname' => Nickname::DISPLAY_FMT));
196 $m->connect(':nickname/bookmarks/rss',
197 array('action' => 'bookmarksrss'),
198 array('nickname' => Nickname::DISPLAY_FMT));
201 $m->connect('api/bookmarks/:id.:format',
202 array('action' => 'ApiTimelineBookmarks',
203 'id' => Nickname::INPUT_FMT,
204 'format' => '(xml|json|rss|atom|as)'));
206 $m->connect('main/bookmark/new',
207 array('action' => 'newbookmark'),
208 array('id' => '[0-9]+'));
210 $m->connect('main/bookmark/popup',
211 array('action' => 'bookmarkpopup'));
213 $m->connect('main/bookmark/import',
214 array('action' => 'importdelicious'));
216 $m->connect('main/bookmark/forurl',
217 array('action' => 'bookmarkforurl'));
219 $m->connect('bookmark/:id',
220 array('action' => 'showbookmark'),
221 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
223 $m->connect('notice/by-url/:id',
224 array('action' => 'noticebyurl'),
225 array('id' => '[0-9]+'));
232 * Add our two queue handlers to the queue manager
234 * @param QueueManager $qm current queue manager
236 * @return boolean hook value
238 function onEndInitializeQueueManager($qm)
240 $qm->connect('dlcsback', 'DeliciousBackupImporter');
241 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
246 * Plugin version data
248 * @param array &$versions array of version data
252 function onPluginVersion(&$versions)
254 $versions[] = array('name' => 'Bookmark',
255 'version' => self::VERSION,
256 'author' => 'Evan Prodromou, Stephane Berube, Jean Baptiste Favre',
257 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
259 // TRANS: Plugin description.
260 _m('Simple extension for supporting bookmarks. ') .
261 'BookmarkList feature has been developped by Stephane Berube. ' .
262 'Integration has been done by Jean Baptiste Favre.');
267 * Load our document if requested
269 * @param string &$title Title to fetch
270 * @param string &$output HTML to output
272 * @return boolean hook value
274 function onStartLoadDoc(&$title, &$output)
276 if ($title == 'bookmarklet') {
277 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
279 $c = file_get_contents($filename);
280 $output = common_markup_to_html($c);
281 return false; // success!
288 * Show a link to our delicious import page on profile settings form
290 * @param Action $action Profile settings action being shown
292 * @return boolean hook value
294 function onEndProfileSettingsActions($action)
296 $user = common_current_user();
298 if (!empty($user) && $user->hasRight(self::IMPORTDELICIOUS)) {
299 $action->elementStart('li');
300 $action->element('a',
301 array('href' => common_local_url('importdelicious')),
302 // TRANS: Link text in proile leading to import form.
303 _m('Import del.icio.us bookmarks'));
304 $action->elementEnd('li');
311 * Output our CSS class for bookmark notice list elements
313 * @param NoticeListItem $nli The item being shown
315 * @return boolean hook value
318 function onStartOpenNoticeListItemElement($nli)
320 if (!$this->isMyNotice($nli->notice)) {
324 $nb = Bookmark::getByNotice($nli->notice);
327 $this->log(LOG_INFO, "Notice {$nli->notice->id} has bookmark class but no matching Bookmark record.");
331 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
332 $class = 'hentry notice bookmark';
333 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
334 $class .= ' limited-scope';
336 $nli->out->elementStart('li', array('class' => $class,
337 'id' => 'notice-' . $id));
339 Event::handle('EndOpenNoticeListItemElement', array($nli));
344 * Modify the default menu to link to our custom action
346 * Using event handlers, it's possible to modify the default UI for pages
347 * almost without limit. In this method, we add a menu item to the default
348 * primary menu for the interface to link to our action.
350 * The Action class provides a rich set of events to hook, as well as output
353 * @param Action $action The current action handler. Use this to
356 * @return boolean hook value; true means continue processing, false means stop.
360 function onEndPersonalGroupNav($action)
362 $this->user = common_current_user();
365 // TRANS: Client error displayed when trying to display bookmarks for a non-existing user.
366 $this->clientError(_('No such user.'));
370 $action->menuItem(common_local_url('bookmarks', array('nickname' => $this->user->nickname)),
371 // TRANS: Menu item in sample plugin.
373 // TRANS: Menu item title in sample plugin.
374 _m('A list of your bookmarks'), false, 'nav_timeline_bookmarks');
379 * Save a remote bookmark (from Salmon or PuSH)
381 * @param Ostatus_profile $author Author of the bookmark
382 * @param Activity $activity Activity to save
384 * @return Notice resulting notice.
386 static private function _postRemoteBookmark(Ostatus_profile $author,
389 $bookmark = $activity->objects[0];
391 $options = array('uri' => $bookmark->id,
392 'url' => $bookmark->link,
393 'is_local' => Notice::REMOTE,
394 'source' => 'ostatus');
396 return self::_postBookmark($author->localProfile(), $activity, $options);
400 * Test if an activity represents posting a bookmark
402 * @param Activity $activity Activity to test
404 * @return true if it's a Post of a Bookmark, else false
406 static private function _isPostBookmark($activity)
408 return ($activity->verb == ActivityVerb::POST &&
409 $activity->objects[0]->type == ActivityObject::BOOKMARK);
414 return array(ActivityObject::BOOKMARK);
418 * When a notice is deleted, delete the related Bookmark
420 * @param Notice $notice Notice being deleted
422 * @return boolean hook value
424 function deleteRelated($notice)
426 if ($this->isMyNotice($notice)) {
428 $nb = Bookmark::getByNotice($notice);
439 * Save a bookmark from an activity
441 * @param Activity $activity Activity to save
442 * @param Profile $profile Profile to use as author
443 * @param array $options Options to pass to bookmark-saving code
445 * @return Notice resulting notice
447 function saveNoticeFromActivity($activity, $profile, $options=array())
449 $bookmark = $activity->objects[0];
451 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
453 if (count($relLinkEls) < 1) {
454 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
455 throw new ClientException(_m('Expected exactly 1 link '.
456 'rel=related in a Bookmark.'));
459 if (count($relLinkEls) > 1) {
460 common_log(LOG_WARNING,
461 "Got too many link rel=related in a Bookmark.");
464 $linkEl = $relLinkEls[0];
466 $url = $linkEl->getAttribute('href');
470 foreach ($activity->categories as $category) {
471 $tags[] = common_canonical_tag($category->term);
474 if (!empty($activity->time)) {
475 $options['created'] = common_sql_date($activity->time);
478 // Fill in location if available
480 $location = $activity->context->location;
483 $options['lat'] = $location->lat;
484 $options['lon'] = $location->lon;
485 if ($location->location_id) {
486 $options['location_ns'] = $location->location_ns;
487 $options['location_id'] = $location->location_id;
491 $replies = $activity->context->attention;
493 $options['groups'] = array();
494 $options['replies'] = array();
496 foreach ($replies as $replyURI) {
497 $other = Profile::fromURI($replyURI);
498 if (!empty($other)) {
499 $options['replies'][] = $replyURI;
501 $group = User_group::getKV('uri', $replyURI);
502 if (!empty($group)) {
503 $options['groups'][] = $replyURI;
508 // Maintain direct reply associations
509 // @fixme what about conversation ID?
511 if (!empty($activity->context->replyToID)) {
512 $orig = Notice::getKV('uri',
513 $activity->context->replyToID);
515 $options['reply_to'] = $orig->id;
519 return Bookmark::saveNew($profile,
527 function activityObjectFromNotice($notice)
529 assert($this->isMyNotice($notice));
532 "Formatting notice {$notice->uri} as a bookmark.");
534 $object = new ActivityObject();
535 $nb = Bookmark::getByNotice($notice);
537 $object->id = $notice->uri;
538 $object->type = ActivityObject::BOOKMARK;
539 $object->title = $nb->title;
540 $object->summary = $nb->description;
541 $object->link = $notice->bestUrl();
543 // Attributes of the URL
545 $attachments = $notice->attachments();
547 if (count($attachments) != 1) {
548 // TRANS: Server exception thrown when a bookmark has multiple attachments.
549 throw new ServerException(_m('Bookmark notice with the '.
550 'wrong number of attachments.'));
553 $target = $attachments[0];
555 $attrs = array('rel' => 'related',
556 'href' => $target->url);
558 if (!empty($target->title)) {
559 $attrs['title'] = $target->title;
562 $object->extra[] = array('link', $attrs, null);
564 // Attributes of the thumbnail, if any
566 $thumbnail = $target->getThumbnail();
568 if (!empty($thumbnail)) {
569 $tattrs = array('rel' => 'preview',
570 'href' => $thumbnail->url);
572 if (!empty($thumbnail->width)) {
573 $tattrs['media:width'] = $thumbnail->width;
576 if (!empty($thumbnail->height)) {
577 $tattrs['media:height'] = $thumbnail->height;
580 $object->extra[] = array('link', $attrs, null);
587 * Given a notice list item, returns an adapter specific
590 * @param NoticeListItem $nli item to adapt
592 * @return NoticeListItemAdapter adapter or null
594 function adaptNoticeListItem($nli)
596 return new BookmarkListItem($nli);
599 function entryForm($out)
601 return new InitialBookmarkForm($out);
611 // TRANS: Application title.
612 return _m('TITLE','Bookmark');
615 function onEndUpgrade()
617 // Version 0.9.x of the plugin didn't stamp notices
618 // with verb and object-type (for obvious reasons). Update
619 // those notices here.
621 $notice = new Notice();
623 $notice->whereAdd('exists (select uri from bookmark where bookmark.uri = notice.uri)');
624 $notice->whereAdd('((object_type is null) or (object_type = "' .ActivityObject::NOTE.'"))');
628 while ($notice->fetch()) {
629 $original = clone($notice);
630 $notice->verb = ActivityVerb::POST;
631 $notice->object_type = ActivityObject::BOOKMARK;
632 $notice->update($original);
636 public function activityObjectOutputJson(ActivityObject $obj, array &$out)
638 assert($obj->type == ActivityObject::BOOKMARK);
640 $bm = Bookmark::getKV('uri', $obj->id);
643 throw new ServerException("Unknown bookmark: " . $obj->id);
646 $out['displayName'] = $bm->title;
647 $out['targetUrl'] = $bm->url;