]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/classes/Bookmark.php
95518631a3468cfb7536f646ec17d4cf44675335
[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     /**
101      * Get the bookmark that a user made for an URL
102      *
103      * @param Profile $profile Profile to check for
104      * @param string  $url     URL to check for
105      *
106      * @return Bookmark bookmark found or null
107      */
108     static function getByURL(Profile $profile, $url)
109     {
110         $nb = new Bookmark();
111
112         $nb->profile_id = $profile->getID();
113         $nb->url        = $url;
114
115         if (!$nb->find(true)) {
116             throw new NoResultException($nb);
117         }
118
119         return $nb;
120     }
121
122     /**
123      * Store a Bookmark object
124      *
125      * @param Profile $profile     To save the bookmark for
126      * @param string  $title       Title of the bookmark
127      * @param string  $url         URL of the bookmark
128      * @param string  $description Description of the bookmark
129      *
130      * @return Bookmark the Bookmark object
131      */
132     static function addNew(Notice $stored, $title, $url, $description)
133     {
134         if ($title === '' or is_null($title)) {
135             throw new ClientException(_m('You must provide a non-empty title.'));
136         }
137         if (!common_valid_http_url($url)) {
138             throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
139         }
140
141         try {
142             $object = self::getByURL($stored->getProfile(), $url);
143             throw new ClientException(_m('You have already bookmarked this URL.'));
144         } catch (NoResultException $e) {
145             // Alright, so then we have to create it.
146         }
147
148         $nb = new Bookmark();
149
150         $nb->id          = UUID::gen();
151         $nb->uri         = $stored->uri;
152         $nb->profile_id  = $stored->getProfile()->getID();
153         $nb->title       = $title;
154         $nb->url         = $url;
155         $nb->description = $description;
156         $nb->created     = $stored->created;
157
158         $result = $nb->insert();
159         if ($result === false) {
160             throw new ServerException('Could not insert Bookmark into database!');
161         }
162
163         /*$hashtags = array();
164         $taglinks = array();
165
166         foreach ($tags as $tag) {
167             $hashtags[] = '#'.$tag;
168             $attrs      = array('href' => Notice_tag::url($tag),
169                                 'rel'  => $tag,
170                                 'class' => 'tag');
171             $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
172         }*/
173
174         return $nb;
175     }
176
177     /**
178      * Save a new notice bookmark
179      *
180      * @param Profile $profile     To save the bookmark for
181      * @param string  $title       Title of the bookmark
182      * @param string  $url         URL of the bookmark
183      * @param array   $rawtags     array of tags
184      * @param string  $description Description of the bookmark
185      * @param array   $options     Options for the Notice::saveNew()
186      *
187      * @return Notice saved notice
188      */
189     static function saveNew(Profile $profile, $title, $url, $rawtags, $description,
190                             array $options=array())
191     {
192         if (!common_valid_http_url($url)) {
193             throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
194         }
195
196         try {
197             $object = self::getByURL($profile, $url);
198             return $object;
199         } catch (NoResultException $e) {
200             // Alright, so then we have to create it.
201         }
202
203         if (array_key_exists('uri', $options)) {
204             $other = Bookmark::getKV('uri', $options['uri']);
205             if (!empty($other)) {
206                 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
207                 throw new ClientException(_m('Bookmark already exists.'));
208             }
209         }
210
211         $nb = new Bookmark();
212
213         $nb->id          = UUID::gen();
214         $nb->profile_id  = $profile->id;
215         $nb->url         = $url;
216         $nb->title       = $title;
217         $nb->description = $description;
218
219         if (array_key_exists('created', $options)) {
220             $nb->created = $options['created'];
221         } else {
222             $nb->created = common_sql_now();
223         }
224
225         if (array_key_exists('uri', $options)) {
226             $nb->uri = $options['uri'];
227         } else {
228             // FIXME: hacks to work around router bugs in
229             // queue daemons
230
231             $r = Router::get();
232
233             $path = $r->build('showbookmark',
234                               array('id' => $nb->id));
235
236             if (empty($path)) {
237                 $nb->uri = common_path('bookmark/'.$nb->id, false, false);
238             } else {
239                 $nb->uri = common_local_url('showbookmark',
240                                             array('id' => $nb->id),
241                                             null,
242                                             null,
243                                             false);
244             }
245         }
246
247         $nb->insert();
248
249         $tags    = array();
250         $replies = array();
251
252         // filter "for:nickname" tags
253
254         foreach ($rawtags as $tag) {
255             if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
256                 // skip if done by caller
257                 if (!array_key_exists('replies', $options)) {
258                     $nickname = mb_substr($tag, 4);
259                     $other    = common_relative_profile($profile,
260                                                         $nickname);
261                     if (!empty($other)) {
262                         $replies[] = $other->getUri();
263                     }
264                 }
265             } else {
266                 $tags[] = common_canonical_tag($tag);
267             }
268         }
269
270         $hashtags = array();
271         $taglinks = array();
272
273         foreach ($tags as $tag) {
274             $hashtags[] = '#'.$tag;
275             $attrs      = array('href' => Notice_tag::url($tag),
276                                 'rel'  => $tag,
277                                 'class' => 'tag');
278             $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
279         }
280
281         // Use user's preferences for short URLs, if possible
282
283         try {
284             $user = User::getKV('id', $profile->id);
285
286             $shortUrl = File_redirection::makeShort($url,
287                                                     empty($user) ? null : $user);
288         } catch (Exception $e) {
289             // Don't let this stop us.
290             $shortUrl = $url;
291         }
292
293         // TRANS: Bookmark content.
294         // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
295         // TRANS: %4$s is space separated list of hash tags.
296         $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'),
297                            $title,
298                            $shortUrl,
299                            $description,
300                            implode(' ', $hashtags));
301
302         // TRANS: Rendered bookmark content.
303         // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
304         // TRANS: %4$s is space separated list of hash tags.
305         $rendered = sprintf(_m('<span class="xfolkentry">'.
306                               '<a class="taggedlink" href="%1$s">%2$s</a> '.
307                               '<span class="description">%3$s</span> '.
308                               '<span class="meta">%4$s</span>'.
309                               '</span>'),
310                             htmlspecialchars($url),
311                             htmlspecialchars($title),
312                             htmlspecialchars($description),
313                             implode(' ', $taglinks));
314
315         $options = array_merge(array('urls' => array($url),
316                                      'rendered' => $rendered,
317                                      'tags' => $tags,
318                                      'replies' => $replies,
319                                      'object_type' => ActivityObject::BOOKMARK),
320                                $options);
321
322         $options['uri'] = $nb->uri;
323
324         try {
325             $saved = Notice::saveNew($profile->id,
326                                      $content,
327                                      array_key_exists('source', $options) ?
328                                      $options['source'] : 'web',
329                                      $options);
330         } catch (Exception $e) {
331             $nb->delete();
332             throw $e;
333         }
334
335         if (empty($saved)) {
336             $nb->delete();
337         }
338
339         return $saved;
340     }
341 }