3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
23 * Table Definition for notice
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
27 /* We keep the first three 20-notice pages, plus one for pagination check,
28 * in the memcached cache. */
30 define('NOTICE_CACHE_WINDOW', 61);
32 class Notice extends Memcached_DataObject
35 /* the code below is auto generated do not remove the above tag */
37 public $__table = 'notice'; // table name
38 public $id; // int(4) primary_key not_null
39 public $profile_id; // int(4) not_null
40 public $uri; // varchar(255) unique_key
41 public $content; // varchar(140)
42 public $rendered; // text()
43 public $url; // varchar(255)
44 public $created; // datetime() not_null
45 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
46 public $reply_to; // int(4)
47 public $is_local; // tinyint(1)
48 public $source; // varchar(32)
51 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Notice',$k,$v); }
53 /* the code above is auto generated do not remove the tag below */
56 function getProfile() {
57 return Profile::staticGet('id', $this->profile_id);
62 $this->blowFavesCache();
68 /* extract all #hastags */
69 $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match);
74 /* elide characters we don't want in the tag */
75 $match[1] = str_replace(array('-', '_', '.'), '', $match[1]);
77 /* Add them to the database */
78 foreach(array_unique($match[1]) as $hashtag) {
79 $tag = DB_DataObject::factory('Notice_tag');
80 $tag->notice_id = $this->id;
82 $tag->created = $this->created;
85 $last_error = PEAR::getStaticProperty('DB_DataObject','lastError');
86 common_log(LOG_ERR, 'DB error inserting hashtag: ' . $last_error->message);
87 common_server_error(sprintf(_('DB error inserting hashtag: %s'), $last_error->message));
94 static function saveNew($profile_id, $content, $source=NULL, $is_local=1, $reply_to=NULL, $uri=NULL) {
96 $notice = new Notice();
97 $notice->profile_id = $profile_id;
98 $notice->is_local = $is_local;
99 $notice->reply_to = $reply_to;
100 $notice->created = common_sql_now();
101 $notice->content = $content;
102 $notice->rendered = common_render_content($notice->content, $notice);
103 $notice->source = $source;
106 $id = $notice->insert();
109 common_log_db_error($notice, 'INSERT', __FILE__);
110 return _('Problem saving notice.');
113 # Update the URI after the notice is in the database
115 $orig = clone($notice);
116 $notice->uri = common_notice_uri($notice);
118 if (!$notice->update($orig)) {
119 common_log_db_error($notice, 'UPDATE', __FILE__);
120 return _('Problem saving notice.');
124 # XXX: do we need to change this for remote users?
126 common_save_replies($notice);
129 # Clear the cache for subscribed users, so they'll update at next request
130 # XXX: someone clever could prepend instead of clearing the cache
132 if (common_config('memcached', 'enabled')) {
133 $notice->blowCaches();
136 $notice->addToInboxes();
140 function blowCaches() {
141 $this->blowSubsCache();
142 $this->blowNoticeCache();
143 $this->blowRepliesCache();
144 $this->blowPublicCache();
145 $this->blowTagCache();
148 function blowTagCache() {
149 $cache = common_memcache();
151 $tag = new Notice_tag();
152 $tag->notice_id = $this->id;
154 while ($tag->fetch()) {
155 $cache->delete(common_cache_key('notice_tag:notice_stream:' . $tag->tag));
163 function blowSubsCache() {
164 $cache = common_memcache();
168 $user->query('SELECT id ' .
169 'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
170 'WHERE subscription.subscribed = ' . $this->profile_id);
172 while ($user->fetch()) {
173 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
181 function blowNoticeCache() {
182 if ($this->is_local) {
183 $cache = common_memcache();
185 $cache->delete(common_cache_key('user:notices:'.$this->profile_id));
190 function blowRepliesCache() {
191 $cache = common_memcache();
193 $reply = new Reply();
194 $reply->notice_id = $this->id;
195 if ($reply->find()) {
196 while ($reply->fetch()) {
197 $cache->delete(common_cache_key('user:replies:'.$reply->profile_id));
205 function blowPublicCache() {
206 if ($this->is_local) {
207 $cache = common_memcache();
209 $cache->delete(common_cache_key('public'));
214 function blowFavesCache() {
215 $cache = common_memcache();
218 $fave->notice_id = $this->id;
220 while ($fave->fetch()) {
221 $cache->delete(common_cache_key('user:faves:'.$fave->user_id));
229 # XXX: too many args; we need to move to named params or even a separate
230 # class for notice streams
232 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $before_id=0, $order=NULL) {
234 if (common_config('memcached', 'enabled')) {
236 # Skip the cache if this is a since_id or before_id qry
237 if ($since_id > 0 || $before_id > 0) {
238 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order);
240 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
244 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order);
247 static function getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order) {
252 if (preg_match('/\bWHERE\b/i', $qry)) {
266 $qry .= ' notice.id > ' . $since_id;
269 if ($before_id > 0) {
278 $qry .= ' notice.id < ' . $before_id;
281 # Allow ORDER override
286 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
289 if (common_config('db','type') == 'pgsql') {
290 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
292 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
295 $notice = new Notice();
297 $notice->query($qry);
302 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
304 # If outside our cache window, just go to the DB
306 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
307 return Notice::getStreamDirect($qry, $offset, $limit, NULL, NULL, $order);
310 # Get the cache; if we can't, just go to the DB
312 $cache = common_memcache();
315 return Notice::getStreamDirect($qry, $offset, $limit, NULL, NULL, $order);
318 # Get the notices out of the cache
320 $notices = $cache->get(common_cache_key($cachekey));
322 # On a cache hit, return a DB-object-like wrapper
324 if ($notices !== FALSE) {
325 $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
329 # Otherwise, get the full cache window out of the DB
331 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, NULL, NULL, $order);
333 # If there are no hits, just return the value
339 # Pack results into an array
343 while ($notice->fetch()) {
344 $notices[] = clone($notice);
347 # Store the array in the cache for next time
349 $result = $cache->set(common_cache_key($cachekey), $notices);
351 # return a wrapper of the array for use now
353 $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
358 function publicStream($offset=0, $limit=20, $since_id=0, $before_id=0) {
363 $qry = 'SELECT * FROM notice ';
365 if (common_config('public', 'localonly')) {
366 $qry .= ' WHERE is_local = 1 ';
371 return Notice::getStream($qry,
373 $offset, $limit, $since_id, $before_id);
376 function addToInboxes() {
378 $inbox = new Notice_inbox();
380 $inbox->query('INSERT INTO notice_inbox (user_id, notice_id) ' .
381 'SELECT user.id, ' . $this->id . ' ' .
382 'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
383 'WHERE subscription.subscribed = ' . $this->profile_id);
388 # Delete from inboxes if we're deleted.
390 function blowInboxes() {
392 $inbox = new Notice_inbox();
393 $inbox->notice_id = $this->id;