"item" is replaced by "post-view" / postupdate check added
[friendica.git/.git] / src / Model / Nodeinfo.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Core\Addon;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use stdClass;
28
29 /**
30  * Model interaction for the nodeinfo
31  */
32 class Nodeinfo
33 {
34         /**
35          * Updates the info about the current node
36          *
37          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
38          */
39         public static function update()
40         {
41                 $config = DI::config();
42                 $logger = DI::logger();
43
44                 // If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
45                 if (Addon::isEnabled('statistics_json')) {
46                         $config->set('system', 'nodeinfo', true);
47                         Addon::uninstall('statistics_json');
48                 }
49
50                 if (empty($config->get('system', 'nodeinfo'))) {
51                         return;
52                 }
53
54                 $userStats = User::getStatistics();
55
56                 $config->set('nodeinfo', 'total_users', $userStats['total_users']);
57                 $config->set('nodeinfo', 'active_users_halfyear', $userStats['active_users_halfyear']);
58                 $config->set('nodeinfo', 'active_users_monthly', $userStats['active_users_monthly']);
59                 $config->set('nodeinfo', 'active_users_weekly', $userStats['active_users_weekly']);
60
61                 $logger->debug('user statistics', $userStats);
62
63                 $items = DBA::p("SELECT COUNT(*) AS `total`, `gravity` FROM `post-view` WHERE `origin` AND NOT `deleted` AND `uid` != 0 AND `gravity` IN (?, ?) GROUP BY `gravity`",
64                         GRAVITY_PARENT, GRAVITY_COMMENT);
65                 while ($item = DBA::fetch($items)) {
66                         if ($item['gravity'] == GRAVITY_PARENT) {
67                                 $config->set('nodeinfo', 'local_posts', $item['total']);
68                         } elseif ($item['gravity'] == GRAVITY_COMMENT) {
69                                 $config->set('nodeinfo', 'local_comments', $item['total']);
70                         }
71                 }
72                 DBA::close($items);
73         }
74
75         /**
76          * Return the supported services
77          *
78          * @return Object with supported services
79         */
80         public static function getUsage(bool $version2 = false)
81         {
82                 $config = DI::config();
83
84                 $usage = new stdClass();
85
86                 if (!empty($config->get('system', 'nodeinfo'))) {
87                         $usage->users = [
88                                 'total'          => intval($config->get('nodeinfo', 'total_users')),
89                                 'activeHalfyear' => intval($config->get('nodeinfo', 'active_users_halfyear')),
90                                 'activeMonth'    => intval($config->get('nodeinfo', 'active_users_monthly'))
91                         ];
92                         $usage->localPosts = intval($config->get('nodeinfo', 'local_posts'));
93                         $usage->localComments = intval($config->get('nodeinfo', 'local_comments'));
94
95                         if ($version2) {
96                                 $usage->users['activeWeek'] = intval($config->get('nodeinfo', 'active_users_weekly'));
97                         }
98                 }
99
100                 return $usage;
101         }
102
103         /**
104          * Return the supported services
105          *
106          * @return array with supported services
107         */
108         public static function getServices()
109         {
110                 $services = [
111                         'inbound'  => [],
112                         'outbound' => [],
113                 ];
114
115                 if (Addon::isEnabled('blogger')) {
116                         $services['outbound'][] = 'blogger';
117                 }
118                 if (Addon::isEnabled('dwpost')) {
119                         $services['outbound'][] = 'dreamwidth';
120                 }
121                 if (Addon::isEnabled('statusnet')) {
122                         $services['inbound'][] = 'gnusocial';
123                         $services['outbound'][] = 'gnusocial';
124                 }
125                 if (Addon::isEnabled('ijpost')) {
126                         $services['outbound'][] = 'insanejournal';
127                 }
128                 if (Addon::isEnabled('libertree')) {
129                         $services['outbound'][] = 'libertree';
130                 }
131                 if (Addon::isEnabled('buffer')) {
132                         $services['outbound'][] = 'linkedin';
133                 }
134                 if (Addon::isEnabled('ljpost')) {
135                         $services['outbound'][] = 'livejournal';
136                 }
137                 if (Addon::isEnabled('buffer')) {
138                         $services['outbound'][] = 'pinterest';
139                 }
140                 if (Addon::isEnabled('posterous')) {
141                         $services['outbound'][] = 'posterous';
142                 }
143                 if (Addon::isEnabled('pumpio')) {
144                         $services['inbound'][] = 'pumpio';
145                         $services['outbound'][] = 'pumpio';
146                 }
147
148                 $services['outbound'][] = 'smtp';
149
150                 if (Addon::isEnabled('tumblr')) {
151                         $services['outbound'][] = 'tumblr';
152                 }
153                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
154                         $services['outbound'][] = 'twitter';
155                 }
156                 if (Addon::isEnabled('wppost')) {
157                         $services['outbound'][] = 'wordpress';
158                 }
159
160                 return $services;
161         }
162
163         public static function getOrganization($config)
164         {
165                 $organization = ['name' => null, 'contact' => null, 'account' => null];
166
167                 if (!empty($config->get('config', 'admin_email'))) {
168                         $adminList = explode(',', str_replace(' ', '', $config->get('config', 'admin_email')));
169                         $organization['contact'] = $adminList[0];
170                         $administrator = User::getByEmail($adminList[0], ['username', 'nickname']);
171                         if (!empty($administrator)) {
172                                 $organization['name'] = $administrator['username'];
173                                 $organization['account'] = DI::baseUrl()->get() . '/profile/' . $administrator['nickname'];
174                         }
175                 }
176
177                 return $organization;
178         }
179 }