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('GNUSOCIAL')) { exit(1); }
34 * Bookmark plugin main class
38 * @author Brion Vibber <brionv@status.net>
39 * @author Evan Prodromou <evan@status.net>
40 * @copyright 2010 StatusNet, Inc.
41 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
42 * @link http://status.net/
44 class BookmarkPlugin extends MicroAppPlugin
46 const PLUGIN_VERSION = '0.1.0';
47 const IMPORTDELICIOUS = 'BookmarkPlugin:IMPORTDELICIOUS';
50 * Authorization for importing delicious bookmarks
52 * By default, everyone can import bookmarks except silenced people.
54 * @param Profile $profile Person whose rights to check
55 * @param string $right Right to check; const value
56 * @param boolean &$result Result of the check, writeable
58 * @return boolean hook value
60 function onUserRightsCheck($profile, $right, &$result)
62 if ($right == self::IMPORTDELICIOUS) {
63 $result = !$profile->isSilenced();
70 * Database schema setup
75 * @return boolean hook value; true means continue processing, false means stop.
77 function onCheckSchema()
79 $schema = Schema::get();
81 $schema->ensureTable('bookmark', Bookmark::schemaDef());
87 * Show the CSS necessary for this plugin
89 * @param Action $action the action being run
91 * @return boolean hook value
93 function onEndShowStyles($action)
95 $action->cssLink($this->path('css/bookmark.css'));
99 function onEndShowScripts($action)
101 $action->script($this->path('js/bookmark.js'));
106 * Map URLs to actions
108 * @param URLMapper $m path-to-action mapper
110 * @return boolean hook value; true means continue processing, false means stop.
112 public function onRouterInitialized(URLMapper $m)
114 if (common_config('singleuser', 'enabled')) {
115 $nickname = User::singleUserNickname();
116 $m->connect('bookmarks',
117 ['action' => 'bookmarks',
118 'nickname' => $nickname]);
119 $m->connect('bookmarks/rss',
120 ['action' => 'bookmarksrss',
121 'nickname' => $nickname]);
123 $m->connect(':nickname/bookmarks',
124 ['action' => 'bookmarks'],
125 ['nickname' => Nickname::DISPLAY_FMT]);
126 $m->connect(':nickname/bookmarks/rss',
127 ['action' => 'bookmarksrss'],
128 ['nickname' => Nickname::DISPLAY_FMT]);
131 $m->connect('api/bookmarks/:id.:format',
132 ['action' => 'ApiTimelineBookmarks'],
133 ['id' => Nickname::INPUT_FMT,
134 'format' => '(xml|json|rss|atom|as)']);
136 $m->connect('main/bookmark/new',
137 ['action' => 'newbookmark']);
139 $m->connect('main/bookmark/popup',
140 ['action' => 'bookmarkpopup']);
142 $m->connect('main/bookmark/import',
143 ['action' => 'importdelicious']);
145 $m->connect('main/bookmark/forurl',
146 ['action' => 'bookmarkforurl']);
148 $m->connect('bookmark/:id',
149 ['action' => 'showbookmark'],
150 ['id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}']);
152 $m->connect('notice/by-url/:id',
153 ['action' => 'noticebyurl'],
161 * Add our two queue handlers to the queue manager
163 * @param QueueManager $qm current queue manager
165 * @return boolean hook value
167 function onEndInitializeQueueManager($qm)
169 $qm->connect('dlcsback', 'DeliciousBackupImporter');
170 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
175 * Plugin version data
177 * @param array &$versions array of version data
181 function onPluginVersion(array &$versions)
183 $versions[] = array('name' => 'Bookmark',
184 'version' => self::PLUGIN_VERSION,
185 'author' => 'Evan Prodromou, Stephane Berube, Jean Baptiste Favre, Mikael Nordfeldth',
186 'homepage' => 'https://gnu.io/social',
188 // TRANS: Plugin description.
189 _m('Plugin for posting bookmarks. ') .
190 'BookmarkList feature has been developped by Stephane Berube. ' .
191 'Integration has been done by Jean Baptiste Favre.');
196 * Load our document if requested
198 * @param string &$title Title to fetch
199 * @param string &$output HTML to output
201 * @return boolean hook value
203 function onStartLoadDoc(&$title, &$output)
205 if ($title == 'bookmarklet') {
206 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
208 $c = file_get_contents($filename);
209 $output = common_markup_to_html($c);
210 return false; // success!
217 * Show a link to our delicious import page on profile settings form
219 * @param Action $action Profile settings action being shown
221 * @return boolean hook value
223 function onEndProfileSettingsActions($action)
225 $user = common_current_user();
227 if (!empty($user) && $user->hasRight(self::IMPORTDELICIOUS)) {
228 $action->elementStart('li');
229 $action->element('a',
230 array('href' => common_local_url('importdelicious')),
231 // TRANS: Link text in proile leading to import form.
232 _m('Import del.icio.us bookmarks'));
233 $action->elementEnd('li');
240 * Modify the default menu to link to our custom action
242 * Using event handlers, it's possible to modify the default UI for pages
243 * almost without limit. In this method, we add a menu item to the default
244 * primary menu for the interface to link to our action.
246 * The Action class provides a rich set of events to hook, as well as output
249 * @param Action $action The current action handler. Use this to
252 * @return boolean hook value; true means continue processing, false means stop.
256 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
258 $menu->menuItem(common_local_url('bookmarks', array('nickname' => $target->getNickname())),
259 // TRANS: Menu item in sample plugin.
261 // TRANS: Menu item title in sample plugin.
262 _m('A list of your bookmarks'), false, 'nav_timeline_bookmarks');
268 return array(ActivityObject::BOOKMARK);
272 * When a notice is deleted, delete the related Bookmark
274 * @param Notice $notice Notice being deleted
276 * @return boolean hook value
278 function deleteRelated(Notice $notice)
281 $nb = Bookmark::fromStored($notice);
282 } catch (NoResultException $e) {
283 throw new AlreadyFulfilledException('Bookmark already gone when deleting: '.$e->getMessage());
291 * Save a bookmark from an activity
293 * @param Activity $activity Activity to save
294 * @param Profile $actor Profile to use as author
295 * @param array $options Options to pass to bookmark-saving code
297 * @return Notice resulting notice
299 protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
301 return Bookmark::saveActivityObject($activity->objects[0], $stored);
304 public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
306 if (!$this->isMyNotice($stored)) {
310 $this->extendActivity($stored, $act, $scoped);
314 public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
316 /*$hashtags = array();
319 foreach ($tags as $tag) {
320 $hashtags[] = '#'.$tag;
321 $attrs = array('href' => Notice_tag::url($tag),
324 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
328 function activityObjectFromNotice(Notice $notice)
330 return Bookmark::fromStored($notice)->asActivityObject();
333 function entryForm($out)
335 return new InitialBookmarkForm($out);
345 // TRANS: Application title.
346 return _m('TITLE','Bookmark');
349 function onEndUpgrade()
351 printfnq('Making sure Bookmark notices have correct verb and object_type...');
353 // Version 0.9.x of the plugin didn't stamp notices
354 // with verb and object-type (for obvious reasons). Update
355 // those notices here.
357 $notice = new Notice();
359 $notice->joinAdd(array('uri', 'bookmark:uri'));
360 $notice->whereAdd('object_type IS NULL OR object_type = '.$notice->_quote(ActivityObject::NOTE));
364 while ($notice->fetch()) {
365 $original = clone($notice);
366 $notice->verb = ActivityVerb::POST;
367 $notice->object_type = ActivityObject::BOOKMARK;
368 $notice->update($original);
374 public function activityObjectOutputJson(ActivityObject $obj, array &$out)
376 assert($obj->type == ActivityObject::BOOKMARK);
378 $bm = Bookmark::getByPK(array('uri' => $obj->id));
380 $out['displayName'] = $bm->getTitle();
381 $out['targetUrl'] = $bm->getUrl();
386 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
388 $nb = Bookmark::fromStored($stored);
390 // Whether to nofollow
391 $attrs = array('href' => $nb->getUrl(), 'class' => 'bookmark-title');
393 $nf = common_config('nofollow', 'external');
395 if ($nf == 'never' || ($nf == 'sometimes' and $out instanceof ShowstreamAction)) {
396 $attrs['rel'] = 'external';
398 $attrs['rel'] = 'nofollow external';
401 $out->elementStart('h3');
402 $out->element('a', $attrs, $nb->getTitle());
403 $out->elementEnd('h3');
405 // Replies look like "for:" tags
406 $replies = $stored->getReplies();
407 $tags = $stored->getTags();
409 if (!empty($nb->description)) {
411 array('class' => 'bookmark-description'),
415 if (!empty($replies) || !empty($tags)) {
417 $out->elementStart('ul', array('class' => 'bookmark-tags'));
419 foreach ($replies as $reply) {
420 $other = Profile::getByPK($reply);
421 $out->elementStart('li');
422 $out->element('a', array('rel' => 'tag',
423 'href' => $other->getUrl(),
424 'title' => $other->getBestName()),
425 sprintf('for:%s', $other->getNickname()));
426 $out->elementEnd('li');
430 foreach ($tags as $tag) {
433 $out->elementStart('li');
435 array('rel' => 'tag',
436 'href' => Notice_tag::url($tag)),
438 $out->elementEnd('li');
443 $out->elementEnd('ul');