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('GNUSOCIAL')) { exit(1); }
33 * For storing the fact that a notice is a bookmark
37 * @author Evan Prodromou <evan@status.net>
38 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
39 * @link http://status.net/
43 class Bookmark extends Managed_DataObject
45 public $__table = 'bookmark'; // table name
46 public $id; // char(36) primary_key not_null
47 public $profile_id; // int(4) not_null
48 public $url; // varchar(191) not_null not 255 because utf8mb4 takes more space
49 public $title; // varchar(191) not 255 because utf8mb4 takes more space
50 public $uri; // varchar(191) not 255 because utf8mb4 takes more space
51 public $description; // text
52 public $created; // datetime
54 public static function schemaDef()
58 'id' => array('type' => 'char',
61 'profile_id' => array('type' => 'int', 'not null' => true),
62 'uri' => array('type' => 'varchar',
65 'url' => array('type' => 'varchar',
68 'title' => array('type' => 'varchar', 'length' => 191),
69 'description' => array('type' => 'text'),
70 'created' => array('type' => 'datetime', 'not null' => true),
72 'primary key' => array('id'),
73 'unique keys' => array(
74 'bookmark_uri_key' => array('uri'),
76 'foreign keys' => array(
77 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
78 'bookmark_uri_fkey' => array('notice', array('uri' => 'uri')),
80 'indexes' => array('bookmark_created_idx' => array('created'),
81 'bookmark_url_idx' => array('url'),
82 'bookmark_profile_id_idx' => array('profile_id'),
88 * Get a bookmark based on a notice
90 * @param Notice $stored Notice activity which represents the Bookmark
92 * @return Bookmark The found bookmark object.
93 * @throws NoResultException When you don't find it after all.
95 static public function fromStored(Notice $stored)
97 $class = get_called_class();
98 return self::getByPK(array('uri', $stored->getUri()));
102 * Get the bookmark that a user made for an URL
104 * @param Profile $profile Profile to check for
105 * @param string $url URL to check for
107 * @return Bookmark bookmark found or null
109 static function getByURL($profile, $url)
111 $nb = new Bookmark();
113 $nb->profile_id = $profile->id;
116 if ($nb->find(true)) {
124 * Save a new notice bookmark
126 * @param Profile $profile To save the bookmark for
127 * @param string $title Title of the bookmark
128 * @param string $url URL of the bookmark
129 * @param mixed $rawtags array of tags or string
130 * @param string $description Description of the bookmark
131 * @param array $options Options for the Notice::saveNew()
133 * @return Notice saved notice
135 static function saveNew($profile, $title, $url, $rawtags, $description,
138 if (!common_valid_http_url($url)) {
139 throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
142 $nb = self::getByURL($profile, $url);
145 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
146 throw new ClientException(_m('Bookmark already exists.'));
149 if (empty($options)) {
153 if (array_key_exists('uri', $options)) {
154 $other = Bookmark::getKV('uri', $options['uri']);
155 if (!empty($other)) {
156 // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
157 throw new ClientException(_m('Bookmark already exists.'));
161 if (is_string($rawtags)) {
162 if (empty($rawtags)) {
165 $rawtags = preg_split('/[\s,]+/', $rawtags);
169 $nb = new Bookmark();
171 $nb->id = UUID::gen();
172 $nb->profile_id = $profile->id;
175 $nb->description = $description;
177 if (array_key_exists('created', $options)) {
178 $nb->created = $options['created'];
180 $nb->created = common_sql_now();
183 if (array_key_exists('uri', $options)) {
184 $nb->uri = $options['uri'];
186 // FIXME: hacks to work around router bugs in
191 $path = $r->build('showbookmark',
192 array('id' => $nb->id));
195 $nb->uri = common_path('bookmark/'.$nb->id, false, false);
197 $nb->uri = common_local_url('showbookmark',
198 array('id' => $nb->id),
210 // filter "for:nickname" tags
212 foreach ($rawtags as $tag) {
213 if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
214 // skip if done by caller
215 if (!array_key_exists('replies', $options)) {
216 $nickname = mb_substr($tag, 4);
217 $other = common_relative_profile($profile,
219 if (!empty($other)) {
220 $replies[] = $other->getUri();
224 $tags[] = common_canonical_tag($tag);
231 foreach ($tags as $tag) {
232 $hashtags[] = '#'.$tag;
233 $attrs = array('href' => Notice_tag::url($tag),
236 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
239 // Use user's preferences for short URLs, if possible
242 $user = User::getKV('id', $profile->id);
244 $shortUrl = File_redirection::makeShort($url,
245 empty($user) ? null : $user);
246 } catch (Exception $e) {
247 // Don't let this stop us.
251 // TRANS: Bookmark content.
252 // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
253 // TRANS: %4$s is space separated list of hash tags.
254 $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'),
258 implode(' ', $hashtags));
260 // TRANS: Rendered bookmark content.
261 // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
262 // TRANS: %4$s is space separated list of hash tags.
263 $rendered = sprintf(_m('<span class="xfolkentry">'.
264 '<a class="taggedlink" href="%1$s">%2$s</a> '.
265 '<span class="description">%3$s</span> '.
266 '<span class="meta">%4$s</span>'.
268 htmlspecialchars($url),
269 htmlspecialchars($title),
270 htmlspecialchars($description),
271 implode(' ', $taglinks));
273 $options = array_merge(array('urls' => array($url),
274 'rendered' => $rendered,
276 'replies' => $replies,
277 'object_type' => ActivityObject::BOOKMARK),
280 $options['uri'] = $nb->uri;
283 $saved = Notice::saveNew($profile->id,
285 array_key_exists('source', $options) ?
286 $options['source'] : 'web',
288 } catch (Exception $e) {