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)
49 public $conversation; // int(4)
52 function staticGet($k,$v=NULL) {
53 return Memcached_DataObject::staticGet('Notice',$k,$v);
56 /* the code above is auto generated do not remove the tag below */
61 return Profile::staticGet('id', $this->profile_id);
66 $this->blowCaches(true);
67 $this->blowFavesCache(true);
68 $this->blowSubsCache(true);
70 $this->query('BEGIN');
71 //Null any notices that are replies to this notice
72 $this->query(sprintf("UPDATE notice set reply_to = null WHERE reply_to = %d", $this->id));
73 $related = array('Reply',
78 if (common_config('inboxes', 'enabled')) {
79 $related[] = 'Notice_inbox';
81 foreach ($related as $cls) {
83 $inst->notice_id = $this->id;
86 $result = parent::delete();
87 $this->query('COMMIT');
92 /* extract all #hastags */
93 $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match);
98 /* Add them to the database */
99 foreach(array_unique($match[1]) as $hashtag) {
100 /* elide characters we don't want in the tag */
101 $this->saveTag($hashtag);
106 function saveTag($hashtag)
108 $hashtag = common_canonical_tag($hashtag);
110 $tag = new Notice_tag();
111 $tag->notice_id = $this->id;
112 $tag->tag = $hashtag;
113 $tag->created = $this->created;
114 $id = $tag->insert();
117 throw new ServerException(sprintf(_('DB error inserting hashtag: %s'),
118 $last_error->message));
123 static function saveNew($profile_id, $content, $source=null,
124 $is_local=1, $reply_to=null, $uri=null, $created=null) {
126 $profile = Profile::staticGet($profile_id);
128 $final = common_shorten_links($content);
130 if (mb_strlen($final) > 140) {
131 common_log(LOG_INFO, 'Rejecting notice that is too long.');
132 return _('Problem saving notice. Too long.');
136 common_log(LOG_ERR, 'Problem saving notice. Unknown user.');
137 return _('Problem saving notice. Unknown user.');
140 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
141 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
142 return _('Too many notices too fast; take a breather and post again in a few minutes.');
145 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
146 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
147 return _('Too many duplicate messages too quickly; take a breather and post again in a few minutes.');
150 $banned = common_config('profile', 'banned');
152 if ( in_array($profile_id, $banned) || in_array($profile->nickname, $banned)) {
153 common_log(LOG_WARNING, "Attempted post from banned user: $profile->nickname (user id = $profile_id).");
154 return _('You are banned from posting notices on this site.');
157 $notice = new Notice();
158 $notice->profile_id = $profile_id;
160 $blacklist = common_config('public', 'blacklist');
161 $autosource = common_config('public', 'autosource');
163 # Blacklisted are non-false, but not 1, either
165 if (($blacklist && in_array($profile_id, $blacklist)) ||
166 ($source && $autosource && in_array($source, $autosource))) {
167 $notice->is_local = -1;
169 $notice->is_local = $is_local;
172 $notice->query('BEGIN');
174 $notice->reply_to = $reply_to;
175 if (!empty($created)) {
176 $notice->created = $created;
178 $notice->created = common_sql_now();
180 $notice->content = $final;
181 $notice->rendered = common_render_content($final, $notice);
182 $notice->source = $source;
185 if (!empty($reply_to)) {
186 $reply_notice = Notice::staticGet('id', $reply_to);
187 if (!empty($reply_notice)) {
188 $notice->reply_to = $reply_to;
189 $notice->conversation = $reply_notice->conversation;
193 if (Event::handle('StartNoticeSave', array(&$notice))) {
195 $id = $notice->insert();
198 common_log_db_error($notice, 'INSERT', __FILE__);
199 return _('Problem saving notice.');
202 # Update the URI after the notice is in the database
204 $orig = clone($notice);
205 $notice->uri = common_notice_uri($notice);
207 if (!$notice->update($orig)) {
208 common_log_db_error($notice, 'UPDATE', __FILE__);
209 return _('Problem saving notice.');
213 # XXX: do we need to change this for remote users?
215 $notice->saveReplies();
218 $notice->addToInboxes();
219 $notice->saveGroups();
221 $notice->query('COMMIT');
223 Event::handle('EndNoticeSave', array($notice));
226 # Clear the cache for subscribed users, so they'll update at next request
227 # XXX: someone clever could prepend instead of clearing the cache
229 $notice->blowCaches();
234 static function checkDupes($profile_id, $content) {
235 $profile = Profile::staticGet($profile_id);
239 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
242 while ($notice->fetch()) {
243 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
245 } else if ($notice->content == $content) {
250 # If we get here, oldest item in cache window is not
251 # old enough for dupe limit; do direct check against DB
252 $notice = new Notice();
253 $notice->profile_id = $profile_id;
254 $notice->content = $content;
255 if (common_config('db','type') == 'pgsql')
256 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
258 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
260 $cnt = $notice->count();
264 static function checkEditThrottle($profile_id) {
265 $profile = Profile::staticGet($profile_id);
270 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
271 if ($notice && $notice->fetch()) {
272 # If the Nth notice was posted less than timespan seconds ago
273 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
278 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
282 function getUploadedAttachment() {
284 $query = 'select file.url as up, file.id as i from file join file_to_post on file.id = file_id where post_id=' . $post->escape($post->id) . ' and url like "%/notice/%/file"';
285 $post->query($query);
287 if (empty($post->up) || empty($post->i)) {
290 $ret = array($post->up, $post->i);
296 function hasAttachments() {
298 $query = "select count(file_id) as n_attachments from file join file_to_post on (file_id = file.id) join notice on (post_id = notice.id) where post_id = " . $post->escape($post->id);
299 $post->query($query);
301 $n_attachments = intval($post->n_attachments);
303 return $n_attachments;
306 function blowCaches($blowLast=false)
308 $this->blowSubsCache($blowLast);
309 $this->blowNoticeCache($blowLast);
310 $this->blowRepliesCache($blowLast);
311 $this->blowPublicCache($blowLast);
312 $this->blowTagCache($blowLast);
313 $this->blowGroupCache($blowLast);
316 function blowGroupCache($blowLast=false)
318 $cache = common_memcache();
320 $group_inbox = new Group_inbox();
321 $group_inbox->notice_id = $this->id;
322 if ($group_inbox->find()) {
323 while ($group_inbox->fetch()) {
324 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id));
326 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id.';last'));
328 $member = new Group_member();
329 $member->group_id = $group_inbox->group_id;
330 if ($member->find()) {
331 while ($member->fetch()) {
332 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
334 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
340 $group_inbox->free();
345 function blowTagCache($blowLast=false)
347 $cache = common_memcache();
349 $tag = new Notice_tag();
350 $tag->notice_id = $this->id;
352 while ($tag->fetch()) {
353 $tag->blowCache($blowLast);
361 function blowSubsCache($blowLast=false)
363 $cache = common_memcache();
367 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
368 $user->query('SELECT id ' .
370 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
371 'WHERE subscription.subscribed = ' . $this->profile_id);
373 while ($user->fetch()) {
374 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id));
376 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id.';last'));
384 function blowNoticeCache($blowLast=false)
386 if ($this->is_local) {
387 $cache = common_memcache();
388 if (!empty($cache)) {
389 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
391 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
397 function blowRepliesCache($blowLast=false)
399 $cache = common_memcache();
401 $reply = new Reply();
402 $reply->notice_id = $this->id;
403 if ($reply->find()) {
404 while ($reply->fetch()) {
405 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
407 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
416 function blowPublicCache($blowLast=false)
418 if ($this->is_local == 1) {
419 $cache = common_memcache();
421 $cache->delete(common_cache_key('public'));
423 $cache->delete(common_cache_key('public').';last');
429 function blowFavesCache($blowLast=false)
431 $cache = common_memcache();
434 $fave->notice_id = $this->id;
436 while ($fave->fetch()) {
437 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
439 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
448 # XXX: too many args; we need to move to named params or even a separate
449 # class for notice streams
451 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $order=null, $since=null) {
453 if (common_config('memcached', 'enabled')) {
455 # Skip the cache if this is a since, since_id or max_id qry
456 if ($since_id > 0 || $max_id > 0 || $since) {
457 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
459 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
463 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
466 static function getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since) {
471 if (preg_match('/\bWHERE\b/i', $qry)) {
485 $qry .= ' notice.id > ' . $since_id;
497 $qry .= ' notice.id <= ' . $max_id;
509 $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
512 # Allow ORDER override
517 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
520 if (common_config('db','type') == 'pgsql') {
521 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
523 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
526 $notice = new Notice();
528 $notice->query($qry);
533 # XXX: this is pretty long and should probably be broken up into
534 # some helper functions
536 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
538 # If outside our cache window, just go to the DB
540 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
541 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
544 # Get the cache; if we can't, just go to the DB
546 $cache = common_memcache();
549 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
552 # Get the notices out of the cache
554 $notices = $cache->get(common_cache_key($cachekey));
556 # On a cache hit, return a DB-object-like wrapper
558 if ($notices !== false) {
559 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
563 # If the cache was invalidated because of new data being
564 # added, we can try and just get the new stuff. We keep an additional
565 # copy of the data at the key + ';last'
567 # No cache hit. Try to get the *last* cached version
569 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
573 # Reverse-chron order, so last ID is last.
575 $last_id = $last_notices[0]->id;
577 # XXX: this assumes monotonically increasing IDs; a fair
580 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
581 $last_id, null, $order, null);
584 $new_notices = array();
585 while ($new_notice->fetch()) {
586 $new_notices[] = clone($new_notice);
589 $notices = array_slice(array_merge($new_notices, $last_notices),
590 0, NOTICE_CACHE_WINDOW);
592 # Store the array in the cache for next time
594 $result = $cache->set(common_cache_key($cachekey), $notices);
595 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
597 # return a wrapper of the array for use now
599 return new ArrayWrapper(array_slice($notices, $offset, $limit));
603 # Otherwise, get the full cache window out of the DB
605 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
607 # If there are no hits, just return the value
613 # Pack results into an array
617 while ($notice->fetch()) {
618 $notices[] = clone($notice);
623 # Store the array in the cache for next time
625 $result = $cache->set(common_cache_key($cachekey), $notices);
626 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
628 # return a wrapper of the array for use now
630 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
635 function getStreamByIds($ids)
637 $cache = common_memcache();
639 if (!empty($cache)) {
641 foreach ($ids as $id) {
642 $notices[] = Notice::staticGet('id', $id);
644 return new ArrayWrapper($notices);
646 $notice = new Notice();
647 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
648 $notice->orderBy('id DESC');
655 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
657 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
660 $offset, $limit, $since_id, $max_id, $since);
662 return Notice::getStreamByIds($ids);
665 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
667 $notice = new Notice();
669 $notice->selectAdd(); // clears it
670 $notice->selectAdd('id');
672 $notice->orderBy('id DESC');
674 if (!is_null($offset)) {
675 $notice->limit($offset, $limit);
678 if (common_config('public', 'localonly')) {
679 $notice->whereAdd('is_local = 1');
682 $notice->whereAdd('is_local != -1');
685 if ($since_id != 0) {
686 $notice->whereAdd('id > ' . $since_id);
690 $notice->whereAdd('id <= ' . $max_id);
693 if (!is_null($since)) {
694 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
699 if ($notice->find()) {
700 while ($notice->fetch()) {
701 $ids[] = $notice->id;
711 function addToInboxes()
713 $enabled = common_config('inboxes', 'enabled');
715 if ($enabled === true || $enabled === 'transitional') {
716 $inbox = new Notice_inbox();
717 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
718 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
719 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "' " .
720 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
721 'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
722 'AND NOT EXISTS (SELECT user_id, notice_id ' .
723 'FROM notice_inbox ' .
724 "WHERE user_id = $UT.id " .
725 'AND notice_id = ' . $this->id . ' )';
726 if ($enabled === 'transitional') {
727 $qry .= " AND $UT.inboxed = 1";
734 function saveGroups()
736 $enabled = common_config('inboxes', 'enabled');
737 if ($enabled !== true && $enabled !== 'transitional') {
741 /* extract all !group */
742 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
743 strtolower($this->content),
749 $profile = $this->getProfile();
751 /* Add them to the database */
753 foreach (array_unique($match[1]) as $nickname) {
754 /* XXX: remote groups. */
755 $group = User_group::getForNickname($nickname);
761 // we automatically add a tag for every group name, too
763 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
764 'notice_id' => $this->id));
767 $this->saveTag($nickname);
770 if ($profile->isMember($group)) {
772 $gi = new Group_inbox();
774 $gi->group_id = $group->id;
775 $gi->notice_id = $this->id;
776 $gi->created = common_sql_now();
778 $result = $gi->insert();
781 common_log_db_error($gi, 'INSERT', __FILE__);
784 // FIXME: do this in an offline daemon
786 $this->addToGroupInboxes($group);
791 function addToGroupInboxes($group)
793 $inbox = new Notice_inbox();
794 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
795 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
796 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "', 2 " .
797 "FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " .
798 'WHERE group_member.group_id = ' . $group->id . ' ' .
799 'AND NOT EXISTS (SELECT user_id, notice_id ' .
800 'FROM notice_inbox ' .
801 "WHERE user_id = $UT.id " .
802 'AND notice_id = ' . $this->id . ' )';
803 if ($enabled === 'transitional') {
804 $qry .= " AND $UT.inboxed = 1";
806 $result = $inbox->query($qry);
809 function saveReplies()
811 // Alternative reply format
813 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
816 // extract all @messages
817 $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
821 if ($cnt || $tname) {
822 // XXX: is there another way to make an array copy?
823 $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
826 $sender = Profile::staticGet($this->profile_id);
830 // store replied only for first @ (what user/notice what the reply directed,
831 // we assume first @ is it)
833 for ($i=0; $i<count($names); $i++) {
834 $nickname = $names[$i];
835 $recipient = common_relative_profile($sender, $nickname, $this->created);
839 if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
840 $reply_for = $recipient;
841 $recipient_notice = $reply_for->getCurrentNotice();
842 if ($recipient_notice) {
843 $orig = clone($this);
844 $this->reply_to = $recipient_notice->id;
845 $this->conversation = $recipient_notice->conversation;
846 $this->update($orig);
849 // Don't save replies from blocked profile to local user
850 $recipient_user = User::staticGet('id', $recipient->id);
851 if ($recipient_user && $recipient_user->hasBlocked($sender)) {
854 $reply = new Reply();
855 $reply->notice_id = $this->id;
856 $reply->profile_id = $recipient->id;
857 $id = $reply->insert();
859 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
860 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
861 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
864 $replied[$recipient->id] = 1;
868 // Hash format replies, too
869 $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
871 foreach ($match[1] as $tag) {
872 $tagged = Profile_tag::getTagged($sender->id, $tag);
873 foreach ($tagged as $t) {
874 if (!$replied[$t->id]) {
875 // Don't save replies from blocked profile to local user
876 $t_user = User::staticGet('id', $t->id);
877 if ($t_user && $t_user->hasBlocked($sender)) {
880 $reply = new Reply();
881 $reply->notice_id = $this->id;
882 $reply->profile_id = $t->id;
883 $id = $reply->insert();
885 common_log_db_error($reply, 'INSERT', __FILE__);
888 $replied[$recipient->id] = 1;
895 // If it's not a reply, make it the root of a new conversation
897 if (empty($this->conversation)) {
898 $orig = clone($this);
899 $this->conversation = $this->id;
900 $this->update($orig);
903 foreach (array_keys($replied) as $recipient) {
904 $user = User::staticGet('id', $recipient);
906 mail_notify_attn($user, $this);
911 function asAtomEntry($namespace=false, $source=false)
913 $profile = $this->getProfile();
915 $xs = new XMLStringer(true);
918 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
919 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
924 $xs->elementStart('entry', $attrs);
927 $xs->elementStart('source');
928 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
929 $xs->element('link', array('href' => $profile->profileurl));
930 $user = User::staticGet('id', $profile->id);
932 $atom_feed = common_local_url('api',
933 array('apiaction' => 'statuses',
934 'method' => 'user_timeline',
935 'argument' => $profile->nickname.'.atom'));
936 $xs->element('link', array('rel' => 'self',
937 'type' => 'application/atom+xml',
938 'href' => $profile->profileurl));
939 $xs->element('link', array('rel' => 'license',
940 'href' => common_config('license', 'url')));
943 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
946 $xs->elementStart('author');
947 $xs->element('name', null, $profile->nickname);
948 $xs->element('uri', null, $profile->profileurl);
949 $xs->elementEnd('author');
952 $xs->elementEnd('source');
955 $xs->element('title', null, $this->content);
956 $xs->element('summary', null, $this->content);
958 $xs->element('link', array('rel' => 'alternate',
959 'href' => $this->bestUrl()));
961 $xs->element('id', null, $this->uri);
963 $xs->element('published', null, common_date_w3dtf($this->created));
964 $xs->element('updated', null, common_date_w3dtf($this->modified));
966 if ($this->reply_to) {
967 $reply_notice = Notice::staticGet('id', $this->reply_to);
968 if (!empty($reply_notice)) {
969 $xs->element('link', array('rel' => 'related',
970 'href' => $reply_notice->bestUrl()));
971 $xs->element('thr:in-reply-to',
972 array('ref' => $reply_notice->uri,
973 'href' => $reply_notice->bestUrl()));
977 $xs->element('content', array('type' => 'html'), $this->rendered);
979 $tag = new Notice_tag();
980 $tag->notice_id = $this->id;
982 while ($tag->fetch()) {
983 $xs->element('category', array('term' => $tag->tag));
988 $xs->elementEnd('entry');
990 return $xs->getString();
995 if (!empty($this->url)) {
997 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
1000 return common_local_url('shownotice',
1001 array('notice' => $this->id));
1005 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
1007 $cache = common_memcache();
1009 if (empty($cache) ||
1010 $since_id != 0 || $max_id != 0 || (!is_null($since) && $since > 0) ||
1011 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1012 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1016 $idkey = common_cache_key($cachekey);
1018 $idstr = $cache->get($idkey);
1020 if (!empty($idstr)) {
1021 // Cache hit! Woohoo!
1022 $window = explode(',', $idstr);
1023 $ids = array_slice($window, $offset, $limit);
1027 $laststr = $cache->get($idkey.';last');
1029 if (!empty($laststr)) {
1030 $window = explode(',', $laststr);
1031 $last_id = $window[0];
1032 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1033 $last_id, 0, null, $tag)));
1035 $new_window = array_merge($new_ids, $window);
1037 $new_windowstr = implode(',', $new_window);
1039 $result = $cache->set($idkey, $new_windowstr);
1040 $result = $cache->set($idkey . ';last', $new_windowstr);
1042 $ids = array_slice($new_window, $offset, $limit);
1047 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1048 0, 0, null, $tag)));
1050 $windowstr = implode(',', $window);
1052 $result = $cache->set($idkey, $windowstr);
1053 $result = $cache->set($idkey . ';last', $windowstr);
1055 $ids = array_slice($window, $offset, $limit);