]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/BookmarkPlugin.php
9b8addf632c9a0fd3d010b7e2510d4ef60372645
[quix0rs-gnu-social.git] / plugins / Bookmark / BookmarkPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * A plugin to enable social-bookmarking functionality
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  SocialBookmark
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Bookmark plugin main class
37  *
38  * @category  Bookmark
39  * @package   StatusNet
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/
45  */
46
47 class BookmarkPlugin extends Plugin
48 {
49     const VERSION = '0.1';
50
51     /**
52      * Database schema setup
53      *
54      * @see Schema
55      * @see ColumnDef
56      *
57      * @return boolean hook value; true means continue processing, false means stop.
58      */
59
60     function onCheckSchema()
61     {
62         $schema = Schema::get();
63
64         // For storing user-submitted flags on profiles
65
66         $schema->ensureTable('notice_bookmark',
67                              array(new ColumnDef('notice_id',
68                                                  'integer',
69                                                  null,
70                                                  false,
71                                                  'PRI'),
72                                    new ColumnDef('title',
73                                                  'varchar',
74                                                  255),
75                                    new ColumnDef('description',
76                                                  'text')));
77
78         return true;
79     }
80
81     /**
82      * When a notice is deleted, delete the related Notice_bookmark
83      *
84      * @param Notice $notice Notice being deleted
85      * 
86      * @return boolean hook value
87      */
88
89     function onNoticeDeleteRelated($notice)
90     {
91         $nb = Notice_bookmark::staticGet('notice_id', $notice->id);
92
93         if (!empty($nb)) {
94             $nb->delete();
95         }
96
97         return true;
98     }
99
100     /**
101      * Show the CSS necessary for this plugin
102      *
103      * @param Action $action the action being run
104      *
105      * @return boolean hook value
106      */
107
108     function onEndShowStyles($action)
109     {
110         $action->style('.bookmark_tags li { display: inline; }');
111         $action->style('.bookmark_mentions li { display: inline; }');
112         return true;
113     }
114
115     /**
116      * Load related modules when needed
117      *
118      * @param string $cls Name of the class to be loaded
119      *
120      * @return boolean hook value; true means continue processing, false means stop.
121      */
122
123     function onAutoload($cls)
124     {
125         $dir = dirname(__FILE__);
126
127         switch ($cls)
128         {
129         case 'NewbookmarkAction':
130         case 'BookmarkpopupAction':
131             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
132             return false;
133         case 'Notice_bookmark':
134             include_once $dir.'/'.$cls.'.php';
135             return false;
136         case 'BookmarkForm':
137         case 'DeliciousBackupImporter':
138         case 'DeliciousBookmarkImporter':
139             include_once $dir.'/'.strtolower($cls).'.php';
140             return false;
141         default:
142             return true;
143         }
144     }
145
146     /**
147      * Map URLs to actions
148      *
149      * @param Net_URL_Mapper $m path-to-action mapper
150      *
151      * @return boolean hook value; true means continue processing, false means stop.
152      */
153
154     function onRouterInitialized($m)
155     {
156         $m->connect('main/bookmark/new',
157                     array('action' => 'newbookmark'),
158                     array('id' => '[0-9]+'));
159
160         $m->connect('main/bookmark/popup', array('action' => 'bookmarkpopup'));
161
162         return true;
163     }
164
165     /**
166      * Output the HTML for a bookmark in a list
167      *
168      * @param NoticeListItem $nli The list item being shown.
169      *
170      * @return boolean hook value
171      */
172
173     function onStartShowNoticeItem($nli)
174     {
175         $nb = Notice_bookmark::staticGet('notice_id',
176                                          $nli->notice->id);
177
178         if (!empty($nb)) {
179             $att = $nli->notice->attachments();
180             $nli->out->elementStart('h3');
181             $nli->out->element('a',
182                                array('href' => $att[0]->url),
183                                $nb->title);
184             $nli->out->elementEnd('h3');
185             $nli->out->element('p',
186                                array('class' => 'bookmark_description'),
187                                $nb->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)),
201                                    $tag);
202                 $nli->out->elementEnd('li');
203                 $nli->out->text(' ');
204             }
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(' ');
218                 }
219                 $nli->out->elementEnd('ul');
220             }
221             return false;
222         }
223         return true;
224     }
225
226     /**
227      * Render a notice as a Bookmark object
228      *
229      * @param Notice         $notice  Notice to render
230      * @param ActivityObject &$object Empty object to fill
231      *
232      * @return boolean hook value
233      */
234      
235     function onStartActivityObjectFromNotice($notice, &$object)
236     {
237         $nb = Notice_bookmark::staticGet('notice_id',
238                                          $notice->id);
239                                          
240         if (!empty($nb)) {
241
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();
247
248             // Attributes of the URL
249
250             $attachments = $notice->attachments();
251
252             if (count($attachments) != 1) {
253                 throw new ServerException(_('Bookmark notice with the '.
254                                             'wrong number of attachments.'));
255             }
256
257             $target = $attachments[0];
258
259             $attrs = array('rel' => 'related',
260                            'href' => $target->url);
261
262             if (!empty($target->title)) {
263                 $attrs['title'] = $target->title;
264             }
265
266             $object->extra[] = array('link', $attrs);
267                                                    
268             // Attributes of the thumbnail, if any
269
270             $thumbnail = $target->getThumbnail();
271
272             if (!empty($thumbnail)) {
273                 $tattrs = array('rel' => 'preview',
274                                 'href' => $thumbnail->url);
275
276                 if (!empty($thumbnail->width)) {
277                     $tattrs['media:width'] = $thumbnail->width;
278                 }
279
280                 if (!empty($thumbnail->height)) {
281                     $tattrs['media:height'] = $thumbnail->height;
282                 }
283
284                 $object->extra[] = array('link', $attrs);
285             }
286
287             return false;
288         }
289
290         return true;
291     }
292
293     /**
294      * Add our two queue handlers to the queue manager
295      *
296      * @param QueueManager $qm current queue manager
297      * 
298      * @return boolean hook value
299      */
300
301     function onEndInitializeQueueManager($qm)
302     {
303         $qm->connect('dlcsback', 'DeliciousBackupImporter');
304         $qm->connect('dlcsbkmk', 'DeliciousBookmarkImporter');
305         return true;
306     }
307
308     /**
309      * Plugin version data
310      *
311      * @param array &$versions array of version data
312      * 
313      * @return value
314      */
315
316     function onPluginVersion(&$versions)
317     {
318         $versions[] = array('name' => 'Sample',
319                             'version' => self::VERSION,
320                             'author' => 'Evan Prodromou',
321                             'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
322                             'rawdescription' =>
323                             _m('Simple extension for supporting bookmarks.'));
324         return true;
325     }
326
327     /**
328      * Load our document if requested
329      *
330      * @param string &$title  Title to fetch
331      * @param string &$output HTML to output
332      *
333      * @return boolean hook value
334      */
335
336     function onStartLoadDoc(&$title, &$output)
337     {
338         if ($title == 'bookmarklet') {
339             $filename = INSTALLDIR.'/plugins/Bookmark/bookmarklet';
340
341             $c      = file_get_contents($filename);
342             $output = common_markup_to_html($c);
343             return false; // success!
344         }
345
346         return true;
347     }
348 }
349