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
56 public static function schemaDef()
60 'id' => array('type' => 'char',
63 'profile_id' => array('type' => 'int', 'not null' => true),
64 'uri' => array('type' => 'varchar',
67 'url' => array('type' => 'varchar',
70 'title' => array('type' => 'varchar', 'length' => 255),
71 'description' => array('type' => 'text'),
72 'created' => array('type' => 'datetime', 'not null' => true),
74 'primary key' => array('id'),
75 'unique keys' => array(
76 'bookmark_uri_key' => array('uri'),
78 'foreign keys' => array(
79 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id'))
81 'indexes' => array('bookmark_created_idx' => array('created'),
82 'bookmark_url_idx' => array('url'),
83 'bookmark_profile_id_idx' => array('profile_id'),
89 * Get a bookmark based on a notice
91 * @param Notice $notice Notice to check for
93 * @return Bookmark found bookmark or null
95 static function getByNotice($notice)
97 return self::getKV('uri', $notice->uri);
101 * Get the bookmark that a user made for an URL
103 * @param Profile $profile Profile to check for
104 * @param string $url URL to check for
106 * @return Bookmark bookmark found or null
108 static function getByURL($profile, $url)
110 $nb = new Bookmark();
112 $nb->profile_id = $profile->id;
115 if ($nb->find(true)) {
123 * Save a new notice bookmark
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()
132 * @return Notice saved notice
134 static function saveNew($profile, $title, $url, $rawtags, $description,
137 if (!common_valid_http_url($url)) {
138 throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
141 $nb = self::getByURL($profile, $url);
144 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
145 throw new ClientException(_m('Bookmark already exists.'));
148 if (empty($options)) {
152 if (array_key_exists('uri', $options)) {
153 $other = Bookmark::getKV('uri', $options['uri']);
154 if (!empty($other)) {
155 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
156 throw new ClientException(_m('Bookmark already exists.'));
160 if (is_string($rawtags)) {
161 if (empty($rawtags)) {
164 $rawtags = preg_split('/[\s,]+/', $rawtags);
168 $nb = new Bookmark();
170 $nb->id = UUID::gen();
171 $nb->profile_id = $profile->id;
174 $nb->description = $description;
176 if (array_key_exists('created', $options)) {
177 $nb->created = $options['created'];
179 $nb->created = common_sql_now();
182 if (array_key_exists('uri', $options)) {
183 $nb->uri = $options['uri'];
185 // FIXME: hacks to work around router bugs in
190 $path = $r->build('showbookmark',
191 array('id' => $nb->id));
194 $nb->uri = common_path('bookmark/'.$nb->id, false, false);
196 $nb->uri = common_local_url('showbookmark',
197 array('id' => $nb->id),
209 // filter "for:nickname" tags
211 foreach ($rawtags as $tag) {
212 if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
213 // skip if done by caller
214 if (!array_key_exists('replies', $options)) {
215 $nickname = mb_substr($tag, 4);
216 $other = common_relative_profile($profile,
218 if (!empty($other)) {
219 $replies[] = $other->getUri();
223 $tags[] = common_canonical_tag($tag);
230 foreach ($tags as $tag) {
231 $hashtags[] = '#'.$tag;
232 $attrs = array('href' => Notice_tag::url($tag),
235 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
238 // Use user's preferences for short URLs, if possible
241 $user = User::getKV('id', $profile->id);
243 $shortUrl = File_redirection::makeShort($url,
244 empty($user) ? null : $user);
245 } catch (Exception $e) {
246 // Don't let this stop us.
250 // TRANS: Bookmark content.
251 // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
252 // TRANS: %4$s is space separated list of hash tags.
253 $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'),
257 implode(' ', $hashtags));
259 // TRANS: Rendered bookmark content.
260 // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
261 // TRANS: %4$s is space separated list of hash tags.
262 $rendered = sprintf(_m('<span class="xfolkentry">'.
263 '<a class="taggedlink" href="%1$s">%2$s</a> '.
264 '<span class="description">%3$s</span> '.
265 '<span class="meta">%4$s</span>'.
267 htmlspecialchars($url),
268 htmlspecialchars($title),
269 htmlspecialchars($description),
270 implode(' ', $taglinks));
272 $options = array_merge(array('urls' => array($url),
273 'rendered' => $rendered,
275 'replies' => $replies,
276 'object_type' => ActivityObject::BOOKMARK),
279 if (!array_key_exists('uri', $options)) {
280 $options['uri'] = $nb->uri;
284 $saved = Notice::saveNew($profile->id,
286 array_key_exists('source', $options) ?
287 $options['source'] : 'web',
289 } catch (Exception $e) {