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/
50 class Sitemap_user_count extends Managed_DataObject
52 public $__table = 'sitemap_user_count'; // table name
54 public $registration_date; // date primary_key not_null
55 public $user_count; // int(4)
60 * return table definition for DB_DataObject
62 * DB_DataObject needs to know something about the table to manipulate
63 * instances. This method provides all the DB_DataObject needs to know.
65 * @return array array of column definitions
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);
76 * return key definitions for DB_DataObject
78 * DB_DataObject needs to know about keys that the table has; this function
81 * @return array key definitions
86 return array('registration_date' => 'K');
89 function sequenceKey()
91 return array(false, false, false);
95 * return key definitions for Memcached_DataObject
97 * Our caching system uses the same key definitions, but uses a different
100 * @return array key definitions
104 return $this->keys();
107 static function getAll()
109 $userCounts = self::cacheGet('sitemap:user:counts');
111 if ($userCounts === false) {
113 $suc = new Sitemap_user_count();
114 $suc->orderBy('registration_date DESC');
116 // Fetch the first one to check up-to-date-itude
118 $n = $suc->find(true);
120 $today = self::today();
121 $userCounts = array();
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();
131 // starts with second-to-last date
133 while ($suc->fetch()) {
134 $userCounts[$suc->registration_date] = $suc->user_count;
137 // Cache user counts for 4 hours.
139 self::cacheSet('sitemap:user:counts', $userCounts, null, time() + 4 * 60 * 60);
145 static function initializeCounts()
147 $firstDate = self::getFirstDate(); // awww
148 $today = self::today();
152 for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
153 $n = self::getCount($d);
154 self::insertCount($d, $n);
161 static function fillInCounts($lastDate)
163 $today = self::today();
167 $n = self::getCount($lastDate);
168 self::updateCount($lastDate, $n);
170 $counts[$lastDate] = $n;
172 for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
173 $n = self::getCount($d);
174 self::insertCount($d, $n);
180 static function updateToday()
182 $today = self::today();
184 $n = self::getCount($today);
185 self::updateCount($today, $n);
190 static function getCount($d)
193 $user->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
199 static function insertCount($d, $n)
201 $suc = new Sitemap_user_count();
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;
208 if (!$suc->insert()) {
209 common_log(LOG_WARNING, "Could not save user counts for '$d'");
213 static function updateCount($d, $n)
215 $suc = Sitemap_user_count::getKV('registration_date', DB_DataObject_Cast::date($d));
218 // TRANS: Exception thrown when a registration date cannot be found.
219 throw new Exception(_m("No such registration date: $d."));
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;
229 if (!$suc->update($orig)) {
230 common_log(LOG_WARNING, "Could not save user counts for '$d'");
234 static function incrementDay($d)
236 $dt = self::dateStrToInt($d);
237 return self::dateIntToStr($dt + 24 * 60 * 60);
240 static function dateStrToInt($d)
242 return strtotime($d.' 00:00:00');
245 static function dateIntToStr($dt)
247 return date('Y-m-d', $dt);
250 static function getFirstDate()
254 $u->selectAdd('date(min(created)) as first_date');
255 if ($u->find(true)) {
256 return $u->first_date;
259 return self::dateIntToStr(time());
263 static function today()
265 return self::dateIntToStr(time());