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 + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
113 * return key definitions for DB_DataObject
115 * @return array list of key field names
120 return array_keys($this->keyTypes());
124 * return key definitions for Memcached_DataObject
126 * @return array associative array of key definitions
131 return array('profile_id' => 'K',
137 * Magic formula for non-autoincrementing integer primary keys
139 * @return array magic three-false array that stops auto-incrementing.
142 function sequenceKey()
144 return array(false, false, false);
148 * Get a bookmark based on a notice
150 * @param Notice $notice Notice to check for
152 * @return Bookmark found bookmark or null
155 function getByNotice($notice)
157 return self::staticGet('uri', $notice->uri);
161 * Get the bookmark that a user made for an URL
163 * @param Profile $profile Profile to check for
164 * @param string $url URL to check for
166 * @return Bookmark bookmark found or null
169 static function getByURL($profile, $url)
171 return self::pkeyGet(array('profile_id' => $profile->id,
177 * Get the bookmark that a user made for an URL
179 * @param Profile $profile Profile to check for
180 * @param integer $crc32 CRC-32 of URL to check for
182 * @return array Bookmark objects found (usually 1 or 0)
185 static function getByCRC32($profile, $crc32)
187 $bookmarks = array();
189 $nb = new Bookmark();
191 $nb->profile_id = $profile->id;
192 $nb->url_crc32 = $crc32;
195 while ($nb->fetch()) {
196 $bookmarks[] = clone($nb);
204 * Save a new notice bookmark
206 * @param Profile $profile To save the bookmark for
207 * @param string $title Title of the bookmark
208 * @param string $url URL of the bookmark
209 * @param mixed $rawtags array of tags or string
210 * @param string $description Description of the bookmark
211 * @param array $options Options for the Notice::saveNew()
213 * @return Notice saved notice
216 static function saveNew($profile, $title, $url, $rawtags, $description,
219 $nb = self::getByURL($profile, $url);
222 throw new ClientException(_('Bookmark already exists.'));
225 if (empty($options)) {
229 if (array_key_exists('uri', $options)) {
230 $other = Bookmark::staticGet('uri', $options['uri']);
231 if (!empty($other)) {
232 throw new ClientException(_('Bookmark already exists.'));
236 if (is_string($rawtags)) {
237 $rawtags = preg_split('/[\s,]+/', $rawtags);
240 $nb = new Bookmark();
242 $nb->profile_id = $profile->id;
245 $nb->description = $description;
246 $nb->url_crc32 = crc32($nb->url);
248 if (array_key_exists('created', $options)) {
249 $nb->created = $options['created'];
251 $nb->created = common_sql_now();
254 if (array_key_exists('uri', $options)) {
255 $nb->uri = $options['uri'];
257 $dt = new DateTime($nb->created, new DateTimeZone('UTC'));
259 // I posit that it's sufficiently impossible
260 // for the same user to generate two CRC-32-clashing
261 // URLs in the same second that this is a safe unique identifier.
262 // If you find a real counterexample, contact me at acct:evan@status.net
263 // and I will publicly apologize for my hubris.
265 $created = $dt->format('YmdHis');
267 $crc32 = sprintf('%08x', $nb->url_crc32);
269 $nb->uri = common_local_url('showbookmark',
270 array('user' => $profile->id,
271 'created' => $created,
280 // filter "for:nickname" tags
282 foreach ($rawtags as $tag) {
283 if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
284 if (!array_key_exists('replies', $options)) { // skip if done by caller
285 $nickname = mb_substr($tag, 4);
286 $other = common_relative_profile($profile,
288 if (!empty($other)) {
289 $replies[] = $other->getUri();
293 $tags[] = common_canonical_tag($tag);
300 foreach ($tags as $tag) {
301 $hashtags[] = '#'.$tag;
302 $attrs = array('href' => Notice_tag::url($tag),
305 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
308 // Use user's preferences for short URLs, if possible
310 $user = User::staticGet('id', $profile->id);
312 $shortUrl = File_redirection::makeShort($url,
313 empty($user) ? null : $user);
315 $content = sprintf(_('"%s" %s %s %s'),
319 implode(' ', $hashtags));
321 $rendered = sprintf(_('<span class="xfolkentry">'.
322 '<a class="taggedlink" href="%s">%s</a> '.
323 '<span class="description">%s</span> '.
324 '<span class="meta">%s</span>'.
326 htmlspecialchars($url),
327 htmlspecialchars($title),
328 htmlspecialchars($description),
329 implode(' ', $taglinks));
331 $options = array_merge(array('urls' => array($url),
332 'rendered' => $rendered,
334 'replies' => $replies),
337 if (!array_key_exists('uri', $options)) {
338 $options['uri'] = $nb->uri;
341 $saved = Notice::saveNew($profile->id,
343 array_key_exists('source', $options) ?
344 $options['source'] : 'web',