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 'NewbookmarkAction':
158 case 'BookmarkpopupAction':
159 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
162 include_once $dir.'/'.$cls.'.php';
165 case 'DeliciousBackupImporter':
166 case 'DeliciousBookmarkImporter':
167 include_once $dir.'/'.strtolower($cls).'.php';
175 * Map URLs to actions
177 * @param Net_URL_Mapper $m path-to-action mapper
179 * @return boolean hook value; true means continue processing, false means stop.
182 function onRouterInitialized($m)
184 $m->connect('main/bookmark/new',
185 array('action' => 'newbookmark'),
186 array('id' => '[0-9]+'));
188 $m->connect('main/bookmark/popup', array('action' => 'bookmarkpopup'));
190 $m->connect('bookmark/:user/:created/:crc32',
191 array('action' => 'showbookmark'),
192 array('user' => '[0-9]+',
193 'created' => '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z',
194 'crc32' => '[0-9A-F]{8}'));
200 * Output the HTML for a bookmark in a list
202 * @param NoticeListItem $nli The list item being shown.
204 * @return boolean hook value
207 function onStartShowNoticeItem($nli)
209 $nb = Bookmark::getByNotice($nli->notice);
214 $notice = $nli->notice;
215 $profile = $nli->profile;
217 $atts = $notice->attachments();
219 if (count($atts) < 1) {
220 // Something wrong; let default code deal with it.
226 $out->elementStart('h3');
228 array('href' => $att->url),
230 $out->elementEnd('h3');
232 $out->elementStart('ul', array('class' => 'bookmark_tags'));
234 // Replies look like "for:" tags
236 $replies = $nli->notice->getReplies();
238 if (!empty($replies)) {
239 foreach ($replies as $reply) {
240 $other = Profile::staticGet('id', $reply);
241 $out->elementStart('li');
242 $out->element('a', array('rel' => 'tag',
243 'href' => $other->profileurl,
244 'title' => $other->getBestName()),
245 sprintf('for:%s', $other->nickname));
246 $out->elementEnd('li');
251 $tags = $nli->notice->getTags();
253 foreach ($tags as $tag) {
254 $out->elementStart('li');
256 array('rel' => 'tag',
257 'href' => Notice_tag::url($tag)),
259 $out->elementEnd('li');
263 $out->elementEnd('ul');
266 array('class' => 'bookmark_description'),
269 $nli->showNoticeAttachments();
271 $out->elementStart('p', array('style' => 'float: left'));
273 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
275 $out->element('img', array('src' => ($avatar) ?
276 $avatar->displayUrl() :
277 Avatar::defaultImage(AVATAR_MINI_SIZE),
278 'class' => 'avatar photo bookmark_avatar',
279 'width' => AVATAR_MINI_SIZE,
280 'height' => AVATAR_MINI_SIZE,
281 'alt' => $profile->getBestName()));
283 $out->element('a', array('href' => $profile->profileurl,
284 'title' => $profile->getBestName()),
287 $nli->showNoticeLink();
288 $nli->showNoticeSource();
289 $nli->showNoticeLocation();
293 $out->elementEnd('p');
295 $nli->showNoticeOptions();
303 * Render a notice as a Bookmark object
305 * @param Notice $notice Notice to render
306 * @param ActivityObject &$object Empty object to fill
308 * @return boolean hook value
311 function onStartActivityObjectFromNotice($notice, &$object)
314 "Checking {$notice->uri} to see if it's a bookmark.");
316 $nb = Bookmark::getByNotice($notice);
321 "Formatting notice {$notice->uri} as a bookmark.");
323 $object->id = $notice->uri;
324 $object->type = ActivityObject::BOOKMARK;
325 $object->title = $nb->title;
326 $object->summary = $nb->description;
327 $object->link = $notice->bestUrl();
329 // Attributes of the URL
331 $attachments = $notice->attachments();
333 if (count($attachments) != 1) {
334 throw new ServerException(_('Bookmark notice with the '.
335 'wrong number of attachments.'));
338 $target = $attachments[0];
340 $attrs = array('rel' => 'related',
341 'href' => $target->url);
343 if (!empty($target->title)) {
344 $attrs['title'] = $target->title;
347 $object->extra[] = array('link', $attrs);
349 // Attributes of the thumbnail, if any
351 $thumbnail = $target->getThumbnail();
353 if (!empty($thumbnail)) {
354 $tattrs = array('rel' => 'preview',
355 'href' => $thumbnail->url);
357 if (!empty($thumbnail->width)) {
358 $tattrs['media:width'] = $thumbnail->width;
361 if (!empty($thumbnail->height)) {
362 $tattrs['media:height'] = $thumbnail->height;
365 $object->extra[] = array('link', $attrs);
375 * Add our two queue handlers to the queue manager
377 * @param QueueManager $qm current queue manager
379 * @return boolean hook value
382 function onEndInitializeQueueManager($qm)
384 $qm->connect('dlcsback', 'DeliciousBackupImporter');
385 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
390 * Plugin version data
392 * @param array &$versions array of version data
397 function onPluginVersion(&$versions)
399 $versions[] = array('name' => 'Sample',
400 'version' => self::VERSION,
401 'author' => 'Evan Prodromou',
402 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
404 _m('Simple extension for supporting bookmarks.'));
409 * Load our document if requested
411 * @param string &$title Title to fetch
412 * @param string &$output HTML to output
414 * @return boolean hook value
417 function onStartLoadDoc(&$title, &$output)
419 if ($title == 'bookmarklet') {
420 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
422 $c = file_get_contents($filename);
423 $output = common_markup_to_html($c);
424 return false; // success!
431 * Handle a posted bookmark from PuSH
433 * @param Activity $activity activity to handle
434 * @param Ostatus_profile $oprofile Profile for the feed
436 * @return boolean hook value
439 function onStartHandleFeedEntryWithProfile($activity, $oprofile) {
441 common_log(LOG_INFO, "BookmarkPlugin called for new feed entry.");
443 if ($activity->verb == ActivityVerb::POST &&
444 $activity->objects[0]->type == ActivityObject::BOOKMARK) {
446 common_log(LOG_INFO, "Importing activity {$activity->id} as a bookmark.");
448 $author = $oprofile->checkAuthorship($activity);
450 if (empty($author)) {
451 throw new ClientException(_('Can\'t get author for activity.'));
454 self::_postRemoteBookmark($author,
463 static private function _postRemoteBookmark(Ostatus_profile $author, Activity $activity)
465 $bookmark = $activity->objects[0];
467 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
469 if (count($relLinkEls) < 1) {
470 throw new ClientException(_('Expected exactly 1 link rel=related in a Bookmark.'));
473 if (count($relLinkEls) > 1) {
474 common_log(LOG_WARNING, "Got too many link rel=related in a Bookmark.");
477 $linkEl = $relLinkEls[0];
479 $url = $linkEl->getAttribute('href');
483 foreach ($activity->categories as $category) {
484 $tags[] = common_canonical_tag($category->term);
487 $options = array('uri' => $bookmark->id,
488 'url' => $bookmark->link,
489 'created' => common_sql_time($activity->time),
490 'is_local' => Notice::REMOTE_OMB,
491 'source' => 'ostatus');
493 // Fill in location if available
495 $location = $activity->context->location;
498 $options['lat'] = $location->lat;
499 $options['lon'] = $location->lon;
500 if ($location->location_id) {
501 $options['location_ns'] = $location->location_ns;
502 $options['location_id'] = $location->location_id;
506 $replies = $activity->context->attention;
507 $options['groups'] = $author->filterReplies($author, $replies);
508 $options['replies'] = $replies;
510 // Maintain direct reply associations
511 // @fixme what about conversation ID?
513 if (!empty($activity->context->replyToID)) {
514 $orig = Notice::staticGet('uri',
515 $activity->context->replyToID);
517 $options['reply_to'] = $orig->id;
521 Bookmark::saveNew($author->localProfile(),