]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/Bookmark.php
Add IdentiCurse to notice sources
[quix0rs-gnu-social.git] / plugins / Bookmark / 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
46 class Bookmark extends Memcached_DataObject
47 {
48     public $__table = 'bookmark'; // table name
49     public $id;          // char(36) primary_key not_null
50     public $profile_id;  // int(4) not_null
51     public $url;         // varchar(255) not_null
52     public $title;       // varchar(255)
53     public $description; // text
54     public $uri;         // varchar(255)
55     public $created;     // datetime
56
57     /**
58      * Get an instance by key
59      *
60      * This is a utility method to get a single instance with a given key value.
61      *
62      * @param string $k Key to use to lookup (usually 'user_id' for this class)
63      * @param mixed  $v Value to lookup
64      *
65      * @return User_greeting_count object found, or null for no hits
66      *
67      */
68
69     function staticGet($k, $v=null)
70     {
71         return Memcached_DataObject::staticGet('Bookmark', $k, $v);
72     }
73
74     /**
75      * Get an instance by compound key
76      *
77      * This is a utility method to get a single instance with a given set of
78      * key-value pairs. Usually used for the primary key for a compound key; thus
79      * the name.
80      *
81      * @param array $kv array of key-value mappings
82      *
83      * @return Bookmark object found, or null for no hits
84      *
85      */
86
87     function pkeyGet($kv)
88     {
89         return Memcached_DataObject::pkeyGet('Bookmark', $kv);
90     }
91
92     /**
93      * return table definition for DB_DataObject
94      *
95      * DB_DataObject needs to know something about the table to manipulate
96      * instances. This method provides all the DB_DataObject needs to know.
97      *
98      * @return array array of column definitions
99      */
100
101     function table()
102     {
103         return array('id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
104                      'profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
105                      'url' => DB_DATAOBJECT_STR,
106                      'title' => DB_DATAOBJECT_STR,
107                      'description' => DB_DATAOBJECT_STR,
108                      'uri' => DB_DATAOBJECT_STR,
109                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + 
110                      DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
111     }
112
113     /**
114      * return key definitions for DB_DataObject
115      *
116      * @return array list of key field names
117      */
118
119     function keys()
120     {
121         return array_keys($this->keyTypes());
122     }
123
124     /**
125      * return key definitions for Memcached_DataObject
126      *
127      * @return array associative array of key definitions
128      */
129
130     function keyTypes()
131     {
132         return array('id' => 'K',
133                      'uri' => 'U');
134     }
135
136     /**
137      * Magic formula for non-autoincrementing integer primary keys
138      *
139      * @return array magic three-false array that stops auto-incrementing.
140      */
141
142     function sequenceKey()
143     {
144         return array(false, false, false);
145     }
146
147     /**
148      * Get a bookmark based on a notice
149      * 
150      * @param Notice $notice Notice to check for
151      *
152      * @return Bookmark found bookmark or null
153      */
154     
155     function getByNotice($notice)
156     {
157         return self::staticGet('uri', $notice->uri);
158     }
159
160     /**
161      * Get the bookmark that a user made for an URL
162      *
163      * @param Profile $profile Profile to check for
164      * @param string  $url     URL to check for
165      *
166      * @return Bookmark bookmark found or null
167      */
168      
169     static function getByURL($profile, $url)
170     {
171         $nb = new Bookmark();
172         
173         $nb->profile_id = $profile->id;
174         $nb->url        = $url;
175
176         if ($nb->find(true)) {
177             return $nb;
178         } else {
179             return null;
180         }
181     }
182
183     /**
184      * Save a new notice bookmark
185      *
186      * @param Profile $profile     To save the bookmark for
187      * @param string  $title       Title of the bookmark
188      * @param string  $url         URL of the bookmark
189      * @param mixed   $rawtags     array of tags or string
190      * @param string  $description Description of the bookmark
191      * @param array   $options     Options for the Notice::saveNew()
192      *
193      * @return Notice saved notice
194      */
195
196     static function saveNew($profile, $title, $url, $rawtags, $description,
197                             $options=null)
198     {
199         $nb = self::getByURL($profile, $url);
200
201         if (!empty($nb)) {
202             throw new ClientException(_('Bookmark already exists.'));
203         }
204
205         if (empty($options)) {
206             $options = array();
207         }
208
209         if (array_key_exists('uri', $options)) {
210             $other = Bookmark::staticGet('uri', $options['uri']);
211             if (!empty($other)) {
212                 throw new ClientException(_('Bookmark already exists.'));
213             }
214         }
215
216         if (is_string($rawtags)) {
217             $rawtags = preg_split('/[\s,]+/', $rawtags);
218         }
219
220         $nb = new Bookmark();
221
222         $nb->id          = UUID::gen();
223         $nb->profile_id  = $profile->id;
224         $nb->url         = $url;
225         $nb->title       = $title;
226         $nb->description = $description;
227
228         if (array_key_exists('created', $options)) {
229             $nb->created = $options['created'];
230         } else {
231             $nb->created = common_sql_now();
232         }
233
234         if (array_key_exists('uri', $options)) {
235             $nb->uri = $options['uri'];
236         } else {
237             $nb->uri = common_local_url('showbookmark',
238                                         array('id' => $nb->id));
239         }
240
241         $nb->insert();
242
243         $tags    = array();
244         $replies = array();
245
246         // filter "for:nickname" tags
247
248         foreach ($rawtags as $tag) {
249             if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
250                 // skip if done by caller
251                 if (!array_key_exists('replies', $options)) {
252                     $nickname = mb_substr($tag, 4);
253                     $other    = common_relative_profile($profile,
254                                                         $nickname);
255                     if (!empty($other)) {
256                         $replies[] = $other->getUri();
257                     }
258                 }
259             } else {
260                 $tags[] = common_canonical_tag($tag);
261             }
262         }
263
264         $hashtags = array();
265         $taglinks = array();
266
267         foreach ($tags as $tag) {
268             $hashtags[] = '#'.$tag;
269             $attrs      = array('href' => Notice_tag::url($tag),
270                                 'rel'  => $tag,
271                                 'class' => 'tag');
272             $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
273         }
274
275         // Use user's preferences for short URLs, if possible
276
277         $user = User::staticGet('id', $profile->id);
278
279         $shortUrl = File_redirection::makeShort($url, 
280                                                 empty($user) ? null : $user);
281
282         $content = sprintf(_('"%s" %s %s %s'),
283                            $title,
284                            $shortUrl,
285                            $description,
286                            implode(' ', $hashtags));
287
288         $rendered = sprintf(_('<span class="xfolkentry">'.
289                               '<a class="taggedlink" href="%s">%s</a> '.
290                               '<span class="description">%s</span> '.
291                               '<span class="meta">%s</span>'.
292                               '</span>'),
293                             htmlspecialchars($url),
294                             htmlspecialchars($title),
295                             htmlspecialchars($description),
296                             implode(' ', $taglinks));
297
298         $options = array_merge(array('urls' => array($url),
299                                      'rendered' => $rendered,
300                                      'tags' => $tags,
301                                      'replies' => $replies),
302                                $options);
303
304         if (!array_key_exists('uri', $options)) {
305             $options['uri'] = $nb->uri;
306         }
307
308         $saved = Notice::saveNew($profile->id,
309                                  $content,
310                                  array_key_exists('source', $options) ?
311                                  $options['source'] : 'web',
312                                  $options);
313
314         return $saved;
315     }
316 }