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');
243 $out->element('a', array('class' => 'bookmark_notice_count',
244 'href' => common_local_url('noticebyurl',
245 array('id' => $att->id))),
246 $att->noticeCount());
249 $out->elementStart('ul', array('class' => 'bookmark_tags'));
251 // Replies look like "for:" tags
253 $replies = $nli->notice->getReplies();
255 if (!empty($replies)) {
256 foreach ($replies as $reply) {
257 $other = Profile::staticGet('id', $reply);
258 $out->elementStart('li');
259 $out->element('a', array('rel' => 'tag',
260 'href' => $other->profileurl,
261 'title' => $other->getBestName()),
262 sprintf('for:%s', $other->nickname));
263 $out->elementEnd('li');
268 $tags = $nli->notice->getTags();
270 foreach ($tags as $tag) {
271 $out->elementStart('li');
273 array('rel' => 'tag',
274 'href' => Notice_tag::url($tag)),
276 $out->elementEnd('li');
280 $out->elementEnd('ul');
283 array('class' => 'bookmark_description'),
286 if (common_config('attachments', 'show_thumbs')) {
287 $al = new InlineAttachmentList($notice, $out);
291 $out->elementStart('p', array('style' => 'float: left'));
293 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
295 $out->element('img', array('src' => ($avatar) ?
296 $avatar->displayUrl() :
297 Avatar::defaultImage(AVATAR_MINI_SIZE),
298 'class' => 'avatar photo bookmark_avatar',
299 'width' => AVATAR_MINI_SIZE,
300 'height' => AVATAR_MINI_SIZE,
301 'alt' => $profile->getBestName()));
303 $out->element('a', array('href' => $profile->profileurl,
304 'title' => $profile->getBestName()),
307 $nli->showNoticeLink();
308 $nli->showNoticeSource();
309 $nli->showNoticeLocation();
313 $out->elementEnd('p');
315 $nli->showNoticeOptions();
323 * Render a notice as a Bookmark object
325 * @param Notice $notice Notice to render
326 * @param ActivityObject &$object Empty object to fill
328 * @return boolean hook value
331 function onStartActivityObjectFromNotice($notice, &$object)
334 "Checking {$notice->uri} to see if it's a bookmark.");
336 $nb = Bookmark::getByNotice($notice);
341 "Formatting notice {$notice->uri} as a bookmark.");
343 $object->id = $notice->uri;
344 $object->type = ActivityObject::BOOKMARK;
345 $object->title = $nb->title;
346 $object->summary = $nb->description;
347 $object->link = $notice->bestUrl();
349 // Attributes of the URL
351 $attachments = $notice->attachments();
353 if (count($attachments) != 1) {
354 throw new ServerException(_('Bookmark notice with the '.
355 'wrong number of attachments.'));
358 $target = $attachments[0];
360 $attrs = array('rel' => 'related',
361 'href' => $target->url);
363 if (!empty($target->title)) {
364 $attrs['title'] = $target->title;
367 $object->extra[] = array('link', $attrs, null);
369 // Attributes of the thumbnail, if any
371 $thumbnail = $target->getThumbnail();
373 if (!empty($thumbnail)) {
374 $tattrs = array('rel' => 'preview',
375 'href' => $thumbnail->url);
377 if (!empty($thumbnail->width)) {
378 $tattrs['media:width'] = $thumbnail->width;
381 if (!empty($thumbnail->height)) {
382 $tattrs['media:height'] = $thumbnail->height;
385 $object->extra[] = array('link', $attrs, null);
395 * Add our two queue handlers to the queue manager
397 * @param QueueManager $qm current queue manager
399 * @return boolean hook value
402 function onEndInitializeQueueManager($qm)
404 $qm->connect('dlcsback', 'DeliciousBackupImporter');
405 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
410 * Plugin version data
412 * @param array &$versions array of version data
417 function onPluginVersion(&$versions)
419 $versions[] = array('name' => 'Sample',
420 'version' => self::VERSION,
421 'author' => 'Evan Prodromou',
422 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
424 _m('Simple extension for supporting bookmarks.'));
429 * Load our document if requested
431 * @param string &$title Title to fetch
432 * @param string &$output HTML to output
434 * @return boolean hook value
437 function onStartLoadDoc(&$title, &$output)
439 if ($title == 'bookmarklet') {
440 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
442 $c = file_get_contents($filename);
443 $output = common_markup_to_html($c);
444 return false; // success!
451 * Handle a posted bookmark from PuSH
453 * @param Activity $activity activity to handle
454 * @param Ostatus_profile $oprofile Profile for the feed
456 * @return boolean hook value
459 function onStartHandleFeedEntryWithProfile($activity, $oprofile) {
461 common_log(LOG_INFO, "BookmarkPlugin called for new feed entry.");
463 if (self::_isPostBookmark($activity)) {
465 common_log(LOG_INFO, "Importing activity {$activity->id} as a bookmark.");
467 $author = $oprofile->checkAuthorship($activity);
469 if (empty($author)) {
470 throw new ClientException(_('Can\'t get author for activity.'));
473 self::_postRemoteBookmark($author,
483 * Handle a posted bookmark from Salmon
485 * @param Activity $activity activity to handle
486 * @param mixed $target user or group targeted
488 * @return boolean hook value
491 function onStartHandleSalmonTarget($activity, $target) {
493 if (self::_isPostBookmark($activity)) {
495 $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
497 if ($target instanceof User_group) {
498 $uri = $target->getUri();
499 if (!in_array($uri, $activity->context->attention)) {
500 throw new ClientException(_("Bookmark not posted to this group."));
502 } else if ($target instanceof User) {
505 if (!empty($activity->context->replyToID)) {
506 $original = Notice::staticGet('uri', $activity->context->replyToID);
508 if (!in_array($uri, $activity->context->attention) &&
509 (empty($original) || $original->profile_id != $target->id)) {
510 throw new ClientException(_("Bookmark not posted to this user."));
513 throw new ServerException(_("Don't know how to handle this kind of target."));
516 $author = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
518 self::_postRemoteBookmark($author,
527 function onStartAtomPubNewActivity(&$activity, $user, &$notice)
529 if (self::_isPostBookmark($activity)) {
530 $options = array('source' => 'atompub');
531 $notice = self::_postBookmark($user->getProfile(), $activity, $options);
538 function onStartImportActivity($user, $author, $activity, $trusted, &$done) {
540 if (self::_isPostBookmark($activity)) {
542 $bookmark = $activity->objects[0];
544 $this->log(LOG_INFO, 'Importing Bookmark ' . $bookmark->id . ' for user ' . $user->nickname);
546 $options = array('uri' => $bookmark->id,
547 'url' => $bookmark->link,
548 'source' => 'restore');
550 $saved = self::_postBookmark($user->getProfile(), $activity, $options);
552 if (!empty($saved)) {
562 static private function _postRemoteBookmark(Ostatus_profile $author, Activity $activity)
564 $bookmark = $activity->objects[0];
566 $options = array('uri' => $bookmark->id,
567 'url' => $bookmark->link,
568 'is_local' => Notice::REMOTE_OMB,
569 'source' => 'ostatus');
571 return self::_postBookmark($author->localProfile(), $activity, $options);
574 static private function _postBookmark(Profile $profile, Activity $activity, $options=array())
576 $bookmark = $activity->objects[0];
578 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
580 if (count($relLinkEls) < 1) {
581 throw new ClientException(_('Expected exactly 1 link rel=related in a Bookmark.'));
584 if (count($relLinkEls) > 1) {
585 common_log(LOG_WARNING, "Got too many link rel=related in a Bookmark.");
588 $linkEl = $relLinkEls[0];
590 $url = $linkEl->getAttribute('href');
594 foreach ($activity->categories as $category) {
595 $tags[] = common_canonical_tag($category->term);
598 if (!empty($activity->time)) {
599 $options['created'] = common_sql_date($activity->time);
602 // Fill in location if available
604 $location = $activity->context->location;
607 $options['lat'] = $location->lat;
608 $options['lon'] = $location->lon;
609 if ($location->location_id) {
610 $options['location_ns'] = $location->location_ns;
611 $options['location_id'] = $location->location_id;
615 $replies = $activity->context->attention;
617 $options['groups'] = array();
618 $options['replies'] = array();
620 foreach ($replies as $replyURI) {
621 $other = Profile::fromURI($replyURI);
622 if (!empty($other)) {
623 $options['replies'][] = $replyURI;
625 $group = User_group::staticGet('uri', $replyURI);
626 if (!empty($group)) {
627 $options['groups'][] = $replyURI;
632 // Maintain direct reply associations
633 // @fixme what about conversation ID?
635 if (!empty($activity->context->replyToID)) {
636 $orig = Notice::staticGet('uri',
637 $activity->context->replyToID);
639 $options['reply_to'] = $orig->id;
643 return Bookmark::saveNew($profile,
651 static private function _isPostBookmark($activity)
653 return ($activity->verb == ActivityVerb::POST &&
654 $activity->objects[0]->type == ActivityObject::BOOKMARK);