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 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
163 include_once $dir.'/'.$cls.'.php';
166 case 'DeliciousBackupImporter':
167 case 'DeliciousBookmarkImporter':
168 include_once $dir.'/'.strtolower($cls).'.php';
176 * Map URLs to actions
178 * @param Net_URL_Mapper $m path-to-action mapper
180 * @return boolean hook value; true means continue processing, false means stop.
183 function onRouterInitialized($m)
185 $m->connect('main/bookmark/new',
186 array('action' => 'newbookmark'),
187 array('id' => '[0-9]+'));
189 $m->connect('main/bookmark/popup', array('action' => 'bookmarkpopup'));
191 $m->connect('bookmark/:user/:created/:crc32',
192 array('action' => 'showbookmark'),
193 array('user' => '[0-9]+',
194 'created' => '[0-9]{14}',
195 'crc32' => '[0-9a-f]{8}'));
201 * Output the HTML for a bookmark in a list
203 * @param NoticeListItem $nli The list item being shown.
205 * @return boolean hook value
208 function onStartShowNoticeItem($nli)
210 $nb = Bookmark::getByNotice($nli->notice);
215 $notice = $nli->notice;
216 $profile = $nli->profile;
218 $atts = $notice->attachments();
220 if (count($atts) < 1) {
221 // Something wrong; let default code deal with it.
227 // XXX: only show the bookmark URL for non-single-page stuff
229 if ($out instanceof ShowbookmarkAction) {
231 $out->elementStart('h3');
233 array('href' => $att->url),
235 $out->elementEnd('h3');
238 $out->elementStart('ul', array('class' => 'bookmark_tags'));
240 // Replies look like "for:" tags
242 $replies = $nli->notice->getReplies();
244 if (!empty($replies)) {
245 foreach ($replies as $reply) {
246 $other = Profile::staticGet('id', $reply);
247 $out->elementStart('li');
248 $out->element('a', array('rel' => 'tag',
249 'href' => $other->profileurl,
250 'title' => $other->getBestName()),
251 sprintf('for:%s', $other->nickname));
252 $out->elementEnd('li');
257 $tags = $nli->notice->getTags();
259 foreach ($tags as $tag) {
260 $out->elementStart('li');
262 array('rel' => 'tag',
263 'href' => Notice_tag::url($tag)),
265 $out->elementEnd('li');
269 $out->elementEnd('ul');
272 array('class' => 'bookmark_description'),
275 if (common_config('attachments', 'show_thumbs')) {
276 $al = new InlineAttachmentList($notice, $out);
280 $out->elementStart('p', array('style' => 'float: left'));
282 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
284 $out->element('img', array('src' => ($avatar) ?
285 $avatar->displayUrl() :
286 Avatar::defaultImage(AVATAR_MINI_SIZE),
287 'class' => 'avatar photo bookmark_avatar',
288 'width' => AVATAR_MINI_SIZE,
289 'height' => AVATAR_MINI_SIZE,
290 'alt' => $profile->getBestName()));
292 $out->element('a', array('href' => $profile->profileurl,
293 'title' => $profile->getBestName()),
296 $nli->showNoticeLink();
297 $nli->showNoticeSource();
298 $nli->showNoticeLocation();
302 $out->elementEnd('p');
304 $nli->showNoticeOptions();
312 * Render a notice as a Bookmark object
314 * @param Notice $notice Notice to render
315 * @param ActivityObject &$object Empty object to fill
317 * @return boolean hook value
320 function onStartActivityObjectFromNotice($notice, &$object)
323 "Checking {$notice->uri} to see if it's a bookmark.");
325 $nb = Bookmark::getByNotice($notice);
330 "Formatting notice {$notice->uri} as a bookmark.");
332 $object->id = $notice->uri;
333 $object->type = ActivityObject::BOOKMARK;
334 $object->title = $nb->title;
335 $object->summary = $nb->description;
336 $object->link = $notice->bestUrl();
338 // Attributes of the URL
340 $attachments = $notice->attachments();
342 if (count($attachments) != 1) {
343 throw new ServerException(_('Bookmark notice with the '.
344 'wrong number of attachments.'));
347 $target = $attachments[0];
349 $attrs = array('rel' => 'related',
350 'href' => $target->url);
352 if (!empty($target->title)) {
353 $attrs['title'] = $target->title;
356 $object->extra[] = array('link', $attrs, null);
358 // Attributes of the thumbnail, if any
360 $thumbnail = $target->getThumbnail();
362 if (!empty($thumbnail)) {
363 $tattrs = array('rel' => 'preview',
364 'href' => $thumbnail->url);
366 if (!empty($thumbnail->width)) {
367 $tattrs['media:width'] = $thumbnail->width;
370 if (!empty($thumbnail->height)) {
371 $tattrs['media:height'] = $thumbnail->height;
374 $object->extra[] = array('link', $attrs, null);
384 * Add our two queue handlers to the queue manager
386 * @param QueueManager $qm current queue manager
388 * @return boolean hook value
391 function onEndInitializeQueueManager($qm)
393 $qm->connect('dlcsback', 'DeliciousBackupImporter');
394 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
399 * Plugin version data
401 * @param array &$versions array of version data
406 function onPluginVersion(&$versions)
408 $versions[] = array('name' => 'Sample',
409 'version' => self::VERSION,
410 'author' => 'Evan Prodromou',
411 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
413 _m('Simple extension for supporting bookmarks.'));
418 * Load our document if requested
420 * @param string &$title Title to fetch
421 * @param string &$output HTML to output
423 * @return boolean hook value
426 function onStartLoadDoc(&$title, &$output)
428 if ($title == 'bookmarklet') {
429 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
431 $c = file_get_contents($filename);
432 $output = common_markup_to_html($c);
433 return false; // success!
440 * Handle a posted bookmark from PuSH
442 * @param Activity $activity activity to handle
443 * @param Ostatus_profile $oprofile Profile for the feed
445 * @return boolean hook value
448 function onStartHandleFeedEntryWithProfile($activity, $oprofile) {
450 common_log(LOG_INFO, "BookmarkPlugin called for new feed entry.");
452 if (self::_isPostBookmark($activity)) {
454 common_log(LOG_INFO, "Importing activity {$activity->id} as a bookmark.");
456 $author = $oprofile->checkAuthorship($activity);
458 if (empty($author)) {
459 throw new ClientException(_('Can\'t get author for activity.'));
462 self::_postRemoteBookmark($author,
472 * Handle a posted bookmark from Salmon
474 * @param Activity $activity activity to handle
475 * @param mixed $target user or group targeted
477 * @return boolean hook value
480 function onStartHandleSalmonTarget($activity, $target) {
482 if (self::_isPostBookmark($activity)) {
484 $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
486 if ($target instanceof User_group) {
487 $uri = $target->getUri();
488 if (!in_array($uri, $activity->context->attention)) {
489 throw new ClientException(_("Bookmark not posted to this group."));
491 } else if ($target instanceof User) {
494 if (!empty($activity->context->replyToID)) {
495 $original = Notice::staticGet('uri', $activity->context->replyToID);
497 if (!in_array($uri, $activity->context->attention) &&
498 (empty($original) || $original->profile_id != $target->id)) {
499 throw new ClientException(_("Bookmark not posted to this user."));
502 throw new ServerException(_("Don't know how to handle this kind of target."));
505 $author = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
507 self::_postRemoteBookmark($author,
516 function onStartAtomPubNewActivity(&$activity, $user)
518 if (self::_isPostBookmark($activity)) {
519 $options = array('source' => 'atompub');
520 self::_postBookmark($user->getProfile(), $activity, $options);
527 static private function _postRemoteBookmark(Ostatus_profile $author, Activity $activity)
529 $bookmark = $activity->objects[0];
531 $options = array('uri' => $bookmark->id,
532 'url' => $bookmark->link,
533 'is_local' => Notice::REMOTE_OMB,
534 'source' => 'ostatus');
536 return self::_postBookmark($author->localProfile(), $activity, $options);
539 static private function _postBookmark(Profile $profile, Activity $activity, $options=array())
541 $bookmark = $activity->objects[0];
543 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
545 if (count($relLinkEls) < 1) {
546 throw new ClientException(_('Expected exactly 1 link rel=related in a Bookmark.'));
549 if (count($relLinkEls) > 1) {
550 common_log(LOG_WARNING, "Got too many link rel=related in a Bookmark.");
553 $linkEl = $relLinkEls[0];
555 $url = $linkEl->getAttribute('href');
559 foreach ($activity->categories as $category) {
560 $tags[] = common_canonical_tag($category->term);
563 if (!empty($activity->time)) {
564 $options['created'] = common_sql_date($activity->time);
567 // Fill in location if available
569 $location = $activity->context->location;
572 $options['lat'] = $location->lat;
573 $options['lon'] = $location->lon;
574 if ($location->location_id) {
575 $options['location_ns'] = $location->location_ns;
576 $options['location_id'] = $location->location_id;
580 $replies = $activity->context->attention;
582 $options['groups'] = array();
583 $options['replies'] = array();
585 foreach ($replies as $replyURI) {
586 $other = Profile::fromURI($replyURI);
587 if (!empty($other)) {
588 $options['replies'][] = $replyURI;
590 $group = User_group::staticGet('uri', $replyURI);
591 if (!empty($group)) {
592 $options['groups'][] = $replyURI;
597 // Maintain direct reply associations
598 // @fixme what about conversation ID?
600 if (!empty($activity->context->replyToID)) {
601 $orig = Notice::staticGet('uri',
602 $activity->context->replyToID);
604 $options['reply_to'] = $orig->id;
608 return Bookmark::saveNew($profile,
616 static private function _isPostBookmark($activity)
618 return ($activity->verb == ActivityVerb::POST &&
619 $activity->objects[0]->type == ActivityObject::BOOKMARK);