fix_stats.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * Fix Nodeinfo statistics
  19. *
  20. * @package NodeInfo
  21. * @author Diogo Cordeiro <diogo@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. define('INSTALLDIR', dirname(__DIR__, 3));
  26. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  27. if (!defined('NODEINFO_UPGRADE')) {
  28. $longoptions = ['type='];
  29. $helptext = <<<END_OF_HELP
  30. fix_stats.php [options]
  31. Counts the stats from database values and updates the table.
  32. --type Type: can be 'users', 'posts' or 'comments'. Or 'all', to update all the types.
  33. END_OF_HELP;
  34. require_once INSTALLDIR . '/scripts/commandline.inc';
  35. $valid_types = ['all', 'users', 'posts', 'comments'];
  36. $verbose = have_option('v', 'verbose');
  37. $type_to_fix = get_option_value('type');
  38. if (!in_array($type_to_fix, $valid_types)) {
  39. echo "You must provide a valid type!\n\n";
  40. show_help();
  41. exit(1);
  42. }
  43. if ($verbose) {
  44. echo "Started.\n\n";
  45. }
  46. } else {
  47. echo "Nodeinfo will now fix stats\n";
  48. $type_to_fix = 'all';
  49. $verbose = true;
  50. }
  51. if ($type_to_fix == 'all' || $type_to_fix == 'users') {
  52. if ($verbose) {
  53. echo "[+] Updating Users stats...\n";
  54. }
  55. $us = Usage_stats::getKV('users');
  56. $us->count = getUserCount();
  57. $us->update();
  58. }
  59. if ($type_to_fix == 'all' || $type_to_fix == 'posts') {
  60. if ($verbose) {
  61. echo "[+] Updating Posts stats...\n";
  62. }
  63. $us = Usage_stats::getKV('posts');
  64. $us->count = getPostCount();
  65. $us->update();
  66. }
  67. if ($type_to_fix == 'all' || $type_to_fix == 'comments') {
  68. if ($verbose) {
  69. echo "[+] Updating Comments stats...\n";
  70. }
  71. $us = Usage_stats::getKV('comments');
  72. $us->count = getCommentCount();
  73. $us->update();
  74. }
  75. if ($verbose) {
  76. echo "\nDONE.\n";
  77. }
  78. /*
  79. * Counting functions
  80. */
  81. /**
  82. * Total number of users
  83. *
  84. * @return int
  85. * @author Stéphane Bérubé <chimo@chromic.org>
  86. */
  87. function getUserCount(): int
  88. {
  89. $users = new User();
  90. $userCount = $users->count();
  91. return $userCount;
  92. }
  93. /**
  94. * Total number of dents
  95. *
  96. * @return int
  97. * @author Stéphane Bérubé <chimo@chromic.org>
  98. */
  99. function getPostCount()
  100. {
  101. $notices = new Notice();
  102. $notices->is_local = Notice::LOCAL_PUBLIC;
  103. $notices->whereAdd('reply_to IS NULL');
  104. $noticeCount = $notices->count();
  105. return $noticeCount;
  106. }
  107. /**
  108. * Total number of replies
  109. *
  110. * @return int
  111. * @author Stéphane Bérubé <chimo@chromic.org>
  112. */
  113. function getCommentCount()
  114. {
  115. $notices = new Notice();
  116. $notices->is_local = Notice::LOCAL_PUBLIC;
  117. $notices->whereAdd('reply_to IS NOT NULL');
  118. $commentCount = $notices->count();
  119. return $commentCount;
  120. }