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';
50 const IMPORTDELICIOUS = 'BookmarkPlugin:IMPORTDELICIOUS';
52 function onUserRightsCheck($profile, $right, &$result)
54 if ($right == self::IMPORTDELICIOUS) {
55 $result = !$profile->isSilenced();
62 * Database schema setup
67 * @return boolean hook value; true means continue processing, false means stop.
70 function onCheckSchema()
72 $schema = Schema::get();
74 // For storing user-submitted flags on profiles
76 $schema->ensureTable('bookmark',
77 array(new ColumnDef('profile_id',
87 new ColumnDef('title',
90 new ColumnDef('description',
97 new ColumnDef('url_crc32',
102 new ColumnDef('created',
109 $schema->createIndex('bookmark',
112 'bookmark_profile_url_idx');
113 } catch (Exception $e) {
114 common_log(LOG_ERR, $e->getMessage());
121 * When a notice is deleted, delete the related Bookmark
123 * @param Notice $notice Notice being deleted
125 * @return boolean hook value
128 function onNoticeDeleteRelated($notice)
130 $nb = Bookmark::getByNotice($notice);
140 * Show the CSS necessary for this plugin
142 * @param Action $action the action being run
144 * @return boolean hook value
147 function onEndShowStyles($action)
149 $action->cssLink('plugins/Bookmark/bookmark.css');
154 * Load related modules when needed
156 * @param string $cls Name of the class to be loaded
158 * @return boolean hook value; true means continue processing, false means stop.
161 function onAutoload($cls)
163 $dir = dirname(__FILE__);
167 case 'ShowbookmarkAction':
168 case 'NewbookmarkAction':
169 case 'BookmarkpopupAction':
170 case 'NoticebyurlAction':
171 case 'ImportdeliciousAction':
172 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
175 include_once $dir.'/'.$cls.'.php';
178 case 'DeliciousBackupImporter':
179 case 'DeliciousBookmarkImporter':
180 include_once $dir.'/'.strtolower($cls).'.php';
188 * Map URLs to actions
190 * @param Net_URL_Mapper $m path-to-action mapper
192 * @return boolean hook value; true means continue processing, false means stop.
195 function onRouterInitialized($m)
197 $m->connect('main/bookmark/new',
198 array('action' => 'newbookmark'),
199 array('id' => '[0-9]+'));
201 $m->connect('main/bookmark/popup',
202 array('action' => 'bookmarkpopup'));
204 $m->connect('main/bookmark/import',
205 array('action' => 'importdelicious'));
207 $m->connect('bookmark/:user/:created/:crc32',
208 array('action' => 'showbookmark'),
209 array('user' => '[0-9]+',
210 'created' => '[0-9]{14}',
211 'crc32' => '[0-9a-f]{8}'));
213 $m->connect('notice/by-url/:id',
214 array('action' => 'noticebyurl'),
215 array('id' => '[0-9]+'));
221 * Output the HTML for a bookmark in a list
223 * @param NoticeListItem $nli The list item being shown.
225 * @return boolean hook value
228 function onStartShowNoticeItem($nli)
230 $nb = Bookmark::getByNotice($nli->notice);
235 $notice = $nli->notice;
236 $profile = $nli->profile;
238 $atts = $notice->attachments();
240 if (count($atts) < 1) {
241 // Something wrong; let default code deal with it.
247 // XXX: only show the bookmark URL for non-single-page stuff
249 if ($out instanceof ShowbookmarkAction) {
251 $out->elementStart('h3');
253 array('href' => $att->url),
255 $out->elementEnd('h3');
257 $out->element('a', array('class' => 'bookmark_notice_count',
258 'href' => common_local_url('noticebyurl',
259 array('id' => $att->id))),
260 $att->noticeCount());
263 $out->elementStart('ul', array('class' => 'bookmark_tags'));
265 // Replies look like "for:" tags
267 $replies = $nli->notice->getReplies();
269 if (!empty($replies)) {
270 foreach ($replies as $reply) {
271 $other = Profile::staticGet('id', $reply);
272 $out->elementStart('li');
273 $out->element('a', array('rel' => 'tag',
274 'href' => $other->profileurl,
275 'title' => $other->getBestName()),
276 sprintf('for:%s', $other->nickname));
277 $out->elementEnd('li');
282 $tags = $nli->notice->getTags();
284 foreach ($tags as $tag) {
285 $out->elementStart('li');
287 array('rel' => 'tag',
288 'href' => Notice_tag::url($tag)),
290 $out->elementEnd('li');
294 $out->elementEnd('ul');
297 array('class' => 'bookmark_description'),
300 if (common_config('attachments', 'show_thumbs')) {
301 $al = new InlineAttachmentList($notice, $out);
305 $out->elementStart('p', array('style' => 'float: left'));
307 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
309 $out->element('img', array('src' => ($avatar) ?
310 $avatar->displayUrl() :
311 Avatar::defaultImage(AVATAR_MINI_SIZE),
312 'class' => 'avatar photo bookmark_avatar',
313 'width' => AVATAR_MINI_SIZE,
314 'height' => AVATAR_MINI_SIZE,
315 'alt' => $profile->getBestName()));
317 $out->element('a', array('href' => $profile->profileurl,
318 'title' => $profile->getBestName()),
321 $nli->showNoticeLink();
322 $nli->showNoticeSource();
323 $nli->showNoticeLocation();
327 $out->elementEnd('p');
329 $nli->showNoticeOptions();
337 * Render a notice as a Bookmark object
339 * @param Notice $notice Notice to render
340 * @param ActivityObject &$object Empty object to fill
342 * @return boolean hook value
345 function onStartActivityObjectFromNotice($notice, &$object)
348 "Checking {$notice->uri} to see if it's a bookmark.");
350 $nb = Bookmark::getByNotice($notice);
355 "Formatting notice {$notice->uri} as a bookmark.");
357 $object->id = $notice->uri;
358 $object->type = ActivityObject::BOOKMARK;
359 $object->title = $nb->title;
360 $object->summary = $nb->description;
361 $object->link = $notice->bestUrl();
363 // Attributes of the URL
365 $attachments = $notice->attachments();
367 if (count($attachments) != 1) {
368 throw new ServerException(_('Bookmark notice with the '.
369 'wrong number of attachments.'));
372 $target = $attachments[0];
374 $attrs = array('rel' => 'related',
375 'href' => $target->url);
377 if (!empty($target->title)) {
378 $attrs['title'] = $target->title;
381 $object->extra[] = array('link', $attrs, null);
383 // Attributes of the thumbnail, if any
385 $thumbnail = $target->getThumbnail();
387 if (!empty($thumbnail)) {
388 $tattrs = array('rel' => 'preview',
389 'href' => $thumbnail->url);
391 if (!empty($thumbnail->width)) {
392 $tattrs['media:width'] = $thumbnail->width;
395 if (!empty($thumbnail->height)) {
396 $tattrs['media:height'] = $thumbnail->height;
399 $object->extra[] = array('link', $attrs, null);
409 * Add our two queue handlers to the queue manager
411 * @param QueueManager $qm current queue manager
413 * @return boolean hook value
416 function onEndInitializeQueueManager($qm)
418 $qm->connect('dlcsback', 'DeliciousBackupImporter');
419 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
424 * Plugin version data
426 * @param array &$versions array of version data
431 function onPluginVersion(&$versions)
433 $versions[] = array('name' => 'Sample',
434 'version' => self::VERSION,
435 'author' => 'Evan Prodromou',
436 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
438 _m('Simple extension for supporting bookmarks.'));
443 * Load our document if requested
445 * @param string &$title Title to fetch
446 * @param string &$output HTML to output
448 * @return boolean hook value
451 function onStartLoadDoc(&$title, &$output)
453 if ($title == 'bookmarklet') {
454 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
456 $c = file_get_contents($filename);
457 $output = common_markup_to_html($c);
458 return false; // success!
465 * Handle a posted bookmark from PuSH
467 * @param Activity $activity activity to handle
468 * @param Ostatus_profile $oprofile Profile for the feed
470 * @return boolean hook value
473 function onStartHandleFeedEntryWithProfile($activity, $oprofile) {
475 common_log(LOG_INFO, "BookmarkPlugin called for new feed entry.");
477 if (self::_isPostBookmark($activity)) {
479 common_log(LOG_INFO, "Importing activity {$activity->id} as a bookmark.");
481 $author = $oprofile->checkAuthorship($activity);
483 if (empty($author)) {
484 throw new ClientException(_('Can\'t get author for activity.'));
487 self::_postRemoteBookmark($author,
497 * Handle a posted bookmark from Salmon
499 * @param Activity $activity activity to handle
500 * @param mixed $target user or group targeted
502 * @return boolean hook value
505 function onStartHandleSalmonTarget($activity, $target) {
507 if (self::_isPostBookmark($activity)) {
509 $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
511 if ($target instanceof User_group) {
512 $uri = $target->getUri();
513 if (!in_array($uri, $activity->context->attention)) {
514 throw new ClientException(_("Bookmark not posted to this group."));
516 } else if ($target instanceof User) {
519 if (!empty($activity->context->replyToID)) {
520 $original = Notice::staticGet('uri', $activity->context->replyToID);
522 if (!in_array($uri, $activity->context->attention) &&
523 (empty($original) || $original->profile_id != $target->id)) {
524 throw new ClientException(_("Bookmark not posted to this user."));
527 throw new ServerException(_("Don't know how to handle this kind of target."));
530 $author = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
532 self::_postRemoteBookmark($author,
541 function onStartAtomPubNewActivity(&$activity, $user, &$notice)
543 if (self::_isPostBookmark($activity)) {
544 $options = array('source' => 'atompub');
545 $notice = self::_postBookmark($user->getProfile(), $activity, $options);
552 function onStartImportActivity($user, $author, $activity, $trusted, &$done) {
554 if (self::_isPostBookmark($activity)) {
556 $bookmark = $activity->objects[0];
558 $this->log(LOG_INFO, 'Importing Bookmark ' . $bookmark->id . ' for user ' . $user->nickname);
560 $options = array('uri' => $bookmark->id,
561 'url' => $bookmark->link,
562 'source' => 'restore');
564 $saved = self::_postBookmark($user->getProfile(), $activity, $options);
566 if (!empty($saved)) {
576 static private function _postRemoteBookmark(Ostatus_profile $author, Activity $activity)
578 $bookmark = $activity->objects[0];
580 $options = array('uri' => $bookmark->id,
581 'url' => $bookmark->link,
582 'is_local' => Notice::REMOTE_OMB,
583 'source' => 'ostatus');
585 return self::_postBookmark($author->localProfile(), $activity, $options);
588 static private function _postBookmark(Profile $profile, Activity $activity, $options=array())
590 $bookmark = $activity->objects[0];
592 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
594 if (count($relLinkEls) < 1) {
595 throw new ClientException(_('Expected exactly 1 link rel=related in a Bookmark.'));
598 if (count($relLinkEls) > 1) {
599 common_log(LOG_WARNING, "Got too many link rel=related in a Bookmark.");
602 $linkEl = $relLinkEls[0];
604 $url = $linkEl->getAttribute('href');
608 foreach ($activity->categories as $category) {
609 $tags[] = common_canonical_tag($category->term);
612 if (!empty($activity->time)) {
613 $options['created'] = common_sql_date($activity->time);
616 // Fill in location if available
618 $location = $activity->context->location;
621 $options['lat'] = $location->lat;
622 $options['lon'] = $location->lon;
623 if ($location->location_id) {
624 $options['location_ns'] = $location->location_ns;
625 $options['location_id'] = $location->location_id;
629 $replies = $activity->context->attention;
631 $options['groups'] = array();
632 $options['replies'] = array();
634 foreach ($replies as $replyURI) {
635 $other = Profile::fromURI($replyURI);
636 if (!empty($other)) {
637 $options['replies'][] = $replyURI;
639 $group = User_group::staticGet('uri', $replyURI);
640 if (!empty($group)) {
641 $options['groups'][] = $replyURI;
646 // Maintain direct reply associations
647 // @fixme what about conversation ID?
649 if (!empty($activity->context->replyToID)) {
650 $orig = Notice::staticGet('uri',
651 $activity->context->replyToID);
653 $options['reply_to'] = $orig->id;
657 return Bookmark::saveNew($profile,
665 static private function _isPostBookmark($activity)
667 return ($activity->verb == ActivityVerb::POST &&
668 $activity->objects[0]->type == ActivityObject::BOOKMARK);