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('notice_bookmark',
67 array(new ColumnDef('notice_id',
72 new ColumnDef('title',
75 new ColumnDef('description',
82 * When a notice is deleted, delete the related Notice_bookmark
84 * @param Notice $notice Notice being deleted
86 * @return boolean hook value
89 function onNoticeDeleteRelated($notice)
91 $nb = Notice_bookmark::staticGet('notice_id', $notice->id);
101 * Show the CSS necessary for this plugin
103 * @param Action $action the action being run
105 * @return boolean hook value
108 function onEndShowStyles($action)
110 $action->cssLink('plugins/Bookmark/bookmark.css');
115 * Load related modules when needed
117 * @param string $cls Name of the class to be loaded
119 * @return boolean hook value; true means continue processing, false means stop.
122 function onAutoload($cls)
124 $dir = dirname(__FILE__);
128 case 'NewbookmarkAction':
129 case 'BookmarkpopupAction':
130 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
132 case 'Notice_bookmark':
133 include_once $dir.'/'.$cls.'.php';
136 case 'DeliciousBackupImporter':
137 case 'DeliciousBookmarkImporter':
138 include_once $dir.'/'.strtolower($cls).'.php';
146 * Map URLs to actions
148 * @param Net_URL_Mapper $m path-to-action mapper
150 * @return boolean hook value; true means continue processing, false means stop.
153 function onRouterInitialized($m)
155 $m->connect('main/bookmark/new',
156 array('action' => 'newbookmark'),
157 array('id' => '[0-9]+'));
159 $m->connect('main/bookmark/popup', array('action' => 'bookmarkpopup'));
165 * Output the HTML for a bookmark in a list
167 * @param NoticeListItem $nli The list item being shown.
169 * @return boolean hook value
172 function onStartShowNoticeItem($nli)
174 $nb = Notice_bookmark::staticGet('notice_id',
180 $notice = $nli->notice;
181 $profile = $nli->profile;
183 $atts = $notice->attachments();
185 if (count($atts) < 1) {
186 // Something wrong; let default code deal with it.
192 $out->elementStart('h3');
194 array('href' => $att->url),
196 $out->elementEnd('h3');
198 $out->elementStart('ul', array('class' => 'bookmark_tags'));
200 // Replies look like "for:" tags
202 $replies = $nli->notice->getReplies();
204 if (!empty($replies)) {
205 foreach ($replies as $reply) {
206 $other = Profile::staticGet('id', $reply);
207 $out->elementStart('li');
208 $out->element('a', array('rel' => 'tag',
209 'href' => $other->profileurl,
210 'title' => $other->getBestName()),
211 sprintf('for:%s', $other->nickname));
212 $out->elementEnd('li');
217 $tags = $nli->notice->getTags();
219 foreach ($tags as $tag) {
220 $out->elementStart('li');
222 array('rel' => 'tag',
223 'href' => Notice_tag::url($tag)),
225 $out->elementEnd('li');
229 $out->elementEnd('ul');
232 array('class' => 'bookmark_description'),
235 $nli->showNoticeAttachments();
237 $out->elementStart('p', array('style' => 'float: left'));
239 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
241 $out->element('img', array('src' => ($avatar) ?
242 $avatar->displayUrl() :
243 Avatar::defaultImage(AVATAR_MINI_SIZE),
244 'class' => 'avatar photo bookmark_avatar',
245 'width' => AVATAR_MINI_SIZE,
246 'height' => AVATAR_MINI_SIZE,
247 'alt' => $profile->getBestName()));
249 $out->element('a', array('href' => $profile->profileurl,
250 'title' => $profile->getBestName()),
253 $nli->showNoticeLink();
254 $nli->showNoticeSource();
255 $nli->showNoticeLocation();
259 $out->elementEnd('p');
261 $nli->showNoticeOptions();
269 * Render a notice as a Bookmark object
271 * @param Notice $notice Notice to render
272 * @param ActivityObject &$object Empty object to fill
274 * @return boolean hook value
277 function onStartActivityObjectFromNotice($notice, &$object)
280 "Checking {$notice->uri} to see if it's a bookmark.");
282 $nb = Notice_bookmark::staticGet('notice_id',
288 "Formatting notice {$notice->uri} as a bookmark.");
290 $object->id = $notice->uri;
291 $object->type = ActivityObject::BOOKMARK;
292 $object->title = $nb->title;
293 $object->summary = $nb->description;
294 $object->link = $notice->bestUrl();
296 // Attributes of the URL
298 $attachments = $notice->attachments();
300 if (count($attachments) != 1) {
301 throw new ServerException(_('Bookmark notice with the '.
302 'wrong number of attachments.'));
305 $target = $attachments[0];
307 $attrs = array('rel' => 'related',
308 'href' => $target->url);
310 if (!empty($target->title)) {
311 $attrs['title'] = $target->title;
314 $object->extra[] = array('link', $attrs);
316 // Attributes of the thumbnail, if any
318 $thumbnail = $target->getThumbnail();
320 if (!empty($thumbnail)) {
321 $tattrs = array('rel' => 'preview',
322 'href' => $thumbnail->url);
324 if (!empty($thumbnail->width)) {
325 $tattrs['media:width'] = $thumbnail->width;
328 if (!empty($thumbnail->height)) {
329 $tattrs['media:height'] = $thumbnail->height;
332 $object->extra[] = array('link', $attrs);
342 * Add our two queue handlers to the queue manager
344 * @param QueueManager $qm current queue manager
346 * @return boolean hook value
349 function onEndInitializeQueueManager($qm)
351 $qm->connect('dlcsback', 'DeliciousBackupImporter');
352 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
357 * Plugin version data
359 * @param array &$versions array of version data
364 function onPluginVersion(&$versions)
366 $versions[] = array('name' => 'Sample',
367 'version' => self::VERSION,
368 'author' => 'Evan Prodromou',
369 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
371 _m('Simple extension for supporting bookmarks.'));
376 * Load our document if requested
378 * @param string &$title Title to fetch
379 * @param string &$output HTML to output
381 * @return boolean hook value
384 function onStartLoadDoc(&$title, &$output)
386 if ($title == 'bookmarklet') {
387 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
389 $c = file_get_contents($filename);
390 $output = common_markup_to_html($c);
391 return false; // success!
398 * Handle a posted bookmark from PuSH
400 * @param Activity $activity activity to handle
401 * @param Ostatus_profile $oprofile Profile for the feed
403 * @return boolean hook value
406 function onStartHandleFeedEntryWithProfile($activity, $oprofile) {
408 common_log(LOG_INFO, "BookmarkPlugin called for new feed entry.");
410 if ($activity->verb == ActivityVerb::POST &&
411 $activity->objects[0]->type == ActivityObject::BOOKMARK) {
413 common_log(LOG_INFO, "Importing activity {$activity->id} as a bookmark.");
415 $author = $oprofile->checkAuthorship($activity);
417 if (empty($author)) {
418 throw new ClientException(_('Can\'t get author for activity.'));
421 self::_postRemoteBookmark($author,
430 static private function _postRemoteBookmark(Ostatus_profile $author, Activity $activity)
432 $bookmark = $activity->objects[0];
434 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
436 if (count($relLinkEls) < 1) {
437 throw new ClientException(_('Expected exactly 1 link rel=related in a Bookmark.'));
440 if (count($relLinkEls) > 1) {
441 common_log(LOG_WARNING, "Got too many link rel=related in a Bookmark.");
444 $linkEl = $relLinkEls[0];
446 $url = $linkEl->getAttribute('href');
450 foreach ($activity->categories as $category) {
451 $tags[] = common_canonical_tag($category->term);
454 $options = array('uri' => $bookmark->id,
455 'url' => $bookmark->link,
456 'created' => common_sql_time($activity->time),
457 'is_local' => Notice::REMOTE_OMB,
458 'source' => 'ostatus');
460 // Fill in location if available
462 $location = $activity->context->location;
465 $options['lat'] = $location->lat;
466 $options['lon'] = $location->lon;
467 if ($location->location_id) {
468 $options['location_ns'] = $location->location_ns;
469 $options['location_id'] = $location->location_id;
473 $replies = $activity->context->attention;
474 $options['groups'] = $author->filterReplies($author, $replies);
475 $options['replies'] = $replies;
477 // Maintain direct reply associations
478 // @fixme what about conversation ID?
480 if (!empty($activity->context->replyToID)) {
481 $orig = Notice::staticGet('uri',
482 $activity->context->replyToID);
484 $options['reply_to'] = $orig->id;
488 Notice_bookmark::saveNew($author->localProfile(),