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();
222 $notice->query('COMMIT');
224 Event::handle('EndNoticeSave', array($notice));
227 # Clear the cache for subscribed users, so they'll update at next request
228 # XXX: someone clever could prepend instead of clearing the cache
230 $notice->blowCaches();
235 /** save all urls in the notice to the db
237 * follow redirects and save all available file information
238 * (mimetype, date, size, oembed, etc.)
240 * @param class $notice Notice to pull URLs from
244 function saveUrls() {
245 common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
248 function saveUrl($data) {
249 list($url, $notice_id) = $data;
250 File::processNew($url, $notice_id);
253 static function checkDupes($profile_id, $content) {
254 $profile = Profile::staticGet($profile_id);
258 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
261 while ($notice->fetch()) {
262 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
264 } else if ($notice->content == $content) {
269 # If we get here, oldest item in cache window is not
270 # old enough for dupe limit; do direct check against DB
271 $notice = new Notice();
272 $notice->profile_id = $profile_id;
273 $notice->content = $content;
274 if (common_config('db','type') == 'pgsql')
275 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
277 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
279 $cnt = $notice->count();
283 static function checkEditThrottle($profile_id) {
284 $profile = Profile::staticGet($profile_id);
289 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
290 if ($notice && $notice->fetch()) {
291 # If the Nth notice was posted less than timespan seconds ago
292 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
297 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
301 function getUploadedAttachment() {
303 $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"';
304 $post->query($query);
306 if (empty($post->up) || empty($post->i)) {
309 $ret = array($post->up, $post->i);
315 function hasAttachments() {
317 $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);
318 $post->query($query);
320 $n_attachments = intval($post->n_attachments);
322 return $n_attachments;
325 function blowCaches($blowLast=false)
327 $this->blowSubsCache($blowLast);
328 $this->blowNoticeCache($blowLast);
329 $this->blowRepliesCache($blowLast);
330 $this->blowPublicCache($blowLast);
331 $this->blowTagCache($blowLast);
332 $this->blowGroupCache($blowLast);
335 function blowGroupCache($blowLast=false)
337 $cache = common_memcache();
339 $group_inbox = new Group_inbox();
340 $group_inbox->notice_id = $this->id;
341 if ($group_inbox->find()) {
342 while ($group_inbox->fetch()) {
343 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id));
345 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id.';last'));
347 $member = new Group_member();
348 $member->group_id = $group_inbox->group_id;
349 if ($member->find()) {
350 while ($member->fetch()) {
351 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
353 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
359 $group_inbox->free();
364 function blowTagCache($blowLast=false)
366 $cache = common_memcache();
368 $tag = new Notice_tag();
369 $tag->notice_id = $this->id;
371 while ($tag->fetch()) {
372 $tag->blowCache($blowLast);
380 function blowSubsCache($blowLast=false)
382 $cache = common_memcache();
386 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
387 $user->query('SELECT id ' .
389 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
390 'WHERE subscription.subscribed = ' . $this->profile_id);
392 while ($user->fetch()) {
393 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id));
395 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id.';last'));
403 function blowNoticeCache($blowLast=false)
405 if ($this->is_local) {
406 $cache = common_memcache();
407 if (!empty($cache)) {
408 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
410 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
416 function blowRepliesCache($blowLast=false)
418 $cache = common_memcache();
420 $reply = new Reply();
421 $reply->notice_id = $this->id;
422 if ($reply->find()) {
423 while ($reply->fetch()) {
424 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
426 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
435 function blowPublicCache($blowLast=false)
437 if ($this->is_local == 1) {
438 $cache = common_memcache();
440 $cache->delete(common_cache_key('public'));
442 $cache->delete(common_cache_key('public').';last');
448 function blowFavesCache($blowLast=false)
450 $cache = common_memcache();
453 $fave->notice_id = $this->id;
455 while ($fave->fetch()) {
456 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
458 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
467 # XXX: too many args; we need to move to named params or even a separate
468 # class for notice streams
470 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $order=null, $since=null) {
472 if (common_config('memcached', 'enabled')) {
474 # Skip the cache if this is a since, since_id or max_id qry
475 if ($since_id > 0 || $max_id > 0 || $since) {
476 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
478 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
482 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
485 static function getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since) {
490 if (preg_match('/\bWHERE\b/i', $qry)) {
504 $qry .= ' notice.id > ' . $since_id;
516 $qry .= ' notice.id <= ' . $max_id;
528 $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
531 # Allow ORDER override
536 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
539 if (common_config('db','type') == 'pgsql') {
540 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
542 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
545 $notice = new Notice();
547 $notice->query($qry);
552 # XXX: this is pretty long and should probably be broken up into
553 # some helper functions
555 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
557 # If outside our cache window, just go to the DB
559 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
560 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
563 # Get the cache; if we can't, just go to the DB
565 $cache = common_memcache();
568 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
571 # Get the notices out of the cache
573 $notices = $cache->get(common_cache_key($cachekey));
575 # On a cache hit, return a DB-object-like wrapper
577 if ($notices !== false) {
578 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
582 # If the cache was invalidated because of new data being
583 # added, we can try and just get the new stuff. We keep an additional
584 # copy of the data at the key + ';last'
586 # No cache hit. Try to get the *last* cached version
588 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
592 # Reverse-chron order, so last ID is last.
594 $last_id = $last_notices[0]->id;
596 # XXX: this assumes monotonically increasing IDs; a fair
599 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
600 $last_id, null, $order, null);
603 $new_notices = array();
604 while ($new_notice->fetch()) {
605 $new_notices[] = clone($new_notice);
608 $notices = array_slice(array_merge($new_notices, $last_notices),
609 0, NOTICE_CACHE_WINDOW);
611 # Store the array in the cache for next time
613 $result = $cache->set(common_cache_key($cachekey), $notices);
614 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
616 # return a wrapper of the array for use now
618 return new ArrayWrapper(array_slice($notices, $offset, $limit));
622 # Otherwise, get the full cache window out of the DB
624 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
626 # If there are no hits, just return the value
632 # Pack results into an array
636 while ($notice->fetch()) {
637 $notices[] = clone($notice);
642 # Store the array in the cache for next time
644 $result = $cache->set(common_cache_key($cachekey), $notices);
645 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
647 # return a wrapper of the array for use now
649 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
654 function getStreamByIds($ids)
656 $cache = common_memcache();
658 if (!empty($cache)) {
660 foreach ($ids as $id) {
661 $notices[] = Notice::staticGet('id', $id);
663 return new ArrayWrapper($notices);
665 $notice = new Notice();
666 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
667 $notice->orderBy('id DESC');
674 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
676 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
679 $offset, $limit, $since_id, $max_id, $since);
681 return Notice::getStreamByIds($ids);
684 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
686 $notice = new Notice();
688 $notice->selectAdd(); // clears it
689 $notice->selectAdd('id');
691 $notice->orderBy('id DESC');
693 if (!is_null($offset)) {
694 $notice->limit($offset, $limit);
697 if (common_config('public', 'localonly')) {
698 $notice->whereAdd('is_local = 1');
701 $notice->whereAdd('is_local != -1');
704 if ($since_id != 0) {
705 $notice->whereAdd('id > ' . $since_id);
709 $notice->whereAdd('id <= ' . $max_id);
712 if (!is_null($since)) {
713 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
718 if ($notice->find()) {
719 while ($notice->fetch()) {
720 $ids[] = $notice->id;
730 function addToInboxes()
732 $enabled = common_config('inboxes', 'enabled');
734 if ($enabled === true || $enabled === 'transitional') {
735 $inbox = new Notice_inbox();
736 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
737 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
738 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "' " .
739 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
740 'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
741 'AND NOT EXISTS (SELECT user_id, notice_id ' .
742 'FROM notice_inbox ' .
743 "WHERE user_id = $UT.id " .
744 'AND notice_id = ' . $this->id . ' )';
745 if ($enabled === 'transitional') {
746 $qry .= " AND $UT.inboxed = 1";
753 function saveGroups()
755 $enabled = common_config('inboxes', 'enabled');
756 if ($enabled !== true && $enabled !== 'transitional') {
760 /* extract all !group */
761 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
762 strtolower($this->content),
768 $profile = $this->getProfile();
770 /* Add them to the database */
772 foreach (array_unique($match[1]) as $nickname) {
773 /* XXX: remote groups. */
774 $group = User_group::getForNickname($nickname);
780 // we automatically add a tag for every group name, too
782 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
783 'notice_id' => $this->id));
786 $this->saveTag($nickname);
789 if ($profile->isMember($group)) {
791 $gi = new Group_inbox();
793 $gi->group_id = $group->id;
794 $gi->notice_id = $this->id;
795 $gi->created = common_sql_now();
797 $result = $gi->insert();
800 common_log_db_error($gi, 'INSERT', __FILE__);
803 // FIXME: do this in an offline daemon
805 $this->addToGroupInboxes($group);
810 function addToGroupInboxes($group)
812 $inbox = new Notice_inbox();
813 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
814 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
815 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "', 2 " .
816 "FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " .
817 'WHERE group_member.group_id = ' . $group->id . ' ' .
818 'AND NOT EXISTS (SELECT user_id, notice_id ' .
819 'FROM notice_inbox ' .
820 "WHERE user_id = $UT.id " .
821 'AND notice_id = ' . $this->id . ' )';
822 if ($enabled === 'transitional') {
823 $qry .= " AND $UT.inboxed = 1";
825 $result = $inbox->query($qry);
828 function saveReplies()
830 // Alternative reply format
832 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
835 // extract all @messages
836 $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
840 if ($cnt || $tname) {
841 // XXX: is there another way to make an array copy?
842 $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
845 $sender = Profile::staticGet($this->profile_id);
849 // store replied only for first @ (what user/notice what the reply directed,
850 // we assume first @ is it)
852 for ($i=0; $i<count($names); $i++) {
853 $nickname = $names[$i];
854 $recipient = common_relative_profile($sender, $nickname, $this->created);
858 if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
859 $reply_for = $recipient;
860 $recipient_notice = $reply_for->getCurrentNotice();
861 if ($recipient_notice) {
862 $orig = clone($this);
863 $this->reply_to = $recipient_notice->id;
864 $this->conversation = $recipient_notice->conversation;
865 $this->update($orig);
868 // Don't save replies from blocked profile to local user
869 $recipient_user = User::staticGet('id', $recipient->id);
870 if ($recipient_user && $recipient_user->hasBlocked($sender)) {
873 $reply = new Reply();
874 $reply->notice_id = $this->id;
875 $reply->profile_id = $recipient->id;
876 $id = $reply->insert();
878 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
879 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
880 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
883 $replied[$recipient->id] = 1;
887 // Hash format replies, too
888 $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
890 foreach ($match[1] as $tag) {
891 $tagged = Profile_tag::getTagged($sender->id, $tag);
892 foreach ($tagged as $t) {
893 if (!$replied[$t->id]) {
894 // Don't save replies from blocked profile to local user
895 $t_user = User::staticGet('id', $t->id);
896 if ($t_user && $t_user->hasBlocked($sender)) {
899 $reply = new Reply();
900 $reply->notice_id = $this->id;
901 $reply->profile_id = $t->id;
902 $id = $reply->insert();
904 common_log_db_error($reply, 'INSERT', __FILE__);
907 $replied[$recipient->id] = 1;
914 // If it's not a reply, make it the root of a new conversation
916 if (empty($this->conversation)) {
917 $orig = clone($this);
918 $this->conversation = $this->id;
919 $this->update($orig);
922 foreach (array_keys($replied) as $recipient) {
923 $user = User::staticGet('id', $recipient);
925 mail_notify_attn($user, $this);
930 function asAtomEntry($namespace=false, $source=false)
932 $profile = $this->getProfile();
934 $xs = new XMLStringer(true);
937 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
938 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
943 $xs->elementStart('entry', $attrs);
946 $xs->elementStart('source');
947 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
948 $xs->element('link', array('href' => $profile->profileurl));
949 $user = User::staticGet('id', $profile->id);
951 $atom_feed = common_local_url('api',
952 array('apiaction' => 'statuses',
953 'method' => 'user_timeline',
954 'argument' => $profile->nickname.'.atom'));
955 $xs->element('link', array('rel' => 'self',
956 'type' => 'application/atom+xml',
957 'href' => $profile->profileurl));
958 $xs->element('link', array('rel' => 'license',
959 'href' => common_config('license', 'url')));
962 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
965 $xs->elementStart('author');
966 $xs->element('name', null, $profile->nickname);
967 $xs->element('uri', null, $profile->profileurl);
968 $xs->elementEnd('author');
971 $xs->elementEnd('source');
974 $xs->element('title', null, $this->content);
975 $xs->element('summary', null, $this->content);
977 $xs->element('link', array('rel' => 'alternate',
978 'href' => $this->bestUrl()));
980 $xs->element('id', null, $this->uri);
982 $xs->element('published', null, common_date_w3dtf($this->created));
983 $xs->element('updated', null, common_date_w3dtf($this->modified));
985 if ($this->reply_to) {
986 $reply_notice = Notice::staticGet('id', $this->reply_to);
987 if (!empty($reply_notice)) {
988 $xs->element('link', array('rel' => 'related',
989 'href' => $reply_notice->bestUrl()));
990 $xs->element('thr:in-reply-to',
991 array('ref' => $reply_notice->uri,
992 'href' => $reply_notice->bestUrl()));
996 $xs->element('content', array('type' => 'html'), $this->rendered);
998 $tag = new Notice_tag();
999 $tag->notice_id = $this->id;
1001 while ($tag->fetch()) {
1002 $xs->element('category', array('term' => $tag->tag));
1007 $xs->elementEnd('entry');
1009 return $xs->getString();
1014 if (!empty($this->url)) {
1016 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
1019 return common_local_url('shownotice',
1020 array('notice' => $this->id));
1024 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
1026 $cache = common_memcache();
1028 if (empty($cache) ||
1029 $since_id != 0 || $max_id != 0 || (!is_null($since) && $since > 0) ||
1030 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1031 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1035 $idkey = common_cache_key($cachekey);
1037 $idstr = $cache->get($idkey);
1039 if (!empty($idstr)) {
1040 // Cache hit! Woohoo!
1041 $window = explode(',', $idstr);
1042 $ids = array_slice($window, $offset, $limit);
1046 $laststr = $cache->get($idkey.';last');
1048 if (!empty($laststr)) {
1049 $window = explode(',', $laststr);
1050 $last_id = $window[0];
1051 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1052 $last_id, 0, null, $tag)));
1054 $new_window = array_merge($new_ids, $window);
1056 $new_windowstr = implode(',', $new_window);
1058 $result = $cache->set($idkey, $new_windowstr);
1059 $result = $cache->set($idkey . ';last', $new_windowstr);
1061 $ids = array_slice($new_window, $offset, $limit);
1066 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1067 0, 0, null, $tag)));
1069 $windowstr = implode(',', $window);
1071 $result = $cache->set($idkey, $windowstr);
1072 $result = $cache->set($idkey . ';last', $windowstr);
1074 $ids = array_slice($window, $offset, $limit);