]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticestream.php
Merge branch '1.0.x' into qna
[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 class NoticeStream
48 {
49     const CACHE_WINDOW = 200;
50
51     public $generator = null;
52     public $args      = null;
53     public $cachekey  = null;
54
55     function __construct($generator, $args, $cachekey)
56     {
57         $this->generator = $generator;
58         $this->args      = $args;
59         $this->cachekey  = $cachekey;
60     }
61
62     function getNotices($offset=0, $limit=20, $sinceId=0, $maxId=0)
63     {
64         $ids = $this->getNoticeIds($offset, $limit, $sinceId, $maxId);
65
66         $notices = self::getStreamByIds($ids);
67
68         return $notices;
69     }
70
71     function getNoticeIds($offset=0, $limit=20, $sinceId=0, $maxId=0)
72     {
73         $cache = Cache::instance();
74
75         // We cache self::CACHE_WINDOW elements at the tip of the stream.
76         // If the cache won't be hit, just generate directly.
77
78         if (empty($cache) ||
79             $sinceId != 0 || $maxId != 0 ||
80             is_null($limit) ||
81             ($offset + $limit) > self::CACHE_WINDOW) {
82             return $this->generate($offset, $limit, $sinceId, $maxId);
83         }
84
85         // Check the cache to see if we have the stream.
86
87         $idkey = Cache::key($this->cachekey);
88
89         $idstr = $cache->get($idkey);
90
91         if ($idstr !== false) {
92             // Cache hit! Woohoo!
93             $window = explode(',', $idstr);
94             $ids = array_slice($window, $offset, $limit);
95             return $ids;
96         }
97
98         // Check the cache to see if we have a "last-known-good" version.
99         // The actual cache gets blown away when new notices are added, but
100         // the "last" value holds a lot of info. We might need to only generate
101         // a few at the "tip", which can bound our queries and save lots
102         // of time.
103
104         $laststr = $cache->get($idkey.';last');
105
106         if ($laststr !== false) {
107             $window = explode(',', $laststr);
108             $last_id = $window[0];
109             $new_ids = $this->generate(0, self::CACHE_WINDOW, $last_id, 0);
110
111             $new_window = array_merge($new_ids, $window);
112
113             $new_windowstr = implode(',', $new_window);
114
115             $result = $cache->set($idkey, $new_windowstr);
116             $result = $cache->set($idkey . ';last', $new_windowstr);
117
118             $ids = array_slice($new_window, $offset, $limit);
119
120             return $ids;
121         }
122
123         // No cache hits :( Generate directly and stick the results
124         // into the cache. Note we generate the full cache window.
125
126         $window = $this->generate(0, self::CACHE_WINDOW, 0, 0);
127
128         $windowstr = implode(',', $window);
129
130         $result = $cache->set($idkey, $windowstr);
131         $result = $cache->set($idkey . ';last', $windowstr);
132
133         // Return just the slice that was requested
134
135         $ids = array_slice($window, $offset, $limit);
136
137         return $ids;
138     }
139
140     static function getStreamByIds($ids)
141     {
142         $cache = Cache::instance();
143
144         if (!empty($cache)) {
145             $notices = array();
146             foreach ($ids as $id) {
147                 $n = Notice::staticGet('id', $id);
148                 if (!empty($n)) {
149                     $notices[] = $n;
150                 }
151             }
152             return new ArrayWrapper($notices);
153         } else {
154             $notice = new Notice();
155             if (empty($ids)) {
156                 //if no IDs requested, just return the notice object
157                 return $notice;
158             }
159             $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
160
161             $notice->find();
162
163             $temp = array();
164
165             while ($notice->fetch()) {
166                 $temp[$notice->id] = clone($notice);
167             }
168
169             $wrapped = array();
170
171             foreach ($ids as $id) {
172                 if (array_key_exists($id, $temp)) {
173                     $wrapped[] = $temp[$id];
174                 }
175             }
176
177             return new ArrayWrapper($wrapped);
178         }
179     }
180
181     function generate($offset, $limit, $sinceId, $maxId)
182     {
183         $args = array_merge($this->args, array($offset,
184                                                $limit,
185                                                $sinceId,
186                                                $maxId));
187
188         return call_user_func_array($this->generator, $args);
189     }
190 }