]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Notice_inbox.php
Remove some debug comments in query-by-id
[quix0rs-gnu-social.git] / classes / Notice_inbox.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, 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 if (!defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23
24 // We keep 5 pages of inbox notices in memcache, +1 for pagination check
25
26 define('INBOX_CACHE_WINDOW', 101);
27
28 class Notice_inbox extends Memcached_DataObject
29 {
30     ###START_AUTOCODE
31     /* the code below is auto generated do not remove the above tag */
32
33     public $__table = 'notice_inbox';                    // table name
34     public $user_id;                         // int(4)  primary_key not_null
35     public $notice_id;                       // int(4)  primary_key not_null
36     public $created;                         // datetime()   not_null
37     public $source;                          // tinyint(1)   default_1
38
39     /* Static get */
40     function staticGet($k,$v=null)
41     { return Memcached_DataObject::staticGet('Notice_inbox',$k,$v); }
42
43     /* the code above is auto generated do not remove the tag below */
44     ###END_AUTOCODE
45
46     function stream($user_id, $offset=0, $limit=20, $since_id=0, $before_id=0, $since=null)
47     {
48         $cache = common_memcache();
49
50         if (empty($cache) ||
51             $since_id != 0 || $before_id != 0 || !is_null($since) ||
52             ($offset + $limit) > INBOX_CACHE_WINDOW) {
53             return Notice_inbox::_streamDirect($user_id, $offset, $limit, $since_id, $before_id, $since);
54         }
55
56         $idkey = common_cache_key('notice_inbox:by_user:'.$user_id);
57
58         $idstr = $cache->get($idkey);
59
60         if (!empty($idstr)) {
61             // Cache hit! Woohoo!
62             $window = explode(',', $idstr);
63             $ids = array_slice($window, $offset, $limit);
64             return $ids;
65         }
66
67         $laststr = common_cache_key($idkey.';last');
68
69         if (!empty($laststr)) {
70             $window = explode(',', $laststr);
71             $last_id = $window[0];
72             $new_ids = Notice_inbox::_streamDirect($user_id, 0, INBOX_CACHE_WINDOW,
73                                                    $last_id, null, null);
74
75             $new_window = array_merge($new_ids, $window);
76
77             $new_windowstr = implode(',', $new_window);
78
79             $result = $cache->set($idkey, $new_windowstr);
80             $result = $cache->set($idkey . ';last', $new_windowstr);
81
82             $ids = array_slice($new_window, $offset, $limit);
83
84             return $ids;
85         }
86
87         $window = Notice_inbox::_streamDirect($user_id, 0, INBOX_CACHE_WINDOW,
88                                               null, null, null);
89
90         $windowstr = implode(',', $new_window);
91
92         $result = $cache->set($idkey, $windowstr);
93         $result = $cache->set($idkey . ';last', $windowstr);
94
95         $ids = array_slice($window, $offset, $limit);
96
97         return $ids;
98     }
99
100     function _streamDirect($user_id, $offset, $limit, $since_id, $before_id, $since)
101     {
102         $inbox = new Notice_inbox();
103
104         $inbox->user_id = $user_id;
105
106         if ($since_id != 0) {
107             $inbox->whereAdd('notice_id > ' . $since_id);
108         }
109
110         if ($before_id != 0) {
111             $inbox->whereAdd('notice_id < ' . $before_id);
112         }
113
114         if (!is_null($since)) {
115             $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
116         }
117
118         $inbox->orderBy('notice_id DESC');
119
120         if (!is_null($offset)) {
121             $inbox->limit($offset, $limit);
122         }
123
124         $ids = array();
125
126         if ($inbox->find()) {
127             while ($inbox->fetch()) {
128                 $ids[] = $inbox->notice_id;
129             }
130         }
131
132         return $ids;
133     }
134 }