]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/useractivitystream.php
More aggressively avoid OOM errors in useractivitystream
[quix0rs-gnu-social.git] / lib / useractivitystream.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010 StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * Class for activity streams
22  *
23  * Includes faves, notices, and subscriptions.
24  *
25  * We extend atomusernoticefeed since it does some nice setup for us.
26  *
27  */
28 class UserActivityStream extends AtomUserNoticeFeed
29 {
30     public $activities = array();
31
32     const OUTPUT_STRING = 1;
33     const OUTPUT_RAW = 2;
34     public $outputMode = self::OUTPUT_STRING;
35
36     /**
37      *
38      * @param User $user
39      * @param boolean $indent
40      * @param boolean $outputMode: UserActivityStream::OUTPUT_STRING to return a string,
41      *                           or UserActivityStream::OUTPUT_RAW to go to raw output.
42      *                           Raw output mode will attempt to stream, keeping less
43      *                           data in memory but will leave $this->activities incomplete.
44      */
45     function __construct($user, $indent = true, $outputMode = UserActivityStream::OUTPUT_STRING)
46     {
47         parent::__construct($user, null, $indent);
48
49         $this->outputMode = $outputMode;
50         if ($this->outputMode == self::OUTPUT_STRING) {
51             // String buffering? Grab all the notices now.
52             $notices = $this->getNotices();
53         } elseif ($this->outputMode == self::OUTPUT_RAW) {
54             // Raw output... need to restructure from the stringer init.
55             $this->xw = new XMLWriter();
56             $this->xw->openURI('php://output');
57             if(is_null($indent)) {
58                 $indent = common_config('site', 'indent');
59             }
60             $this->xw->setIndent($indent);
61
62             // We'll fetch notices later.
63             $notices = array();
64         } else {
65             throw new Exception('Invalid outputMode provided to ' . __METHOD__);
66         }
67
68         // Assume that everything but notices is feasible
69         // to pull at once and work with in memory...
70
71         $subscriptions = $this->getSubscriptions();
72         $subscribers   = $this->getSubscribers();
73         $groups        = $this->getGroups();
74         $faves         = $this->getFaves();
75
76         $objs = array_merge($subscriptions, $subscribers, $groups, $faves, $notices);
77
78         $subscriptions = null;
79         $subscribers   = null;
80         $groups        = null;
81         $faves         = null;
82
83         unset($subscriptions);
84         unset($subscribers);
85         unset($groups);
86         unset($faves);
87
88         // Sort by create date
89
90         usort($objs, 'UserActivityStream::compareObject');
91
92         // We'll keep these around for later, and interleave them into
93         // the output stream with the user's notices.
94
95         $this->objs = $objs;
96     }
97
98     /**
99      * Interleave the pre-sorted subs/groups/faves with the user's
100      * notices, all in reverse chron order.
101      */
102     function renderEntries()
103     {
104         $end = time() + 1;
105         $i = 0;
106         foreach ($this->objs as $obj) {
107             $i++;
108             try {
109                 $act = $obj->asActivity();
110             } catch (Exception $e) {
111                 common_log(LOG_ERR, $e->getMessage());
112                 continue;
113             }
114
115             $start = $act->time;
116
117             if ($this->outputMode == self::OUTPUT_RAW && $start != $end) {
118                 // In raw mode, we haven't pre-fetched notices.
119                 // Grab the chunks of notices between other activities.
120                 try {
121                     $notices = $this->getNoticesBetween($start, $end);
122                     foreach ($notices as $noticeAct) {
123                         try {
124                             $nact = $noticeAct->asActivity();
125                             $nact->outputTo($this, false, false);
126                         } catch (Exception $e) {
127                             common_log(LOG_ERR, $e->getMessage());
128                             continue;
129                         }
130                         $nact = null;
131                         unset($nact);
132                     }
133                 } catch (Exception $e) {
134                     common_log(LOG_ERR, $e->getMessage());
135                 }
136             }
137
138             $notices = null;
139             unset($notices);
140
141             // Only show the author sub-element if it's different from default user
142             $act->outputTo($this, false, ($act->actor->id != $this->user->uri));
143
144             $act = null;
145             unset($act);
146
147             $end = $start;
148         }
149
150         if ($this->outputMode == self::OUTPUT_RAW) {
151             // Grab anything after the last pre-sorted activity.
152             try {
153                 $notices = $this->getNoticesBetween(0, $end);
154                 foreach ($notices as $noticeAct) {
155                     try {
156                         $nact = $noticeAct->asActivity();
157                         $nact->outputTo($this, false, false);
158                     } catch (Exception $e) {
159                         common_log(LOG_ERR, $e->getMessage());
160                         continue;
161                     }
162                 }
163             } catch (Exception $e) {
164                 common_log(LOG_ERR, $e->getMessage());
165             }
166         }
167     }
168
169     function compareObject($a, $b)
170     {
171         $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
172         $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
173
174         return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
175     }
176
177     function getSubscriptions()
178     {
179         $subs = array();
180
181         $sub = new Subscription();
182
183         $sub->subscriber = $this->user->id;
184
185         if ($sub->find()) {
186             while ($sub->fetch()) {
187                 if ($sub->subscribed != $this->user->id) {
188                     $subs[] = clone($sub);
189                 }
190             }
191         }
192
193         return $subs;
194     }
195
196     function getSubscribers()
197     {
198         $subs = array();
199
200         $sub = new Subscription();
201
202         $sub->subscribed = $this->user->id;
203
204         if ($sub->find()) {
205             while ($sub->fetch()) {
206                 if ($sub->subscriber != $this->user->id) {
207                     $subs[] = clone($sub);
208                 }
209             }
210         }
211
212         return $subs;
213     }
214
215     function getFaves()
216     {
217         $faves = array();
218
219         $fave = new Fave();
220
221         $fave->user_id = $this->user->id;
222
223         if ($fave->find()) {
224             while ($fave->fetch()) {
225                 $faves[] = clone($fave);
226             }
227         }
228
229         return $faves;
230     }
231
232     /**
233      *
234      * @param int $start unix timestamp for earliest
235      * @param int $end unix timestamp for latest
236      * @return array of Notice objects
237      */
238     function getNoticesBetween($start=0, $end=0)
239     {
240         $notices = array();
241
242         $notice = new Notice();
243
244         $notice->profile_id = $this->user->id;
245
246         if ($start) {
247             $tsstart = common_sql_date($start);
248             $notice->whereAdd("created >= '$tsstart'");
249         }
250         if ($end) {
251             $tsend = common_sql_date($end);
252             $notice->whereAdd("created < '$tsend'");
253         }
254
255         $notice->orderBy('created DESC');
256
257         if ($notice->find()) {
258             while ($notice->fetch()) {
259                 $notices[] = clone($notice);
260             }
261         }
262
263         return $notices;
264     }
265
266     function getNotices()
267     {
268         return $this->getNoticesBetween();
269     }
270
271     function getGroups()
272     {
273         $groups = array();
274
275         $gm = new Group_member();
276
277         $gm->profile_id = $this->user->id;
278
279         if ($gm->find()) {
280             while ($gm->fetch()) {
281                 $groups[] = clone($gm);
282             }
283         }
284
285         return $groups;
286     }
287 }