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/
46 class BookmarkPlugin extends MicroAppPlugin
48 const VERSION = '0.1';
49 const IMPORTDELICIOUS = 'BookmarkPlugin:IMPORTDELICIOUS';
52 * Authorization for importing delicious bookmarks
54 * By default, everyone can import bookmarks except silenced people.
56 * @param Profile $profile Person whose rights to check
57 * @param string $right Right to check; const value
58 * @param boolean &$result Result of the check, writeable
60 * @return boolean hook value
62 function onUserRightsCheck($profile, $right, &$result)
64 if ($right == self::IMPORTDELICIOUS) {
65 $result = !$profile->isSilenced();
72 * Database schema setup
77 * @return boolean hook value; true means continue processing, false means stop.
79 function onCheckSchema()
81 $schema = Schema::get();
83 // For storing user-submitted flags on profiles
85 $schema->ensureTable('bookmark',
86 array(new ColumnDef('id',
91 new ColumnDef('profile_id',
101 new ColumnDef('title',
104 new ColumnDef('description',
111 new ColumnDef('created',
121 * Show the CSS necessary for this plugin
123 * @param Action $action the action being run
125 * @return boolean hook value
127 function onEndShowStyles($action)
129 $action->cssLink($this->path('bookmark.css'));
134 * Load related modules when needed
136 * @param string $cls Name of the class to be loaded
138 * @return boolean hook value; true means continue processing, false means stop.
140 function onAutoload($cls)
142 $dir = dirname(__FILE__);
146 case 'ShowbookmarkAction':
147 case 'NewbookmarkAction':
148 case 'BookmarkpopupAction':
149 case 'NoticebyurlAction':
150 case 'ImportdeliciousAction':
151 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
154 include_once $dir.'/'.$cls.'.php';
157 case 'DeliciousBackupImporter':
158 case 'DeliciousBookmarkImporter':
159 include_once $dir.'/'.strtolower($cls).'.php';
167 * Map URLs to actions
169 * @param Net_URL_Mapper $m path-to-action mapper
171 * @return boolean hook value; true means continue processing, false means stop.
173 function onRouterInitialized($m)
175 $m->connect('main/bookmark/new',
176 array('action' => 'newbookmark'),
177 array('id' => '[0-9]+'));
179 $m->connect('main/bookmark/popup',
180 array('action' => 'bookmarkpopup'));
182 $m->connect('main/bookmark/import',
183 array('action' => 'importdelicious'));
185 $m->connect('bookmark/:id',
186 array('action' => 'showbookmark'),
187 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
189 $m->connect('notice/by-url/:id',
190 array('action' => 'noticebyurl'),
191 array('id' => '[0-9]+'));
198 * Add our two queue handlers to the queue manager
200 * @param QueueManager $qm current queue manager
202 * @return boolean hook value
204 function onEndInitializeQueueManager($qm)
206 $qm->connect('dlcsback', 'DeliciousBackupImporter');
207 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
212 * Plugin version data
214 * @param array &$versions array of version data
218 function onPluginVersion(&$versions)
220 $versions[] = array('name' => 'Sample',
221 'version' => self::VERSION,
222 'author' => 'Evan Prodromou',
223 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
225 // TRANS: Plugin description.
226 _m('Simple extension for supporting bookmarks.'));
231 * Load our document if requested
233 * @param string &$title Title to fetch
234 * @param string &$output HTML to output
236 * @return boolean hook value
238 function onStartLoadDoc(&$title, &$output)
240 if ($title == 'bookmarklet') {
241 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
243 $c = file_get_contents($filename);
244 $output = common_markup_to_html($c);
245 return false; // success!
252 * Show a link to our delicious import page on profile settings form
254 * @param Action $action Profile settings action being shown
256 * @return boolean hook value
258 function onEndProfileSettingsActions($action)
260 $user = common_current_user();
262 if (!empty($user) && $user->hasRight(self::IMPORTDELICIOUS)) {
263 $action->elementStart('li');
264 $action->element('a',
265 array('href' => common_local_url('importdelicious')),
266 // TRANS: Link text in proile leading to import form.
267 _m('Import del.icio.us bookmarks'));
268 $action->elementEnd('li');
275 * Output our CSS class for bookmark notice list elements
277 * @param NoticeListItem $nli The item being shown
279 * @return boolean hook value
282 function onStartOpenNoticeListItemElement($nli)
284 $nb = Bookmark::getByNotice($nli->notice);
286 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
287 $class = 'hentry notice bookmark';
288 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
289 $class .= ' limited-scope';
291 $nli->out->elementStart('li', array('class' => $class,
292 'id' => 'notice-' . $id));
293 Event::handle('EndOpenNoticeListItemElement', array($nli));
300 * Save a remote bookmark (from Salmon or PuSH)
302 * @param Ostatus_profile $author Author of the bookmark
303 * @param Activity $activity Activity to save
305 * @return Notice resulting notice.
307 static private function _postRemoteBookmark(Ostatus_profile $author,
310 $bookmark = $activity->objects[0];
312 $options = array('uri' => $bookmark->id,
313 'url' => $bookmark->link,
314 'is_local' => Notice::REMOTE_OMB,
315 'source' => 'ostatus');
317 return self::_postBookmark($author->localProfile(), $activity, $options);
321 * Test if an activity represents posting a bookmark
323 * @param Activity $activity Activity to test
325 * @return true if it's a Post of a Bookmark, else false
327 static private function _isPostBookmark($activity)
329 return ($activity->verb == ActivityVerb::POST &&
330 $activity->objects[0]->type == ActivityObject::BOOKMARK);
335 return array(ActivityObject::BOOKMARK);
339 * When a notice is deleted, delete the related Bookmark
341 * @param Notice $notice Notice being deleted
343 * @return boolean hook value
345 function deleteRelated($notice)
347 $nb = Bookmark::getByNotice($notice);
357 * Save a bookmark from an activity
359 * @param Activity $activity Activity to save
360 * @param Profile $profile Profile to use as author
361 * @param array $options Options to pass to bookmark-saving code
363 * @return Notice resulting notice
365 function saveNoticeFromActivity($activity, $profile, $options=array())
367 $bookmark = $activity->objects[0];
369 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
371 if (count($relLinkEls) < 1) {
372 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
373 throw new ClientException(_m('Expected exactly 1 link '.
374 'rel=related in a Bookmark.'));
377 if (count($relLinkEls) > 1) {
378 common_log(LOG_WARNING,
379 "Got too many link rel=related in a Bookmark.");
382 $linkEl = $relLinkEls[0];
384 $url = $linkEl->getAttribute('href');
388 foreach ($activity->categories as $category) {
389 $tags[] = common_canonical_tag($category->term);
392 if (!empty($activity->time)) {
393 $options['created'] = common_sql_date($activity->time);
396 // Fill in location if available
398 $location = $activity->context->location;
401 $options['lat'] = $location->lat;
402 $options['lon'] = $location->lon;
403 if ($location->location_id) {
404 $options['location_ns'] = $location->location_ns;
405 $options['location_id'] = $location->location_id;
409 $replies = $activity->context->attention;
411 $options['groups'] = array();
412 $options['replies'] = array();
414 foreach ($replies as $replyURI) {
415 $other = Profile::fromURI($replyURI);
416 if (!empty($other)) {
417 $options['replies'][] = $replyURI;
419 $group = User_group::staticGet('uri', $replyURI);
420 if (!empty($group)) {
421 $options['groups'][] = $replyURI;
426 // Maintain direct reply associations
427 // @fixme what about conversation ID?
429 if (!empty($activity->context->replyToID)) {
430 $orig = Notice::staticGet('uri',
431 $activity->context->replyToID);
433 $options['reply_to'] = $orig->id;
437 return Bookmark::saveNew($profile,
445 function activityObjectFromNotice($notice)
447 assert($this->isMyNotice($notice));
450 "Formatting notice {$notice->uri} as a bookmark.");
452 $object = new ActivityObject();
453 $nb = Bookmark::getByNotice($notice);
455 $object->id = $notice->uri;
456 $object->type = ActivityObject::BOOKMARK;
457 $object->title = $nb->title;
458 $object->summary = $nb->description;
459 $object->link = $notice->bestUrl();
461 // Attributes of the URL
463 $attachments = $notice->attachments();
465 if (count($attachments) != 1) {
466 // TRANS: Server exception thrown when a bookmark has multiple attachments.
467 throw new ServerException(_m('Bookmark notice with the '.
468 'wrong number of attachments.'));
471 $target = $attachments[0];
473 $attrs = array('rel' => 'related',
474 'href' => $target->url);
476 if (!empty($target->title)) {
477 $attrs['title'] = $target->title;
480 $object->extra[] = array('link', $attrs, null);
482 // Attributes of the thumbnail, if any
484 $thumbnail = $target->getThumbnail();
486 if (!empty($thumbnail)) {
487 $tattrs = array('rel' => 'preview',
488 'href' => $thumbnail->url);
490 if (!empty($thumbnail->width)) {
491 $tattrs['media:width'] = $thumbnail->width;
494 if (!empty($thumbnail->height)) {
495 $tattrs['media:height'] = $thumbnail->height;
498 $object->extra[] = array('link', $attrs, null);
505 * @fixme WARNING WARNING WARNING this opens a 'div' that is apparently closed by MicroAppPlugin
506 * @fixme that's probably wrong?
508 * @param Notice $notice
509 * @param HTMLOutputter $out
511 function showNotice($notice, $out)
513 $nb = Bookmark::getByNotice($notice);
515 $profile = $notice->getProfile();
517 $atts = $notice->attachments();
519 if (count($atts) < 1) {
520 // Something wrong; let default code deal with it.
521 // TRANS: Exception thrown when a bookmark has no attachments.
522 throw new Exception(_m('Bookmark has no attachments.'));
527 // XXX: only show the bookmark URL for non-single-page stuff
529 if ($out instanceof ShowbookmarkAction) {
531 $out->elementStart('h3');
533 array('href' => $att->url,
534 'class' => 'bookmark-title entry-title'),
536 $out->elementEnd('h3');
538 $countUrl = common_local_url('noticebyurl',
539 array('id' => $att->id));
541 $out->element('a', array('class' => 'bookmark-notice-count',
542 'href' => $countUrl),
543 $att->noticeCount());
546 // Replies look like "for:" tags
548 $replies = $notice->getReplies();
549 $tags = $notice->getTags();
551 if (!empty($replies) || !empty($tags)) {
553 $out->elementStart('ul', array('class' => 'bookmark-tags'));
555 foreach ($replies as $reply) {
556 $other = Profile::staticGet('id', $reply);
557 if (!empty($other)) {
558 $out->elementStart('li');
559 $out->element('a', array('rel' => 'tag',
560 'href' => $other->profileurl,
561 'title' => $other->getBestName()),
562 sprintf('for:%s', $other->nickname));
563 $out->elementEnd('li');
568 foreach ($tags as $tag) {
569 $out->elementStart('li');
571 array('rel' => 'tag',
572 'href' => Notice_tag::url($tag)),
574 $out->elementEnd('li');
578 $out->elementEnd('ul');
581 if (!empty($nb->description)) {
583 array('class' => 'bookmark-description'),
587 if (common_config('attachments', 'show_thumbs')) {
589 foreach ($atts as $check) {
590 $thumbnail = File_thumbnail::staticGet('file_id', $check->id);
591 if (!empty($thumbnail)) {
597 $al = new InlineAttachmentList($notice, $out);
602 $out->elementStart('div', array('class' => 'bookmark-info entry-content'));
604 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
607 array('src' => ($avatar) ?
608 $avatar->displayUrl() :
609 Avatar::defaultImage(AVATAR_MINI_SIZE),
610 'class' => 'avatar photo bookmark-avatar',
611 'width' => AVATAR_MINI_SIZE,
612 'height' => AVATAR_MINI_SIZE,
613 'alt' => $profile->getBestName()));
615 $out->raw(' '); // avoid for AJAX XML compatibility
617 $out->elementStart('span', 'vcard author'); // hack for belongsOnTimeline; JS needs to be able to find the author
619 array('class' => 'url',
620 'href' => $profile->profileurl,
621 'title' => $profile->getBestName()),
623 $out->elementEnd('span');
626 function entryForm($out)
628 return new BookmarkForm($out);
638 // TRANS: Application title.
639 return _m('TITLE','Bookmark');