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 MicroAppPlugin
49 const VERSION = '0.1';
50 const IMPORTDELICIOUS = 'BookmarkPlugin:IMPORTDELICIOUS';
53 * Authorization for importing delicious bookmarks
55 * By default, everyone can import bookmarks except silenced people.
57 * @param Profile $profile Person whose rights to check
58 * @param string $right Right to check; const value
59 * @param boolean &$result Result of the check, writeable
61 * @return boolean hook value
64 function onUserRightsCheck($profile, $right, &$result)
66 if ($right == self::IMPORTDELICIOUS) {
67 $result = !$profile->isSilenced();
74 * Database schema setup
79 * @return boolean hook value; true means continue processing, false means stop.
82 function onCheckSchema()
84 $schema = Schema::get();
86 // For storing user-submitted flags on profiles
88 $schema->ensureTable('bookmark',
89 array(new ColumnDef('id',
94 new ColumnDef('profile_id',
104 new ColumnDef('title',
107 new ColumnDef('description',
114 new ColumnDef('created',
124 * Show the CSS necessary for this plugin
126 * @param Action $action the action being run
128 * @return boolean hook value
131 function onEndShowStyles($action)
133 $action->cssLink($this->path('bookmark.css'));
138 * Load related modules when needed
140 * @param string $cls Name of the class to be loaded
142 * @return boolean hook value; true means continue processing, false means stop.
145 function onAutoload($cls)
147 $dir = dirname(__FILE__);
151 case 'ShowbookmarkAction':
152 case 'NewbookmarkAction':
153 case 'BookmarkpopupAction':
154 case 'NoticebyurlAction':
155 case 'ImportdeliciousAction':
156 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
159 include_once $dir.'/'.$cls.'.php';
162 case 'DeliciousBackupImporter':
163 case 'DeliciousBookmarkImporter':
164 include_once $dir.'/'.strtolower($cls).'.php';
172 * Map URLs to actions
174 * @param Net_URL_Mapper $m path-to-action mapper
176 * @return boolean hook value; true means continue processing, false means stop.
179 function onRouterInitialized($m)
181 $m->connect('main/bookmark/new',
182 array('action' => 'newbookmark'),
183 array('id' => '[0-9]+'));
185 $m->connect('main/bookmark/popup',
186 array('action' => 'bookmarkpopup'));
188 $m->connect('main/bookmark/import',
189 array('action' => 'importdelicious'));
191 $m->connect('bookmark/:id',
192 array('action' => 'showbookmark'),
193 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
195 $m->connect('notice/by-url/:id',
196 array('action' => 'noticebyurl'),
197 array('id' => '[0-9]+'));
204 * Add our two queue handlers to the queue manager
206 * @param QueueManager $qm current queue manager
208 * @return boolean hook value
211 function onEndInitializeQueueManager($qm)
213 $qm->connect('dlcsback', 'DeliciousBackupImporter');
214 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
219 * Plugin version data
221 * @param array &$versions array of version data
226 function onPluginVersion(&$versions)
228 $versions[] = array('name' => 'Sample',
229 'version' => self::VERSION,
230 'author' => 'Evan Prodromou',
231 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
233 _m('Simple extension for supporting bookmarks.'));
238 * Load our document if requested
240 * @param string &$title Title to fetch
241 * @param string &$output HTML to output
243 * @return boolean hook value
246 function onStartLoadDoc(&$title, &$output)
248 if ($title == 'bookmarklet') {
249 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
251 $c = file_get_contents($filename);
252 $output = common_markup_to_html($c);
253 return false; // success!
262 * Show a link to our delicious import page on profile settings form
264 * @param Action $action Profile settings action being shown
266 * @return boolean hook value
269 function onEndProfileSettingsActions($action)
271 $user = common_current_user();
273 if (!empty($user) && $user->hasRight(self::IMPORTDELICIOUS)) {
274 $action->elementStart('li');
275 $action->element('a',
276 array('href' => common_local_url('importdelicious')),
277 _('Import del.icio.us bookmarks'));
278 $action->elementEnd('li');
285 * Output our CSS class for bookmark notice list elements
287 * @param NoticeListItem $nli The item being shown
289 * @return boolean hook value
292 function onStartOpenNoticeListItemElement($nli)
294 $nb = Bookmark::getByNotice($nli->notice);
296 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
297 $nli->out->elementStart('li', array('class' => 'hentry notice bookmark',
298 'id' => 'notice-' . $id));
299 Event::handle('EndOpenNoticeListItemElement', array($nli));
306 * Save a remote bookmark (from Salmon or PuSH)
308 * @param Ostatus_profile $author Author of the bookmark
309 * @param Activity $activity Activity to save
311 * @return Notice resulting notice.
314 static private function _postRemoteBookmark(Ostatus_profile $author,
317 $bookmark = $activity->objects[0];
319 $options = array('uri' => $bookmark->id,
320 'url' => $bookmark->link,
321 'is_local' => Notice::REMOTE_OMB,
322 'source' => 'ostatus');
324 return self::_postBookmark($author->localProfile(), $activity, $options);
328 * Test if an activity represents posting a bookmark
330 * @param Activity $activity Activity to test
332 * @return true if it's a Post of a Bookmark, else false
335 static private function _isPostBookmark($activity)
337 return ($activity->verb == ActivityVerb::POST &&
338 $activity->objects[0]->type == ActivityObject::BOOKMARK);
343 return array(ActivityObject::BOOKMARK);
347 * When a notice is deleted, delete the related Bookmark
349 * @param Notice $notice Notice being deleted
351 * @return boolean hook value
354 function deleteRelated($notice)
356 $nb = Bookmark::getByNotice($notice);
366 * Save a bookmark from an activity
368 * @param Activity $activity Activity to save
369 * @param Profile $profile Profile to use as author
370 * @param array $options Options to pass to bookmark-saving code
372 * @return Notice resulting notice
375 function saveNoticeFromActivity($activity, $profile, $options=array())
377 $bookmark = $activity->objects[0];
379 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
381 if (count($relLinkEls) < 1) {
382 throw new ClientException(_('Expected exactly 1 link '.
383 'rel=related in a Bookmark.'));
386 if (count($relLinkEls) > 1) {
387 common_log(LOG_WARNING,
388 "Got too many link rel=related in a Bookmark.");
391 $linkEl = $relLinkEls[0];
393 $url = $linkEl->getAttribute('href');
397 foreach ($activity->categories as $category) {
398 $tags[] = common_canonical_tag($category->term);
401 if (!empty($activity->time)) {
402 $options['created'] = common_sql_date($activity->time);
405 // Fill in location if available
407 $location = $activity->context->location;
410 $options['lat'] = $location->lat;
411 $options['lon'] = $location->lon;
412 if ($location->location_id) {
413 $options['location_ns'] = $location->location_ns;
414 $options['location_id'] = $location->location_id;
418 $replies = $activity->context->attention;
420 $options['groups'] = array();
421 $options['replies'] = array();
423 foreach ($replies as $replyURI) {
424 $other = Profile::fromURI($replyURI);
425 if (!empty($other)) {
426 $options['replies'][] = $replyURI;
428 $group = User_group::staticGet('uri', $replyURI);
429 if (!empty($group)) {
430 $options['groups'][] = $replyURI;
435 // Maintain direct reply associations
436 // @fixme what about conversation ID?
438 if (!empty($activity->context->replyToID)) {
439 $orig = Notice::staticGet('uri',
440 $activity->context->replyToID);
442 $options['reply_to'] = $orig->id;
446 return Bookmark::saveNew($profile,
454 function activityObjectFromNotice($notice)
456 assert($this->isMyNotice($notice));
459 "Formatting notice {$notice->uri} as a bookmark.");
461 $object = new ActivityObject();
463 $object->id = $notice->uri;
464 $object->type = ActivityObject::BOOKMARK;
465 $object->title = $nb->title;
466 $object->summary = $nb->description;
467 $object->link = $notice->bestUrl();
469 // Attributes of the URL
471 $attachments = $notice->attachments();
473 if (count($attachments) != 1) {
474 throw new ServerException(_('Bookmark notice with the '.
475 'wrong number of attachments.'));
478 $target = $attachments[0];
480 $attrs = array('rel' => 'related',
481 'href' => $target->url);
483 if (!empty($target->title)) {
484 $attrs['title'] = $target->title;
487 $object->extra[] = array('link', $attrs, null);
489 // Attributes of the thumbnail, if any
491 $thumbnail = $target->getThumbnail();
493 if (!empty($thumbnail)) {
494 $tattrs = array('rel' => 'preview',
495 'href' => $thumbnail->url);
497 if (!empty($thumbnail->width)) {
498 $tattrs['media:width'] = $thumbnail->width;
501 if (!empty($thumbnail->height)) {
502 $tattrs['media:height'] = $thumbnail->height;
505 $object->extra[] = array('link', $attrs, null);
512 * @fixme WARNING WARNING WARNING this opens a 'div' that is apparently closed by MicroAppPlugin
513 * @fixme that's probably wrong?
515 * @param Notice $notice
516 * @param HTMLOutputter $out
518 function showNotice($notice, $out)
520 $nb = Bookmark::getByNotice($notice);
522 $profile = $notice->getProfile();
524 $atts = $notice->attachments();
526 if (count($atts) < 1) {
527 // Something wrong; let default code deal with it.
528 throw new Exception("That can't be right.");
533 // XXX: only show the bookmark URL for non-single-page stuff
535 if ($out instanceof ShowbookmarkAction) {
537 $out->elementStart('h3');
539 array('href' => $att->url,
540 'class' => 'bookmark-title entry-title'),
542 $out->elementEnd('h3');
544 $countUrl = common_local_url('noticebyurl',
545 array('id' => $att->id));
547 $out->element('a', array('class' => 'bookmark-notice-count',
548 'href' => $countUrl),
549 $att->noticeCount());
552 // Replies look like "for:" tags
554 $replies = $notice->getReplies();
555 $tags = $notice->getTags();
557 if (!empty($replies) || !empty($tags)) {
559 $out->elementStart('ul', array('class' => 'bookmark-tags'));
561 foreach ($replies as $reply) {
562 $other = Profile::staticGet('id', $reply);
563 $out->elementStart('li');
564 $out->element('a', array('rel' => 'tag',
565 'href' => $other->profileurl,
566 'title' => $other->getBestName()),
567 sprintf('for:%s', $other->nickname));
568 $out->elementEnd('li');
572 foreach ($tags as $tag) {
573 $out->elementStart('li');
575 array('rel' => 'tag',
576 'href' => Notice_tag::url($tag)),
578 $out->elementEnd('li');
582 $out->elementEnd('ul');
585 if (!empty($nb->description)) {
587 array('class' => 'bookmark-description'),
591 if (common_config('attachments', 'show_thumbs')) {
593 foreach ($atts as $check) {
594 $thumbnail = File_thumbnail::staticGet('file_id', $check->id);
595 if (!empty($thumbnail)) {
601 $al = new InlineAttachmentList($notice, $out);
606 $out->elementStart('div', array('class' => 'bookmark-info entry-content'));
608 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
611 array('src' => ($avatar) ?
612 $avatar->displayUrl() :
613 Avatar::defaultImage(AVATAR_MINI_SIZE),
614 'class' => 'avatar photo bookmark-avatar',
615 'width' => AVATAR_MINI_SIZE,
616 'height' => AVATAR_MINI_SIZE,
617 'alt' => $profile->getBestName()));
619 $out->raw(' '); // avoid for AJAX XML compatibility
621 $out->elementStart('span', 'vcard author'); // hack for belongsOnTimeline; JS needs to be able to find the author
623 array('class' => 'url',
624 'href' => $profile->profileurl,
625 'title' => $profile->getBestName()),
627 $out->elementEnd('span');
630 function entryForm($out)
632 return new BookmarkForm($out);
642 return _m('Bookmark');