]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/BookmarkPlugin.php
3f367703911277f4261ce7ed9b796fdf69c368bd
[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         /**
50          * Database schema setup
51          *
52          * @see Schema
53          * @see ColumnDef
54          *
55          * @return boolean hook value; true means continue processing, false means stop.
56          */
57
58         function onCheckSchema()
59         {
60                 $schema = Schema::get();
61
62                 // For storing user-submitted flags on profiles
63
64                 $schema->ensureTable('notice_bookmark',
65                                                          array(new ColumnDef('notice_id',
66                                                                                                  'integer',
67                                                                                                  null,
68                                                                                                  false,
69                                                                                                  'PRI'),
70                                                                    new ColumnDef('title',
71                                                                                                  'varchar',
72                                                                                                  255),
73                                                                    new ColumnDef('description',
74                                                                                                  'text')));
75
76                 return true;
77         }
78
79         function onNoticeDeleteRelated($notice)
80         {
81                 $nb = Notice_bookmark::staticGet('notice_id', $notice->id);
82
83                 if (!empty($nb)) {
84                         $nb->delete();
85                 }
86
87                 return true;
88         }
89
90         function onEndShowStyles($action)
91         {
92                 $action->style('.bookmark_tags li { display: inline; }');
93                 $action->style('.bookmark_mentions li { display: inline; }');
94                 return true;
95         }
96
97         /**
98          * Load related modules when needed
99          *
100          * @param string $cls Name of the class to be loaded
101          *
102          * @return boolean hook value; true means continue processing, false means stop.
103          */
104
105         function onAutoload($cls)
106         {
107                 $dir = dirname(__FILE__);
108
109                 switch ($cls)
110                 {
111                 case 'NewbookmarkAction':
112                         include_once $dir.'/newbookmark.php';
113                         return false;
114                 case 'Notice_bookmark':
115                         include_once $dir.'/'.$cls.'.php';
116                         return false;
117                 case 'BookmarkForm':
118                         include_once $dir.'/'.strtolower($cls).'.php';
119                         return false;
120                 default:
121                         return true;
122                 }
123         }
124
125         /**
126          * Map URLs to actions
127          *
128          * @param Net_URL_Mapper $m path-to-action mapper
129          *
130          * @return boolean hook value; true means continue processing, false means stop.
131          */
132
133         function onRouterInitialized($m)
134         {
135                 $m->connect('main/bookmark/new',
136                                         array('action' => 'newbookmark'),
137                                         array('id' => '[0-9]+'));
138
139                 return true;
140         }
141
142         function onStartShowNoticeItem($nli)
143         {
144                 $nb = Notice_bookmark::staticGet('notice_id',
145                                                                                  $nli->notice->id);
146
147                 if (!empty($nb)) {
148                         $att = $nli->notice->attachments();
149                         $nli->out->elementStart('h3');
150                         $nli->out->element('a',
151                                                            array('href' => $att[0]->url),
152                                                            $nb->title);
153                         $nli->out->elementEnd('h3');
154                         $nli->out->element('p',
155                                                            array('class' => 'bookmark_description'),
156                                                            $nb->description);
157                         $nli->out->elementStart('p');
158                         $nli->out->element('a', array('href' => $nli->profile->profileurl,
159                                                                                   'class' => 'bookmark_author',
160                                                                                   'title' => $nli->profile->getBestName()),
161                                                            $nli->profile->getBestName());
162                         $nli->out->elementEnd('p');
163                         $tags = $nli->notice->getTags();
164                         $nli->out->elementStart('ul', array('class' => 'bookmark_tags'));
165                         foreach ($tags as $tag) {
166                                 $nli->out->elementStart('li');
167                                 $nli->out->element('a', 
168                                                                    array('rel' => 'tag',
169                                                                                  'href' => Notice_tag::url($tag)),
170                                                                    $tag);
171                                 $nli->out->elementEnd('li');
172                                 $nli->out->text(' ');
173                         }
174                         $nli->out->elementEnd('ul');
175                         $replies = $nli->notice->getReplies();
176                         if (!empty($replies)) {
177                                 $nli->out->elementStart('ul', array('class' => 'bookmark_mentions'));
178                                 foreach ($replies as $reply) {
179                                         $other = Profile::staticGet('id', $reply);
180                                         $nli->out->elementStart('li');
181                                         $nli->out->element('a', array('rel' => 'tag',
182                                                                                                   'href' => $other->profileurl,
183                                                                                                   'title' => $other->getBestName()),
184                                                                            sprintf('for:%s', $other->nickname));
185                                         $nli->out->elementEnd('li');
186                                         $nli->out->text(' ');
187                                 }
188                                 $nli->out->elementEnd('ul');
189                         }
190                         return false;
191                 }
192                 return true;
193         }
194
195         function onStartActivityObjectFromNotice($notice, &$object)
196         {
197                 $nb = Notice_bookmark::staticGet('notice_id',
198                                                                                  $notice->id);
199
200                 if (!empty($nb)) {
201
202                         $object->id      = $notice->uri;
203                         $object->type    = ActivityObject::BOOKMARK;
204                         $object->title   = $nb->title;
205                         $object->summary = $nb->summary;
206
207                         // Attributes of the URL
208
209                         $attachments = $notice->attachments();
210
211                         if (count($attachments) != 1) {
212                                 throw new ServerException(_('Bookmark notice with the wrong number of attachments.'));
213                         }
214
215                         $target = $attachments[0];
216
217                         $attrs = array('rel' => 'related',
218                                                    'href' => $target->url);
219
220                         if (!empty($target->title)) {
221                                 $attrs['title'] = $target->title;
222                         }
223
224                         $object->extra[] = array('link', $attrs);
225                                                                                                    
226                         // Attributes of the thumbnail, if any
227
228                         $thumbnail = $target->getThumbnail();
229
230                         if (!empty($thumbnail)) {
231                                 $tattrs = array('rel' => 'preview',
232                                                                 'href' => $thumbnail->url);
233
234                                 if (!empty($thumbnail->width)) {
235                                         $tattrs['media:width'] = $thumbnail->width;
236                                 }
237
238                                 if (!empty($thumbnail->height)) {
239                                         $tattrs['media:height'] = $thumbnail->height;
240                                 }
241
242                                 $object->extra[] = array('link', $attrs);
243                         }
244
245                         return false;
246                 }
247
248                 return true;
249         }
250
251         function onPluginVersion(&$versions)
252         {
253                 $versions[] = array('name' => 'Sample',
254                                                         'version' => STATUSNET_VERSION,
255                                                         'author' => 'Evan Prodromou',
256                                                         'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
257                                                         'rawdescription' =>
258                                                         _m('Simple extension for supporting bookmarks.'));
259                 return true;
260         }
261 }
262