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/
47 class BookmarkPlugin extends Plugin
49 const VERSION = '0.1';
52 * Database schema setup
57 * @return boolean hook value; true means continue processing, false means stop.
60 function onCheckSchema()
62 $schema = Schema::get();
64 // For storing user-submitted flags on profiles
66 $schema->ensureTable('notice_bookmark',
67 array(new ColumnDef('notice_id',
72 new ColumnDef('title',
75 new ColumnDef('description',
82 * When a notice is deleted, delete the related Notice_bookmark
84 * @param Notice $notice Notice being deleted
86 * @return boolean hook value
89 function onNoticeDeleteRelated($notice)
91 $nb = Notice_bookmark::staticGet('notice_id', $notice->id);
101 * Show the CSS necessary for this plugin
103 * @param Action $action the action being run
105 * @return boolean hook value
108 function onEndShowStyles($action)
110 $action->style('.bookmark_tags li { display: inline; }');
111 $action->style('.bookmark_mentions li { display: inline; }');
116 * Load related modules when needed
118 * @param string $cls Name of the class to be loaded
120 * @return boolean hook value; true means continue processing, false means stop.
123 function onAutoload($cls)
125 $dir = dirname(__FILE__);
129 case 'NewbookmarkAction':
130 case 'BookmarkpopupAction':
131 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
133 case 'Notice_bookmark':
134 include_once $dir.'/'.$cls.'.php';
137 case 'DeliciousBackupImporter':
138 case 'DeliciousBookmarkImporter':
139 include_once $dir.'/'.strtolower($cls).'.php';
147 * Map URLs to actions
149 * @param Net_URL_Mapper $m path-to-action mapper
151 * @return boolean hook value; true means continue processing, false means stop.
154 function onRouterInitialized($m)
156 $m->connect('main/bookmark/new',
157 array('action' => 'newbookmark'),
158 array('id' => '[0-9]+'));
160 $m->connect('main/bookmark/popup', array('action' => 'bookmarkpopup'));
166 * Output the HTML for a bookmark in a list
168 * @param NoticeListItem $nli The list item being shown.
170 * @return boolean hook value
173 function onStartShowNoticeItem($nli)
175 $nb = Notice_bookmark::staticGet('notice_id',
179 $att = $nli->notice->attachments();
180 $nli->out->elementStart('h3');
181 $nli->out->element('a',
182 array('href' => $att[0]->url),
184 $nli->out->elementEnd('h3');
185 $nli->out->element('p',
186 array('class' => 'bookmark_description'),
188 $nli->out->elementStart('p');
189 $nli->out->element('a', array('href' => $nli->profile->profileurl,
190 'class' => 'bookmark_author',
191 'title' => $nli->profile->getBestName()),
192 $nli->profile->getBestName());
193 $nli->out->elementEnd('p');
194 $tags = $nli->notice->getTags();
195 $nli->out->elementStart('ul', array('class' => 'bookmark_tags'));
196 foreach ($tags as $tag) {
197 $nli->out->elementStart('li');
198 $nli->out->element('a',
199 array('rel' => 'tag',
200 'href' => Notice_tag::url($tag)),
202 $nli->out->elementEnd('li');
203 $nli->out->text(' ');
205 $nli->out->elementEnd('ul');
206 $replies = $nli->notice->getReplies();
207 if (!empty($replies)) {
208 $nli->out->elementStart('ul', array('class' => 'bookmark_mentions'));
209 foreach ($replies as $reply) {
210 $other = Profile::staticGet('id', $reply);
211 $nli->out->elementStart('li');
212 $nli->out->element('a', array('rel' => 'tag',
213 'href' => $other->profileurl,
214 'title' => $other->getBestName()),
215 sprintf('for:%s', $other->nickname));
216 $nli->out->elementEnd('li');
217 $nli->out->text(' ');
219 $nli->out->elementEnd('ul');
227 * Render a notice as a Bookmark object
229 * @param Notice $notice Notice to render
230 * @param ActivityObject &$object Empty object to fill
232 * @return boolean hook value
235 function onStartActivityObjectFromNotice($notice, &$object)
237 $nb = Notice_bookmark::staticGet('notice_id',
242 $object->id = $notice->uri;
243 $object->type = ActivityObject::BOOKMARK;
244 $object->title = $nb->title;
245 $object->summary = $nb->description;
246 $object->link = $notice->bestUrl();
248 // Attributes of the URL
250 $attachments = $notice->attachments();
252 if (count($attachments) != 1) {
253 throw new ServerException(_('Bookmark notice with the '.
254 'wrong number of attachments.'));
257 $target = $attachments[0];
259 $attrs = array('rel' => 'related',
260 'href' => $target->url);
262 if (!empty($target->title)) {
263 $attrs['title'] = $target->title;
266 $object->extra[] = array('link', $attrs);
268 // Attributes of the thumbnail, if any
270 $thumbnail = $target->getThumbnail();
272 if (!empty($thumbnail)) {
273 $tattrs = array('rel' => 'preview',
274 'href' => $thumbnail->url);
276 if (!empty($thumbnail->width)) {
277 $tattrs['media:width'] = $thumbnail->width;
280 if (!empty($thumbnail->height)) {
281 $tattrs['media:height'] = $thumbnail->height;
284 $object->extra[] = array('link', $attrs);
294 * Add our two queue handlers to the queue manager
296 * @param QueueManager $qm current queue manager
298 * @return boolean hook value
301 function onEndInitializeQueueManager($qm)
303 $qm->connect('dlcsback', 'DeliciousBackupImporter');
304 $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
309 * Plugin version data
311 * @param array &$versions array of version data
316 function onPluginVersion(&$versions)
318 $versions[] = array('name' => 'Sample',
319 'version' => self::VERSION,
320 'author' => 'Evan Prodromou',
321 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
323 _m('Simple extension for supporting bookmarks.'));
328 * Load our document if requested
330 * @param string &$title Title to fetch
331 * @param string &$output HTML to output
333 * @return boolean hook value
336 function onStartLoadDoc(&$title, &$output)
338 if ($title == 'bookmarklet') {
339 $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
341 $c = file_get_contents($filename);
342 $output = common_markup_to_html($c);
343 return false; // success!