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 Memcached_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 * Get an instance by key
62 * This is a utility method to get a single instance with a given key value.
64 * @param string $k Key to use to lookup (usually 'user_id' for this class)
65 * @param mixed $v Value to lookup
67 * @return Sitemap_user_count object found, or null for no hits
70 function staticGet($k, $v=null)
72 return Memcached_DataObject::staticGet('Sitemap_user_count', $k, $v);
76 * return table definition for DB_DataObject
78 * DB_DataObject needs to know something about the table to manipulate
79 * instances. This method provides all the DB_DataObject needs to know.
81 * @return array array of column definitions
85 return array('registration_date' => DB_DATAOBJECT_DATE + DB_DATAOBJECT_NOTNULL,
86 'user_count' => DB_DATAOBJECT_INT,
87 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
88 'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
92 * return key definitions for DB_DataObject
94 * DB_DataObject needs to know about keys that the table has; this function
97 * @return array key definitions
102 return array('registration_date' => 'K');
105 function sequenceKey()
107 return array(false, false, false);
111 * return key definitions for Memcached_DataObject
113 * Our caching system uses the same key definitions, but uses a different
114 * method to get them.
116 * @return array key definitions
120 return $this->keys();
123 static function getAll()
125 $userCounts = self::cacheGet('sitemap:user:counts');
127 if ($userCounts === false) {
129 $suc = new Sitemap_user_count();
130 $suc->orderBy('registration_date DESC');
132 // Fetch the first one to check up-to-date-itude
134 $n = $suc->find(true);
136 $today = self::today();
137 $userCounts = array();
139 if (!$n) { // No counts saved yet
140 $userCounts = self::initializeCounts();
141 } else if ($suc->registration_date < $today) { // There are counts but not up to today
142 $userCounts = self::fillInCounts($suc->registration_date);
143 } else if ($suc->registration_date == $today) { // Refresh today's
144 $userCounts[$today] = self::updateToday();
147 // starts with second-to-last date
149 while ($suc->fetch()) {
150 $userCounts[$suc->registration_date] = $suc->user_count;
153 // Cache user counts for 4 hours.
155 self::cacheSet('sitemap:user:counts', $userCounts, null, time() + 4 * 60 * 60);
161 static function initializeCounts()
163 $firstDate = self::getFirstDate(); // awww
164 $today = self::today();
168 for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
169 $n = self::getCount($d);
170 self::insertCount($d, $n);
177 static function fillInCounts($lastDate)
179 $today = self::today();
183 $n = self::getCount($lastDate);
184 self::updateCount($lastDate, $n);
186 $counts[$lastDate] = $n;
188 for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
189 $n = self::getCount($d);
190 self::insertCount($d, $n);
196 static function updateToday()
198 $today = self::today();
200 $n = self::getCount($today);
201 self::updateCount($today, $n);
206 static function getCount($d)
209 $user->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
215 static function insertCount($d, $n)
217 $suc = new Sitemap_user_count();
219 $suc->registration_date = DB_DataObject_Cast::date($d);
220 $suc->user_count = $n;
221 $suc->created = common_sql_now();
222 $suc->modified = $suc->created;
224 if (!$suc->insert()) {
225 common_log(LOG_WARNING, "Could not save user counts for '$d'");
229 static function updateCount($d, $n)
231 $suc = Sitemap_user_count::staticGet('registration_date', DB_DataObject_Cast::date($d));
234 // TRANS: Exception thrown when a registration date cannot be found.
235 throw new Exception(_m("No such registration date: $d."));
240 $suc->registration_date = DB_DataObject_Cast::date($d);
241 $suc->user_count = $n;
242 $suc->created = common_sql_now();
243 $suc->modified = $suc->created;
245 if (!$suc->update($orig)) {
246 common_log(LOG_WARNING, "Could not save user counts for '$d'");
250 static function incrementDay($d)
252 $dt = self::dateStrToInt($d);
253 return self::dateIntToStr($dt + 24 * 60 * 60);
256 static function dateStrToInt($d)
258 return strtotime($d.' 00:00:00');
261 static function dateIntToStr($dt)
263 return date('Y-m-d', $dt);
266 static function getFirstDate()
270 $u->selectAdd('date(min(created)) as first_date');
271 if ($u->find(true)) {
272 return $u->first_date;
275 return self::dateIntToStr(time());
279 static function today()
281 return self::dateIntToStr(time());