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);
61 $this->blowCaches(true);
62 $this->blowFavesCache(true);
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;
99 $blacklist = common_config('public', 'blacklist');
101 # Blacklisted are non-false, but not 1, either
103 if ($blacklist && in_array($profile_id, $blacklist)) {
104 $notice->is_local = -1;
106 $notice->is_local = $is_local;
109 $notice->reply_to = $reply_to;
110 $notice->created = common_sql_now();
111 $notice->content = $content;
112 $notice->rendered = common_render_content($notice->content, $notice);
113 $notice->source = $source;
116 $id = $notice->insert();
119 common_log_db_error($notice, 'INSERT', __FILE__);
120 return _('Problem saving notice.');
123 # Update the URI after the notice is in the database
125 $orig = clone($notice);
126 $notice->uri = common_notice_uri($notice);
128 if (!$notice->update($orig)) {
129 common_log_db_error($notice, 'UPDATE', __FILE__);
130 return _('Problem saving notice.');
134 # XXX: do we need to change this for remote users?
136 common_save_replies($notice);
139 # Clear the cache for subscribed users, so they'll update at next request
140 # XXX: someone clever could prepend instead of clearing the cache
142 if (common_config('memcached', 'enabled')) {
143 $notice->blowCaches();
146 $notice->addToInboxes();
150 function blowCaches($blowLast=false) {
151 $this->blowSubsCache($blowLast);
152 $this->blowNoticeCache($blowLast);
153 $this->blowRepliesCache($blowLast);
154 $this->blowPublicCache($blowLast);
155 $this->blowTagCache($blowLast);
158 function blowTagCache($blowLast=false) {
159 $cache = common_memcache();
161 $tag = new Notice_tag();
162 $tag->notice_id = $this->id;
164 while ($tag->fetch()) {
165 $cache->delete(common_cache_key('notice_tag:notice_stream:' . $tag->tag));
167 $cache->delete(common_cache_key('notice_tag:notice_stream:' . $tag->tag . ';last'));
176 function blowSubsCache($blowLast=false) {
177 $cache = common_memcache();
181 $user->query('SELECT id ' .
182 'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
183 'WHERE subscription.subscribed = ' . $this->profile_id);
185 while ($user->fetch()) {
186 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
188 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id . ';last'));
196 function blowNoticeCache($blowLast=false) {
197 if ($this->is_local) {
198 $cache = common_memcache();
200 $cache->delete(common_cache_key('user:notices:'.$this->profile_id));
202 $cache->delete(common_cache_key('user:notices:'.$this->profile_id.';last'));
208 function blowRepliesCache($blowLast=false) {
209 $cache = common_memcache();
211 $reply = new Reply();
212 $reply->notice_id = $this->id;
213 if ($reply->find()) {
214 while ($reply->fetch()) {
215 $cache->delete(common_cache_key('user:replies:'.$reply->profile_id));
217 $cache->delete(common_cache_key('user:replies:'.$reply->profile_id.';last'));
226 function blowPublicCache($blowLast=false) {
227 if ($this->is_local == 1) {
228 $cache = common_memcache();
230 $cache->delete(common_cache_key('public'));
232 $cache->delete(common_cache_key('public').';last');
238 function blowFavesCache($blowLast=false) {
239 $cache = common_memcache();
242 $fave->notice_id = $this->id;
244 while ($fave->fetch()) {
245 $cache->delete(common_cache_key('user:faves:'.$fave->user_id));
247 $cache->delete(common_cache_key('user:faves:'.$fave->user_id.';last'));
256 # XXX: too many args; we need to move to named params or even a separate
257 # class for notice streams
259 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $before_id=0, $order=NULL) {
261 if (common_config('memcached', 'enabled')) {
263 # Skip the cache if this is a since_id or before_id qry
264 if ($since_id > 0 || $before_id > 0) {
265 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order);
267 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
271 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order);
274 static function getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order) {
279 if (preg_match('/\bWHERE\b/i', $qry)) {
293 $qry .= ' notice.id > ' . $since_id;
296 if ($before_id > 0) {
305 $qry .= ' notice.id < ' . $before_id;
308 # Allow ORDER override
313 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
316 if (common_config('db','type') == 'pgsql') {
317 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
319 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
322 $notice = new Notice();
324 $notice->query($qry);
329 # XXX: this is pretty long and should probably be broken up into
330 # some helper functions
332 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
334 # If outside our cache window, just go to the DB
336 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
337 return Notice::getStreamDirect($qry, $offset, $limit, NULL, NULL, $order);
340 # Get the cache; if we can't, just go to the DB
342 $cache = common_memcache();
345 return Notice::getStreamDirect($qry, $offset, $limit, NULL, NULL, $order);
348 # Get the notices out of the cache
350 $notices = $cache->get(common_cache_key($cachekey));
352 # On a cache hit, return a DB-object-like wrapper
354 if ($notices !== FALSE) {
355 $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
359 # If the cache was invalidated because of new data being
360 # added, we can try and just get the new stuff. We keep an additional
361 # copy of the data at the key + ';last'
363 # No cache hit. Try to get the *last* cached version
365 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
369 # Reverse-chron order, so last ID is last.
371 $last_id = $last_notices[0]->id;
373 # XXX: this assumes monotonically increasing IDs; a fair
376 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
377 $last_id, NULL, $order);
380 $new_notices = array();
381 while ($new_notice->fetch()) {
382 $new_notices[] = clone($new_notice);
385 $notices = array_slice(array_merge($new_notices, $last_notices),
386 0, NOTICE_CACHE_WINDOW);
388 # Store the array in the cache for next time
390 $result = $cache->set(common_cache_key($cachekey), $notices);
391 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
393 # return a wrapper of the array for use now
395 return new NoticeWrapper(array_slice($notices, $offset, $limit));
399 # Otherwise, get the full cache window out of the DB
401 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, NULL, NULL, $order);
403 # If there are no hits, just return the value
409 # Pack results into an array
413 while ($notice->fetch()) {
414 $notices[] = clone($notice);
419 # Store the array in the cache for next time
421 $result = $cache->set(common_cache_key($cachekey), $notices);
422 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
424 # return a wrapper of the array for use now
426 $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
431 function publicStream($offset=0, $limit=20, $since_id=0, $before_id=0) {
435 $qry = 'SELECT * FROM notice ';
437 if (common_config('public', 'localonly')) {
438 $parts[] = 'is_local = 1';
441 $parts[] = 'is_local != -1';
445 $qry .= ' WHERE ' . implode(' AND ', $parts);
448 return Notice::getStream($qry,
450 $offset, $limit, $since_id, $before_id);
453 function addToInboxes() {
454 $enabled = common_config('inboxes', 'enabled');
456 if ($enabled === true || $enabled === 'transitional') {
457 $inbox = new Notice_inbox();
458 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
459 'SELECT user.id, ' . $this->id . ', "' . $this->created . '" ' .
460 'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
461 'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
462 'AND NOT EXISTS (SELECT user_id, notice_id ' .
463 'FROM notice_inbox ' .
464 'WHERE user_id = user.id ' .
465 'AND notice_id = ' . $this->id . ' )';
466 if ($enabled === 'transitional') {
467 $qry .= ' AND user.inboxed = 1';
474 # Delete from inboxes if we're deleted.
476 function blowInboxes() {
478 $enabled = common_config('inboxes', 'enabled');
480 if ($enabled === true || $enabled === 'transitional') {
481 $inbox = new Notice_inbox();
482 $inbox->notice_id = $this->id;