]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Nodeinfo/scripts/fix_stats.php
Fix undefined variables on Favorite plugin by XRevan86
[quix0rs-gnu-social.git] / plugins / Nodeinfo / scripts / fix_stats.php
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
26 define('INSTALLDIR', realpath(__DIR__ . '/../../..'));
27
28 $longoptions = ['type='];
29
30 $helptext = <<<END_OF_HELP
31 fix_stats.php [options]
32 Counts the stats from database values and updates the table.
33
34     --type          Optional flag to specify the type to update. They all are updated by default.
35                        Type: can be 'users', 'posts' or 'comments'. Or 'all', to update all the types.
36
37 END_OF_HELP;
38
39 require_once INSTALLDIR . '/scripts/commandline.inc';
40
41 $valid_types = ['all', 'users', 'posts', 'comments'];
42
43 $verbose = have_option('v', 'verbose');
44
45 $type_to_fix = get_option_value('type');
46 if (!in_array($type_to_fix, $valid_types)) {
47     echo "You must provide a valid type!\n\n";
48     show_help();
49     exit(1);
50 }
51
52 if ($verbose) {
53     echo "Started.\n\n";
54 }
55
56 if ($type_to_fix == 'all' || $type_to_fix == 'users') {
57     if ($verbose) {
58         echo "[+] Updating Users stats...\n";
59     }
60     $us = Usage_stats::getKV('users');
61     $us->count = getUserCount();
62     $us->update();
63 }
64
65 if ($type_to_fix == 'all' || $type_to_fix == 'posts') {
66     if ($verbose) {
67         echo "[+] Updating Posts stats...\n";
68     }
69     $us = Usage_stats::getKV('posts');
70     $us->count = getPostCount();
71     $us->update();
72 }
73
74 if ($type_to_fix == 'all' || $type_to_fix == 'comments') {
75     if ($verbose) {
76         echo "[+] Updating Comments stats...\n";
77     }
78     $us = Usage_stats::getKV('comments');
79     $us->count = getCommentCount();
80     $us->update();
81 }
82
83 if ($verbose) {
84     echo "\nDONE.\n";
85 }
86
87 /*
88  * Counting functions
89  */
90
91 function getUserCount()
92 {
93     $users = new User();
94     $userCount = $users->count();
95
96     return $userCount;
97 }
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
106     return $noticeCount;
107 }
108
109 function getCommentCount()
110 {
111     $notices = new Notice();
112     $notices->is_local = Notice::LOCAL_PUBLIC;
113     $notices->whereAdd('reply_to IS NOT NULL');
114     $commentCount = $notices->count();
115
116     return $commentCount;
117 }