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'));
133 function onEndShowScripts($action)
135 $action->script($this->path('js/bookmark.js'));
139 * Load related modules when needed
141 * @param string $cls Name of the class to be loaded
143 * @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 'BookmarkforurlAction':
156 case 'ImportdeliciousAction':
157 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
160 include_once $dir.'/'.$cls.'.php';
162 case 'BookmarkListItem':
164 case 'InitialBookmarkForm':
165 case 'DeliciousBackupImporter':
166 case 'DeliciousBookmarkImporter':
167 include_once $dir.'/'.strtolower($cls).'.php';
175 * Map URLs to actions
177 * @param Net_URL_Mapper $m path-to-action mapper
179 * @return boolean hook value; true means continue processing, false means stop.
181 function onRouterInitialized($m)
183 $m->connect('main/bookmark/new',
184 array('action' => 'newbookmark'),
185 array('id' => '[0-9]+'));
187 $m->connect('main/bookmark/popup',
188 array('action' => 'bookmarkpopup'));
190 $m->connect('main/bookmark/import',
191 array('action' => 'importdelicious'));
193 $m->connect('main/bookmark/forurl',
194 array('action' => 'bookmarkforurl'));
196 $m->connect('bookmark/:id',
197 array('action' => 'showbookmark'),
198 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
200 $m->connect('notice/by-url/:id',
201 array('action' => 'noticebyurl'),
202 array('id' => '[0-9]+'));
209 * Add our two queue handlers to the queue manager
211 * @param QueueManager $qm current queue manager
213 * @return boolean hook value
215 function onEndInitializeQueueManager($qm)
217 $qm->connect('dlcsback', 'DeliciousBackupImporter');
218 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
223 * Plugin version data
225 * @param array &$versions array of version data
229 function onPluginVersion(&$versions)
231 $versions[] = array('name' => 'Bookmark',
232 'version' => self::VERSION,
233 'author' => 'Evan Prodromou',
234 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
236 // TRANS: Plugin description.
237 _m('Simple extension for supporting bookmarks.'));
242 * Load our document if requested
244 * @param string &$title Title to fetch
245 * @param string &$output HTML to output
247 * @return boolean hook value
249 function onStartLoadDoc(&$title, &$output)
251 if ($title == 'bookmarklet') {
252 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
254 $c = file_get_contents($filename);
255 $output = common_markup_to_html($c);
256 return false; // success!
263 * Show a link to our delicious import page on profile settings form
265 * @param Action $action Profile settings action being shown
267 * @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 // TRANS: Link text in proile leading to import form.
278 _m('Import del.icio.us bookmarks'));
279 $action->elementEnd('li');
286 * Output our CSS class for bookmark notice list elements
288 * @param NoticeListItem $nli The item being shown
290 * @return boolean hook value
293 function onStartOpenNoticeListItemElement($nli)
295 $nb = Bookmark::getByNotice($nli->notice);
297 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
298 $class = 'hentry notice bookmark';
299 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
300 $class .= ' limited-scope';
302 $nli->out->elementStart('li', array('class' => $class,
303 'id' => 'notice-' . $id));
304 Event::handle('EndOpenNoticeListItemElement', array($nli));
311 * Save a remote bookmark (from Salmon or PuSH)
313 * @param Ostatus_profile $author Author of the bookmark
314 * @param Activity $activity Activity to save
316 * @return Notice resulting notice.
318 static private function _postRemoteBookmark(Ostatus_profile $author,
321 $bookmark = $activity->objects[0];
323 $options = array('uri' => $bookmark->id,
324 'url' => $bookmark->link,
325 'is_local' => Notice::REMOTE_OMB,
326 'source' => 'ostatus');
328 return self::_postBookmark($author->localProfile(), $activity, $options);
332 * Test if an activity represents posting a bookmark
334 * @param Activity $activity Activity to test
336 * @return true if it's a Post of a Bookmark, else false
338 static private function _isPostBookmark($activity)
340 return ($activity->verb == ActivityVerb::POST &&
341 $activity->objects[0]->type == ActivityObject::BOOKMARK);
346 return array(ActivityObject::BOOKMARK);
350 * When a notice is deleted, delete the related Bookmark
352 * @param Notice $notice Notice being deleted
354 * @return boolean hook value
356 function deleteRelated($notice)
358 $nb = Bookmark::getByNotice($notice);
368 * Save a bookmark from an activity
370 * @param Activity $activity Activity to save
371 * @param Profile $profile Profile to use as author
372 * @param array $options Options to pass to bookmark-saving code
374 * @return Notice resulting notice
376 function saveNoticeFromActivity($activity, $profile, $options=array())
378 $bookmark = $activity->objects[0];
380 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
382 if (count($relLinkEls) < 1) {
383 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
384 throw new ClientException(_m('Expected exactly 1 link '.
385 'rel=related in a Bookmark.'));
388 if (count($relLinkEls) > 1) {
389 common_log(LOG_WARNING,
390 "Got too many link rel=related in a Bookmark.");
393 $linkEl = $relLinkEls[0];
395 $url = $linkEl->getAttribute('href');
399 foreach ($activity->categories as $category) {
400 $tags[] = common_canonical_tag($category->term);
403 if (!empty($activity->time)) {
404 $options['created'] = common_sql_date($activity->time);
407 // Fill in location if available
409 $location = $activity->context->location;
412 $options['lat'] = $location->lat;
413 $options['lon'] = $location->lon;
414 if ($location->location_id) {
415 $options['location_ns'] = $location->location_ns;
416 $options['location_id'] = $location->location_id;
420 $replies = $activity->context->attention;
422 $options['groups'] = array();
423 $options['replies'] = array();
425 foreach ($replies as $replyURI) {
426 $other = Profile::fromURI($replyURI);
427 if (!empty($other)) {
428 $options['replies'][] = $replyURI;
430 $group = User_group::staticGet('uri', $replyURI);
431 if (!empty($group)) {
432 $options['groups'][] = $replyURI;
437 // Maintain direct reply associations
438 // @fixme what about conversation ID?
440 if (!empty($activity->context->replyToID)) {
441 $orig = Notice::staticGet('uri',
442 $activity->context->replyToID);
444 $options['reply_to'] = $orig->id;
448 return Bookmark::saveNew($profile,
456 function activityObjectFromNotice($notice)
458 assert($this->isMyNotice($notice));
461 "Formatting notice {$notice->uri} as a bookmark.");
463 $object = new ActivityObject();
464 $nb = Bookmark::getByNotice($notice);
466 $object->id = $notice->uri;
467 $object->type = ActivityObject::BOOKMARK;
468 $object->title = $nb->title;
469 $object->summary = $nb->description;
470 $object->link = $notice->bestUrl();
472 // Attributes of the URL
474 $attachments = $notice->attachments();
476 if (count($attachments) != 1) {
477 // TRANS: Server exception thrown when a bookmark has multiple attachments.
478 throw new ServerException(_m('Bookmark notice with the '.
479 'wrong number of attachments.'));
482 $target = $attachments[0];
484 $attrs = array('rel' => 'related',
485 'href' => $target->url);
487 if (!empty($target->title)) {
488 $attrs['title'] = $target->title;
491 $object->extra[] = array('link', $attrs, null);
493 // Attributes of the thumbnail, if any
495 $thumbnail = $target->getThumbnail();
497 if (!empty($thumbnail)) {
498 $tattrs = array('rel' => 'preview',
499 'href' => $thumbnail->url);
501 if (!empty($thumbnail->width)) {
502 $tattrs['media:width'] = $thumbnail->width;
505 if (!empty($thumbnail->height)) {
506 $tattrs['media:height'] = $thumbnail->height;
509 $object->extra[] = array('link', $attrs, null);
516 * Given a notice list item, returns an adapter specific
519 * @param NoticeListItem $nli item to adapt
521 * @return NoticeListItemAdapter adapter or null
523 function adaptNoticeListItem($nli)
525 return new BookmarkListItem($nli);
528 function entryForm($out)
530 return new InitialBookmarkForm($out);
540 // TRANS: Application title.
541 return _m('TITLE','Bookmark');