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 $id; // char(36) primary_key not_null
50 public $profile_id; // int(4) not_null
51 public $url; // varchar(255) not_null
52 public $title; // varchar(255)
53 public $description; // text
54 public $uri; // varchar(255)
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('id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
104 'profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
105 'url' => DB_DATAOBJECT_STR,
106 'title' => DB_DATAOBJECT_STR,
107 'description' => DB_DATAOBJECT_STR,
108 'uri' => DB_DATAOBJECT_STR,
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('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 $nb = new Bookmark();
173 $nb->profile_id = $profile->id;
176 if ($nb->find(true)) {
184 * Save a new notice bookmark
186 * @param Profile $profile To save the bookmark for
187 * @param string $title Title of the bookmark
188 * @param string $url URL of the bookmark
189 * @param mixed $rawtags array of tags or string
190 * @param string $description Description of the bookmark
191 * @param array $options Options for the Notice::saveNew()
193 * @return Notice saved notice
196 static function saveNew($profile, $title, $url, $rawtags, $description,
199 $nb = self::getByURL($profile, $url);
202 throw new ClientException(_('Bookmark already exists.'));
205 if (empty($options)) {
209 if (array_key_exists('uri', $options)) {
210 $other = Bookmark::staticGet('uri', $options['uri']);
211 if (!empty($other)) {
212 throw new ClientException(_('Bookmark already exists.'));
216 if (is_string($rawtags)) {
217 if (empty($rawtags)) {
220 $rawtags = preg_split('/[\s,]+/', $rawtags);
224 $nb = new Bookmark();
226 $nb->id = UUID::gen();
227 $nb->profile_id = $profile->id;
230 $nb->description = $description;
232 if (array_key_exists('created', $options)) {
233 $nb->created = $options['created'];
235 $nb->created = common_sql_now();
238 if (array_key_exists('uri', $options)) {
239 $nb->uri = $options['uri'];
241 $nb->uri = common_local_url('showbookmark',
242 array('id' => $nb->id));
250 // filter "for:nickname" tags
252 foreach ($rawtags as $tag) {
253 if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
254 // skip if done by caller
255 if (!array_key_exists('replies', $options)) {
256 $nickname = mb_substr($tag, 4);
257 $other = common_relative_profile($profile,
259 if (!empty($other)) {
260 $replies[] = $other->getUri();
264 $tags[] = common_canonical_tag($tag);
271 foreach ($tags as $tag) {
272 $hashtags[] = '#'.$tag;
273 $attrs = array('href' => Notice_tag::url($tag),
276 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
279 // Use user's preferences for short URLs, if possible
282 $user = User::staticGet('id', $profile->id);
284 $shortUrl = File_redirection::makeShort($url,
285 empty($user) ? null : $user);
286 } catch (Exception $e) {
287 // Don't let this stop us.
291 $content = sprintf(_('"%s" %s %s %s'),
295 implode(' ', $hashtags));
297 $rendered = sprintf(_('<span class="xfolkentry">'.
298 '<a class="taggedlink" href="%s">%s</a> '.
299 '<span class="description">%s</span> '.
300 '<span class="meta">%s</span>'.
302 htmlspecialchars($url),
303 htmlspecialchars($title),
304 htmlspecialchars($description),
305 implode(' ', $taglinks));
307 $options = array_merge(array('urls' => array($url),
308 'rendered' => $rendered,
310 'replies' => $replies,
311 'object_type' => ActivityObject::BOOKMARK),
314 if (!array_key_exists('uri', $options)) {
315 $options['uri'] = $nb->uri;
318 $saved = Notice::saveNew($profile->id,
320 array_key_exists('source', $options) ?
321 $options['source'] : 'web',