]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticestream.php
New NoticeStream class to reify streams of notices
[quix0rs-gnu-social.git] / lib / noticestream.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * A stream of notices
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Stream
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Class for notice streams
39  *
40  * @category  Stream
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2011 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47
48 class NoticeStream
49 {
50     public $generator = null;
51     public $args      = null;
52     public $cachekey  = null;
53
54     function __construct($generator, $args, $cachekey)
55     {
56         $this->generator = $generator;
57         $this->args      = $args;
58         $this->cachekey  = $cachekey;
59     }
60
61     function getNotices($offset=0, $limit=20, $sinceId=0, $maxId=0)
62     {
63         $ids = $this->getNoticeIds($offset, $limit, $sinceId, $maxId);
64
65         $notices = $this->getStreamByIds($ids);
66
67         return $notices;
68     }
69
70     function getNoticeIds($offset=0, $limit=20, $sinceId=0, $maxId=0)
71     {
72         $cache = Cache::instance();
73
74         // We cache NOTICE_CACHE_WINDOW elements at the tip of the stream.
75         // If the cache won't be hit, just generate directly.
76
77         if (empty($cache) ||
78             $sinceId != 0 || $maxId != 0 ||
79             is_null($limit) ||
80             ($offset + $limit) > NOTICE_CACHE_WINDOW) {
81             return $this->generate($offset, $limit, $sinceId, $maxId);
82         }
83
84         // Check the cache to see if we have the stream.
85
86         $idkey = Cache::key($this->cachekey);
87
88         $idstr = $cache->get($idkey);
89
90         if ($idstr !== false) {
91             // Cache hit! Woohoo!
92             $window = explode(',', $idstr);
93             $ids = array_slice($window, $offset, $limit);
94             return $ids;
95         }
96
97         // Check the cache to see if we have a "last-known-good" version.
98         // The actual cache gets blown away when new notices are added, but
99         // the "last" value holds a lot of info. We might need to only generate
100         // a few at the "tip", which can bound our queries and save lots
101         // of time.
102
103         $laststr = $cache->get($idkey.';last');
104
105         if ($laststr !== false) {
106             $window = explode(',', $laststr);
107             $last_id = $window[0];
108             $new_ids = $this->generate(0, NOTICE_CACHE_WINDOW, $last_id, 0);
109
110             $new_window = array_merge($new_ids, $window);
111
112             $new_windowstr = implode(',', $new_window);
113
114             $result = $cache->set($idkey, $new_windowstr);
115             $result = $cache->set($idkey . ';last', $new_windowstr);
116
117             $ids = array_slice($new_window, $offset, $limit);
118
119             return $ids;
120         }
121
122         // No cache hits :( Generate directly and stick the results
123         // into the cache. Note we generate the full cache window.
124
125         $window = $this->generate(0, NOTICE_CACHE_WINDOW, 0, 0);
126
127         $windowstr = implode(',', $window);
128
129         $result = $cache->set($idkey, $windowstr);
130         $result = $cache->set($idkey . ';last', $windowstr);
131
132         // Return just the slice that was requested
133
134         $ids = array_slice($window, $offset, $limit);
135
136         return $ids;
137     }
138
139     function getStreamByIds($ids)
140     {
141         $cache = Cache::instance();
142
143         if (!empty($cache)) {
144             $notices = array();
145             foreach ($ids as $id) {
146                 $n = Notice::staticGet('id', $id);
147                 if (!empty($n)) {
148                     $notices[] = $n;
149                 }
150             }
151             return new ArrayWrapper($notices);
152         } else {
153             $notice = new Notice();
154             if (empty($ids)) {
155                 //if no IDs requested, just return the notice object
156                 return $notice;
157             }
158             $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
159
160             $notice->find();
161
162             $temp = array();
163
164             while ($notice->fetch()) {
165                 $temp[$notice->id] = clone($notice);
166             }
167
168             $wrapped = array();
169
170             foreach ($ids as $id) {
171                 if (array_key_exists($id, $temp)) {
172                     $wrapped[] = $temp[$id];
173                 }
174             }
175
176             return new ArrayWrapper($wrapped);
177         }
178     }
179
180     function generate($offset, $limit, $sinceId, $maxId)
181     {
182         $args = array_merge($this->args, array($offset,
183                                                $limit,
184                                                $sinceId,
185                                                $maxId));
186
187         return call_user_func_array($this->generator, $args);
188     }
189 }