3 * Data class to mark notices as bookmarks
9 * @author Evan Prodromou <evan@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2009, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('STATUSNET')) {
35 * For storing the fact that a notice is a bookmark
39 * @author Evan Prodromou <evan@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41 * @link http://status.net/
46 class Bookmark extends Memcached_DataObject
48 public $__table = 'bookmark'; // table name
49 public $profile_id; // int(4) primary_key not_null
50 public $url; // varchar(255) primary_key not_null
51 public $title; // varchar(255)
52 public $description; // text
53 public $uri; // varchar(255)
54 public $url_crc32; // int(4) not_null
55 public $created; // datetime
58 * Get an instance by key
60 * This is a utility method to get a single instance with a given key value.
62 * @param string $k Key to use to lookup (usually 'user_id' for this class)
63 * @param mixed $v Value to lookup
65 * @return User_greeting_count object found, or null for no hits
69 function staticGet($k, $v=null)
71 return Memcached_DataObject::staticGet('Bookmark', $k, $v);
75 * Get an instance by compound key
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
81 * @param array $kv array of key-value mappings
83 * @return Bookmark object found, or null for no hits
89 return Memcached_DataObject::pkeyGet('Bookmark', $kv);
93 * return table definition for DB_DataObject
95 * DB_DataObject needs to know something about the table to manipulate
96 * instances. This method provides all the DB_DataObject needs to know.
98 * @return array array of column definitions
103 return array('profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
104 'url' => DB_DATAOBJECT_STR,
105 'title' => DB_DATAOBJECT_STR,
106 'description' => DB_DATAOBJECT_STR,
107 'uri' => DB_DATAOBJECT_STR,
108 'url_crc32' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
109 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE +
110 DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
114 * return key definitions for DB_DataObject
116 * @return array list of key field names
121 return array_keys($this->keyTypes());
125 * return key definitions for Memcached_DataObject
127 * @return array associative array of key definitions
132 return array('profile_id' => 'K',
138 * Magic formula for non-autoincrementing integer primary keys
140 * @return array magic three-false array that stops auto-incrementing.
143 function sequenceKey()
145 return array(false, false, false);
149 * Get a bookmark based on a notice
151 * @param Notice $notice Notice to check for
153 * @return Bookmark found bookmark or null
156 function getByNotice($notice)
158 return self::staticGet('uri', $notice->uri);
162 * Get the bookmark that a user made for an URL
164 * @param Profile $profile Profile to check for
165 * @param string $url URL to check for
167 * @return Bookmark bookmark found or null
170 static function getByURL($profile, $url)
172 return self::pkeyGet(array('profile_id' => $profile->id,
178 * Get the bookmark that a user made for an URL
180 * @param Profile $profile Profile to check for
181 * @param integer $crc32 CRC-32 of URL to check for
183 * @return array Bookmark objects found (usually 1 or 0)
186 static function getByCRC32($profile, $crc32)
188 $bookmarks = array();
190 $nb = new Bookmark();
192 $nb->profile_id = $profile->id;
193 $nb->url_crc32 = $crc32;
196 while ($nb->fetch()) {
197 $bookmarks[] = clone($nb);
205 * Save a new notice bookmark
207 * @param Profile $profile To save the bookmark for
208 * @param string $title Title of the bookmark
209 * @param string $url URL of the bookmark
210 * @param mixed $rawtags array of tags or string
211 * @param string $description Description of the bookmark
212 * @param array $options Options for the Notice::saveNew()
214 * @return Notice saved notice
217 static function saveNew($profile, $title, $url, $rawtags, $description,
220 $nb = self::getByURL($profile, $url);
223 throw new ClientException(_('Bookmark already exists.'));
226 if (empty($options)) {
230 if (array_key_exists('uri', $options)) {
231 $other = Bookmark::staticGet('uri', $options['uri']);
232 if (!empty($other)) {
233 throw new ClientException(_('Bookmark already exists.'));
237 if (is_string($rawtags)) {
238 $rawtags = preg_split('/[\s,]+/', $rawtags);
241 $nb = new Bookmark();
243 $nb->profile_id = $profile->id;
246 $nb->description = $description;
247 $nb->url_crc32 = crc32($nb->url);
249 if (array_key_exists('created', $options)) {
250 $nb->created = $options['created'];
252 $nb->created = common_sql_now();
255 if (array_key_exists('uri', $options)) {
256 $nb->uri = $options['uri'];
258 $dt = new DateTime($nb->created, new DateTimeZone('UTC'));
260 // I posit that it's sufficiently impossible
261 // for the same user to generate two CRC-32-clashing
262 // URLs in the same second that this is a safe unique identifier.
263 // If you find a real counterexample, contact me at acct:evan@status.net
264 // and I will publicly apologize for my hubris.
266 $created = $dt->format('YmdHis');
268 $crc32 = sprintf('%08x', $nb->url_crc32);
270 $nb->uri = common_local_url('showbookmark',
271 array('user' => $profile->id,
272 'created' => $created,
281 // filter "for:nickname" tags
283 foreach ($rawtags as $tag) {
284 if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
285 // skip if done by caller
286 if (!array_key_exists('replies', $options)) {
287 $nickname = mb_substr($tag, 4);
288 $other = common_relative_profile($profile,
290 if (!empty($other)) {
291 $replies[] = $other->getUri();
295 $tags[] = common_canonical_tag($tag);
302 foreach ($tags as $tag) {
303 $hashtags[] = '#'.$tag;
304 $attrs = array('href' => Notice_tag::url($tag),
307 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
310 // Use user's preferences for short URLs, if possible
312 $user = User::staticGet('id', $profile->id);
314 $shortUrl = File_redirection::makeShort($url,
315 empty($user) ? null : $user);
317 $content = sprintf(_('"%s" %s %s %s'),
321 implode(' ', $hashtags));
323 $rendered = sprintf(_('<span class="xfolkentry">'.
324 '<a class="taggedlink" href="%s">%s</a> '.
325 '<span class="description">%s</span> '.
326 '<span class="meta">%s</span>'.
328 htmlspecialchars($url),
329 htmlspecialchars($title),
330 htmlspecialchars($description),
331 implode(' ', $taglinks));
333 $options = array_merge(array('urls' => array($url),
334 'rendered' => $rendered,
336 'replies' => $replies),
339 if (!array_key_exists('uri', $options)) {
340 $options['uri'] = $nb->uri;
343 $saved = Notice::saveNew($profile->id,
345 array_key_exists('source', $options) ?
346 $options['source'] : 'web',