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 VERSION = '0.1';
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 array('action' => 'bookmarks', 'nickname' => $nickname));
118 $m->connect('bookmarks/rss',
119 array('action' => 'bookmarksrss', 'nickname' => $nickname));
121 $m->connect(':nickname/bookmarks',
122 array('action' => 'bookmarks'),
123 array('nickname' => Nickname::DISPLAY_FMT));
124 $m->connect(':nickname/bookmarks/rss',
125 array('action' => 'bookmarksrss'),
126 array('nickname' => Nickname::DISPLAY_FMT));
129 $m->connect('api/bookmarks/:id.:format',
130 array('action' => 'ApiTimelineBookmarks',
131 'id' => Nickname::INPUT_FMT,
132 'format' => '(xml|json|rss|atom|as)'));
134 $m->connect('main/bookmark/new',
135 array('action' => 'newbookmark'),
136 array('id' => '[0-9]+'));
138 $m->connect('main/bookmark/popup',
139 array('action' => 'bookmarkpopup'));
141 $m->connect('main/bookmark/import',
142 array('action' => 'importdelicious'));
144 $m->connect('main/bookmark/forurl',
145 array('action' => 'bookmarkforurl'));
147 $m->connect('bookmark/:id',
148 array('action' => 'showbookmark'),
149 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
151 $m->connect('notice/by-url/:id',
152 array('action' => 'noticebyurl'),
153 array('id' => '[0-9]+'));
160 * Add our two queue handlers to the queue manager
162 * @param QueueManager $qm current queue manager
164 * @return boolean hook value
166 function onEndInitializeQueueManager($qm)
168 $qm->connect('dlcsback', 'DeliciousBackupImporter');
169 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
174 * Plugin version data
176 * @param array &$versions array of version data
180 function onPluginVersion(array &$versions)
182 $versions[] = array('name' => 'Bookmark',
183 'version' => GNUSOCIAL_VERSION,
184 'author' => 'Evan Prodromou, Stephane Berube, Jean Baptiste Favre, Mikael Nordfeldth',
185 'homepage' => 'https://gnu.io/social',
187 // TRANS: Plugin description.
188 _m('Plugin for posting bookmarks. ') .
189 'BookmarkList feature has been developped by Stephane Berube. ' .
190 'Integration has been done by Jean Baptiste Favre.');
195 * Load our document if requested
197 * @param string &$title Title to fetch
198 * @param string &$output HTML to output
200 * @return boolean hook value
202 function onStartLoadDoc(&$title, &$output)
204 if ($title == 'bookmarklet') {
205 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
207 $c = file_get_contents($filename);
208 $output = common_markup_to_html($c);
209 return false; // success!
216 * Show a link to our delicious import page on profile settings form
218 * @param Action $action Profile settings action being shown
220 * @return boolean hook value
222 function onEndProfileSettingsActions($action)
224 $user = common_current_user();
226 if (!empty($user) && $user->hasRight(self::IMPORTDELICIOUS)) {
227 $action->elementStart('li');
228 $action->element('a',
229 array('href' => common_local_url('importdelicious')),
230 // TRANS: Link text in proile leading to import form.
231 _m('Import del.icio.us bookmarks'));
232 $action->elementEnd('li');
239 * Modify the default menu to link to our custom action
241 * Using event handlers, it's possible to modify the default UI for pages
242 * almost without limit. In this method, we add a menu item to the default
243 * primary menu for the interface to link to our action.
245 * The Action class provides a rich set of events to hook, as well as output
248 * @param Action $action The current action handler. Use this to
251 * @return boolean hook value; true means continue processing, false means stop.
255 function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped=null)
257 $menu->menuItem(common_local_url('bookmarks', array('nickname' => $target->getNickname())),
258 // TRANS: Menu item in sample plugin.
260 // TRANS: Menu item title in sample plugin.
261 _m('A list of your bookmarks'), false, 'nav_timeline_bookmarks');
267 return array(ActivityObject::BOOKMARK);
271 * When a notice is deleted, delete the related Bookmark
273 * @param Notice $notice Notice being deleted
275 * @return boolean hook value
277 function deleteRelated(Notice $notice)
280 $nb = Bookmark::fromStored($notice);
281 } catch (NoResultException $e) {
282 throw new AlreadyFulfilledException('Bookmark already gone when deleting: '.$e->getMessage());
290 * Save a bookmark from an activity
292 * @param Activity $activity Activity to save
293 * @param Profile $actor Profile to use as author
294 * @param array $options Options to pass to bookmark-saving code
296 * @return Notice resulting notice
298 protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
300 return Bookmark::saveActivityObject($activity->objects[0], $stored);
303 public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
305 if (!$this->isMyNotice($stored)) {
309 $this->extendActivity($stored, $act, $scoped);
313 public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
315 /*$hashtags = array();
318 foreach ($tags as $tag) {
319 $hashtags[] = '#'.$tag;
320 $attrs = array('href' => Notice_tag::url($tag),
323 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
327 function activityObjectFromNotice(Notice $notice)
329 return Bookmark::fromStored($notice)->asActivityObject();
332 function entryForm($out)
334 return new InitialBookmarkForm($out);
344 // TRANS: Application title.
345 return _m('TITLE','Bookmark');
348 function onEndUpgrade()
350 printfnq('Making sure Bookmark notices have correct verb and object_type...');
352 // Version 0.9.x of the plugin didn't stamp notices
353 // with verb and object-type (for obvious reasons). Update
354 // those notices here.
356 $notice = new Notice();
358 $notice->joinAdd(array('uri', 'bookmark:uri'));
359 $notice->whereAdd('object_type IS NULL OR object_type = '.$notice->escape(ActivityObject::NOTE));
363 while ($notice->fetch()) {
364 $original = clone($notice);
365 $notice->verb = ActivityVerb::POST;
366 $notice->object_type = ActivityObject::BOOKMARK;
367 $notice->update($original);
373 public function activityObjectOutputJson(ActivityObject $obj, array &$out)
375 assert($obj->type == ActivityObject::BOOKMARK);
377 $bm = Bookmark::getByPK(array('uri' => $obj->id));
379 $out['displayName'] = $bm->getTitle();
380 $out['targetUrl'] = $bm->getUrl();
385 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
387 $nb = Bookmark::fromStored($stored);
389 // Whether to nofollow
390 $attrs = array('href' => $nb->getUrl(), 'class' => 'bookmark-title');
392 $nf = common_config('nofollow', 'external');
394 if ($nf == 'never' || ($nf == 'sometimes' and $out instanceof ShowstreamAction)) {
395 $attrs['rel'] = 'external';
397 $attrs['rel'] = 'nofollow external';
400 $out->elementStart('h3');
401 $out->element('a', $attrs, $nb->getTitle());
402 $out->elementEnd('h3');
404 // Replies look like "for:" tags
405 $replies = $stored->getReplies();
406 $tags = $stored->getTags();
408 if (!empty($nb->description)) {
410 array('class' => 'bookmark-description'),
414 if (!empty($replies) || !empty($tags)) {
416 $out->elementStart('ul', array('class' => 'bookmark-tags'));
418 foreach ($replies as $reply) {
419 $other = Profile::getByPK($reply);
420 $out->elementStart('li');
421 $out->element('a', array('rel' => 'tag',
422 'href' => $other->getUrl(),
423 'title' => $other->getBestName()),
424 sprintf('for:%s', $other->getNickname()));
425 $out->elementEnd('li');
429 foreach ($tags as $tag) {
432 $out->elementStart('li');
434 array('rel' => 'tag',
435 'href' => Notice_tag::url($tag)),
437 $out->elementEnd('li');
442 $out->elementEnd('ul');