Sitemap_user_count.php 6.8 KB

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