Sitemap_user_count.php 6.7 KB

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