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 'InitialBookmarkForm':
158 case 'DeliciousBackupImporter':
159 case 'DeliciousBookmarkImporter':
160 include_once $dir.'/'.strtolower($cls).'.php';
168 * Map URLs to actions
170 * @param Net_URL_Mapper $m path-to-action mapper
172 * @return boolean hook value; true means continue processing, false means stop.
174 function onRouterInitialized($m)
176 $m->connect('main/bookmark/new',
177 array('action' => 'newbookmark'),
178 array('id' => '[0-9]+'));
180 $m->connect('main/bookmark/popup',
181 array('action' => 'bookmarkpopup'));
183 $m->connect('main/bookmark/import',
184 array('action' => 'importdelicious'));
186 $m->connect('bookmark/:id',
187 array('action' => 'showbookmark'),
188 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
190 $m->connect('notice/by-url/:id',
191 array('action' => 'noticebyurl'),
192 array('id' => '[0-9]+'));
199 * Add our two queue handlers to the queue manager
201 * @param QueueManager $qm current queue manager
203 * @return boolean hook value
205 function onEndInitializeQueueManager($qm)
207 $qm->connect('dlcsback', 'DeliciousBackupImporter');
208 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
213 * Plugin version data
215 * @param array &$versions array of version data
219 function onPluginVersion(&$versions)
221 $versions[] = array('name' => 'Bookmark',
222 'version' => self::VERSION,
223 'author' => 'Evan Prodromou',
224 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
226 // TRANS: Plugin description.
227 _m('Simple extension for supporting bookmarks.'));
232 * Load our document if requested
234 * @param string &$title Title to fetch
235 * @param string &$output HTML to output
237 * @return boolean hook value
239 function onStartLoadDoc(&$title, &$output)
241 if ($title == 'bookmarklet') {
242 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
244 $c = file_get_contents($filename);
245 $output = common_markup_to_html($c);
246 return false; // success!
253 * Show a link to our delicious import page on profile settings form
255 * @param Action $action Profile settings action being shown
257 * @return boolean hook value
259 function onEndProfileSettingsActions($action)
261 $user = common_current_user();
263 if (!empty($user) && $user->hasRight(self::IMPORTDELICIOUS)) {
264 $action->elementStart('li');
265 $action->element('a',
266 array('href' => common_local_url('importdelicious')),
267 // TRANS: Link text in proile leading to import form.
268 _m('Import del.icio.us bookmarks'));
269 $action->elementEnd('li');
276 * Output our CSS class for bookmark notice list elements
278 * @param NoticeListItem $nli The item being shown
280 * @return boolean hook value
283 function onStartOpenNoticeListItemElement($nli)
285 $nb = Bookmark::getByNotice($nli->notice);
287 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
288 $class = 'hentry notice bookmark';
289 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
290 $class .= ' limited-scope';
292 $nli->out->elementStart('li', array('class' => $class,
293 'id' => 'notice-' . $id));
294 Event::handle('EndOpenNoticeListItemElement', array($nli));
301 * Save a remote bookmark (from Salmon or PuSH)
303 * @param Ostatus_profile $author Author of the bookmark
304 * @param Activity $activity Activity to save
306 * @return Notice resulting notice.
308 static private function _postRemoteBookmark(Ostatus_profile $author,
311 $bookmark = $activity->objects[0];
313 $options = array('uri' => $bookmark->id,
314 'url' => $bookmark->link,
315 'is_local' => Notice::REMOTE_OMB,
316 'source' => 'ostatus');
318 return self::_postBookmark($author->localProfile(), $activity, $options);
322 * Test if an activity represents posting a bookmark
324 * @param Activity $activity Activity to test
326 * @return true if it's a Post of a Bookmark, else false
328 static private function _isPostBookmark($activity)
330 return ($activity->verb == ActivityVerb::POST &&
331 $activity->objects[0]->type == ActivityObject::BOOKMARK);
336 return array(ActivityObject::BOOKMARK);
340 * When a notice is deleted, delete the related Bookmark
342 * @param Notice $notice Notice being deleted
344 * @return boolean hook value
346 function deleteRelated($notice)
348 $nb = Bookmark::getByNotice($notice);
358 * Save a bookmark from an activity
360 * @param Activity $activity Activity to save
361 * @param Profile $profile Profile to use as author
362 * @param array $options Options to pass to bookmark-saving code
364 * @return Notice resulting notice
366 function saveNoticeFromActivity($activity, $profile, $options=array())
368 $bookmark = $activity->objects[0];
370 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
372 if (count($relLinkEls) < 1) {
373 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
374 throw new ClientException(_m('Expected exactly 1 link '.
375 'rel=related in a Bookmark.'));
378 if (count($relLinkEls) > 1) {
379 common_log(LOG_WARNING,
380 "Got too many link rel=related in a Bookmark.");
383 $linkEl = $relLinkEls[0];
385 $url = $linkEl->getAttribute('href');
389 foreach ($activity->categories as $category) {
390 $tags[] = common_canonical_tag($category->term);
393 if (!empty($activity->time)) {
394 $options['created'] = common_sql_date($activity->time);
397 // Fill in location if available
399 $location = $activity->context->location;
402 $options['lat'] = $location->lat;
403 $options['lon'] = $location->lon;
404 if ($location->location_id) {
405 $options['location_ns'] = $location->location_ns;
406 $options['location_id'] = $location->location_id;
410 $replies = $activity->context->attention;
412 $options['groups'] = array();
413 $options['replies'] = array();
415 foreach ($replies as $replyURI) {
416 $other = Profile::fromURI($replyURI);
417 if (!empty($other)) {
418 $options['replies'][] = $replyURI;
420 $group = User_group::staticGet('uri', $replyURI);
421 if (!empty($group)) {
422 $options['groups'][] = $replyURI;
427 // Maintain direct reply associations
428 // @fixme what about conversation ID?
430 if (!empty($activity->context->replyToID)) {
431 $orig = Notice::staticGet('uri',
432 $activity->context->replyToID);
434 $options['reply_to'] = $orig->id;
438 return Bookmark::saveNew($profile,
446 function activityObjectFromNotice($notice)
448 assert($this->isMyNotice($notice));
451 "Formatting notice {$notice->uri} as a bookmark.");
453 $object = new ActivityObject();
454 $nb = Bookmark::getByNotice($notice);
456 $object->id = $notice->uri;
457 $object->type = ActivityObject::BOOKMARK;
458 $object->title = $nb->title;
459 $object->summary = $nb->description;
460 $object->link = $notice->bestUrl();
462 // Attributes of the URL
464 $attachments = $notice->attachments();
466 if (count($attachments) != 1) {
467 // TRANS: Server exception thrown when a bookmark has multiple attachments.
468 throw new ServerException(_m('Bookmark notice with the '.
469 'wrong number of attachments.'));
472 $target = $attachments[0];
474 $attrs = array('rel' => 'related',
475 'href' => $target->url);
477 if (!empty($target->title)) {
478 $attrs['title'] = $target->title;
481 $object->extra[] = array('link', $attrs, null);
483 // Attributes of the thumbnail, if any
485 $thumbnail = $target->getThumbnail();
487 if (!empty($thumbnail)) {
488 $tattrs = array('rel' => 'preview',
489 'href' => $thumbnail->url);
491 if (!empty($thumbnail->width)) {
492 $tattrs['media:width'] = $thumbnail->width;
495 if (!empty($thumbnail->height)) {
496 $tattrs['media:height'] = $thumbnail->height;
499 $object->extra[] = array('link', $attrs, null);
506 * @fixme WARNING WARNING WARNING this opens a 'div' that is apparently closed by MicroAppPlugin
507 * @fixme that's probably wrong?
509 * @param Notice $notice
510 * @param HTMLOutputter $out
512 function showNotice($notice, $out)
514 $nb = Bookmark::getByNotice($notice);
516 $profile = $notice->getProfile();
518 $atts = $notice->attachments();
520 if (count($atts) < 1) {
521 // Something wrong; let default code deal with it.
522 // TRANS: Exception thrown when a bookmark has no attachments.
523 // TRANS: %1$s is a bookmark ID, %2$s is a notice ID (number).
524 throw new Exception(sprintf(_m('Bookmark %1$s (notice %2$d) has no attachments.'),
531 // XXX: only show the bookmark URL for non-single-page stuff
533 if ($out instanceof ShowbookmarkAction) {
535 $out->elementStart('h3');
537 array('href' => $att->url,
538 'class' => 'bookmark-title entry-title'),
540 $out->elementEnd('h3');
542 $countUrl = common_local_url('noticebyurl',
543 array('id' => $att->id));
545 $out->element('a', array('class' => 'bookmark-notice-count',
546 'href' => $countUrl),
547 $att->noticeCount());
550 // Replies look like "for:" tags
552 $replies = $notice->getReplies();
553 $tags = $notice->getTags();
555 if (!empty($replies) || !empty($tags)) {
557 $out->elementStart('ul', array('class' => 'bookmark-tags'));
559 foreach ($replies as $reply) {
560 $other = Profile::staticGet('id', $reply);
561 if (!empty($other)) {
562 $out->elementStart('li');
563 $out->element('a', array('rel' => 'tag',
564 'href' => $other->profileurl,
565 'title' => $other->getBestName()),
566 sprintf('for:%s', $other->nickname));
567 $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 // TRANS: Application title.
643 return _m('TITLE','Bookmark');