]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Sitemap/Sitemap_user_count.php
Merge branch 'sitemap' of gitorious.org:~evan/statusnet/evans-mainline into sitemap
[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
51 class Sitemap_user_count extends Memcached_DataObject
52 {
53     public $__table = 'sitemap_user_count'; // table name
54
55     public $registration_date;               // date primary_key not_null
56     public $user_count;                      // int(4)
57     public $created;
58     public $modified;
59
60     /**
61      * Get an instance by key
62      *
63      * This is a utility method to get a single instance with a given key value.
64      *
65      * @param string $k Key to use to lookup (usually 'user_id' for this class)
66      * @param mixed  $v Value to lookup
67      *
68      * @return Sitemap_user_count object found, or null for no hits
69      *
70      */
71
72     function staticGet($k, $v=null)
73     {
74         return Memcached_DataObject::staticGet('Sitemap_user_count', $k, $v);
75     }
76
77     /**
78      * return table definition for DB_DataObject
79      *
80      * DB_DataObject needs to know something about the table to manipulate
81      * instances. This method provides all the DB_DataObject needs to know.
82      *
83      * @return array array of column definitions
84      */
85
86     function table()
87     {
88         return array('registration_date' => DB_DATAOBJECT_DATE + DB_DATAOBJECT_NOTNULL,
89                      'user_count' => DB_DATAOBJECT_INT,
90                      'created'   => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
91                      'modified'  => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
92     }
93
94     /**
95      * return key definitions for DB_DataObject
96      *
97      * DB_DataObject needs to know about keys that the table has; this function
98      * defines them.
99      *
100      * @return array key definitions
101      */
102
103     function keys()
104     {
105         return array('registration_date' => 'K');
106     }
107
108     function sequenceKey()
109     {
110         return array(false, false, false);
111     }
112
113     /**
114      * return key definitions for Memcached_DataObject
115      *
116      * Our caching system uses the same key definitions, but uses a different
117      * method to get them.
118      *
119      * @return array key definitions
120      */
121
122     function keyTypes()
123     {
124         return $this->keys();
125     }
126
127     static function getAll()
128     {
129         $userCounts = self::cacheGet('sitemap:user:counts');
130
131         if ($userCounts === false) {
132
133             $suc = new Sitemap_user_count();
134             $suc->orderBy('registration_date DESC');
135
136             // Fetch the first one to check up-to-date-itude
137
138             $n = $suc->find(true);
139
140             $today = self::today();
141             $userCounts = array();
142
143             if (!$n) { // No counts saved yet
144                 $userCounts = self::initializeCounts();
145             } else if ($suc->registration_date < $today) { // There are counts but not up to today
146                 $userCounts = self::fillInCounts($suc->registration_date);
147             } else if ($suc->registration_date == $today) { // Refresh today's
148                 $userCounts[$today] = self::updateToday();
149             }
150
151             // starts with second-to-last date
152
153             while ($suc->fetch()) {
154                 $userCounts[$suc->registration_date] = $suc->user_count;
155             }
156
157             self::cacheSet('sitemap:user:counts', $userCounts);
158         }
159
160         return $userCounts;
161     }
162
163     static function initializeCounts()
164     {
165         $firstDate = self::getFirstDate(); // awww
166         $today     = self::today();
167
168         $counts = array();
169
170         for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
171             common_debug("Date = '$d'");
172             $n = self::getCount($d);
173             self::insertCount($d, $n);
174             $counts[$d] = $n;
175         }
176
177         return $counts;
178     }
179
180     static function fillInCounts($lastDate)
181     {
182         $today = self::today();
183
184         $counts = array();
185
186         $n = self::getCount($lastDate);
187         self::updateCount($lastDate, $n);
188
189         $counts[$lastDate] = $n;
190
191         for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
192             $n = self::getCount($d);
193             self::insertCount($d, $n);
194         }
195
196         return $counts;
197     }
198
199     static function updateToday()
200     {
201         $today = self::today();
202
203         $n = self::getCount($today);
204         self::updateCount($today, $n);
205
206         return $n;
207     }
208
209     static function getCount($d)
210     {
211         $user = new User();
212         $user->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
213         $n = $user->count();
214
215         return $n;
216     }
217
218     static function insertCount($d, $n)
219     {
220         common_debug("Inserting count '$n' for '$d'");
221
222         $suc = new Sitemap_user_count();
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->insert()) {
230             common_log(LOG_WARNING, "Could not save user counts for '$d'");
231         }
232     }
233
234     static function updateCount($d, $n)
235     {
236         $suc = Sitemap_user_count::staticGet('registration_date', DB_DataObject_Cast::date($d));
237
238         if (empty($suc)) {
239             throw new Exception("No such registration date: $d");
240         }
241
242         $orig = clone($suc);
243
244         $suc->registration_date = DB_DataObject_Cast::date($d);
245         $suc->user_count        = $n;
246         $suc->created           = common_sql_now();
247         $suc->modified          = $suc->created;
248
249         if (!$suc->update($orig)) {
250             common_log(LOG_WARNING, "Could not save user counts for '$d'");
251         }
252     }
253
254     static function incrementDay($d)
255     {
256         $dt = self::dateStrToInt($d);
257         return self::dateIntToStr($dt + 24 * 60 * 60);
258     }
259
260     static function dateStrToInt($d)
261     {
262         return strtotime($d.' 00:00:00');
263     }
264
265     static function dateIntToStr($dt)
266     {
267         return date('Y-m-d', $dt);
268     }
269
270     static function getFirstDate()
271     {
272         $u = new User();
273         $u->selectAdd();
274         $u->selectAdd('date(min(created)) as first_date');
275         if ($u->find(true)) {
276             return $u->first_date;
277         } else {
278             // Is this right?
279             return self::dateIntToStr(time());
280         }
281     }
282
283     static function today()
284     {
285         return self::dateIntToStr(time());
286     }
287 }