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/
45 class Bookmark extends Managed_DataObject
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
57 * Get an instance by compound key
59 * This is a utility method to get a single instance with a given set of
60 * key-value pairs. Usually used for the primary key for a compound key; thus
63 * @param array $kv array of key-value mappings
65 * @return Bookmark object found, or null for no hits
70 return Memcached_DataObject::pkeyGet('Bookmark', $kv);
74 * return table definition for DB_DataObject
76 * DB_DataObject needs to know something about the table to manipulate
77 * instances. This method provides all the DB_DataObject needs to know.
79 * @return array array of column definitions
83 return array('id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
84 'profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
85 'url' => DB_DATAOBJECT_STR,
86 'title' => DB_DATAOBJECT_STR,
87 'description' => DB_DATAOBJECT_STR,
88 'uri' => DB_DATAOBJECT_STR,
89 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE +
90 DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
94 * return key definitions for DB_DataObject
96 * @return array list of key field names
100 return array_keys($this->keyTypes());
104 * return key definitions for Memcached_DataObject
106 * @return array associative array of key definitions
110 return array('id' => 'K',
115 * Magic formula for non-autoincrementing integer primary keys
117 * @return array magic three-false array that stops auto-incrementing.
119 function sequenceKey()
121 return array(false, false, false);
125 * Get a bookmark based on a notice
127 * @param Notice $notice Notice to check for
129 * @return Bookmark found bookmark or null
131 static function getByNotice($notice)
133 return self::staticGet('uri', $notice->uri);
137 * Get the bookmark that a user made for an URL
139 * @param Profile $profile Profile to check for
140 * @param string $url URL to check for
142 * @return Bookmark bookmark found or null
144 static function getByURL($profile, $url)
146 $nb = new Bookmark();
148 $nb->profile_id = $profile->id;
151 if ($nb->find(true)) {
159 * Save a new notice bookmark
161 * @param Profile $profile To save the bookmark for
162 * @param string $title Title of the bookmark
163 * @param string $url URL of the bookmark
164 * @param mixed $rawtags array of tags or string
165 * @param string $description Description of the bookmark
166 * @param array $options Options for the Notice::saveNew()
168 * @return Notice saved notice
170 static function saveNew($profile, $title, $url, $rawtags, $description,
173 $nb = self::getByURL($profile, $url);
176 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
177 throw new ClientException(_m('Bookmark already exists.'));
180 if (empty($options)) {
184 if (array_key_exists('uri', $options)) {
185 $other = Bookmark::staticGet('uri', $options['uri']);
186 if (!empty($other)) {
187 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
188 throw new ClientException(_m('Bookmark already exists.'));
192 if (is_string($rawtags)) {
193 if (empty($rawtags)) {
196 $rawtags = preg_split('/[\s,]+/', $rawtags);
200 $nb = new Bookmark();
202 $nb->id = UUID::gen();
203 $nb->profile_id = $profile->id;
206 $nb->description = $description;
208 if (array_key_exists('created', $options)) {
209 $nb->created = $options['created'];
211 $nb->created = common_sql_now();
214 if (array_key_exists('uri', $options)) {
215 $nb->uri = $options['uri'];
217 // FIXME: hacks to work around router bugs in
222 $path = $r->build('showbookmark',
223 array('id' => $nb->id));
226 $nb->uri = common_path('bookmark/'.$nb->id, false, false);
228 $nb->uri = common_local_url('showbookmark',
229 array('id' => $nb->id),
241 // filter "for:nickname" tags
243 foreach ($rawtags as $tag) {
244 if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
245 // skip if done by caller
246 if (!array_key_exists('replies', $options)) {
247 $nickname = mb_substr($tag, 4);
248 $other = common_relative_profile($profile,
250 if (!empty($other)) {
251 $replies[] = $other->getUri();
255 $tags[] = common_canonical_tag($tag);
262 foreach ($tags as $tag) {
263 $hashtags[] = '#'.$tag;
264 $attrs = array('href' => Notice_tag::url($tag),
267 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
270 // Use user's preferences for short URLs, if possible
273 $user = User::staticGet('id', $profile->id);
275 $shortUrl = File_redirection::makeShort($url,
276 empty($user) ? null : $user);
277 } catch (Exception $e) {
278 // Don't let this stop us.
282 // TRANS: Bookmark content.
283 // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
284 // TRANS: %4$s is space separated list of hash tags.
285 $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'),
289 implode(' ', $hashtags));
291 // TRANS: Rendered bookmark content.
292 // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
293 // TRANS: %4$s is space separated list of hash tags.
294 $rendered = sprintf(_m('<span class="xfolkentry">'.
295 '<a class="taggedlink" href="%1$s">%2$s</a> '.
296 '<span class="description">%3$s</span> '.
297 '<span class="meta">%4$s</span>'.
299 htmlspecialchars($url),
300 htmlspecialchars($title),
301 htmlspecialchars($description),
302 implode(' ', $taglinks));
304 $options = array_merge(array('urls' => array($url),
305 'rendered' => $rendered,
307 'replies' => $replies,
308 'object_type' => ActivityObject::BOOKMARK),
311 if (!array_key_exists('uri', $options)) {
312 $options['uri'] = $nb->uri;
316 $saved = Notice::saveNew($profile->id,
318 array_key_exists('source', $options) ?
319 $options['source'] : 'web',
321 } catch (Exception $e) {