]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/classes/Bookmark.php
Merge branch 'master' into social-master
[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('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * For storing the fact that a notice is a bookmark
36  *
37  * @category Bookmark
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      DB_DataObject
44  */
45 class Bookmark extends Managed_DataObject
46 {
47     public $__table = 'bookmark'; // table name
48     public $id;          // char(36) primary_key not_null
49     public $profile_id;  // int(4) not_null
50     public $url;         // varchar(255) not_null
51     public $title;       // varchar(255)
52     public $description; // text
53     public $uri;         // varchar(255)
54     public $created;     // datetime
55
56     public static function schemaDef()
57     {
58         return array(
59             'fields' => array(
60                 'id' => array('type' => 'char',
61                             'length' => 36,
62                             'not null' => true),
63                 'profile_id' => array('type' => 'int', 'not null' => true),
64                 'uri' => array('type' => 'varchar',
65                             'length' => 255,
66                             'not null' => true),
67                 'url' => array('type' => 'varchar',
68                             'length' => 255,
69                             'not null' => true),
70                 'title' => array('type' => 'varchar', 'length' => 255),
71                 'description' => array('type' => 'text'),
72                 'created' => array('type' => 'datetime', 'not null' => true),
73             ),
74             'primary key' => array('id'),
75             'unique keys' => array(
76                 'bookmark_uri_key' => array('uri'),
77             ),
78             'foreign keys' => array(
79                 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id'))
80             ),
81             'indexes' => array('bookmark_created_idx' => array('created'),
82                             'bookmark_url_idx' => array('url'),
83                             'bookmark_profile_id_idx' => array('profile_id'),
84             ),
85         );
86     }
87
88     /**
89      * Get a bookmark based on a notice
90      *
91      * @param Notice $notice Notice to check for
92      *
93      * @return Bookmark found bookmark or null
94      */
95     static function getByNotice(Notice $notice)
96     {
97         return self::getKV('uri', $notice->uri);
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->id;
113         $nb->url        = $url;
114
115         if ($nb->find(true)) {
116             return $nb;
117         } else {
118             return null;
119         }
120     }
121
122     /**
123      * Save a new notice bookmark
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 mixed   $rawtags     array of tags or string
129      * @param string  $description Description of the bookmark
130      * @param array   $options     Options for the Notice::saveNew()
131      *
132      * @return Notice saved notice
133      */
134     static function saveNew(Profile $profile, $title, $url, $rawtags, $description,
135                             array $options=array())
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         $nb = self::getByURL($profile, $url);
142
143         if (!empty($nb)) {
144             // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
145             throw new ClientException(_m('Bookmark already exists.'));
146         }
147
148         if (array_key_exists('uri', $options)) {
149             $other = Bookmark::getKV('uri', $options['uri']);
150             if (!empty($other)) {
151                 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
152                 throw new ClientException(_m('Bookmark already exists.'));
153             }
154         }
155
156         if (is_string($rawtags)) {
157             if (empty($rawtags)) {
158                 $rawtags = array();
159             } else {
160                 $rawtags = preg_split('/[\s,]+/', $rawtags);
161             }
162         }
163
164         $nb = new Bookmark();
165
166         $nb->id          = UUID::gen();
167         $nb->profile_id  = $profile->id;
168         $nb->url         = $url;
169         $nb->title       = $title;
170         $nb->description = $description;
171
172         if (array_key_exists('created', $options)) {
173             $nb->created = $options['created'];
174         } else {
175             $nb->created = common_sql_now();
176         }
177
178         if (array_key_exists('uri', $options)) {
179             $nb->uri = $options['uri'];
180         } else {
181             // FIXME: hacks to work around router bugs in
182             // queue daemons
183
184             $r = Router::get();
185
186             $path = $r->build('showbookmark',
187                               array('id' => $nb->id));
188
189             if (empty($path)) {
190                 $nb->uri = common_path('bookmark/'.$nb->id, false, false);
191             } else {
192                 $nb->uri = common_local_url('showbookmark',
193                                             array('id' => $nb->id),
194                                             null,
195                                             null,
196                                             false);
197             }
198         }
199
200         $nb->insert();
201
202         $tags    = array();
203         $replies = array();
204
205         // filter "for:nickname" tags
206
207         foreach ($rawtags as $tag) {
208             if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
209                 // skip if done by caller
210                 if (!array_key_exists('replies', $options)) {
211                     $nickname = mb_substr($tag, 4);
212                     $other    = common_relative_profile($profile,
213                                                         $nickname);
214                     if (!empty($other)) {
215                         $replies[] = $other->getUri();
216                     }
217                 }
218             } else {
219                 $tags[] = common_canonical_tag($tag);
220             }
221         }
222
223         $hashtags = array();
224         $taglinks = array();
225
226         foreach ($tags as $tag) {
227             $hashtags[] = '#'.$tag;
228             $attrs      = array('href' => Notice_tag::url($tag),
229                                 'rel'  => $tag,
230                                 'class' => 'tag');
231             $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
232         }
233
234         // Use user's preferences for short URLs, if possible
235
236         try {
237             $user = User::getKV('id', $profile->id);
238
239             $shortUrl = File_redirection::makeShort($url,
240                                                     empty($user) ? null : $user);
241         } catch (Exception $e) {
242             // Don't let this stop us.
243             $shortUrl = $url;
244         }
245
246         // TRANS: Bookmark content.
247         // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
248         // TRANS: %4$s is space separated list of hash tags.
249         $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'),
250                            $title,
251                            $shortUrl,
252                            $description,
253                            implode(' ', $hashtags));
254
255         // TRANS: Rendered bookmark content.
256         // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
257         // TRANS: %4$s is space separated list of hash tags.
258         $rendered = sprintf(_m('<span class="xfolkentry">'.
259                               '<a class="taggedlink" href="%1$s">%2$s</a> '.
260                               '<span class="description">%3$s</span> '.
261                               '<span class="meta">%4$s</span>'.
262                               '</span>'),
263                             htmlspecialchars($url),
264                             htmlspecialchars($title),
265                             htmlspecialchars($description),
266                             implode(' ', $taglinks));
267
268         $options = array_merge(array('urls' => array($url),
269                                      'rendered' => $rendered,
270                                      'tags' => $tags,
271                                      'replies' => $replies,
272                                      'object_type' => ActivityObject::BOOKMARK),
273                                $options);
274
275         if (!array_key_exists('uri', $options)) {
276             $options['uri'] = $nb->uri;
277         }
278
279         try {
280             $saved = Notice::saveNew($profile->id,
281                                      $content,
282                                      array_key_exists('source', $options) ?
283                                      $options['source'] : 'web',
284                                      $options);
285         } catch (Exception $e) {
286             $nb->delete();
287             throw $e;
288         }
289
290         if (empty($saved)) {
291             $nb->delete();
292         }
293
294         return $saved;
295     }
296 }