Sitemap_notice_count.php 7.0 KB

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