fix_stats.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * GNU social - a federating social network
  5. *
  6. * LICENCE: This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @category Plugin
  20. * @package GNUsocial
  21. * @copyright 2018 Free Software Foundation http://fsf.org
  22. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  23. * @link https://www.gnu.org/software/social/
  24. */
  25. define('INSTALLDIR', realpath(__DIR__ . '/../../..'));
  26. $longoptions = ['type='];
  27. $helptext = <<<END_OF_HELP
  28. fix_stats.php [options]
  29. Counts the stats from database values and updates the table.
  30. --type Type: can be 'users', 'posts' or 'comments'. Or 'all', to update all the types.
  31. END_OF_HELP;
  32. require_once INSTALLDIR . '/scripts/commandline.inc';
  33. $valid_types = ['all', 'users', 'posts', 'comments'];
  34. $verbose = have_option('v', 'verbose');
  35. $type_to_fix = get_option_value('type');
  36. if (!in_array($type_to_fix, $valid_types)) {
  37. echo "You must provide a valid type!\n\n";
  38. show_help();
  39. exit(1);
  40. }
  41. if ($verbose) {
  42. echo "Started.\n\n";
  43. }
  44. if ($type_to_fix == 'all' || $type_to_fix == 'users') {
  45. if ($verbose) {
  46. echo "[+] Updating Users stats...\n";
  47. }
  48. $us = Usage_stats::getKV('users');
  49. $us->count = getUserCount();
  50. $us->update();
  51. }
  52. if ($type_to_fix == 'all' || $type_to_fix == 'posts') {
  53. if ($verbose) {
  54. echo "[+] Updating Posts stats...\n";
  55. }
  56. $us = Usage_stats::getKV('posts');
  57. $us->count = getPostCount();
  58. $us->update();
  59. }
  60. if ($type_to_fix == 'all' || $type_to_fix == 'comments') {
  61. if ($verbose) {
  62. echo "[+] Updating Comments stats...\n";
  63. }
  64. $us = Usage_stats::getKV('comments');
  65. $us->count = getCommentCount();
  66. $us->update();
  67. }
  68. if ($verbose) {
  69. echo "\nDONE.\n";
  70. }
  71. /*
  72. * Counting functions
  73. */
  74. function getUserCount()
  75. {
  76. $users = new User();
  77. $userCount = $users->count();
  78. return $userCount;
  79. }
  80. function getPostCount()
  81. {
  82. $notices = new Notice();
  83. $notices->is_local = Notice::LOCAL_PUBLIC;
  84. $notices->whereAdd('reply_to IS NULL');
  85. $noticeCount = $notices->count();
  86. return $noticeCount;
  87. }
  88. function getCommentCount()
  89. {
  90. $notices = new Notice();
  91. $notices->is_local = Notice::LOCAL_PUBLIC;
  92. $notices->whereAdd('reply_to IS NOT NULL');
  93. $commentCount = $notices->count();
  94. return $commentCount;
  95. }