3 * Data class for counting notice postings 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 notices by date
39 * We make a separate sitemap for each notice posted by date.
40 * To save ourselves some (not inconsiderable) processing effort,
41 * we cache this data in the sitemap_notice_count table. Each
42 * row represents a day since the site has been started, with a count
43 * of notices posted on that day. Since, after the end of the day,
44 * this number doesn't change, it's a good candidate for persistent caching.
48 * @author Evan Prodromou <evan@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
50 * @link http://status.net/
55 class Sitemap_notice_count extends Memcached_DataObject
57 public $__table = 'sitemap_notice_count'; // table name
59 public $notice_date; // date primary_key not_null
60 public $notice_count; // int(4)
65 * Get an instance by key
67 * This is a utility method to get a single instance with a given key value.
69 * @param string $k Key to use to lookup (usually 'notice_id' for this class)
70 * @param mixed $v Value to lookup
72 * @return Sitemap_notice_count object found, or null for no hits
76 function staticGet($k, $v=null)
78 return Memcached_DataObject::staticGet('Sitemap_notice_count', $k, $v);
82 * return table definition for DB_DataObject
84 * DB_DataObject needs to know something about the table to manipulate
85 * instances. This method provides all the DB_DataObject needs to know.
87 * @return array array of column definitions
92 return array('notice_date' => DB_DATAOBJECT_DATE + DB_DATAOBJECT_NOTNULL,
93 'notice_count' => DB_DATAOBJECT_INT,
94 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
95 'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
99 * return key definitions for DB_DataObject
101 * DB_DataObject needs to know about keys that the table has; this function
104 * @return array key definitions
109 return array('notice_date' => 'K');
113 * return key definitions for Memcached_DataObject
115 * Our caching system uses the same key definitions, but uses a different
116 * method to get them.
118 * @return array key definitions
123 return $this->keys();
126 static function getAll()
128 $noticeCounts = self::cacheGet('sitemap:notice:counts');
130 if ($noticeCounts === false) {
132 $snc = new Sitemap_notice_count();
133 $snc->orderBy('notice_date DESC');
135 // Fetch the first one to check up-to-date-itude
137 $n = $snc->find(true);
139 $today = self::today();
140 $noticeCounts = array();
142 if (!$n) { // No counts saved yet
143 $noticeCounts = self::initializeCounts();
144 } else if ($snc->notice_date < $today) { // There are counts but not up to today
145 $noticeCounts = self::fillInCounts($snc->notice_date);
146 } else if ($snc->notice_date == $today) { // Refresh today's
147 $noticeCounts[$today] = self::updateToday();
150 // starts with second-to-last date
152 while ($snc->fetch()) {
153 $noticeCounts[$snc->notice_date] = $snc->notice_count;
156 self::cacheSet('sitemap:notice:counts', $noticeCounts);
159 return $noticeCounts;
162 static function initializeCounts()
164 $firstDate = self::getFirstDate(); // awww
165 $today = self::today();
169 for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
170 $n = self::getCount($d);
171 self::insertCount($d, $n);
178 static function fillInCounts($lastDate)
180 $today = self::today();
184 $n = self::getCount($lastDate);
185 self::updateCount($lastDate, $n);
187 $counts[$lastDate] = $n;
189 for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
190 $n = self::getCount($d);
191 self::insertCount($d, $n);
197 static function updateToday()
199 $today = self::today();
201 $n = self::getCount($today);
202 self::updateCount($today, $n);
207 static function getCount($d)
209 $notice = new Notice();
210 $notice->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
211 $n = $notice->count();
216 static function insertCount($d, $n)
218 $snc = new Sitemap_notice_count();
220 $snc->notice_date = DB_DataObject_Cast::date($d);
222 $snc->notice_count = $n;
223 $snc->created = common_sql_now();
224 $snc->modified = $snc->created;
226 if (!$snc->insert()) {
227 common_log(LOG_WARNING, "Could not save user counts for '$d'");
231 static function updateCount($d, $n)
233 $snc = Sitemap_notice_count::staticGet('notice_date', DB_DataObject_Cast::date($d));
236 throw new Exception("No such registration date: $d");
241 $snc->notice_date = DB_DataObject_Cast::date($d);
243 $snc->notice_count = $n;
244 $snc->created = common_sql_now();
245 $snc->modified = $snc->created;
247 if (!$snc->update($orig)) {
248 common_log(LOG_WARNING, "Could not save user counts for '$d'");
252 static function incrementDay($d)
254 $dt = self::dateStrToInt($d);
255 return self::dateIntToStr($dt + 24 * 60 * 60);
258 static function dateStrToInt($d)
260 return strtotime($d.' 00:00:00');
263 static function dateIntToStr($dt)
265 return date('Y-m-d', $dt);
268 static function getFirstDate()
273 $n->selectAdd('date(min(created)) as first_date');
275 if ($n->find(true)) {
276 return $n->first_date;
279 return self::dateIntToStr(time());
283 static function today()
285 return self::dateIntToStr(time());