]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/useractivitystream.php
Add JSON output for backups
[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($format=Feed::ATOM, $handle=null)
103     {
104         $end = time() + 1;
105         foreach ($this->objs as $obj) {
106             try {
107                 $act = $obj->asActivity();
108             } catch (Exception $e) {
109                 common_log(LOG_ERR, $e->getMessage());
110                 continue;
111             }
112
113             $start = $act->time;
114
115             if ($this->outputMode == self::OUTPUT_RAW && $start != $end) {
116                 // In raw mode, we haven't pre-fetched notices.
117                 // Grab the chunks of notices between other activities.
118                 try {
119                     $notices = $this->getNoticesBetween($start, $end);
120                     foreach ($notices as $noticeAct) {
121                         try {
122                             $nact = $noticeAct->asActivity();
123                             if ($format == Feed::ATOM) {
124                                 $nact->outputTo($this, false, false);
125                             } else {
126                                 fwrite($handle, json_encode($nact->asArray()));
127                             }
128                         } catch (Exception $e) {
129                             common_log(LOG_ERR, $e->getMessage());
130                             continue;
131                         }
132                         $nact = null;
133                         unset($nact);
134                     }
135                 } catch (Exception $e) {
136                     common_log(LOG_ERR, $e->getMessage());
137                 }
138             }
139
140             $notices = null;
141             unset($notices);
142
143             try {
144                 if ($format == Feed::ATOM) {
145                     // Only show the author sub-element if it's different from default user
146                     $act->outputTo($this, false, ($act->actor->id != $this->user->uri));
147                 } else {
148                     fwrite($handle, json_encode($act->asArray()));
149                 }
150             } catch (Exception $e) {
151                 common_log(LOG_ERR, $e->getMessage());
152             }
153
154             $act = null;
155             unset($act);
156
157             $end = $start;
158         }
159
160         if ($this->outputMode == self::OUTPUT_RAW) {
161             // Grab anything after the last pre-sorted activity.
162             try {
163                 $notices = $this->getNoticesBetween(0, $end);
164                 foreach ($notices as $noticeAct) {
165                     try {
166                         $nact = $noticeAct->asActivity();
167                         if ($format == Feed::ATOM) {
168                             $nact->outputTo($this, false, false);
169                         } else {
170                             fwrite($handle, json_encode($nact->asArray()));
171                         }
172                     } catch (Exception $e) {
173                         common_log(LOG_ERR, $e->getMessage());
174                         continue;
175                     }
176                 }
177             } catch (Exception $e) {
178                 common_log(LOG_ERR, $e->getMessage());
179             }
180         }
181     }
182
183     function compareObject($a, $b)
184     {
185         $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
186         $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
187
188         return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
189     }
190
191     function getSubscriptions()
192     {
193         $subs = array();
194
195         $sub = new Subscription();
196
197         $sub->subscriber = $this->user->id;
198
199         if ($sub->find()) {
200             while ($sub->fetch()) {
201                 if ($sub->subscribed != $this->user->id) {
202                     $subs[] = clone($sub);
203                 }
204             }
205         }
206
207         return $subs;
208     }
209
210     function getSubscribers()
211     {
212         $subs = array();
213
214         $sub = new Subscription();
215
216         $sub->subscribed = $this->user->id;
217
218         if ($sub->find()) {
219             while ($sub->fetch()) {
220                 if ($sub->subscriber != $this->user->id) {
221                     $subs[] = clone($sub);
222                 }
223             }
224         }
225
226         return $subs;
227     }
228
229     function getFaves()
230     {
231         $faves = array();
232
233         $fave = new Fave();
234
235         $fave->user_id = $this->user->id;
236
237         if ($fave->find()) {
238             while ($fave->fetch()) {
239                 $faves[] = clone($fave);
240             }
241         }
242
243         return $faves;
244     }
245
246     /**
247      *
248      * @param int $start unix timestamp for earliest
249      * @param int $end unix timestamp for latest
250      * @return array of Notice objects
251      */
252     function getNoticesBetween($start=0, $end=0)
253     {
254         $notices = array();
255
256         $notice = new Notice();
257
258         $notice->profile_id = $this->user->id;
259
260         if ($start) {
261             $tsstart = common_sql_date($start);
262             $notice->whereAdd("created >= '$tsstart'");
263         }
264         if ($end) {
265             $tsend = common_sql_date($end);
266             $notice->whereAdd("created < '$tsend'");
267         }
268
269         $notice->orderBy('created DESC');
270
271         if ($notice->find()) {
272             while ($notice->fetch()) {
273                 $notices[] = clone($notice);
274             }
275         }
276
277         return $notices;
278     }
279
280     function getNotices()
281     {
282         return $this->getNoticesBetween();
283     }
284
285     function getGroups()
286     {
287         $groups = array();
288
289         $gm = new Group_member();
290
291         $gm->profile_id = $this->user->id;
292
293         if ($gm->find()) {
294             while ($gm->fetch()) {
295                 $groups[] = clone($gm);
296             }
297         }
298
299         return $groups;
300     }
301
302     function writeJSON($handle)
303     {
304         require_once INSTALLDIR.'/lib/activitystreamjsondocument.php';
305         fwrite($handle, '{"items": [');
306         $this->renderEntries(Feed::JSON, $handle);
307         fwrite($handle, ']');
308     }
309 }