]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/Realtime_channel.php
Localisation updates from http://translatewiki.net.
[quix0rs-gnu-social.git] / plugins / Realtime / Realtime_channel.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * A channel for real-time browser data
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  Realtime
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     exit(1);
33 }
34
35 /**
36  * A channel for real-time browser data
37  * 
38  * For each user currently browsing the site, we want to know which page they're on
39  * so we can send real-time updates to their browser.
40  *
41  * @category Realtime
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45  * @link     http://status.net/
46  *
47  * @see      DB_DataObject
48  */
49
50 class Realtime_channel extends Managed_DataObject
51 {
52         const TIMEOUT = 1800; // 30 minutes
53         
54     public $__table = 'realtime_channel'; // table name
55     
56     public $user_id;       // int -> user.id, can be null
57     public $action;        // string
58     public $arg1;          // argument
59     public $arg2;          // argument, usually null
60     public $channel_key;   // 128-bit shared secret key
61     public $audience;      // listener count
62     public $created;       // created date
63     public $modified;      // modified date
64
65     /**
66      * Get an instance by key
67      *
68      * This is a utility method to get a single instance with a given key value.
69      *
70      * @param string $k Key to use to lookup (usually 'user_id' for this class)
71      * @param mixed  $v Value to lookup
72      *
73      * @return Realtime_channel object found, or null for no hits
74      *
75      */
76     function staticGet($k, $v=null)
77     {
78         return Managed_DataObject::staticGet('Realtime_channel', $k, $v);
79     }
80
81     /**
82      * Get an instance by compound key
83      *
84      * @param array $kv array of key-value mappings
85      *
86      * @return Realtime_channel object found, or null for no hits
87      *
88      */
89     function pkeyGet($kv)
90     {
91         return Managed_DataObject::pkeyGet('Realtime_channel', $kv);
92     }
93
94     /**
95      * The One True Thingy that must be defined and declared.
96      */
97     public static function schemaDef()
98     {
99         return array(
100             'description' => 'A channel of realtime notice data',
101             'fields' => array(
102                 'user_id' => array('type' => 'int',
103                                                                    'not null' => false,
104                                                                    'description' => 'user viewing page; can be null'),
105                 'action' => array('type' => 'varchar',
106                                                   'length' => 255,
107                                                                   'not null' => true,
108                                                                   'description' => 'page being viewed'),
109                                 'arg1' => array('type' => 'varchar',
110                                                             'length' => 255,
111                                                             'not null' => false,
112                                                             'description' => 'page argument, like username or tag'),
113                             'arg2' => array('type' => 'varchar',
114                                                         'length' => 255,
115                                                         'not null' => false,
116                                                         'description' => 'second page argument, like tag for showstream'),
117                             'channel_key' => array('type' => 'varchar',
118                                                    'length' => 32,
119                                                    'not null' => true,
120                                                    'description' => 'shared secret key for this channel'),
121                             'audience' => array('type' => 'integer',
122                                     'not null' => true,
123                                     'default' => 0,
124                                     'description' => 'reference count'),
125                 'created' => array('type' => 'datetime',
126                                                                    'not null' => true,
127                                                                    'description' => 'date this record was created'),
128                 'modified' => array('type' => 'datetime',
129                                                                 'not null' => true,
130                                                             'description' => 'date this record was modified'),
131             ),
132             'primary key' => array('channel_key'),
133             'unique keys' => array('realtime_channel_user_page_idx' => array('user_id', 'action', 'arg1', 'arg2')),
134             'foreign keys' => array(
135                 'realtime_channel_user_id_fkey' => array('user', array('user_id' => 'id')),
136             ),
137             'indexes' => array(
138                 'realtime_channel_modified_idx' => array('modified'),
139                 'realtime_channel_page_idx' => array('action', 'arg1', 'arg2')
140             ),
141         );
142     }
143     
144     static function saveNew($user_id, $action, $arg1, $arg2)
145     {
146         $channel = new Realtime_channel();
147         
148         $channel->user_id = $user_id;
149         $channel->action  = $action;
150         $channel->arg1    = $arg1;
151         $channel->arg2    = $arg2;
152         $channel->audience  = 1;
153         
154         $channel->channel_key = common_good_rand(16); // 128-bit key, 32 hex chars
155         
156         $channel->created  = common_sql_now();
157         $channel->modified = $channel->created;
158         
159                 $channel->insert();
160                 
161                 return $channel;
162     }
163     
164     static function getChannel($user_id, $action, $arg1, $arg2)
165     {
166         $channel = self::fetchChannel($user_id, $action, $arg1, $arg2);
167         
168         // Ignore (and delete!) old channels
169                                    
170         if (!empty($channel)) {
171                         $modTime = strtotime($channel->modified);
172                         if ((time() - $modTime) > self::TIMEOUT) {
173                                 $channel->delete();
174                                 $channel = null;
175                         }
176         }
177         
178         if (empty($channel)) {
179                 $channel = self::saveNew($user_id, $action, $arg1, $arg2);
180         }
181         
182         return $channel;
183     }
184     
185     static function getAllChannels($action, $arg1, $arg2)
186     {
187         $channel = new Realtime_channel();
188         
189         $channel->action = $action;
190         
191         if (is_null($arg1)) {
192                 $channel->whereAdd('arg1 is null');
193         } else {
194                 $channel->arg1 = $arg1;
195         }
196         
197         if (is_null($arg2)) {
198                 $channel->whereAdd('arg2 is null');
199         } else {
200                 $channel->arg2 = $arg2;
201         }
202         
203         $channel->whereAdd('modified > "' . common_sql_date(time() - self::TIMEOUT) . '"');
204         
205         $channels = array();
206         
207         if ($channel->find()) {
208                 $channels = $channel->fetchAll();
209         }
210         
211         return $channels;
212     }
213     
214     static function fetchChannel($user_id, $action, $arg1, $arg2)
215     {   
216         $channel = new Realtime_channel();
217         
218         if (is_null($user_id)) {
219                 $channel->whereAdd('user_id is null');
220         } else {
221                 $channel->user_id = $user_id;
222         }
223         
224         $channel->action = $action;
225         
226         if (is_null($arg1)) {
227                 $channel->whereAdd('arg1 is null');
228         } else {
229                 $channel->arg1 = $arg1;
230         }
231         
232         if (is_null($arg2)) {
233                 $channel->whereAdd('arg2 is null');
234         } else {
235                 $channel->arg2 = $arg2;
236         }
237         
238         if ($channel->find(true)) {
239             $channel->increment();
240                 return $channel;
241         } else {
242                 return null;
243         }
244     }
245
246     function increment()
247     {
248         // XXX: race
249         $orig = clone($this);
250         $this->audience++;
251         $this->modified = common_sql_now();
252         $this->update($orig);
253     }
254
255     function touch()
256     {
257         // XXX: race
258         $orig = clone($this);
259         $this->modified = common_sql_now();
260         $this->update($orig);
261     }
262
263     function decrement()
264     {
265         // XXX: race
266         if ($this->audience == 1) {
267             $this->delete();
268         } else {
269             $orig = clone($this);
270             $this->audience--;
271             $this->modified = common_sql_now();
272             $this->update($orig);
273         }
274     }
275 }