]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Sitemap/Sitemap_user_count.php
The overloaded DB_DataObject function staticGet is now called getKV
[quix0rs-gnu-social.git] / plugins / Sitemap / Sitemap_user_count.php
1 <?php
2 /**
3  * Data class for counting user registrations by date
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2010, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Data class for counting users by date
38  *
39  * We make a separate sitemap for each user registered by date.
40  * To save ourselves some processing effort, we cache this data
41  *
42  * @category Action
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46  * @link     http://status.net/
47  *
48  * @see      DB_DataObject
49  */
50 class Sitemap_user_count extends Managed_DataObject
51 {
52     public $__table = 'sitemap_user_count'; // table name
53
54     public $registration_date;               // date primary_key not_null
55     public $user_count;                      // int(4)
56     public $created;
57     public $modified;
58
59     /**
60      * return table definition for DB_DataObject
61      *
62      * DB_DataObject needs to know something about the table to manipulate
63      * instances. This method provides all the DB_DataObject needs to know.
64      *
65      * @return array array of column definitions
66      */
67     function table()
68     {
69         return array('registration_date' => DB_DATAOBJECT_DATE + DB_DATAOBJECT_NOTNULL,
70                      'user_count' => DB_DATAOBJECT_INT,
71                      'created'   => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
72                      'modified'  => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
73     }
74
75     /**
76      * return key definitions for DB_DataObject
77      *
78      * DB_DataObject needs to know about keys that the table has; this function
79      * defines them.
80      *
81      * @return array key definitions
82      */
83
84     function keys()
85     {
86         return array('registration_date' => 'K');
87     }
88
89     function sequenceKey()
90     {
91         return array(false, false, false);
92     }
93
94     /**
95      * return key definitions for Memcached_DataObject
96      *
97      * Our caching system uses the same key definitions, but uses a different
98      * method to get them.
99      *
100      * @return array key definitions
101      */
102     function keyTypes()
103     {
104         return $this->keys();
105     }
106
107     static function getAll()
108     {
109         $userCounts = self::cacheGet('sitemap:user:counts');
110
111         if ($userCounts === false) {
112
113             $suc = new Sitemap_user_count();
114             $suc->orderBy('registration_date DESC');
115
116             // Fetch the first one to check up-to-date-itude
117
118             $n = $suc->find(true);
119
120             $today = self::today();
121             $userCounts = array();
122
123             if (!$n) { // No counts saved yet
124                 $userCounts = self::initializeCounts();
125             } else if ($suc->registration_date < $today) { // There are counts but not up to today
126                 $userCounts = self::fillInCounts($suc->registration_date);
127             } else if ($suc->registration_date == $today) { // Refresh today's
128                 $userCounts[$today] = self::updateToday();
129             }
130
131             // starts with second-to-last date
132
133             while ($suc->fetch()) {
134                 $userCounts[$suc->registration_date] = $suc->user_count;
135             }
136
137             // Cache user counts for 4 hours.
138
139             self::cacheSet('sitemap:user:counts', $userCounts, null, time() + 4 * 60 * 60);
140         }
141
142         return $userCounts;
143     }
144
145     static function initializeCounts()
146     {
147         $firstDate = self::getFirstDate(); // awww
148         $today     = self::today();
149
150         $counts = array();
151
152         for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
153             $n = self::getCount($d);
154             self::insertCount($d, $n);
155             $counts[$d] = $n;
156         }
157
158         return $counts;
159     }
160
161     static function fillInCounts($lastDate)
162     {
163         $today = self::today();
164
165         $counts = array();
166
167         $n = self::getCount($lastDate);
168         self::updateCount($lastDate, $n);
169
170         $counts[$lastDate] = $n;
171
172         for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
173             $n = self::getCount($d);
174             self::insertCount($d, $n);
175         }
176
177         return $counts;
178     }
179
180     static function updateToday()
181     {
182         $today = self::today();
183
184         $n = self::getCount($today);
185         self::updateCount($today, $n);
186
187         return $n;
188     }
189
190     static function getCount($d)
191     {
192         $user = new User();
193         $user->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
194         $n = $user->count();
195
196         return $n;
197     }
198
199     static function insertCount($d, $n)
200     {
201         $suc = new Sitemap_user_count();
202
203         $suc->registration_date = DB_DataObject_Cast::date($d);
204         $suc->user_count        = $n;
205         $suc->created           = common_sql_now();
206         $suc->modified          = $suc->created;
207
208         if (!$suc->insert()) {
209             common_log(LOG_WARNING, "Could not save user counts for '$d'");
210         }
211     }
212
213     static function updateCount($d, $n)
214     {
215         $suc = Sitemap_user_count::getKV('registration_date', DB_DataObject_Cast::date($d));
216
217         if (empty($suc)) {
218             // TRANS: Exception thrown when a registration date cannot be found.
219             throw new Exception(_m("No such registration date: $d."));
220         }
221
222         $orig = clone($suc);
223
224         $suc->registration_date = DB_DataObject_Cast::date($d);
225         $suc->user_count        = $n;
226         $suc->created           = common_sql_now();
227         $suc->modified          = $suc->created;
228
229         if (!$suc->update($orig)) {
230             common_log(LOG_WARNING, "Could not save user counts for '$d'");
231         }
232     }
233
234     static function incrementDay($d)
235     {
236         $dt = self::dateStrToInt($d);
237         return self::dateIntToStr($dt + 24 * 60 * 60);
238     }
239
240     static function dateStrToInt($d)
241     {
242         return strtotime($d.' 00:00:00');
243     }
244
245     static function dateIntToStr($dt)
246     {
247         return date('Y-m-d', $dt);
248     }
249
250     static function getFirstDate()
251     {
252         $u = new User();
253         $u->selectAdd();
254         $u->selectAdd('date(min(created)) as first_date');
255         if ($u->find(true)) {
256             return $u->first_date;
257         } else {
258             // Is this right?
259             return self::dateIntToStr(time());
260         }
261     }
262
263     static function today()
264     {
265         return self::dateIntToStr(time());
266     }
267 }