3 * Data class for counting user registrations by date
9 * @author Evan Prodromou <evan@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
37 * Data class for counting users by date
39 * We make a separate sitemap for each user registered by date.
40 * To save ourselves some processing effort, we cache this data
44 * @author Evan Prodromou <evan@status.net>
45 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46 * @link http://status.net/
51 class Sitemap_user_count extends Memcached_DataObject
53 public $__table = 'sitemap_user_count'; // table name
55 public $registration_date; // date primary_key not_null
56 public $user_count; // int(4)
61 * Get an instance by key
63 * This is a utility method to get a single instance with a given key value.
65 * @param string $k Key to use to lookup (usually 'user_id' for this class)
66 * @param mixed $v Value to lookup
68 * @return Sitemap_user_count object found, or null for no hits
72 function staticGet($k, $v=null)
74 return Memcached_DataObject::staticGet('Sitemap_user_count', $k, $v);
78 * return table definition for DB_DataObject
80 * DB_DataObject needs to know something about the table to manipulate
81 * instances. This method provides all the DB_DataObject needs to know.
83 * @return array array of column definitions
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);
95 * return key definitions for DB_DataObject
97 * DB_DataObject needs to know about keys that the table has; this function
100 * @return array key definitions
105 return array('registration_date' => 'K');
108 function sequenceKey()
110 return array(false, false, false);
114 * return key definitions for Memcached_DataObject
116 * Our caching system uses the same key definitions, but uses a different
117 * method to get them.
119 * @return array key definitions
124 return $this->keys();
127 static function getAll()
129 $userCounts = self::cacheGet('sitemap:user:counts');
131 if ($userCounts === false) {
133 $suc = new Sitemap_user_count();
134 $suc->orderBy('registration_date DESC');
136 // Fetch the first one to check up-to-date-itude
138 $n = $suc->find(true);
140 $today = self::today();
141 $userCounts = array();
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();
151 // starts with second-to-last date
153 while ($suc->fetch()) {
154 $userCounts[$suc->registration_date] = $suc->user_count;
157 // Cache user counts for 4 hours.
159 self::cacheSet('sitemap:user:counts', $userCounts, null, time() + 4 * 60 * 60);
165 static function initializeCounts()
167 $firstDate = self::getFirstDate(); // awww
168 $today = self::today();
172 for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
173 $n = self::getCount($d);
174 self::insertCount($d, $n);
181 static function fillInCounts($lastDate)
183 $today = self::today();
187 $n = self::getCount($lastDate);
188 self::updateCount($lastDate, $n);
190 $counts[$lastDate] = $n;
192 for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
193 $n = self::getCount($d);
194 self::insertCount($d, $n);
200 static function updateToday()
202 $today = self::today();
204 $n = self::getCount($today);
205 self::updateCount($today, $n);
210 static function getCount($d)
213 $user->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
219 static function insertCount($d, $n)
221 $suc = new Sitemap_user_count();
223 $suc->registration_date = DB_DataObject_Cast::date($d);
224 $suc->user_count = $n;
225 $suc->created = common_sql_now();
226 $suc->modified = $suc->created;
228 if (!$suc->insert()) {
229 common_log(LOG_WARNING, "Could not save user counts for '$d'");
233 static function updateCount($d, $n)
235 $suc = Sitemap_user_count::staticGet('registration_date', DB_DataObject_Cast::date($d));
238 throw new Exception("No such registration date: $d");
243 $suc->registration_date = DB_DataObject_Cast::date($d);
244 $suc->user_count = $n;
245 $suc->created = common_sql_now();
246 $suc->modified = $suc->created;
248 if (!$suc->update($orig)) {
249 common_log(LOG_WARNING, "Could not save user counts for '$d'");
253 static function incrementDay($d)
255 $dt = self::dateStrToInt($d);
256 return self::dateIntToStr($dt + 24 * 60 * 60);
259 static function dateStrToInt($d)
261 return strtotime($d.' 00:00:00');
264 static function dateIntToStr($dt)
266 return date('Y-m-d', $dt);
269 static function getFirstDate()
273 $u->selectAdd('date(min(created)) as first_date');
274 if ($u->find(true)) {
275 return $u->first_date;
278 return self::dateIntToStr(time());
282 static function today()
284 return self::dateIntToStr(time());