fix_stats.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. 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. function getUserCount()
  81. {
  82. $users = new User();
  83. $userCount = $users->count();
  84. return $userCount;
  85. }
  86. function getPostCount()
  87. {
  88. $notices = new Notice();
  89. $notices->is_local = Notice::LOCAL_PUBLIC;
  90. $notices->whereAdd('reply_to IS NULL');
  91. $noticeCount = $notices->count();
  92. return $noticeCount;
  93. }
  94. function getCommentCount()
  95. {
  96. $notices = new Notice();
  97. $notices->is_local = Notice::LOCAL_PUBLIC;
  98. $notices->whereAdd('reply_to IS NOT NULL');
  99. $commentCount = $notices->count();
  100. return $commentCount;
  101. }