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($this->profile_id);
62 $this->blowFavesCache();
67 /* extract all #hastags */
68 $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match);
73 /* elide characters we don't want in the tag */
74 $match[1] = str_replace(array('-', '_', '.'), '', $match[1]);
76 /* Add them to the database */
77 foreach(array_unique($match[1]) as $hashtag) {
78 $tag = DB_DataObject::factory('Notice_tag');
79 $tag->notice_id = $this->id;
81 $tag->created = $this->created;
84 $last_error = PEAR::getStaticProperty('DB_DataObject','lastError');
85 common_log(LOG_ERR, 'DB error inserting hashtag: ' . $last_error->message);
86 common_server_error(sprintf(_('DB error inserting hashtag: %s'), $last_error->message));
93 static function saveNew($profile_id, $content, $source=NULL, $is_local=1, $reply_to=NULL, $uri=NULL) {
95 $notice = new Notice();
96 $notice->profile_id = $profile_id;
97 $notice->is_local = $is_local;
98 $notice->reply_to = $reply_to;
99 $notice->created = common_sql_now();
100 $notice->content = $content;
101 $notice->rendered = common_render_content($notice->content, $notice);
102 $notice->source = $source;
105 $id = $notice->insert();
108 common_log_db_error($notice, 'INSERT', __FILE__);
109 return _('Problem saving notice.');
112 # Update the URI after the notice is in the database
114 $orig = clone($notice);
115 $notice->uri = common_notice_uri($notice);
117 if (!$notice->update($orig)) {
118 common_log_db_error($notice, 'UPDATE', __FILE__);
119 return _('Problem saving notice.');
123 # XXX: do we need to change this for remote users?
125 common_save_replies($notice);
128 # Clear the cache for subscribed users, so they'll update at next request
129 # XXX: someone clever could prepend instead of clearing the cache
131 if (common_config('memcached', 'enabled')) {
132 $notice->blowCaches();
138 function blowCaches() {
139 $this->blowSubsCache();
140 $this->blowNoticeCache();
141 $this->blowRepliesCache();
142 $this->blowPublicCache();
143 $this->blowTagCache();
146 function blowTagCache() {
147 $cache = common_memcache();
149 $tag = new Notice_tag();
150 $tag->notice_id = $this->id;
152 while ($tag->fetch()) {
153 $cache->delete(common_cache_key('notice_tag:notice_stream:' . $tag->tag));
161 function blowSubsCache() {
162 $cache = common_memcache();
166 $user->query('SELECT id ' .
167 'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
168 'WHERE subscription.subscribed = ' . $this->profile_id);
170 while ($user->fetch()) {
171 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
179 function blowNoticeCache() {
180 if ($this->is_local) {
181 $cache = common_memcache();
183 $cache->delete(common_cache_key('user:notices:'.$this->profile_id));
188 function blowRepliesCache() {
189 $cache = common_memcache();
191 $reply = new Reply();
192 $reply->notice_id = $this->id;
193 if ($reply->find()) {
194 while ($reply->fetch()) {
195 $cache->delete(common_cache_key('user:replies:'.$reply->profile_id));
203 function blowPublicCache() {
204 if ($this->is_local) {
205 $cache = common_memcache();
207 $cache->delete(common_cache_key('public'));
212 function blowFavesCache() {
213 $cache = common_memcache();
216 $fave->notice_id = $this->id;
218 while ($fave->fetch()) {
219 $cache->delete(common_cache_key('user:faves:'.$fave->user_id));
227 static function getStream($qry, $cachekey, $offset=0, $limit=20) {
229 if (common_config('memcached', 'enabled')) {
230 return Notice::getCachedStream($qry, $cachekey, $offset, $limit);
232 return Notice::getStreamDirect($qry, $offset, $limit);
237 static function getStreamDirect($qry, $offset, $limit) {
239 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
241 if(common_config('db','type')=='pgsql') {
242 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
244 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
247 $notice = new Notice();
249 $notice->query($qry);
254 static function getCachedStream($qry, $cachekey, $offset, $limit) {
256 # If outside our cache window, just go to the DB
258 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
259 return Notice::getStreamDirect($qry, $offset, $limit);
262 # Get the cache; if we can't, just go to the DB
264 $cache = common_memcache();
268 return Notice::getStreamDirect($qry, $offset, $limit);
271 # Get the notices out of the cache
273 $notices = $cache->get(common_cache_key($cachekey));
275 # On a cache hit, return a DB-object-like wrapper
277 if ($notices !== FALSE) {
278 $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
282 # Otherwise, get the full cache window out of the DB
284 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW);
286 # If there are no hits, just return the value
292 # Pack results into an array
296 while ($notice->fetch()) {
297 $notices[] = clone($notice);
300 # Store the array in the cache for next time
302 $result = $cache->set(common_cache_key($cachekey), $notices);
304 # return a wrapper of the array for use now
306 $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
311 function publicStream($offset=0, $limit=20) {
313 $qry = 'SELECT * FROM notice ';
315 if (common_config('public', 'localonly')) {
316 $qry .= ' WHERE is_local = 1 ';
319 return Notice::getStream($qry,