Sitemap_user_count.php 6.8 KB

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