Sitemap_notice_count.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Data class for counting notice postings by date
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2010, StatusNet, Inc.
  15. *
  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.
  20. *
  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.
  25. *
  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/>.
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
  33. /**
  34. * Data class for counting notices by date
  35. *
  36. * We make a separate sitemap for each notice posted by date.
  37. * To save ourselves some (not inconsiderable) processing effort,
  38. * we cache this data in the sitemap_notice_count table. Each
  39. * row represents a day since the site has been started, with a count
  40. * of notices posted on that day. Since, after the end of the day,
  41. * this number doesn't change, it's a good candidate for persistent caching.
  42. *
  43. * @category Data
  44. * @package StatusNet
  45. * @author Evan Prodromou <evan@status.net>
  46. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  47. * @link http://status.net/
  48. *
  49. * @see DB_DataObject
  50. */
  51. class Sitemap_notice_count extends Managed_DataObject
  52. {
  53. public $__table = 'sitemap_notice_count'; // table name
  54. public $notice_date; // date primary_key not_null
  55. public $notice_count; // int(4)
  56. public $created; // datetime() not_null
  57. public $modified; // datetime not_null default_0000-00-00%2000%3A00%3A00
  58. public static function schemaDef()
  59. {
  60. return array(
  61. 'fields' => array(
  62. 'notice_date' => array('type' => 'date', 'not null' => true, 'description' => 'record date'),
  63. 'notice_count' => array('type' => 'int', 'not null' => true, 'description' => 'the notice count'),
  64. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  65. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  66. ),
  67. 'primary key' => array('notice_date'),
  68. );
  69. }
  70. static function getAll()
  71. {
  72. $noticeCounts = self::cacheGet('sitemap:notice:counts');
  73. if ($noticeCounts === false) {
  74. $snc = new Sitemap_notice_count();
  75. $snc->orderBy('notice_date DESC');
  76. // Fetch the first one to check up-to-date-itude
  77. $n = $snc->find(true);
  78. $today = self::today();
  79. $noticeCounts = array();
  80. if (!$n) { // No counts saved yet
  81. $noticeCounts = self::initializeCounts();
  82. } else if ($snc->notice_date < $today) { // There are counts but not up to today
  83. $noticeCounts = self::fillInCounts($snc->notice_date);
  84. } else if ($snc->notice_date == $today) { // Refresh today's
  85. $noticeCounts[$today] = self::updateToday();
  86. }
  87. // starts with second-to-last date
  88. while ($snc->fetch()) {
  89. $noticeCounts[$snc->notice_date] = $snc->notice_count;
  90. }
  91. // Cache notice counts for 4 hours.
  92. self::cacheSet('sitemap:notice:counts', $noticeCounts, null, time() + 4 * 60 * 60);
  93. }
  94. return $noticeCounts;
  95. }
  96. static function initializeCounts()
  97. {
  98. $firstDate = self::getFirstDate(); // awww
  99. $today = self::today();
  100. $counts = array();
  101. for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
  102. $n = self::getCount($d);
  103. self::insertCount($d, $n);
  104. $counts[$d] = $n;
  105. }
  106. return $counts;
  107. }
  108. static function fillInCounts($lastDate)
  109. {
  110. $today = self::today();
  111. $counts = array();
  112. $n = self::getCount($lastDate);
  113. self::updateCount($lastDate, $n);
  114. $counts[$lastDate] = $n;
  115. for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
  116. $n = self::getCount($d);
  117. self::insertCount($d, $n);
  118. }
  119. return $counts;
  120. }
  121. static function updateToday()
  122. {
  123. $today = self::today();
  124. $n = self::getCount($today);
  125. self::updateCount($today, $n);
  126. return $n;
  127. }
  128. static function getCount($d)
  129. {
  130. $notice = new Notice();
  131. $notice->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
  132. $notice->whereAdd('is_local = ' . Notice::LOCAL_PUBLIC);
  133. $n = $notice->count();
  134. return $n;
  135. }
  136. static function insertCount($d, $n)
  137. {
  138. $snc = new Sitemap_notice_count();
  139. $snc->notice_date = DB_DataObject_Cast::date($d);
  140. $snc->notice_count = $n;
  141. $snc->created = common_sql_now();
  142. $snc->modified = $snc->created;
  143. if (!$snc->insert()) {
  144. common_log(LOG_WARNING, "Could not save user counts for '$d'");
  145. }
  146. }
  147. static function updateCount($d, $n)
  148. {
  149. $snc = Sitemap_notice_count::getKV('notice_date', DB_DataObject_Cast::date($d));
  150. if (empty($snc)) {
  151. // TRANS: Exception
  152. throw new Exception(_m("No such registration date: $d."));
  153. }
  154. $orig = clone($snc);
  155. $snc->notice_date = DB_DataObject_Cast::date($d);
  156. $snc->notice_count = $n;
  157. $snc->created = common_sql_now();
  158. $snc->modified = $snc->created;
  159. if (!$snc->update($orig)) {
  160. common_log(LOG_WARNING, "Could not save user counts for '$d'");
  161. }
  162. }
  163. static function incrementDay($d)
  164. {
  165. $dt = self::dateStrToInt($d);
  166. return self::dateIntToStr($dt + 24 * 60 * 60);
  167. }
  168. static function dateStrToInt($d)
  169. {
  170. return strtotime($d.' 00:00:00');
  171. }
  172. static function dateIntToStr($dt)
  173. {
  174. return date('Y-m-d', $dt);
  175. }
  176. static function getFirstDate()
  177. {
  178. $n = new Notice();
  179. $n->selectAdd();
  180. $n->selectAdd('date(min(created)) as first_date');
  181. if ($n->find(true)) {
  182. return $n->first_date;
  183. } else {
  184. // Is this right?
  185. return self::dateIntToStr(time());
  186. }
  187. }
  188. static function today()
  189. {
  190. return self::dateIntToStr(time());
  191. }
  192. }