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 Notice_bookmark extends Memcached_DataObject
48 public $__table = 'notice_bookmark'; // table name
49 public $notice_id; // int(4) primary_key not_null
50 public $title; // varchar(255)
51 public $description; // text
54 * Get an instance by key
56 * This is a utility method to get a single instance with a given key value.
58 * @param string $k Key to use to lookup (usually 'user_id' for this class)
59 * @param mixed $v Value to lookup
61 * @return User_greeting_count object found, or null for no hits
65 function staticGet($k, $v=null)
67 return Memcached_DataObject::staticGet('Notice_bookmark', $k, $v);
71 * return table definition for DB_DataObject
73 * DB_DataObject needs to know something about the table to manipulate
74 * instances. This method provides all the DB_DataObject needs to know.
76 * @return array array of column definitions
81 return array('notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
82 'title' => DB_DATAOBJECT_STR,
83 'description' => DB_DATAOBJECT_STR);
87 * return key definitions for DB_DataObject
89 * @return array list of key field names
94 return array_keys($this->keyTypes());
98 * return key definitions for Memcached_DataObject
100 * @return array associative array of key definitions
105 return array('notice_id' => 'K');
109 * Magic formula for non-autoincrementing integer primary keys
111 * @return array magic three-false array that stops auto-incrementing.
114 function sequenceKey()
116 return array(false, false, false);
119 static function getByURL($user, $url)
121 $file = File::staticGet('url', $url);
123 $f2p = new File_to_post();
124 $f2p->file_id = $file->id;
126 while ($f2p->fetch()) {
127 $n = Notice::staticGet('id', $f2p->post_id);
129 if ($n->profile_id == $user->id) {
130 $nb = Notice_bookmark::staticGet('notice_id', $n->id);
142 static function saveNew($user, $title, $url, $rawtags, $description, $options=null)
144 $nb = self::getByURL($user, $url);
147 throw new ClientException(_('Bookmark already exists.'));
150 if (empty($options)) {
154 if (is_string($rawtags)) {
155 $rawtags = preg_split('/[\s,]+/', $rawtags);
161 // filter "for:nickname" tags
163 foreach ($rawtags as $tag) {
164 if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
165 $nickname = mb_substr($tag, 4);
166 $other = common_relative_profile($user->getProfile(),
168 if (!empty($other)) {
169 $replies[] = $other->getUri();
172 $tags[] = common_canonical_tag($tag);
179 foreach ($tags as $tag) {
180 $hashtags[] = '#'.$tag;
181 $attrs = array('href' => Notice_tag::url($tag),
184 $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
187 $content = sprintf(_('"%s" %s %s %s'),
189 File_redirection::makeShort($url, $user),
191 implode(' ', $hashtags));
193 $rendered = sprintf(_('<span class="xfolkentry">'.
194 '<a class="taggedlink" href="%s">%s</a> '.
195 '<span class="description">%s</span> '.
196 '<span class="meta">%s</span>'.
198 htmlspecialchars($url),
199 htmlspecialchars($title),
200 htmlspecialchars($description),
201 implode(' ', $taglinks));
203 $options = array_merge($options, array('urls' => array($url),
204 'rendered' => $rendered,
206 'replies' => $replies));
208 $saved = Notice::saveNew($user->id,
213 if (!empty($saved)) {
214 $nb = new Notice_bookmark();
215 $nb->notice_id = $saved->id;
217 $nb->description = $description;