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, $is_local=1, $reply_to=null, $uri=null) {
125 $profile = Profile::staticGet($profile_id);
127 $final = common_shorten_links($content);
130 common_log(LOG_ERR, 'Problem saving notice. Unknown user.');
131 return _('Problem saving notice. Unknown user.');
134 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
135 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
136 return _('Too many notices too fast; take a breather and post again in a few minutes.');
139 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
140 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
141 return _('Too many duplicate messages too quickly; take a breather and post again in a few minutes.');
144 $banned = common_config('profile', 'banned');
146 if ( in_array($profile_id, $banned) || in_array($profile->nickname, $banned)) {
147 common_log(LOG_WARNING, "Attempted post from banned user: $profile->nickname (user id = $profile_id).");
148 return _('You are banned from posting notices on this site.');
151 $notice = new Notice();
152 $notice->profile_id = $profile_id;
154 $blacklist = common_config('public', 'blacklist');
155 $autosource = common_config('public', 'autosource');
157 # Blacklisted are non-false, but not 1, either
159 if (($blacklist && in_array($profile_id, $blacklist)) ||
160 ($source && $autosource && in_array($source, $autosource))) {
161 $notice->is_local = -1;
163 $notice->is_local = $is_local;
166 $notice->query('BEGIN');
168 $notice->reply_to = $reply_to;
169 $notice->created = common_sql_now();
170 $notice->content = $final;
171 $notice->rendered = common_render_content($final, $notice);
172 $notice->source = $source;
175 if (!empty($reply_to)) {
176 $reply_notice = Notice::staticGet('id', $reply_to);
177 if (!empty($reply_notice)) {
178 $notice->reply_to = $reply_to;
179 $notice->conversation = $reply_notice->conversation;
183 if (Event::handle('StartNoticeSave', array(&$notice))) {
185 $id = $notice->insert();
188 common_log_db_error($notice, 'INSERT', __FILE__);
189 return _('Problem saving notice.');
192 # Update the URI after the notice is in the database
194 $orig = clone($notice);
195 $notice->uri = common_notice_uri($notice);
197 if (!$notice->update($orig)) {
198 common_log_db_error($notice, 'UPDATE', __FILE__);
199 return _('Problem saving notice.');
203 # XXX: do we need to change this for remote users?
205 $notice->saveReplies();
208 $notice->addToInboxes();
209 $notice->saveGroups();
211 $notice->query('COMMIT');
213 Event::handle('EndNoticeSave', array($notice));
216 # Clear the cache for subscribed users, so they'll update at next request
217 # XXX: someone clever could prepend instead of clearing the cache
219 $notice->blowCaches();
224 static function checkDupes($profile_id, $content) {
225 $profile = Profile::staticGet($profile_id);
229 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
232 while ($notice->fetch()) {
233 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
235 } else if ($notice->content == $content) {
240 # If we get here, oldest item in cache window is not
241 # old enough for dupe limit; do direct check against DB
242 $notice = new Notice();
243 $notice->profile_id = $profile_id;
244 $notice->content = $content;
245 if (common_config('db','type') == 'pgsql')
246 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
248 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
250 $cnt = $notice->count();
254 static function checkEditThrottle($profile_id) {
255 $profile = Profile::staticGet($profile_id);
260 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
261 if ($notice && $notice->fetch()) {
262 # If the Nth notice was posted less than timespan seconds ago
263 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
268 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
272 function getUploadedAttachment() {
274 $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"';
275 $post->query($query);
277 if (empty($post->up) || empty($post->i)) {
280 $ret = array($post->up, $post->i);
286 function hasAttachments() {
288 $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);
289 $post->query($query);
291 $n_attachments = intval($post->n_attachments);
293 return $n_attachments;
296 function blowCaches($blowLast=false)
298 $this->blowSubsCache($blowLast);
299 $this->blowNoticeCache($blowLast);
300 $this->blowRepliesCache($blowLast);
301 $this->blowPublicCache($blowLast);
302 $this->blowTagCache($blowLast);
303 $this->blowGroupCache($blowLast);
306 function blowGroupCache($blowLast=false)
308 $cache = common_memcache();
310 $group_inbox = new Group_inbox();
311 $group_inbox->notice_id = $this->id;
312 if ($group_inbox->find()) {
313 while ($group_inbox->fetch()) {
314 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id));
316 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id.';last'));
318 $member = new Group_member();
319 $member->group_id = $group_inbox->group_id;
320 if ($member->find()) {
321 while ($member->fetch()) {
322 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
324 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
330 $group_inbox->free();
335 function blowTagCache($blowLast=false)
337 $cache = common_memcache();
339 $tag = new Notice_tag();
340 $tag->notice_id = $this->id;
342 while ($tag->fetch()) {
343 $tag->blowCache($blowLast);
351 function blowSubsCache($blowLast=false)
353 $cache = common_memcache();
357 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
358 $user->query('SELECT id ' .
360 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
361 'WHERE subscription.subscribed = ' . $this->profile_id);
363 while ($user->fetch()) {
364 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id));
366 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id.';last'));
374 function blowNoticeCache($blowLast=false)
376 if ($this->is_local) {
377 $cache = common_memcache();
378 if (!empty($cache)) {
379 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
381 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
387 function blowRepliesCache($blowLast=false)
389 $cache = common_memcache();
391 $reply = new Reply();
392 $reply->notice_id = $this->id;
393 if ($reply->find()) {
394 while ($reply->fetch()) {
395 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
397 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
406 function blowPublicCache($blowLast=false)
408 if ($this->is_local == 1) {
409 $cache = common_memcache();
411 $cache->delete(common_cache_key('public'));
413 $cache->delete(common_cache_key('public').';last');
419 function blowFavesCache($blowLast=false)
421 $cache = common_memcache();
424 $fave->notice_id = $this->id;
426 while ($fave->fetch()) {
427 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
429 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
438 # XXX: too many args; we need to move to named params or even a separate
439 # class for notice streams
441 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $order=null, $since=null) {
443 if (common_config('memcached', 'enabled')) {
445 # Skip the cache if this is a since, since_id or max_id qry
446 if ($since_id > 0 || $max_id > 0 || $since) {
447 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
449 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
453 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
456 static function getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since) {
461 if (preg_match('/\bWHERE\b/i', $qry)) {
475 $qry .= ' notice.id > ' . $since_id;
487 $qry .= ' notice.id <= ' . $max_id;
499 $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
502 # Allow ORDER override
507 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
510 if (common_config('db','type') == 'pgsql') {
511 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
513 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
516 $notice = new Notice();
518 $notice->query($qry);
523 # XXX: this is pretty long and should probably be broken up into
524 # some helper functions
526 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
528 # If outside our cache window, just go to the DB
530 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
531 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
534 # Get the cache; if we can't, just go to the DB
536 $cache = common_memcache();
539 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
542 # Get the notices out of the cache
544 $notices = $cache->get(common_cache_key($cachekey));
546 # On a cache hit, return a DB-object-like wrapper
548 if ($notices !== false) {
549 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
553 # If the cache was invalidated because of new data being
554 # added, we can try and just get the new stuff. We keep an additional
555 # copy of the data at the key + ';last'
557 # No cache hit. Try to get the *last* cached version
559 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
563 # Reverse-chron order, so last ID is last.
565 $last_id = $last_notices[0]->id;
567 # XXX: this assumes monotonically increasing IDs; a fair
570 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
571 $last_id, null, $order, null);
574 $new_notices = array();
575 while ($new_notice->fetch()) {
576 $new_notices[] = clone($new_notice);
579 $notices = array_slice(array_merge($new_notices, $last_notices),
580 0, NOTICE_CACHE_WINDOW);
582 # Store the array in the cache for next time
584 $result = $cache->set(common_cache_key($cachekey), $notices);
585 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
587 # return a wrapper of the array for use now
589 return new ArrayWrapper(array_slice($notices, $offset, $limit));
593 # Otherwise, get the full cache window out of the DB
595 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
597 # If there are no hits, just return the value
603 # Pack results into an array
607 while ($notice->fetch()) {
608 $notices[] = clone($notice);
613 # Store the array in the cache for next time
615 $result = $cache->set(common_cache_key($cachekey), $notices);
616 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
618 # return a wrapper of the array for use now
620 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
625 function getStreamByIds($ids)
627 $cache = common_memcache();
629 if (!empty($cache)) {
631 foreach ($ids as $id) {
632 $notices[] = Notice::staticGet('id', $id);
634 return new ArrayWrapper($notices);
636 $notice = new Notice();
637 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
638 $notice->orderBy('id DESC');
645 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
647 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
650 $offset, $limit, $since_id, $max_id, $since);
652 return Notice::getStreamByIds($ids);
655 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
657 $notice = new Notice();
659 $notice->selectAdd(); // clears it
660 $notice->selectAdd('id');
662 $notice->orderBy('id DESC');
664 if (!is_null($offset)) {
665 $notice->limit($offset, $limit);
668 if (common_config('public', 'localonly')) {
669 $notice->whereAdd('is_local = 1');
672 $notice->whereAdd('is_local != -1');
675 if ($since_id != 0) {
676 $notice->whereAdd('id > ' . $since_id);
680 $notice->whereAdd('id <= ' . $max_id);
683 if (!is_null($since)) {
684 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
689 if ($notice->find()) {
690 while ($notice->fetch()) {
691 $ids[] = $notice->id;
701 function addToInboxes()
703 $enabled = common_config('inboxes', 'enabled');
705 if ($enabled === true || $enabled === 'transitional') {
706 $inbox = new Notice_inbox();
707 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
708 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
709 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "' " .
710 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
711 'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
712 'AND NOT EXISTS (SELECT user_id, notice_id ' .
713 'FROM notice_inbox ' .
714 "WHERE user_id = $UT.id " .
715 'AND notice_id = ' . $this->id . ' )';
716 if ($enabled === 'transitional') {
717 $qry .= " AND $UT.inboxed = 1";
724 function saveGroups()
726 $enabled = common_config('inboxes', 'enabled');
727 if ($enabled !== true && $enabled !== 'transitional') {
731 /* extract all !group */
732 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
733 strtolower($this->content),
739 $profile = $this->getProfile();
741 /* Add them to the database */
743 foreach (array_unique($match[1]) as $nickname) {
744 /* XXX: remote groups. */
745 $group = User_group::staticGet('nickname', $nickname);
751 // we automatically add a tag for every group name, too
753 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
754 'notice_id' => $this->id));
757 $this->saveTag($nickname);
760 if ($profile->isMember($group)) {
762 $gi = new Group_inbox();
764 $gi->group_id = $group->id;
765 $gi->notice_id = $this->id;
766 $gi->created = common_sql_now();
768 $result = $gi->insert();
771 common_log_db_error($gi, 'INSERT', __FILE__);
774 // FIXME: do this in an offline daemon
776 $this->addToGroupInboxes($group);
781 function addToGroupInboxes($group)
783 $inbox = new Notice_inbox();
784 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
785 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
786 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "', 2 " .
787 "FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " .
788 'WHERE group_member.group_id = ' . $group->id . ' ' .
789 'AND NOT EXISTS (SELECT user_id, notice_id ' .
790 'FROM notice_inbox ' .
791 "WHERE user_id = $UT.id " .
792 'AND notice_id = ' . $this->id . ' )';
793 if ($enabled === 'transitional') {
794 $qry .= " AND $UT.inboxed = 1";
796 $result = $inbox->query($qry);
799 function saveReplies()
801 // Alternative reply format
803 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
806 // extract all @messages
807 $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
811 if ($cnt || $tname) {
812 // XXX: is there another way to make an array copy?
813 $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
816 $sender = Profile::staticGet($this->profile_id);
820 // store replied only for first @ (what user/notice what the reply directed,
821 // we assume first @ is it)
823 for ($i=0; $i<count($names); $i++) {
824 $nickname = $names[$i];
825 $recipient = common_relative_profile($sender, $nickname, $this->created);
829 if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
830 $reply_for = $recipient;
831 $recipient_notice = $reply_for->getCurrentNotice();
832 if ($recipient_notice) {
833 $orig = clone($this);
834 $this->reply_to = $recipient_notice->id;
835 $this->conversation = $recipient_notice->conversation;
836 $this->update($orig);
839 // Don't save replies from blocked profile to local user
840 $recipient_user = User::staticGet('id', $recipient->id);
841 if ($recipient_user && $recipient_user->hasBlocked($sender)) {
844 $reply = new Reply();
845 $reply->notice_id = $this->id;
846 $reply->profile_id = $recipient->id;
847 $id = $reply->insert();
849 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
850 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
851 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
854 $replied[$recipient->id] = 1;
858 // Hash format replies, too
859 $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
861 foreach ($match[1] as $tag) {
862 $tagged = Profile_tag::getTagged($sender->id, $tag);
863 foreach ($tagged as $t) {
864 if (!$replied[$t->id]) {
865 // Don't save replies from blocked profile to local user
866 $t_user = User::staticGet('id', $t->id);
867 if ($t_user && $t_user->hasBlocked($sender)) {
870 $reply = new Reply();
871 $reply->notice_id = $this->id;
872 $reply->profile_id = $t->id;
873 $id = $reply->insert();
875 common_log_db_error($reply, 'INSERT', __FILE__);
878 $replied[$recipient->id] = 1;
885 // If it's not a reply, make it the root of a new conversation
887 if (empty($this->conversation)) {
888 $orig = clone($this);
889 $this->conversation = $this->id;
890 $this->update($orig);
893 foreach (array_keys($replied) as $recipient) {
894 $user = User::staticGet('id', $recipient);
896 mail_notify_attn($user, $this);
901 function asAtomEntry($namespace=false, $source=false)
903 $profile = $this->getProfile();
905 $xs = new XMLStringer(true);
908 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
909 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
914 $xs->elementStart('entry', $attrs);
917 $xs->elementStart('source');
918 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
919 $xs->element('link', array('href' => $profile->profileurl));
920 $user = User::staticGet('id', $profile->id);
922 $atom_feed = common_local_url('api',
923 array('apiaction' => 'statuses',
924 'method' => 'user_timeline',
925 'argument' => $profile->nickname.'.atom'));
926 $xs->element('link', array('rel' => 'self',
927 'type' => 'application/atom+xml',
928 'href' => $profile->profileurl));
929 $xs->element('link', array('rel' => 'license',
930 'href' => common_config('license', 'url')));
933 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
936 $xs->elementStart('author');
937 $xs->element('name', null, $profile->nickname);
938 $xs->element('uri', null, $profile->profileurl);
939 $xs->elementEnd('author');
942 $xs->elementEnd('source');
945 $xs->element('title', null, $this->content);
946 $xs->element('summary', null, $this->content);
948 $xs->element('link', array('rel' => 'alternate',
949 'href' => $this->bestUrl()));
951 $xs->element('id', null, $this->uri);
953 $xs->element('published', null, common_date_w3dtf($this->created));
954 $xs->element('updated', null, common_date_w3dtf($this->modified));
956 if ($this->reply_to) {
957 $reply_notice = Notice::staticGet('id', $this->reply_to);
958 if (!empty($reply_notice)) {
959 $xs->element('link', array('rel' => 'related',
960 'href' => $reply_notice->bestUrl()));
961 $xs->element('thr:in-reply-to',
962 array('ref' => $reply_notice->uri,
963 'href' => $reply_notice->bestUrl()));
967 $xs->element('content', array('type' => 'html'), $this->rendered);
969 $tag = new Notice_tag();
970 $tag->notice_id = $this->id;
972 while ($tag->fetch()) {
973 $xs->element('category', array('term' => $tag->tag));
978 $xs->elementEnd('entry');
980 return $xs->getString();
985 if (!empty($this->url)) {
987 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
990 return common_local_url('shownotice',
991 array('notice' => $this->id));
995 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
997 $cache = common_memcache();
1000 $since_id != 0 || $max_id != 0 || (!is_null($since) && $since > 0) ||
1001 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1002 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1006 $idkey = common_cache_key($cachekey);
1008 $idstr = $cache->get($idkey);
1010 if (!empty($idstr)) {
1011 // Cache hit! Woohoo!
1012 $window = explode(',', $idstr);
1013 $ids = array_slice($window, $offset, $limit);
1017 $laststr = $cache->get($idkey.';last');
1019 if (!empty($laststr)) {
1020 $window = explode(',', $laststr);
1021 $last_id = $window[0];
1022 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1023 $last_id, 0, null, $tag)));
1025 $new_window = array_merge($new_ids, $window);
1027 $new_windowstr = implode(',', $new_window);
1029 $result = $cache->set($idkey, $new_windowstr);
1030 $result = $cache->set($idkey . ';last', $new_windowstr);
1032 $ids = array_slice($new_window, $offset, $limit);
1037 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1038 0, 0, null, $tag)));
1040 $windowstr = implode(',', $window);
1042 $result = $cache->set($idkey, $windowstr);
1043 $result = $cache->set($idkey . ';last', $windowstr);
1045 $ids = array_slice($window, $offset, $limit);