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);
131 common_log(LOG_ERR, 'Problem saving notice. Unknown user.');
132 return _('Problem saving notice. Unknown user.');
135 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
136 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
137 return _('Too many notices too fast; take a breather and post again in a few minutes.');
140 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
141 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
142 return _('Too many duplicate messages too quickly; take a breather and post again in a few minutes.');
145 $banned = common_config('profile', 'banned');
147 if ( in_array($profile_id, $banned) || in_array($profile->nickname, $banned)) {
148 common_log(LOG_WARNING, "Attempted post from banned user: $profile->nickname (user id = $profile_id).");
149 return _('You are banned from posting notices on this site.');
152 $notice = new Notice();
153 $notice->profile_id = $profile_id;
155 $blacklist = common_config('public', 'blacklist');
156 $autosource = common_config('public', 'autosource');
158 # Blacklisted are non-false, but not 1, either
160 if (($blacklist && in_array($profile_id, $blacklist)) ||
161 ($source && $autosource && in_array($source, $autosource))) {
162 $notice->is_local = -1;
164 $notice->is_local = $is_local;
167 $notice->query('BEGIN');
169 $notice->reply_to = $reply_to;
170 if (!empty($created)) {
171 $notice->created = $created;
173 $notice->created = common_sql_now();
175 $notice->content = $final;
176 $notice->rendered = common_render_content($final, $notice);
177 $notice->source = $source;
180 if (!empty($reply_to)) {
181 $reply_notice = Notice::staticGet('id', $reply_to);
182 if (!empty($reply_notice)) {
183 $notice->reply_to = $reply_to;
184 $notice->conversation = $reply_notice->conversation;
188 if (Event::handle('StartNoticeSave', array(&$notice))) {
190 $id = $notice->insert();
193 common_log_db_error($notice, 'INSERT', __FILE__);
194 return _('Problem saving notice.');
197 # Update the URI after the notice is in the database
199 $orig = clone($notice);
200 $notice->uri = common_notice_uri($notice);
202 if (!$notice->update($orig)) {
203 common_log_db_error($notice, 'UPDATE', __FILE__);
204 return _('Problem saving notice.');
208 # XXX: do we need to change this for remote users?
210 $notice->saveReplies();
213 $notice->addToInboxes();
214 $notice->saveGroups();
216 $notice->query('COMMIT');
218 Event::handle('EndNoticeSave', array($notice));
221 # Clear the cache for subscribed users, so they'll update at next request
222 # XXX: someone clever could prepend instead of clearing the cache
224 $notice->blowCaches();
229 static function checkDupes($profile_id, $content) {
230 $profile = Profile::staticGet($profile_id);
234 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
237 while ($notice->fetch()) {
238 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
240 } else if ($notice->content == $content) {
245 # If we get here, oldest item in cache window is not
246 # old enough for dupe limit; do direct check against DB
247 $notice = new Notice();
248 $notice->profile_id = $profile_id;
249 $notice->content = $content;
250 if (common_config('db','type') == 'pgsql')
251 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
253 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
255 $cnt = $notice->count();
259 static function checkEditThrottle($profile_id) {
260 $profile = Profile::staticGet($profile_id);
265 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
266 if ($notice && $notice->fetch()) {
267 # If the Nth notice was posted less than timespan seconds ago
268 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
273 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
277 function getUploadedAttachment() {
279 $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"';
280 $post->query($query);
282 if (empty($post->up) || empty($post->i)) {
285 $ret = array($post->up, $post->i);
291 function hasAttachments() {
293 $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);
294 $post->query($query);
296 $n_attachments = intval($post->n_attachments);
298 return $n_attachments;
301 function blowCaches($blowLast=false)
303 $this->blowSubsCache($blowLast);
304 $this->blowNoticeCache($blowLast);
305 $this->blowRepliesCache($blowLast);
306 $this->blowPublicCache($blowLast);
307 $this->blowTagCache($blowLast);
308 $this->blowGroupCache($blowLast);
311 function blowGroupCache($blowLast=false)
313 $cache = common_memcache();
315 $group_inbox = new Group_inbox();
316 $group_inbox->notice_id = $this->id;
317 if ($group_inbox->find()) {
318 while ($group_inbox->fetch()) {
319 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id));
321 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id.';last'));
323 $member = new Group_member();
324 $member->group_id = $group_inbox->group_id;
325 if ($member->find()) {
326 while ($member->fetch()) {
327 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
329 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
335 $group_inbox->free();
340 function blowTagCache($blowLast=false)
342 $cache = common_memcache();
344 $tag = new Notice_tag();
345 $tag->notice_id = $this->id;
347 while ($tag->fetch()) {
348 $tag->blowCache($blowLast);
356 function blowSubsCache($blowLast=false)
358 $cache = common_memcache();
362 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
363 $user->query('SELECT id ' .
365 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
366 'WHERE subscription.subscribed = ' . $this->profile_id);
368 while ($user->fetch()) {
369 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id));
371 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id.';last'));
379 function blowNoticeCache($blowLast=false)
381 if ($this->is_local) {
382 $cache = common_memcache();
383 if (!empty($cache)) {
384 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
386 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
392 function blowRepliesCache($blowLast=false)
394 $cache = common_memcache();
396 $reply = new Reply();
397 $reply->notice_id = $this->id;
398 if ($reply->find()) {
399 while ($reply->fetch()) {
400 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
402 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
411 function blowPublicCache($blowLast=false)
413 if ($this->is_local == 1) {
414 $cache = common_memcache();
416 $cache->delete(common_cache_key('public'));
418 $cache->delete(common_cache_key('public').';last');
424 function blowFavesCache($blowLast=false)
426 $cache = common_memcache();
429 $fave->notice_id = $this->id;
431 while ($fave->fetch()) {
432 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
434 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
443 # XXX: too many args; we need to move to named params or even a separate
444 # class for notice streams
446 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $order=null, $since=null) {
448 if (common_config('memcached', 'enabled')) {
450 # Skip the cache if this is a since, since_id or max_id qry
451 if ($since_id > 0 || $max_id > 0 || $since) {
452 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
454 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
458 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
461 static function getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since) {
466 if (preg_match('/\bWHERE\b/i', $qry)) {
480 $qry .= ' notice.id > ' . $since_id;
492 $qry .= ' notice.id <= ' . $max_id;
504 $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
507 # Allow ORDER override
512 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
515 if (common_config('db','type') == 'pgsql') {
516 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
518 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
521 $notice = new Notice();
523 $notice->query($qry);
528 # XXX: this is pretty long and should probably be broken up into
529 # some helper functions
531 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
533 # If outside our cache window, just go to the DB
535 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
536 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
539 # Get the cache; if we can't, just go to the DB
541 $cache = common_memcache();
544 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
547 # Get the notices out of the cache
549 $notices = $cache->get(common_cache_key($cachekey));
551 # On a cache hit, return a DB-object-like wrapper
553 if ($notices !== false) {
554 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
558 # If the cache was invalidated because of new data being
559 # added, we can try and just get the new stuff. We keep an additional
560 # copy of the data at the key + ';last'
562 # No cache hit. Try to get the *last* cached version
564 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
568 # Reverse-chron order, so last ID is last.
570 $last_id = $last_notices[0]->id;
572 # XXX: this assumes monotonically increasing IDs; a fair
575 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
576 $last_id, null, $order, null);
579 $new_notices = array();
580 while ($new_notice->fetch()) {
581 $new_notices[] = clone($new_notice);
584 $notices = array_slice(array_merge($new_notices, $last_notices),
585 0, NOTICE_CACHE_WINDOW);
587 # Store the array in the cache for next time
589 $result = $cache->set(common_cache_key($cachekey), $notices);
590 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
592 # return a wrapper of the array for use now
594 return new ArrayWrapper(array_slice($notices, $offset, $limit));
598 # Otherwise, get the full cache window out of the DB
600 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
602 # If there are no hits, just return the value
608 # Pack results into an array
612 while ($notice->fetch()) {
613 $notices[] = clone($notice);
618 # Store the array in the cache for next time
620 $result = $cache->set(common_cache_key($cachekey), $notices);
621 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
623 # return a wrapper of the array for use now
625 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
630 function getStreamByIds($ids)
632 $cache = common_memcache();
634 if (!empty($cache)) {
636 foreach ($ids as $id) {
637 $notices[] = Notice::staticGet('id', $id);
639 return new ArrayWrapper($notices);
641 $notice = new Notice();
642 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
643 $notice->orderBy('id DESC');
650 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
652 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
655 $offset, $limit, $since_id, $max_id, $since);
657 return Notice::getStreamByIds($ids);
660 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
662 $notice = new Notice();
664 $notice->selectAdd(); // clears it
665 $notice->selectAdd('id');
667 $notice->orderBy('id DESC');
669 if (!is_null($offset)) {
670 $notice->limit($offset, $limit);
673 if (common_config('public', 'localonly')) {
674 $notice->whereAdd('is_local = 1');
677 $notice->whereAdd('is_local != -1');
680 if ($since_id != 0) {
681 $notice->whereAdd('id > ' . $since_id);
685 $notice->whereAdd('id <= ' . $max_id);
688 if (!is_null($since)) {
689 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
694 if ($notice->find()) {
695 while ($notice->fetch()) {
696 $ids[] = $notice->id;
706 function addToInboxes()
708 $enabled = common_config('inboxes', 'enabled');
710 if ($enabled === true || $enabled === 'transitional') {
711 $inbox = new Notice_inbox();
712 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
713 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
714 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "' " .
715 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
716 'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
717 'AND NOT EXISTS (SELECT user_id, notice_id ' .
718 'FROM notice_inbox ' .
719 "WHERE user_id = $UT.id " .
720 'AND notice_id = ' . $this->id . ' )';
721 if ($enabled === 'transitional') {
722 $qry .= " AND $UT.inboxed = 1";
729 function saveGroups()
731 $enabled = common_config('inboxes', 'enabled');
732 if ($enabled !== true && $enabled !== 'transitional') {
736 /* extract all !group */
737 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
738 strtolower($this->content),
744 $profile = $this->getProfile();
746 /* Add them to the database */
748 foreach (array_unique($match[1]) as $nickname) {
749 /* XXX: remote groups. */
750 $group = User_group::staticGet('nickname', $nickname);
756 // we automatically add a tag for every group name, too
758 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
759 'notice_id' => $this->id));
762 $this->saveTag($nickname);
765 if ($profile->isMember($group)) {
767 $gi = new Group_inbox();
769 $gi->group_id = $group->id;
770 $gi->notice_id = $this->id;
771 $gi->created = common_sql_now();
773 $result = $gi->insert();
776 common_log_db_error($gi, 'INSERT', __FILE__);
779 // FIXME: do this in an offline daemon
781 $this->addToGroupInboxes($group);
786 function addToGroupInboxes($group)
788 $inbox = new Notice_inbox();
789 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
790 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
791 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "', 2 " .
792 "FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " .
793 'WHERE group_member.group_id = ' . $group->id . ' ' .
794 'AND NOT EXISTS (SELECT user_id, notice_id ' .
795 'FROM notice_inbox ' .
796 "WHERE user_id = $UT.id " .
797 'AND notice_id = ' . $this->id . ' )';
798 if ($enabled === 'transitional') {
799 $qry .= " AND $UT.inboxed = 1";
801 $result = $inbox->query($qry);
804 function saveReplies()
806 // Alternative reply format
808 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
811 // extract all @messages
812 $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
816 if ($cnt || $tname) {
817 // XXX: is there another way to make an array copy?
818 $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
821 $sender = Profile::staticGet($this->profile_id);
825 // store replied only for first @ (what user/notice what the reply directed,
826 // we assume first @ is it)
828 for ($i=0; $i<count($names); $i++) {
829 $nickname = $names[$i];
830 $recipient = common_relative_profile($sender, $nickname, $this->created);
834 if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
835 $reply_for = $recipient;
836 $recipient_notice = $reply_for->getCurrentNotice();
837 if ($recipient_notice) {
838 $orig = clone($this);
839 $this->reply_to = $recipient_notice->id;
840 $this->conversation = $recipient_notice->conversation;
841 $this->update($orig);
844 // Don't save replies from blocked profile to local user
845 $recipient_user = User::staticGet('id', $recipient->id);
846 if ($recipient_user && $recipient_user->hasBlocked($sender)) {
849 $reply = new Reply();
850 $reply->notice_id = $this->id;
851 $reply->profile_id = $recipient->id;
852 $id = $reply->insert();
854 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
855 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
856 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
859 $replied[$recipient->id] = 1;
863 // Hash format replies, too
864 $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
866 foreach ($match[1] as $tag) {
867 $tagged = Profile_tag::getTagged($sender->id, $tag);
868 foreach ($tagged as $t) {
869 if (!$replied[$t->id]) {
870 // Don't save replies from blocked profile to local user
871 $t_user = User::staticGet('id', $t->id);
872 if ($t_user && $t_user->hasBlocked($sender)) {
875 $reply = new Reply();
876 $reply->notice_id = $this->id;
877 $reply->profile_id = $t->id;
878 $id = $reply->insert();
880 common_log_db_error($reply, 'INSERT', __FILE__);
883 $replied[$recipient->id] = 1;
890 // If it's not a reply, make it the root of a new conversation
892 if (empty($this->conversation)) {
893 $orig = clone($this);
894 $this->conversation = $this->id;
895 $this->update($orig);
898 foreach (array_keys($replied) as $recipient) {
899 $user = User::staticGet('id', $recipient);
901 mail_notify_attn($user, $this);
906 function asAtomEntry($namespace=false, $source=false)
908 $profile = $this->getProfile();
910 $xs = new XMLStringer(true);
913 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
914 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
919 $xs->elementStart('entry', $attrs);
922 $xs->elementStart('source');
923 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
924 $xs->element('link', array('href' => $profile->profileurl));
925 $user = User::staticGet('id', $profile->id);
927 $atom_feed = common_local_url('api',
928 array('apiaction' => 'statuses',
929 'method' => 'user_timeline',
930 'argument' => $profile->nickname.'.atom'));
931 $xs->element('link', array('rel' => 'self',
932 'type' => 'application/atom+xml',
933 'href' => $profile->profileurl));
934 $xs->element('link', array('rel' => 'license',
935 'href' => common_config('license', 'url')));
938 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
941 $xs->elementStart('author');
942 $xs->element('name', null, $profile->nickname);
943 $xs->element('uri', null, $profile->profileurl);
944 $xs->elementEnd('author');
947 $xs->elementEnd('source');
950 $xs->element('title', null, $this->content);
951 $xs->element('summary', null, $this->content);
953 $xs->element('link', array('rel' => 'alternate',
954 'href' => $this->bestUrl()));
956 $xs->element('id', null, $this->uri);
958 $xs->element('published', null, common_date_w3dtf($this->created));
959 $xs->element('updated', null, common_date_w3dtf($this->modified));
961 if ($this->reply_to) {
962 $reply_notice = Notice::staticGet('id', $this->reply_to);
963 if (!empty($reply_notice)) {
964 $xs->element('link', array('rel' => 'related',
965 'href' => $reply_notice->bestUrl()));
966 $xs->element('thr:in-reply-to',
967 array('ref' => $reply_notice->uri,
968 'href' => $reply_notice->bestUrl()));
972 $xs->element('content', array('type' => 'html'), $this->rendered);
974 $tag = new Notice_tag();
975 $tag->notice_id = $this->id;
977 while ($tag->fetch()) {
978 $xs->element('category', array('term' => $tag->tag));
983 $xs->elementEnd('entry');
985 return $xs->getString();
990 if (!empty($this->url)) {
992 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
995 return common_local_url('shownotice',
996 array('notice' => $this->id));
1000 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
1002 $cache = common_memcache();
1004 if (empty($cache) ||
1005 $since_id != 0 || $max_id != 0 || (!is_null($since) && $since > 0) ||
1006 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1007 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1011 $idkey = common_cache_key($cachekey);
1013 $idstr = $cache->get($idkey);
1015 if (!empty($idstr)) {
1016 // Cache hit! Woohoo!
1017 $window = explode(',', $idstr);
1018 $ids = array_slice($window, $offset, $limit);
1022 $laststr = $cache->get($idkey.';last');
1024 if (!empty($laststr)) {
1025 $window = explode(',', $laststr);
1026 $last_id = $window[0];
1027 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1028 $last_id, 0, null, $tag)));
1030 $new_window = array_merge($new_ids, $window);
1032 $new_windowstr = implode(',', $new_window);
1034 $result = $cache->set($idkey, $new_windowstr);
1035 $result = $cache->set($idkey . ';last', $new_windowstr);
1037 $ids = array_slice($new_window, $offset, $limit);
1042 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1043 0, 0, null, $tag)));
1045 $windowstr = implode(',', $window);
1047 $result = $cache->set($idkey, $windowstr);
1048 $result = $cache->set($idkey . ';last', $windowstr);
1050 $ids = array_slice($window, $offset, $limit);