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 'BookmarkforurlAction':
151 case 'ImportdeliciousAction':
152 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
155 include_once $dir.'/'.$cls.'.php';
158 case 'InitialBookmarkForm':
159 case 'DeliciousBackupImporter':
160 case 'DeliciousBookmarkImporter':
161 include_once $dir.'/'.strtolower($cls).'.php';
169 * Map URLs to actions
171 * @param Net_URL_Mapper $m path-to-action mapper
173 * @return boolean hook value; true means continue processing, false means stop.
175 function onRouterInitialized($m)
177 $m->connect('main/bookmark/new',
178 array('action' => 'newbookmark'),
179 array('id' => '[0-9]+'));
181 $m->connect('main/bookmark/popup',
182 array('action' => 'bookmarkpopup'));
184 $m->connect('main/bookmark/import',
185 array('action' => 'importdelicious'));
187 $m->connect('main/bookmark/forurl',
188 array('action' => 'bookmarkforurl'));
190 $m->connect('bookmark/:id',
191 array('action' => 'showbookmark'),
192 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
194 $m->connect('notice/by-url/:id',
195 array('action' => 'noticebyurl'),
196 array('id' => '[0-9]+'));
203 * Add our two queue handlers to the queue manager
205 * @param QueueManager $qm current queue manager
207 * @return boolean hook value
209 function onEndInitializeQueueManager($qm)
211 $qm->connect('dlcsback', 'DeliciousBackupImporter');
212 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
217 * Plugin version data
219 * @param array &$versions array of version data
223 function onPluginVersion(&$versions)
225 $versions[] = array('name' => 'Bookmark',
226 'version' => self::VERSION,
227 'author' => 'Evan Prodromou',
228 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
230 // TRANS: Plugin description.
231 _m('Simple extension for supporting bookmarks.'));
236 * Load our document if requested
238 * @param string &$title Title to fetch
239 * @param string &$output HTML to output
241 * @return boolean hook value
243 function onStartLoadDoc(&$title, &$output)
245 if ($title == 'bookmarklet') {
246 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
248 $c = file_get_contents($filename);
249 $output = common_markup_to_html($c);
250 return false; // success!
257 * Show a link to our delicious import page on profile settings form
259 * @param Action $action Profile settings action being shown
261 * @return boolean hook value
263 function onEndProfileSettingsActions($action)
265 $user = common_current_user();
267 if (!empty($user) && $user->hasRight(self::IMPORTDELICIOUS)) {
268 $action->elementStart('li');
269 $action->element('a',
270 array('href' => common_local_url('importdelicious')),
271 // TRANS: Link text in proile leading to import form.
272 _m('Import del.icio.us bookmarks'));
273 $action->elementEnd('li');
280 * Output our CSS class for bookmark notice list elements
282 * @param NoticeListItem $nli The item being shown
284 * @return boolean hook value
287 function onStartOpenNoticeListItemElement($nli)
289 $nb = Bookmark::getByNotice($nli->notice);
291 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
292 $class = 'hentry notice bookmark';
293 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
294 $class .= ' limited-scope';
296 $nli->out->elementStart('li', array('class' => $class,
297 'id' => 'notice-' . $id));
298 Event::handle('EndOpenNoticeListItemElement', array($nli));
305 * Save a remote bookmark (from Salmon or PuSH)
307 * @param Ostatus_profile $author Author of the bookmark
308 * @param Activity $activity Activity to save
310 * @return Notice resulting notice.
312 static private function _postRemoteBookmark(Ostatus_profile $author,
315 $bookmark = $activity->objects[0];
317 $options = array('uri' => $bookmark->id,
318 'url' => $bookmark->link,
319 'is_local' => Notice::REMOTE_OMB,
320 'source' => 'ostatus');
322 return self::_postBookmark($author->localProfile(), $activity, $options);
326 * Test if an activity represents posting a bookmark
328 * @param Activity $activity Activity to test
330 * @return true if it's a Post of a Bookmark, else false
332 static private function _isPostBookmark($activity)
334 return ($activity->verb == ActivityVerb::POST &&
335 $activity->objects[0]->type == ActivityObject::BOOKMARK);
340 return array(ActivityObject::BOOKMARK);
344 * When a notice is deleted, delete the related Bookmark
346 * @param Notice $notice Notice being deleted
348 * @return boolean hook value
350 function deleteRelated($notice)
352 $nb = Bookmark::getByNotice($notice);
362 * Save a bookmark from an activity
364 * @param Activity $activity Activity to save
365 * @param Profile $profile Profile to use as author
366 * @param array $options Options to pass to bookmark-saving code
368 * @return Notice resulting notice
370 function saveNoticeFromActivity($activity, $profile, $options=array())
372 $bookmark = $activity->objects[0];
374 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
376 if (count($relLinkEls) < 1) {
377 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
378 throw new ClientException(_m('Expected exactly 1 link '.
379 'rel=related in a Bookmark.'));
382 if (count($relLinkEls) > 1) {
383 common_log(LOG_WARNING,
384 "Got too many link rel=related in a Bookmark.");
387 $linkEl = $relLinkEls[0];
389 $url = $linkEl->getAttribute('href');
393 foreach ($activity->categories as $category) {
394 $tags[] = common_canonical_tag($category->term);
397 if (!empty($activity->time)) {
398 $options['created'] = common_sql_date($activity->time);
401 // Fill in location if available
403 $location = $activity->context->location;
406 $options['lat'] = $location->lat;
407 $options['lon'] = $location->lon;
408 if ($location->location_id) {
409 $options['location_ns'] = $location->location_ns;
410 $options['location_id'] = $location->location_id;
414 $replies = $activity->context->attention;
416 $options['groups'] = array();
417 $options['replies'] = array();
419 foreach ($replies as $replyURI) {
420 $other = Profile::fromURI($replyURI);
421 if (!empty($other)) {
422 $options['replies'][] = $replyURI;
424 $group = User_group::staticGet('uri', $replyURI);
425 if (!empty($group)) {
426 $options['groups'][] = $replyURI;
431 // Maintain direct reply associations
432 // @fixme what about conversation ID?
434 if (!empty($activity->context->replyToID)) {
435 $orig = Notice::staticGet('uri',
436 $activity->context->replyToID);
438 $options['reply_to'] = $orig->id;
442 return Bookmark::saveNew($profile,
450 function activityObjectFromNotice($notice)
452 assert($this->isMyNotice($notice));
455 "Formatting notice {$notice->uri} as a bookmark.");
457 $object = new ActivityObject();
458 $nb = Bookmark::getByNotice($notice);
460 $object->id = $notice->uri;
461 $object->type = ActivityObject::BOOKMARK;
462 $object->title = $nb->title;
463 $object->summary = $nb->description;
464 $object->link = $notice->bestUrl();
466 // Attributes of the URL
468 $attachments = $notice->attachments();
470 if (count($attachments) != 1) {
471 // TRANS: Server exception thrown when a bookmark has multiple attachments.
472 throw new ServerException(_m('Bookmark notice with the '.
473 'wrong number of attachments.'));
476 $target = $attachments[0];
478 $attrs = array('rel' => 'related',
479 'href' => $target->url);
481 if (!empty($target->title)) {
482 $attrs['title'] = $target->title;
485 $object->extra[] = array('link', $attrs, null);
487 // Attributes of the thumbnail, if any
489 $thumbnail = $target->getThumbnail();
491 if (!empty($thumbnail)) {
492 $tattrs = array('rel' => 'preview',
493 'href' => $thumbnail->url);
495 if (!empty($thumbnail->width)) {
496 $tattrs['media:width'] = $thumbnail->width;
499 if (!empty($thumbnail->height)) {
500 $tattrs['media:height'] = $thumbnail->height;
503 $object->extra[] = array('link', $attrs, null);
510 * @fixme WARNING WARNING WARNING this opens a 'div' that is apparently closed by MicroAppPlugin
511 * @fixme that's probably wrong?
513 * @param Notice $notice
514 * @param HTMLOutputter $out
516 function showNotice($notice, $out)
518 $nb = Bookmark::getByNotice($notice);
520 $profile = $notice->getProfile();
522 $atts = $notice->attachments();
524 if (count($atts) < 1) {
525 // Something wrong; let default code deal with it.
526 // TRANS: Exception thrown when a bookmark has no attachments.
527 // TRANS: %1$s is a bookmark ID, %2$s is a notice ID (number).
528 throw new Exception(sprintf(_m('Bookmark %1$s (notice %2$d) has no attachments.'),
535 // XXX: only show the bookmark URL for non-single-page stuff
537 if ($out instanceof ShowbookmarkAction) {
539 $out->elementStart('h3');
541 array('href' => $att->url,
542 'class' => 'bookmark-title entry-title'),
544 $out->elementEnd('h3');
546 $countUrl = common_local_url('noticebyurl',
547 array('id' => $att->id));
549 $out->element('a', array('class' => 'bookmark-notice-count',
550 'href' => $countUrl),
551 $att->noticeCount());
554 // Replies look like "for:" tags
556 $replies = $notice->getReplies();
557 $tags = $notice->getTags();
559 if (!empty($replies) || !empty($tags)) {
561 $out->elementStart('ul', array('class' => 'bookmark-tags'));
563 foreach ($replies as $reply) {
564 $other = Profile::staticGet('id', $reply);
565 if (!empty($other)) {
566 $out->elementStart('li');
567 $out->element('a', array('rel' => 'tag',
568 'href' => $other->profileurl,
569 'title' => $other->getBestName()),
570 sprintf('for:%s', $other->nickname));
571 $out->elementEnd('li');
576 foreach ($tags as $tag) {
577 $out->elementStart('li');
579 array('rel' => 'tag',
580 'href' => Notice_tag::url($tag)),
582 $out->elementEnd('li');
586 $out->elementEnd('ul');
589 if (!empty($nb->description)) {
591 array('class' => 'bookmark-description'),
595 if (common_config('attachments', 'show_thumbs')) {
597 foreach ($atts as $check) {
598 $thumbnail = File_thumbnail::staticGet('file_id', $check->id);
599 if (!empty($thumbnail)) {
605 $al = new InlineAttachmentList($notice, $out);
610 $out->elementStart('div', array('class' => 'bookmark-info entry-content'));
612 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
615 array('src' => ($avatar) ?
616 $avatar->displayUrl() :
617 Avatar::defaultImage(AVATAR_MINI_SIZE),
618 'class' => 'avatar photo bookmark-avatar',
619 'width' => AVATAR_MINI_SIZE,
620 'height' => AVATAR_MINI_SIZE,
621 'alt' => $profile->getBestName()));
623 $out->raw(' '); // avoid for AJAX XML compatibility
625 $out->elementStart('span', 'vcard author'); // hack for belongsOnTimeline; JS needs to be able to find the author
627 array('class' => 'url',
628 'href' => $profile->profileurl,
629 'title' => $profile->getBestName()),
631 $out->elementEnd('span');
634 function entryForm($out)
636 return new InitialBookmarkForm($out);
646 // TRANS: Application title.
647 return _m('TITLE','Bookmark');