fix_stats.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(dirname(dirname(__DIR__))));
  26. if (!defined('NODEINFO_UPGRADE')) {
  27. $longoptions = ['type='];
  28. $helptext = <<<END_OF_HELP
  29. fix_stats.php [options]
  30. Counts the stats from database values and updates the table.
  31. --type Type: can be 'users', 'posts' or 'comments'. Or 'all', to update all the types.
  32. END_OF_HELP;
  33. require_once INSTALLDIR . '/scripts/commandline.inc';
  34. $valid_types = ['all', 'users', 'posts', 'comments'];
  35. $verbose = have_option('v', 'verbose');
  36. $type_to_fix = get_option_value('type');
  37. if (!in_array($type_to_fix, $valid_types)) {
  38. echo "You must provide a valid type!\n\n";
  39. show_help();
  40. exit(1);
  41. }
  42. if ($verbose) {
  43. echo "Started.\n\n";
  44. }
  45. } else {
  46. echo "Nodeinfo will now fix stats\n";
  47. $type_to_fix = 'all';
  48. $verbose = true;
  49. }
  50. if ($type_to_fix == 'all' || $type_to_fix == 'users') {
  51. if ($verbose) {
  52. echo "[+] Updating Users stats...\n";
  53. }
  54. $us = Usage_stats::getKV('users');
  55. $us->count = getUserCount();
  56. $us->update();
  57. }
  58. if ($type_to_fix == 'all' || $type_to_fix == 'posts') {
  59. if ($verbose) {
  60. echo "[+] Updating Posts stats...\n";
  61. }
  62. $us = Usage_stats::getKV('posts');
  63. $us->count = getPostCount();
  64. $us->update();
  65. }
  66. if ($type_to_fix == 'all' || $type_to_fix == 'comments') {
  67. if ($verbose) {
  68. echo "[+] Updating Comments stats...\n";
  69. }
  70. $us = Usage_stats::getKV('comments');
  71. $us->count = getCommentCount();
  72. $us->update();
  73. }
  74. if ($verbose) {
  75. echo "\nDONE.\n";
  76. }
  77. /*
  78. * Counting functions
  79. */
  80. /**
  81. * Total number of users
  82. *
  83. * @return int
  84. * @author Stéphane Bérubé <chimo@chromic.org>
  85. */
  86. function getUserCount(): int
  87. {
  88. $users = new User();
  89. $userCount = $users->count();
  90. return $userCount;
  91. }
  92. /**
  93. * Total number of dents
  94. *
  95. * @return int
  96. * @author Stéphane Bérubé <chimo@chromic.org>
  97. */
  98. function getPostCount()
  99. {
  100. $notices = new Notice();
  101. $notices->is_local = Notice::LOCAL_PUBLIC;
  102. $notices->whereAdd('reply_to IS NULL');
  103. $noticeCount = $notices->count();
  104. return $noticeCount;
  105. }
  106. /**
  107. * Total number of replies
  108. *
  109. * @return int
  110. * @author Stéphane Bérubé <chimo@chromic.org>
  111. */
  112. function getCommentCount()
  113. {
  114. $notices = new Notice();
  115. $notices->is_local = Notice::LOCAL_PUBLIC;
  116. $notices->whereAdd('reply_to IS NOT NULL');
  117. $commentCount = $notices->count();
  118. return $commentCount;
  119. }