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) {
52 return Memcached_DataObject::staticGet('Notice',$k,$v);
55 /* the code above is auto generated do not remove the tag below */
60 return Profile::staticGet('id', $this->profile_id);
65 $this->blowCaches(true);
66 $this->blowFavesCache(true);
67 $this->blowSubsCache(true);
69 $this->query('BEGIN');
70 //Null any notices that are replies to this notice
71 $this->query(sprintf("UPDATE notice set reply_to = null WHERE reply_to = %d", $this->id));
72 $related = array('Reply',
77 if (common_config('inboxes', 'enabled')) {
78 $related[] = 'Notice_inbox';
80 foreach ($related as $cls) {
82 $inst->notice_id = $this->id;
85 $result = parent::delete();
86 $this->query('COMMIT');
91 /* extract all #hastags */
92 $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match);
97 /* Add them to the database */
98 foreach(array_unique($match[1]) as $hashtag) {
99 /* elide characters we don't want in the tag */
100 $this->saveTag($hashtag);
105 function saveTag($hashtag)
107 $hashtag = common_canonical_tag($hashtag);
109 $tag = new Notice_tag();
110 $tag->notice_id = $this->id;
111 $tag->tag = $hashtag;
112 $tag->created = $this->created;
113 $id = $tag->insert();
116 throw new ServerException(sprintf(_('DB error inserting hashtag: %s'),
117 $last_error->message));
122 static function saveNew($profile_id, $content, $source=null, $is_local=1, $reply_to=null, $uri=null) {
124 $profile = Profile::staticGet($profile_id);
126 $final = common_shorten_links($content);
129 common_log(LOG_ERR, 'Problem saving notice. Unknown user.');
130 return _('Problem saving notice. Unknown user.');
133 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
134 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
135 return _('Too many notices too fast; take a breather and post again in a few minutes.');
138 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
139 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
140 return _('Too many duplicate messages too quickly; take a breather and post again in a few minutes.');
143 $banned = common_config('profile', 'banned');
145 if ( in_array($profile_id, $banned) || in_array($profile->nickname, $banned)) {
146 common_log(LOG_WARNING, "Attempted post from banned user: $profile->nickname (user id = $profile_id).");
147 return _('You are banned from posting notices on this site.');
150 $notice = new Notice();
151 $notice->profile_id = $profile_id;
153 $blacklist = common_config('public', 'blacklist');
154 $autosource = common_config('public', 'autosource');
156 # Blacklisted are non-false, but not 1, either
158 if (($blacklist && in_array($profile_id, $blacklist)) ||
159 ($source && $autosource && in_array($source, $autosource))) {
160 $notice->is_local = -1;
162 $notice->is_local = $is_local;
165 $notice->query('BEGIN');
167 $notice->reply_to = $reply_to;
168 $notice->created = common_sql_now();
169 $notice->content = $final;
170 $notice->rendered = common_render_content($final, $notice);
171 $notice->source = $source;
174 if (Event::handle('StartNoticeSave', array(&$notice))) {
176 $id = $notice->insert();
179 common_log_db_error($notice, 'INSERT', __FILE__);
180 return _('Problem saving notice.');
183 # Update the URI after the notice is in the database
185 $orig = clone($notice);
186 $notice->uri = common_notice_uri($notice);
188 if (!$notice->update($orig)) {
189 common_log_db_error($notice, 'UPDATE', __FILE__);
190 return _('Problem saving notice.');
194 # XXX: do we need to change this for remote users?
196 $notice->saveReplies();
198 $notice->saveGroups();
200 if (common_config('queue', 'enabled')) {
201 $notice->addToAuthorInbox();
203 $notice->addToInboxes();
206 $notice->query('COMMIT');
208 Event::handle('EndNoticeSave', array($notice));
211 # Clear the cache for subscribed users, so they'll update at next request
212 # XXX: someone clever could prepend instead of clearing the cache
214 if (common_config('memcached', 'enabled')) {
215 if (common_config('queue', 'enabled')) {
216 $notice->blowAuthorCaches();
218 $notice->blowCaches();
225 static function checkDupes($profile_id, $content) {
226 $profile = Profile::staticGet($profile_id);
230 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
233 while ($notice->fetch()) {
234 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
236 } else if ($notice->content == $content) {
241 # If we get here, oldest item in cache window is not
242 # old enough for dupe limit; do direct check against DB
243 $notice = new Notice();
244 $notice->profile_id = $profile_id;
245 $notice->content = $content;
246 if (common_config('db','type') == 'pgsql')
247 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
249 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
251 $cnt = $notice->count();
255 static function checkEditThrottle($profile_id) {
256 $profile = Profile::staticGet($profile_id);
261 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
262 if ($notice && $notice->fetch()) {
263 # If the Nth notice was posted less than timespan seconds ago
264 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
269 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
273 function blowCaches($blowLast=false)
275 $this->blowSubsCache($blowLast);
276 $this->blowNoticeCache($blowLast);
277 $this->blowRepliesCache($blowLast);
278 $this->blowPublicCache($blowLast);
279 $this->blowTagCache($blowLast);
280 $this->blowGroupCache($blowLast);
283 function blowAuthorCaches($blowLast=false)
285 // Clear the user's cache
286 $cache = common_memcache();
287 if (!empty($cache)) {
288 $cache->delete(common_cache_key('notice_inbox:by_user:'.$this->profile_id));
290 $this->blowNoticeCache($blowLast);
291 $this->blowPublicCache($blowLast);
294 function blowGroupCache($blowLast=false)
296 $cache = common_memcache();
298 $group_inbox = new Group_inbox();
299 $group_inbox->notice_id = $this->id;
300 if ($group_inbox->find()) {
301 while ($group_inbox->fetch()) {
302 $cache->delete(common_cache_key('group:notices:'.$group_inbox->group_id));
304 $cache->delete(common_cache_key('group:notices:'.$group_inbox->group_id.';last'));
306 $member = new Group_member();
307 $member->group_id = $group_inbox->group_id;
308 if ($member->find()) {
309 while ($member->fetch()) {
310 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
312 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
318 $group_inbox->free();
323 function blowTagCache($blowLast=false)
325 $cache = common_memcache();
327 $tag = new Notice_tag();
328 $tag->notice_id = $this->id;
330 while ($tag->fetch()) {
331 $tag->blowCache($blowLast);
339 function blowSubsCache($blowLast=false)
341 $cache = common_memcache();
345 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
346 $user->query('SELECT id ' .
348 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
349 'WHERE subscription.subscribed = ' . $this->profile_id);
351 while ($user->fetch()) {
352 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id));
354 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id.';last'));
362 function blowNoticeCache($blowLast=false)
364 if ($this->is_local) {
365 $cache = common_memcache();
366 if (!empty($cache)) {
367 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
369 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
375 function blowRepliesCache($blowLast=false)
377 $cache = common_memcache();
379 $reply = new Reply();
380 $reply->notice_id = $this->id;
381 if ($reply->find()) {
382 while ($reply->fetch()) {
383 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
385 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
394 function blowPublicCache($blowLast=false)
396 if ($this->is_local == 1) {
397 $cache = common_memcache();
399 $cache->delete(common_cache_key('public'));
401 $cache->delete(common_cache_key('public').';last');
407 function blowFavesCache($blowLast=false)
409 $cache = common_memcache();
412 $fave->notice_id = $this->id;
414 while ($fave->fetch()) {
415 $cache->delete(common_cache_key('user:faves:'.$fave->user_id));
417 $cache->delete(common_cache_key('user:faves:'.$fave->user_id.';last'));
426 # XXX: too many args; we need to move to named params or even a separate
427 # class for notice streams
429 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $before_id=0, $order=null, $since=null) {
431 if (common_config('memcached', 'enabled')) {
433 # Skip the cache if this is a since, since_id or before_id qry
434 if ($since_id > 0 || $before_id > 0 || $since) {
435 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order, $since);
437 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
441 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order, $since);
444 static function getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order, $since) {
449 if (preg_match('/\bWHERE\b/i', $qry)) {
463 $qry .= ' notice.id > ' . $since_id;
466 if ($before_id > 0) {
475 $qry .= ' notice.id < ' . $before_id;
487 $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
490 # Allow ORDER override
495 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
498 if (common_config('db','type') == 'pgsql') {
499 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
501 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
504 $notice = new Notice();
506 $notice->query($qry);
511 # XXX: this is pretty long and should probably be broken up into
512 # some helper functions
514 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
516 # If outside our cache window, just go to the DB
518 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
519 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
522 # Get the cache; if we can't, just go to the DB
524 $cache = common_memcache();
527 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
530 # Get the notices out of the cache
532 $notices = $cache->get(common_cache_key($cachekey));
534 # On a cache hit, return a DB-object-like wrapper
536 if ($notices !== false) {
537 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
541 # If the cache was invalidated because of new data being
542 # added, we can try and just get the new stuff. We keep an additional
543 # copy of the data at the key + ';last'
545 # No cache hit. Try to get the *last* cached version
547 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
551 # Reverse-chron order, so last ID is last.
553 $last_id = $last_notices[0]->id;
555 # XXX: this assumes monotonically increasing IDs; a fair
558 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
559 $last_id, null, $order, null);
562 $new_notices = array();
563 while ($new_notice->fetch()) {
564 $new_notices[] = clone($new_notice);
567 $notices = array_slice(array_merge($new_notices, $last_notices),
568 0, NOTICE_CACHE_WINDOW);
570 # Store the array in the cache for next time
572 $result = $cache->set(common_cache_key($cachekey), $notices);
573 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
575 # return a wrapper of the array for use now
577 return new ArrayWrapper(array_slice($notices, $offset, $limit));
581 # Otherwise, get the full cache window out of the DB
583 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
585 # If there are no hits, just return the value
591 # Pack results into an array
595 while ($notice->fetch()) {
596 $notices[] = clone($notice);
601 # Store the array in the cache for next time
603 $result = $cache->set(common_cache_key($cachekey), $notices);
604 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
606 # return a wrapper of the array for use now
608 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
613 function getStreamByIds($ids)
615 $cache = common_memcache();
617 if (!empty($cache)) {
619 foreach ($ids as $id) {
620 $notices[] = Notice::staticGet('id', $id);
622 return new ArrayWrapper($notices);
624 $notice = new Notice();
625 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
626 $notice->orderBy('id DESC');
633 function publicStream($offset=0, $limit=20, $since_id=0, $before_id=0, $since=null)
635 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
638 $offset, $limit, $since_id, $before_id, $since);
640 return Notice::getStreamByIds($ids);
643 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $before_id=0, $since=null)
645 $notice = new Notice();
647 $notice->selectAdd(); // clears it
648 $notice->selectAdd('id');
650 $notice->orderBy('id DESC');
652 if (!is_null($offset)) {
653 $notice->limit($offset, $limit);
656 if (common_config('public', 'localonly')) {
657 $notice->whereAdd('is_local = 1');
660 $notice->whereAdd('is_local != -1');
663 if ($since_id != 0) {
664 $notice->whereAdd('id > ' . $since_id);
667 if ($before_id != 0) {
668 $notice->whereAdd('id < ' . $before_id);
671 if (!is_null($since)) {
672 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
677 if ($notice->find()) {
678 while ($notice->fetch()) {
679 $ids[] = $notice->id;
689 function addToInboxes()
691 $enabled = common_config('inboxes', 'enabled');
693 if ($enabled === true || $enabled === 'transitional') {
694 $inbox = new Notice_inbox();
695 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
696 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
697 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "' " .
698 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
699 'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
700 'AND NOT EXISTS (SELECT user_id, notice_id ' .
701 'FROM notice_inbox ' .
702 "WHERE user_id = $UT.id " .
703 'AND notice_id = ' . $this->id . ' )';
704 if ($enabled === 'transitional') {
705 $qry .= " AND $UT.inboxed = 1";
712 function addToAuthorInbox()
714 $enabled = common_config('inboxes', 'enabled');
716 if ($enabled === true || $enabled === 'transitional') {
717 $user = User::staticGet('id', $this->profile_id);
721 $inbox = new Notice_inbox();
722 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
723 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
724 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "' " .
726 "WHERE $UT.id = " . $this->profile_id . ' ' .
727 'AND NOT EXISTS (SELECT user_id, notice_id ' .
728 'FROM notice_inbox ' .
729 "WHERE user_id = " . $this->profile_id . ' '.
730 'AND notice_id = ' . $this->id . ' )';
731 if ($enabled === 'transitional') {
732 $qry .= " AND $UT.inboxed = 1";
739 function saveGroups()
741 $enabled = common_config('inboxes', 'enabled');
742 if ($enabled !== true && $enabled !== 'transitional') {
746 /* extract all !group */
747 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
748 strtolower($this->content),
754 $profile = $this->getProfile();
756 /* Add them to the database */
758 foreach (array_unique($match[1]) as $nickname) {
759 /* XXX: remote groups. */
760 $group = User_group::staticGet('nickname', $nickname);
766 // we automatically add a tag for every group name, too
768 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
769 'notice_id' => $this->id));
772 $this->saveTag($nickname);
775 if ($profile->isMember($group)) {
777 $gi = new Group_inbox();
779 $gi->group_id = $group->id;
780 $gi->notice_id = $this->id;
781 $gi->created = common_sql_now();
783 $result = $gi->insert();
786 common_log_db_error($gi, 'INSERT', __FILE__);
789 // FIXME: do this in an offline daemon
791 $this->addToGroupInboxes($group);
796 function addToGroupInboxes($group)
798 $inbox = new Notice_inbox();
799 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
800 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
801 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "', 2 " .
802 "FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " .
803 'WHERE group_member.group_id = ' . $group->id . ' ' .
804 'AND NOT EXISTS (SELECT user_id, notice_id ' .
805 'FROM notice_inbox ' .
806 "WHERE user_id = $UT.id " .
807 'AND notice_id = ' . $this->id . ' )';
808 if ($enabled === 'transitional') {
809 $qry .= " AND $UT.inboxed = 1";
811 $result = $inbox->query($qry);
814 function saveReplies()
816 // Alternative reply format
818 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
821 // extract all @messages
822 $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
826 if ($cnt || $tname) {
827 // XXX: is there another way to make an array copy?
828 $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
831 $sender = Profile::staticGet($this->profile_id);
835 // store replied only for first @ (what user/notice what the reply directed,
836 // we assume first @ is it)
838 for ($i=0; $i<count($names); $i++) {
839 $nickname = $names[$i];
840 $recipient = common_relative_profile($sender, $nickname, $this->created);
844 if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
845 $reply_for = $recipient;
846 $recipient_notice = $reply_for->getCurrentNotice();
847 if ($recipient_notice) {
848 $orig = clone($this);
849 $this->reply_to = $recipient_notice->id;
850 $this->update($orig);
853 // Don't save replies from blocked profile to local user
854 $recipient_user = User::staticGet('id', $recipient->id);
855 if ($recipient_user && $recipient_user->hasBlocked($sender)) {
858 $reply = new Reply();
859 $reply->notice_id = $this->id;
860 $reply->profile_id = $recipient->id;
861 $id = $reply->insert();
863 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
864 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
865 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
868 $replied[$recipient->id] = 1;
872 // Hash format replies, too
873 $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
875 foreach ($match[1] as $tag) {
876 $tagged = Profile_tag::getTagged($sender->id, $tag);
877 foreach ($tagged as $t) {
878 if (!$replied[$t->id]) {
879 // Don't save replies from blocked profile to local user
880 $t_user = User::staticGet('id', $t->id);
881 if ($t_user && $t_user->hasBlocked($sender)) {
884 $reply = new Reply();
885 $reply->notice_id = $this->id;
886 $reply->profile_id = $t->id;
887 $id = $reply->insert();
889 common_log_db_error($reply, 'INSERT', __FILE__);
892 $replied[$recipient->id] = 1;
899 foreach (array_keys($replied) as $recipient) {
900 $user = User::staticGet('id', $recipient);
902 mail_notify_attn($user, $this);
907 function asAtomEntry($namespace=false, $source=false)
909 $profile = $this->getProfile();
911 $xs = new XMLStringer(true);
914 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
915 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
920 $xs->elementStart('entry', $attrs);
923 $xs->elementStart('source');
924 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
925 $xs->element('link', array('href' => $profile->profileurl));
926 $user = User::staticGet('id', $profile->id);
928 $atom_feed = common_local_url('api',
929 array('apiaction' => 'statuses',
930 'method' => 'user_timeline',
931 'argument' => $profile->nickname.'.atom'));
932 $xs->element('link', array('rel' => 'self',
933 'type' => 'application/atom+xml',
934 'href' => $profile->profileurl));
935 $xs->element('link', array('rel' => 'license',
936 'href' => common_config('license', 'url')));
939 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
942 $xs->elementStart('author');
943 $xs->element('name', null, $profile->nickname);
944 $xs->element('uri', null, $profile->profileurl);
945 $xs->elementEnd('author');
948 $xs->elementEnd('source');
951 $xs->element('title', null, $this->content);
952 $xs->element('summary', null, $this->content);
954 $xs->element('link', array('rel' => 'alternate',
955 'href' => $this->bestUrl()));
957 $xs->element('id', null, $this->uri);
959 $xs->element('published', null, common_date_w3dtf($this->created));
960 $xs->element('updated', null, common_date_w3dtf($this->modified));
962 if ($this->reply_to) {
963 $reply_notice = Notice::staticGet('id', $this->reply_to);
964 if (!empty($reply_notice)) {
965 $xs->element('link', array('rel' => 'related',
966 'href' => $reply_notice->bestUrl()));
967 $xs->element('thr:in-reply-to',
968 array('ref' => $reply_notice->uri,
969 'href' => $reply_notice->bestUrl()));
973 $xs->element('content', array('type' => 'html'), $this->rendered);
975 $tag = new Notice_tag();
976 $tag->notice_id = $this->id;
978 while ($tag->fetch()) {
979 $xs->element('category', array('term' => $tag->tag));
984 $xs->elementEnd('entry');
986 return $xs->getString();
991 if (!empty($this->url)) {
993 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
996 return common_local_url('shownotice',
997 array('notice' => $this->id));
1001 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $before_id=0, $since=null)
1003 $cache = common_memcache();
1005 if (empty($cache) ||
1006 $since_id != 0 || $before_id != 0 || !is_null($since) ||
1007 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1008 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1009 $before_id, $since)));
1012 $idkey = common_cache_key($cachekey);
1014 $idstr = $cache->get($idkey);
1016 if (!empty($idstr)) {
1017 // Cache hit! Woohoo!
1018 $window = explode(',', $idstr);
1019 $ids = array_slice($window, $offset, $limit);
1023 $laststr = $cache->get($idkey.';last');
1025 if (!empty($laststr)) {
1026 $window = explode(',', $laststr);
1027 $last_id = $window[0];
1028 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1029 $last_id, 0, null)));
1031 $new_window = array_merge($new_ids, $window);
1033 $new_windowstr = implode(',', $new_window);
1035 $result = $cache->set($idkey, $new_windowstr);
1036 $result = $cache->set($idkey . ';last', $new_windowstr);
1038 $ids = array_slice($new_window, $offset, $limit);
1043 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1046 $windowstr = implode(',', $window);
1048 $result = $cache->set($idkey, $windowstr);
1049 $result = $cache->set($idkey . ';last', $windowstr);
1051 $ids = array_slice($window, $offset, $limit);