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')) {
35 require_once INSTALLDIR.'/plugins/Bookmark/bookmarknoticelistitemadapter.php';
38 * Bookmark plugin main class
42 * @author Brion Vibber <brionv@status.net>
43 * @author Evan Prodromou <evan@status.net>
44 * @copyright 2010 StatusNet, Inc.
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
46 * @link http://status.net/
48 class BookmarkPlugin extends MicroAppPlugin
50 const VERSION = '0.1';
51 const IMPORTDELICIOUS = 'BookmarkPlugin:IMPORTDELICIOUS';
54 * Authorization for importing delicious bookmarks
56 * By default, everyone can import bookmarks except silenced people.
58 * @param Profile $profile Person whose rights to check
59 * @param string $right Right to check; const value
60 * @param boolean &$result Result of the check, writeable
62 * @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.
81 function onCheckSchema()
83 $schema = Schema::get();
85 // For storing user-submitted flags on profiles
87 $schema->ensureTable('bookmark',
88 array(new ColumnDef('id',
93 new ColumnDef('profile_id',
103 new ColumnDef('title',
106 new ColumnDef('description',
113 new ColumnDef('created',
123 * Show the CSS necessary for this plugin
125 * @param Action $action the action being run
127 * @return boolean hook value
129 function onEndShowStyles($action)
131 $action->cssLink($this->path('bookmark.css'));
135 function onEndShowScripts($action)
137 $action->script($this->path('js/bookmark.js'));
141 * Load related modules when needed
143 * @param string $cls Name of the class to be loaded
145 * @return boolean hook value; true means continue processing, false means stop.
147 function onAutoload($cls)
149 $dir = dirname(__FILE__);
153 case 'ShowbookmarkAction':
154 case 'NewbookmarkAction':
155 case 'BookmarkpopupAction':
156 case 'NoticebyurlAction':
157 case 'BookmarkforurlAction':
158 case 'ImportdeliciousAction':
159 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
162 include_once $dir.'/'.$cls.'.php';
165 case 'InitialBookmarkForm':
166 case 'DeliciousBackupImporter':
167 case 'DeliciousBookmarkImporter':
168 case 'BookmarkNoticeListItemAdapter':
169 include_once $dir.'/'.strtolower($cls).'.php';
177 * Map URLs to actions
179 * @param Net_URL_Mapper $m path-to-action mapper
181 * @return boolean hook value; true means continue processing, false means stop.
183 function onRouterInitialized($m)
185 $m->connect('main/bookmark/new',
186 array('action' => 'newbookmark'),
187 array('id' => '[0-9]+'));
189 $m->connect('main/bookmark/popup',
190 array('action' => 'bookmarkpopup'));
192 $m->connect('main/bookmark/import',
193 array('action' => 'importdelicious'));
195 $m->connect('main/bookmark/forurl',
196 array('action' => 'bookmarkforurl'));
198 $m->connect('bookmark/:id',
199 array('action' => 'showbookmark'),
200 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
202 $m->connect('notice/by-url/:id',
203 array('action' => 'noticebyurl'),
204 array('id' => '[0-9]+'));
211 * Add our two queue handlers to the queue manager
213 * @param QueueManager $qm current queue manager
215 * @return boolean hook value
217 function onEndInitializeQueueManager($qm)
219 $qm->connect('dlcsback', 'DeliciousBackupImporter');
220 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
225 * Plugin version data
227 * @param array &$versions array of version data
231 function onPluginVersion(&$versions)
233 $versions[] = array('name' => 'Bookmark',
234 'version' => self::VERSION,
235 'author' => 'Evan Prodromou',
236 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
238 // TRANS: Plugin description.
239 _m('Simple extension for supporting bookmarks.'));
244 * Load our document if requested
246 * @param string &$title Title to fetch
247 * @param string &$output HTML to output
249 * @return boolean hook value
251 function onStartLoadDoc(&$title, &$output)
253 if ($title == 'bookmarklet') {
254 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
256 $c = file_get_contents($filename);
257 $output = common_markup_to_html($c);
258 return false; // success!
265 * Show a link to our delicious import page on profile settings form
267 * @param Action $action Profile settings action being shown
269 * @return boolean hook value
271 function onEndProfileSettingsActions($action)
273 $user = common_current_user();
275 if (!empty($user) && $user->hasRight(self::IMPORTDELICIOUS)) {
276 $action->elementStart('li');
277 $action->element('a',
278 array('href' => common_local_url('importdelicious')),
279 // TRANS: Link text in proile leading to import form.
280 _m('Import del.icio.us bookmarks'));
281 $action->elementEnd('li');
288 * Output our CSS class for bookmark notice list elements
290 * @param NoticeListItem $nli The item being shown
292 * @return boolean hook value
295 function onStartOpenNoticeListItemElement($nli)
297 $nb = Bookmark::getByNotice($nli->notice);
299 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
300 $class = 'hentry notice bookmark';
301 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
302 $class .= ' limited-scope';
304 $nli->out->elementStart('li', array('class' => $class,
305 'id' => 'notice-' . $id));
306 Event::handle('EndOpenNoticeListItemElement', array($nli));
313 * Save a remote bookmark (from Salmon or PuSH)
315 * @param Ostatus_profile $author Author of the bookmark
316 * @param Activity $activity Activity to save
318 * @return Notice resulting notice.
320 static private function _postRemoteBookmark(Ostatus_profile $author,
323 $bookmark = $activity->objects[0];
325 $options = array('uri' => $bookmark->id,
326 'url' => $bookmark->link,
327 'is_local' => Notice::REMOTE_OMB,
328 'source' => 'ostatus');
330 return self::_postBookmark($author->localProfile(), $activity, $options);
334 * Test if an activity represents posting a bookmark
336 * @param Activity $activity Activity to test
338 * @return true if it's a Post of a Bookmark, else false
340 static private function _isPostBookmark($activity)
342 return ($activity->verb == ActivityVerb::POST &&
343 $activity->objects[0]->type == ActivityObject::BOOKMARK);
348 return array(ActivityObject::BOOKMARK);
352 * When a notice is deleted, delete the related Bookmark
354 * @param Notice $notice Notice being deleted
356 * @return boolean hook value
358 function deleteRelated($notice)
360 $nb = Bookmark::getByNotice($notice);
370 * Save a bookmark from an activity
372 * @param Activity $activity Activity to save
373 * @param Profile $profile Profile to use as author
374 * @param array $options Options to pass to bookmark-saving code
376 * @return Notice resulting notice
378 function saveNoticeFromActivity($activity, $profile, $options=array())
380 $bookmark = $activity->objects[0];
382 $relLinkEls = ActivityUtils::getLinks($bookmark->element, 'related');
384 if (count($relLinkEls) < 1) {
385 // TRANS: Client exception thrown when a bookmark is formatted incorrectly.
386 throw new ClientException(_m('Expected exactly 1 link '.
387 'rel=related in a Bookmark.'));
390 if (count($relLinkEls) > 1) {
391 common_log(LOG_WARNING,
392 "Got too many link rel=related in a Bookmark.");
395 $linkEl = $relLinkEls[0];
397 $url = $linkEl->getAttribute('href');
401 foreach ($activity->categories as $category) {
402 $tags[] = common_canonical_tag($category->term);
405 if (!empty($activity->time)) {
406 $options['created'] = common_sql_date($activity->time);
409 // Fill in location if available
411 $location = $activity->context->location;
414 $options['lat'] = $location->lat;
415 $options['lon'] = $location->lon;
416 if ($location->location_id) {
417 $options['location_ns'] = $location->location_ns;
418 $options['location_id'] = $location->location_id;
422 $replies = $activity->context->attention;
424 $options['groups'] = array();
425 $options['replies'] = array();
427 foreach ($replies as $replyURI) {
428 $other = Profile::fromURI($replyURI);
429 if (!empty($other)) {
430 $options['replies'][] = $replyURI;
432 $group = User_group::staticGet('uri', $replyURI);
433 if (!empty($group)) {
434 $options['groups'][] = $replyURI;
439 // Maintain direct reply associations
440 // @fixme what about conversation ID?
442 if (!empty($activity->context->replyToID)) {
443 $orig = Notice::staticGet('uri',
444 $activity->context->replyToID);
446 $options['reply_to'] = $orig->id;
450 return Bookmark::saveNew($profile,
458 function activityObjectFromNotice($notice)
460 assert($this->isMyNotice($notice));
463 "Formatting notice {$notice->uri} as a bookmark.");
465 $object = new ActivityObject();
466 $nb = Bookmark::getByNotice($notice);
468 $object->id = $notice->uri;
469 $object->type = ActivityObject::BOOKMARK;
470 $object->title = $nb->title;
471 $object->summary = $nb->description;
472 $object->link = $notice->bestUrl();
474 // Attributes of the URL
476 $attachments = $notice->attachments();
478 if (count($attachments) != 1) {
479 // TRANS: Server exception thrown when a bookmark has multiple attachments.
480 throw new ServerException(_m('Bookmark notice with the '.
481 'wrong number of attachments.'));
484 $target = $attachments[0];
486 $attrs = array('rel' => 'related',
487 'href' => $target->url);
489 if (!empty($target->title)) {
490 $attrs['title'] = $target->title;
493 $object->extra[] = array('link', $attrs, null);
495 // Attributes of the thumbnail, if any
497 $thumbnail = $target->getThumbnail();
499 if (!empty($thumbnail)) {
500 $tattrs = array('rel' => 'preview',
501 'href' => $thumbnail->url);
503 if (!empty($thumbnail->width)) {
504 $tattrs['media:width'] = $thumbnail->width;
507 if (!empty($thumbnail->height)) {
508 $tattrs['media:height'] = $thumbnail->height;
511 $object->extra[] = array('link', $attrs, null);
518 * Given a notice list item, returns an adapter specific
521 * @param NoticeListItem $nli item to adapt
523 * @return NoticeListItemAdapter adapter or null
525 function adaptNoticeListItem($nli)
527 return new BookmarkNoticeListItemAdapter($nli);
530 function entryForm($out)
532 return new InitialBookmarkForm($out);
542 // TRANS: Application title.
543 return _m('TITLE','Bookmark');