Sitemap_notice_count.php 6.9 KB

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