]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/classes/Bookmark.php
87e81240d07b0bd9280e4401a1e82a7b3b1c2618
[quix0rs-gnu-social.git] / plugins / Bookmark / classes / Bookmark.php
1 <?php
2 /**
3  * Data class to mark notices as bookmarks
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2009, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('GNUSOCIAL')) { exit(1); }
31
32 /**
33  * For storing the fact that a notice is a bookmark
34  *
35  * @category Bookmark
36  * @package  StatusNet
37  * @author   Evan Prodromou <evan@status.net>
38  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
39  * @link     http://status.net/
40  *
41  * @see      DB_DataObject
42  */
43 class Bookmark extends Managed_DataObject
44 {
45     public $__table = 'bookmark'; // table name
46     public $id;          // char(36) primary_key not_null
47     public $profile_id;  // int(4) not_null
48     public $url;         // varchar(191) not_null   not 255 because utf8mb4 takes more space
49     public $title;       // varchar(191)   not 255 because utf8mb4 takes more space
50     public $uri;         // varchar(191)   not 255 because utf8mb4 takes more space
51     public $description; // text
52     public $created;     // datetime
53
54     public static function schemaDef()
55     {
56         return array(
57             'fields' => array(
58                 'id' => array('type' => 'char',
59                             'length' => 36,
60                             'not null' => true),
61                 'profile_id' => array('type' => 'int', 'not null' => true),
62                 'uri' => array('type' => 'varchar',
63                             'length' => 191,
64                             'not null' => true),
65                 'url' => array('type' => 'varchar',
66                             'length' => 191,
67                             'not null' => true),
68                 'title' => array('type' => 'varchar', 'length' => 191),
69                 'description' => array('type' => 'text'),
70                 'created' => array('type' => 'datetime', 'not null' => true),
71             ),
72             'primary key' => array('uri'),
73             'unique keys' => array(
74                 'bookmark_id_key' => array('id'),
75             ),
76             'foreign keys' => array(
77                 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
78                 'bookmark_uri_fkey' => array('notice', array('uri' => 'uri')),
79             ),
80             'indexes' => array('bookmark_created_idx' => array('created'),
81                             'bookmark_url_idx' => array('url'),
82                             'bookmark_profile_id_idx' => array('profile_id'),
83             ),
84         );
85     }
86
87     /**
88      * Get a bookmark based on a notice
89      *
90      * @param   Notice              $stored Notice activity which represents the Bookmark
91      *
92      * @return  Bookmark            The found bookmark object.
93      * @throws  NoResultException   When you don't find it after all.
94      */
95     static public function fromStored(Notice $stored)
96     {
97         return self::getByPK(array('uri' => $stored->getUri()));
98     }
99
100     public function getDescription()
101     {
102         return $this->description;
103     }
104
105     public function getTitle()
106     {
107         return $this->title;
108     }
109
110     public function getUrl()
111     {
112         if (empty($this->url)) {
113             throw new InvalidUrlException($this->url);
114         }
115         return $this->url;
116     }
117
118     /**
119      * Get the bookmark that a user made for an URL
120      *
121      * @param Profile $profile Profile to check for
122      * @param string  $url     URL to check for
123      *
124      * @return Bookmark bookmark found or null
125      */
126     static function getByURL(Profile $profile, $url)
127     {
128         $nb = new Bookmark();
129
130         $nb->profile_id = $profile->getID();
131         $nb->url        = $url;
132
133         if (!$nb->find(true)) {
134             throw new NoResultException($nb);
135         }
136
137         return $nb;
138     }
139
140     /**
141      * Store a Bookmark object
142      *
143      * @param Profile $profile     To save the bookmark for
144      * @param string  $title       Title of the bookmark
145      * @param string  $url         URL of the bookmark
146      * @param string  $description Description of the bookmark
147      *
148      * @return Bookmark the Bookmark object
149      */
150     static function addNew(Notice $stored, $title, $url, $description)
151     {
152         if ($title === '' or is_null($title)) {
153             throw new ClientException(_m('You must provide a non-empty title.'));
154         }
155         if (!common_valid_http_url($url)) {
156             throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
157         }
158
159         try {
160             $object = self::getByURL($stored->getProfile(), $url);
161             throw new ClientException(_m('You have already bookmarked this URL.'));
162         } catch (NoResultException $e) {
163             // Alright, so then we have to create it.
164         }
165
166         $nb = new Bookmark();
167
168         $nb->id          = UUID::gen();
169         $nb->uri         = $stored->uri;
170         $nb->profile_id  = $stored->getProfile()->getID();
171         $nb->title       = $title;
172         $nb->url         = $url;
173         $nb->description = $description;
174         $nb->created     = $stored->created;
175
176         $result = $nb->insert();
177         if ($result === false) {
178             throw new ServerException('Could not insert Bookmark into database!');
179         }
180
181         /*$hashtags = array();
182         $taglinks = array();
183
184         foreach ($tags as $tag) {
185             $hashtags[] = '#'.$tag;
186             $attrs      = array('href' => Notice_tag::url($tag),
187                                 'rel'  => $tag,
188                                 'class' => 'tag');
189             $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
190         }*/
191
192         return $nb;
193     }
194
195     /**
196      * Save a new notice bookmark
197      *
198      * @param Profile $profile     To save the bookmark for
199      * @param string  $title       Title of the bookmark
200      * @param string  $url         URL of the bookmark
201      * @param array   $rawtags     array of tags
202      * @param string  $description Description of the bookmark
203      * @param array   $options     Options for the Notice::saveNew()
204      *
205      * @return Notice saved notice
206      */
207     static function saveNew(Profile $profile, $title, $url, $rawtags, $description,
208                             array $options=array())
209     {
210         if (!common_valid_http_url($url)) {
211             throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
212         }
213
214         try {
215             $object = self::getByURL($profile, $url);
216             return $object;
217         } catch (NoResultException $e) {
218             // Alright, so then we have to create it.
219         }
220
221         if (array_key_exists('uri', $options)) {
222             $other = Bookmark::getKV('uri', $options['uri']);
223             if (!empty($other)) {
224                 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
225                 throw new ClientException(_m('Bookmark already exists.'));
226             }
227         }
228
229         $nb = new Bookmark();
230
231         $nb->id          = UUID::gen();
232         $nb->profile_id  = $profile->id;
233         $nb->url         = $url;
234         $nb->title       = $title;
235         $nb->description = $description;
236
237         if (array_key_exists('created', $options)) {
238             $nb->created = $options['created'];
239         } else {
240             $nb->created = common_sql_now();
241         }
242
243         if (array_key_exists('uri', $options)) {
244             $nb->uri = $options['uri'];
245         } else {
246             // FIXME: hacks to work around router bugs in
247             // queue daemons
248
249             $r = Router::get();
250
251             $path = $r->build('showbookmark',
252                               array('id' => $nb->id));
253
254             if (empty($path)) {
255                 $nb->uri = common_path('bookmark/'.$nb->id, false, false);
256             } else {
257                 $nb->uri = common_local_url('showbookmark',
258                                             array('id' => $nb->id),
259                                             null,
260                                             null,
261                                             false);
262             }
263         }
264
265         $nb->insert();
266
267         $tags    = array();
268         $replies = array();
269
270         // filter "for:nickname" tags
271
272         foreach ($rawtags as $tag) {
273             if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
274                 // skip if done by caller
275                 if (!array_key_exists('replies', $options)) {
276                     $nickname = mb_substr($tag, 4);
277                     $other    = common_relative_profile($profile,
278                                                         $nickname);
279                     if (!empty($other)) {
280                         $replies[] = $other->getUri();
281                     }
282                 }
283             } else {
284                 $tags[] = common_canonical_tag($tag);
285             }
286         }
287
288         $hashtags = array();
289         $taglinks = array();
290
291         foreach ($tags as $tag) {
292             $hashtags[] = '#'.$tag;
293             $attrs      = array('href' => Notice_tag::url($tag),
294                                 'rel'  => $tag,
295                                 'class' => 'tag');
296             $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
297         }
298
299         // Use user's preferences for short URLs, if possible
300
301         try {
302             $user = User::getKV('id', $profile->id);
303
304             $shortUrl = File_redirection::makeShort($url,
305                                                     empty($user) ? null : $user);
306         } catch (Exception $e) {
307             // Don't let this stop us.
308             $shortUrl = $url;
309         }
310
311         // TRANS: Bookmark content.
312         // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
313         // TRANS: %4$s is space separated list of hash tags.
314         $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'),
315                            $title,
316                            $shortUrl,
317                            $description,
318                            implode(' ', $hashtags));
319
320         // TRANS: Rendered bookmark content.
321         // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
322         // TRANS: %4$s is space separated list of hash tags.
323         $rendered = sprintf(_m('<span class="xfolkentry">'.
324                               '<a class="taggedlink" href="%1$s">%2$s</a> '.
325                               '<span class="description">%3$s</span> '.
326                               '<span class="meta">%4$s</span>'.
327                               '</span>'),
328                             htmlspecialchars($url),
329                             htmlspecialchars($title),
330                             htmlspecialchars($description),
331                             implode(' ', $taglinks));
332
333         $options = array_merge(array('urls' => array($url),
334                                      'rendered' => $rendered,
335                                      'tags' => $tags,
336                                      'replies' => $replies,
337                                      'object_type' => ActivityObject::BOOKMARK),
338                                $options);
339
340         $options['uri'] = $nb->uri;
341
342         try {
343             $saved = Notice::saveNew($profile->id,
344                                      $content,
345                                      array_key_exists('source', $options) ?
346                                      $options['source'] : 'web',
347                                      $options);
348         } catch (Exception $e) {
349             $nb->delete();
350             throw $e;
351         }
352
353         if (empty($saved)) {
354             $nb->delete();
355         }
356
357         return $saved;
358     }
359 }