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/
54 class Sitemap_notice_count extends Managed_DataObject
56 public $__table = 'sitemap_notice_count'; // table name
58 public $notice_date; // date primary_key not_null
59 public $notice_count; // int(4)
60 public $created; // datetime() not_null
61 public $modified; // datetime not_null default_0000-00-00%2000%3A00%3A00
63 public static function schemaDef()
67 'notice_date' => array('type' => 'date', 'not null' => true, 'description' => 'record date'),
68 'notice_count' => array('type' => 'int', 'not null' => true, 'description' => 'the notice count'),
69 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
70 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
72 'primary key' => array('notice_date'),
76 static function getAll()
78 $noticeCounts = self::cacheGet('sitemap:notice:counts');
80 if ($noticeCounts === false) {
81 $snc = new Sitemap_notice_count();
82 $snc->orderBy('notice_date DESC');
84 // Fetch the first one to check up-to-date-itude
86 $n = $snc->find(true);
88 $today = self::today();
89 $noticeCounts = array();
91 if (!$n) { // No counts saved yet
92 $noticeCounts = self::initializeCounts();
93 } else if ($snc->notice_date < $today) { // There are counts but not up to today
94 $noticeCounts = self::fillInCounts($snc->notice_date);
95 } else if ($snc->notice_date == $today) { // Refresh today's
96 $noticeCounts[$today] = self::updateToday();
99 // starts with second-to-last date
101 while ($snc->fetch()) {
102 $noticeCounts[$snc->notice_date] = $snc->notice_count;
105 // Cache notice counts for 4 hours.
107 self::cacheSet('sitemap:notice:counts', $noticeCounts, null, time() + 4 * 60 * 60);
110 return $noticeCounts;
113 static function initializeCounts()
115 $firstDate = self::getFirstDate(); // awww
116 $today = self::today();
120 for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
121 $n = self::getCount($d);
122 self::insertCount($d, $n);
129 static function fillInCounts($lastDate)
131 $today = self::today();
135 $n = self::getCount($lastDate);
136 self::updateCount($lastDate, $n);
138 $counts[$lastDate] = $n;
140 for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
141 $n = self::getCount($d);
142 self::insertCount($d, $n);
148 static function updateToday()
150 $today = self::today();
152 $n = self::getCount($today);
153 self::updateCount($today, $n);
158 static function getCount($d)
160 $notice = new Notice();
161 $notice->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
162 $notice->whereAdd('is_local = ' . Notice::LOCAL_PUBLIC);
163 $n = $notice->count();
168 static function insertCount($d, $n)
170 $snc = new Sitemap_notice_count();
172 $snc->notice_date = DB_DataObject_Cast::date($d);
174 $snc->notice_count = $n;
175 $snc->created = common_sql_now();
176 $snc->modified = $snc->created;
178 if (!$snc->insert()) {
179 common_log(LOG_WARNING, "Could not save user counts for '$d'");
183 static function updateCount($d, $n)
185 $snc = Sitemap_notice_count::getKV('notice_date', DB_DataObject_Cast::date($d));
189 throw new Exception(_m("No such registration date: $d."));
194 $snc->notice_date = DB_DataObject_Cast::date($d);
196 $snc->notice_count = $n;
197 $snc->created = common_sql_now();
198 $snc->modified = $snc->created;
200 if (!$snc->update($orig)) {
201 common_log(LOG_WARNING, "Could not save user counts for '$d'");
205 static function incrementDay($d)
207 $dt = self::dateStrToInt($d);
208 return self::dateIntToStr($dt + 24 * 60 * 60);
211 static function dateStrToInt($d)
213 return strtotime($d.' 00:00:00');
216 static function dateIntToStr($dt)
218 return date('Y-m-d', $dt);
221 static function getFirstDate()
226 $n->selectAdd('date(min(created)) as first_date');
228 if ($n->find(true)) {
229 return $n->first_date;
232 return self::dateIntToStr(time());
236 static function today()
238 return self::dateIntToStr(time());