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/
47 class BookmarkPlugin extends Plugin
49 const VERSION = '0.1';
52 * Database schema setup
57 * @return boolean hook value; true means continue processing, false means stop.
60 function onCheckSchema()
62 $schema = Schema::get();
64 // For storing user-submitted flags on profiles
66 $schema->ensureTable('bookmark',
67 array(new ColumnDef('profile_id',
77 new ColumnDef('title',
80 new ColumnDef('description',
87 new ColumnDef('url_crc32',
92 new ColumnDef('created',
99 $schema->createIndex('bookmark',
102 'bookmark_profile_url_idx');
103 } catch (Exception $e) {
104 common_log(LOG_ERR, $e->getMessage());
111 * When a notice is deleted, delete the related Bookmark
113 * @param Notice $notice Notice being deleted
115 * @return boolean hook value
118 function onNoticeDeleteRelated($notice)
120 $nb = Bookmark::getByNotice($notice);
130 * Show the CSS necessary for this plugin
132 * @param Action $action the action being run
134 * @return boolean hook value
137 function onEndShowStyles($action)
139 $action->cssLink('plugins/Bookmark/bookmark.css');
144 * Load related modules when needed
146 * @param string $cls Name of the class to be loaded
148 * @return boolean hook value; true means continue processing, false means stop.
151 function onAutoload($cls)
153 $dir = dirname(__FILE__);
157 case 'ShowbookmarkAction':
158 case 'NewbookmarkAction':
159 case 'BookmarkpopupAction':
160 case 'NoticebyurlAction':
161 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
164 include_once $dir.'/'.$cls.'.php';
167 case 'DeliciousBackupImporter':
168 case 'DeliciousBookmarkImporter':
169 include_once $dir.'/'.strtolower($cls).'.php';
177 * Map URLs to actions
179 * @param Net_URL_Mapper $m path-to-action mapper
181 * @return boolean hook value; true means continue processing, false means stop.
184 function onRouterInitialized($m)
186 $m->connect('main/bookmark/new',
187 array('action' => 'newbookmark'),
188 array('id' => '[0-9]+'));
190 $m->connect('main/bookmark/popup',
191 array('action' => 'bookmarkpopup'));
193 $m->connect('bookmark/:user/:created/:crc32',
194 array('action' => 'showbookmark'),
195 array('user' => '[0-9]+',
196 'created' => '[0-9]{14}',
197 'crc32' => '[0-9a-f]{8}'));
199 $m->connect('notice/by-url/:id',
200 array('action' => 'noticebyurl'),
201 array('id' => '[0-9]+'));
207 * Output the HTML for a bookmark in a list
209 * @param NoticeListItem $nli The list item being shown.
211 * @return boolean hook value
214 function onStartShowNoticeItem($nli)
216 $nb = Bookmark::getByNotice($nli->notice);
221 $notice = $nli->notice;
222 $profile = $nli->profile;
224 $atts = $notice->attachments();
226 if (count($atts) < 1) {
227 // Something wrong; let default code deal with it.
233 // XXX: only show the bookmark URL for non-single-page stuff
235 if ($out instanceof ShowbookmarkAction) {
237 $out->elementStart('h3');
239 array('href' => $att->url),
241 $out->elementEnd('h3');
244 $out->elementStart('ul', array('class' => 'bookmark_tags'));
246 // Replies look like "for:" tags
248 $replies = $nli->notice->getReplies();
250 if (!empty($replies)) {
251 foreach ($replies as $reply) {
252 $other = Profile::staticGet('id', $reply);
253 $out->elementStart('li');
254 $out->element('a', array('rel' => 'tag',
255 'href' => $other->profileurl,
256 'title' => $other->getBestName()),
257 sprintf('for:%s', $other->nickname));
258 $out->elementEnd('li');
263 $tags = $nli->notice->getTags();
265 foreach ($tags as $tag) {
266 $out->elementStart('li');
268 array('rel' => 'tag',
269 'href' => Notice_tag::url($tag)),
271 $out->elementEnd('li');
275 $out->elementEnd('ul');
278 array('class' => 'bookmark_description'),
281 if (common_config('attachments', 'show_thumbs')) {
282 $al = new InlineAttachmentList($notice, $out);
286 $out->elementStart('p', array('style' => 'float: left'));
288 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
290 $out->element('img', array('src' => ($avatar) ?
291 $avatar->displayUrl() :
292 Avatar::defaultImage(AVATAR_MINI_SIZE),
293 'class' => 'avatar photo bookmark_avatar',
294 'width' => AVATAR_MINI_SIZE,
295 'height' => AVATAR_MINI_SIZE,
296 'alt' => $profile->getBestName()));
298 $out->element('a', array('href' => $profile->profileurl,
299 'title' => $profile->getBestName()),
302 $nli->showNoticeLink();
303 $nli->showNoticeSource();
304 $nli->showNoticeLocation();
308 $out->elementEnd('p');
310 $nli->showNoticeOptions();
318 * Render a notice as a Bookmark object
320 * @param Notice $notice Notice to render
321 * @param ActivityObject &$object Empty object to fill
323 * @return boolean hook value
326 function onStartActivityObjectFromNotice($notice, &$object)
329 "Checking {$notice->uri} to see if it's a bookmark.");
331 $nb = Bookmark::getByNotice($notice);
336 "Formatting notice {$notice->uri} as a bookmark.");
338 $object->id = $notice->uri;
339 $object->type = ActivityObject::BOOKMARK;
340 $object->title = $nb->title;
341 $object->summary = $nb->description;
342 $object->link = $notice->bestUrl();
344 // Attributes of the URL
346 $attachments = $notice->attachments();
348 if (count($attachments) != 1) {
349 throw new ServerException(_('Bookmark notice with the '.
350 'wrong number of attachments.'));
353 $target = $attachments[0];
355 $attrs = array('rel' => 'related',
356 'href' => $target->url);
358 if (!empty($target->title)) {
359 $attrs['title'] = $target->title;
362 $object->extra[] = array('link', $attrs, null);
364 // Attributes of the thumbnail, if any
366 $thumbnail = $target->getThumbnail();
368 if (!empty($thumbnail)) {
369 $tattrs = array('rel' => 'preview',
370 'href' => $thumbnail->url);
372 if (!empty($thumbnail->width)) {
373 $tattrs['media:width'] = $thumbnail->width;
376 if (!empty($thumbnail->height)) {
377 $tattrs['media:height'] = $thumbnail->height;
380 $object->extra[] = array('link', $attrs, null);
390 * Add our two queue handlers to the queue manager
392 * @param QueueManager $qm current queue manager
394 * @return boolean hook value
397 function onEndInitializeQueueManager($qm)
399 $qm->connect('dlcsback', 'DeliciousBackupImporter');
400 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
405 * Plugin version data
407 * @param array &$versions array of version data
412 function onPluginVersion(&$versions)
414 $versions[] = array('name' => 'Sample',
415 'version' => self::VERSION,
416 'author' => 'Evan Prodromou',
417 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
419 _m('Simple extension for supporting bookmarks.'));
424 * Load our document if requested
426 * @param string &$title Title to fetch
427 * @param string &$output HTML to output
429 * @return boolean hook value
432 function onStartLoadDoc(&$title, &$output)
434 if ($title == 'bookmarklet') {
435 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
437 $c = file_get_contents($filename);
438 $output = common_markup_to_html($c);
439 return false; // success!
446 * Handle a posted bookmark from PuSH
448 * @param Activity $activity activity to handle
449 * @param Ostatus_profile $oprofile Profile for the feed
451 * @return boolean hook value
454 function onStartHandleFeedEntryWithProfile($activity, $oprofile) {
456 common_log(LOG_INFO, "BookmarkPlugin called for new feed entry.");
458 if (self::_isPostBookmark($activity)) {
460 common_log(LOG_INFO, "Importing activity {$activity->id} as a bookmark.");
462 $author = $oprofile->checkAuthorship($activity);
464 if (empty($author)) {
465 throw new ClientException(_('Can\'t get author for activity.'));
468 self::_postRemoteBookmark($author,
478 * Handle a posted bookmark from Salmon
480 * @param Activity $activity activity to handle
481 * @param mixed $target user or group targeted
483 * @return boolean hook value
486 function onStartHandleSalmonTarget($activity, $target) {
488 if (self::_isPostBookmark($activity)) {
490 $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
492 if ($target instanceof User_group) {
493 $uri = $target->getUri();
494 if (!in_array($uri, $activity->context->attention)) {
495 throw new ClientException(_("Bookmark not posted to this group."));
497 } else if ($target instanceof User) {
500 if (!empty($activity->context->replyToID)) {
501 $original = Notice::staticGet('uri', $activity->context->replyToID);
503 if (!in_array($uri, $activity->context->attention) &&
504 (empty($original) || $original->profile_id != $target->id)) {
505 throw new ClientException(_("Bookmark not posted to this user."));
508 throw new ServerException(_("Don't know how to handle this kind of target."));
511 $author = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
513 self::_postRemoteBookmark($author,
522 function onStartAtomPubNewActivity(&$activity, $user, &$notice)
524 if (self::_isPostBookmark($activity)) {
525 $options = array('source' => 'atompub');
526 $notice = self::_postBookmark($user->getProfile(), $activity, $options);
533 function onStartImportActivity($user, $author, $activity, $trusted, &$done) {
535 if (self::_isPostBookmark($activity)) {
537 $bookmark = $activity->objects[0];
539 $this->log(LOG_INFO, 'Importing Bookmark ' . $bookmark->id . ' for user ' . $user->nickname);
541 $options = array('uri' => $bookmark->id,
542 'url' => $bookmark->link,
543 'source' => 'restore');
545 $saved = self::_postBookmark($user->getProfile(), $activity, $options);
547 if (!empty($saved)) {
557 static private function _postRemoteBookmark(Ostatus_profile $author, Activity $activity)
559 $bookmark = $activity->objects[0];
561 $options = array('uri' => $bookmark->id,
562 'url' => $bookmark->link,
563 'is_local' => Notice::REMOTE_OMB,
564 'source' => 'ostatus');
566 return self::_postBookmark($author->localProfile(), $activity, $options);
569 static private function _postBookmark(Profile $profile, Activity $activity, $options=array())
571 $bookmark = $activity->objects[0];
573 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
575 if (count($relLinkEls) < 1) {
576 throw new ClientException(_('Expected exactly 1 link rel=related in a Bookmark.'));
579 if (count($relLinkEls) > 1) {
580 common_log(LOG_WARNING, "Got too many link rel=related in a Bookmark.");
583 $linkEl = $relLinkEls[0];
585 $url = $linkEl->getAttribute('href');
589 foreach ($activity->categories as $category) {
590 $tags[] = common_canonical_tag($category->term);
593 if (!empty($activity->time)) {
594 $options['created'] = common_sql_date($activity->time);
597 // Fill in location if available
599 $location = $activity->context->location;
602 $options['lat'] = $location->lat;
603 $options['lon'] = $location->lon;
604 if ($location->location_id) {
605 $options['location_ns'] = $location->location_ns;
606 $options['location_id'] = $location->location_id;
610 $replies = $activity->context->attention;
612 $options['groups'] = array();
613 $options['replies'] = array();
615 foreach ($replies as $replyURI) {
616 $other = Profile::fromURI($replyURI);
617 if (!empty($other)) {
618 $options['replies'][] = $replyURI;
620 $group = User_group::staticGet('uri', $replyURI);
621 if (!empty($group)) {
622 $options['groups'][] = $replyURI;
627 // Maintain direct reply associations
628 // @fixme what about conversation ID?
630 if (!empty($activity->context->replyToID)) {
631 $orig = Notice::staticGet('uri',
632 $activity->context->replyToID);
634 $options['reply_to'] = $orig->id;
638 return Bookmark::saveNew($profile,
646 static private function _isPostBookmark($activity)
648 return ($activity->verb == ActivityVerb::POST &&
649 $activity->objects[0]->type == ActivityObject::BOOKMARK);