]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Federation.php
Show detailled statistics per platform
[friendica.git] / src / Module / Admin / Federation.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Module\Admin;
23
24 use Friendica\Core\Protocol;
25 use Friendica\Core\Renderer;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\GServer;
29 use Friendica\Module\BaseAdmin;
30
31 class Federation extends BaseAdmin
32 {
33         protected function content(array $request = []): string
34         {
35                 parent::content();
36
37                 // get counts on active federation systems this node is knowing
38                 // We list the more common systems by name. The rest is counted as "other"
39                 $systems = [
40                         'friendica'    => ['name' => 'Friendica', 'color' => '#ffc018'], // orange from the logo
41                         'birdsitelive' => ['name' => 'BirdsiteLIVE', 'color' => '#1b6ec2'], // Color from the page
42                         'bookwyrm'     => ['name' => 'BookWyrm', 'color' => '#00d1b2'], // Color from the page
43                         'diaspora'     => ['name' => 'Diaspora', 'color' => '#a1a1a1'], // logo is black and white, makes a gray
44                         'funkwhale'    => ['name' => 'Funkwhale', 'color' => '#4082B4'], // From the homepage
45                         'gnusocial'    => ['name' => 'GNU Social/Statusnet', 'color' => '#a22430'], // dark red from the logo
46                         'hometown'     => ['name' => 'Hometown', 'color' => '#1f70c1'], // Color from the Patreon page
47                         'hubzilla'     => ['name' => 'Hubzilla/Red Matrix', 'color' => '#43488a'], // blue from the logo
48                         'lemmy'        => ['name' => 'Lemmy', 'color' => '#00c853'], // Green from the page
49                         'mastodon'     => ['name' => 'Mastodon', 'color' => '#1a9df9'], // blue from the Mastodon logo
50                         'misskey'      => ['name' => 'Misskey', 'color' => '#ccfefd'], // Font color of the homepage
51                         'mobilizon'    => ['name' => 'Mobilizon', 'color' => '#ffd599'], // Background color of parts of the homepage
52                         'nextcloud'    => ['name' => 'Nextcloud', 'color' => '#1cafff'], // Logo color
53                         'mistpark'     => ['name' => 'Nomad projects (Mistpark, Osada, Roadhouse, Zap)', 'color' => '#348a4a'], // Green like the Mistpark green
54                         'peertube'     => ['name' => 'Peertube', 'color' => '#ffad5c'], // One of the logo colors
55                         'pixelfed'     => ['name' => 'Pixelfed', 'color' => '#11da47'], // One of the logo colors
56                         'pleroma'      => ['name' => 'Pleroma', 'color' => '#E46F0F'], // Orange from the text that is used on Pleroma instances
57                         'plume'        => ['name' => 'Plume', 'color' => '#7765e3'], // From the homepage
58                         'relay'        => ['name' => 'ActivityPub Relay', 'color' => '#888888'], // Grey like the second color of the ActivityPub logo
59                         'socialhome'   => ['name' => 'SocialHome', 'color' => '#52056b'], // lilac from the Django Image used at the Socialhome homepage
60                         'wordpress'    => ['name' => 'WordPress', 'color' => '#016087'], // Background color of the homepage
61                         'write.as'     => ['name' => 'Write.as', 'color' => '#00ace3'], // Border color of the homepage
62                         'writefreely'  => ['name' => 'WriteFreely', 'color' => '#292929'], // Font color of the homepage
63                         'other'        => ['name' => DI::l10n()->t('Other'), 'color' => '#F1007E'], // ActivityPub main color
64                 ];
65
66                 $platforms = array_keys($systems);
67
68                 $counts = [];
69                 foreach ($platforms as $platform) {
70                         $counts[$platform] = [];
71                 }
72
73                 $total    = 0;
74                 $users    = 0;
75                 $month    = 0;
76                 $halfyear = 0;
77                 $posts    = 0;
78
79                 $gservers = DBA::p("SELECT COUNT(*) AS `total`, SUM(`registered-users`) AS `users`,
80                         SUM(IFNULL(`local-posts`, 0) + IFNULL(`local-comments`, 0)) AS `posts`,
81                         SUM(IFNULL(`active-month-users`, `active-week-users`)) AS `month`,
82                         SUM(IFNULL(`active-halfyear-users`, `active-week-users`)) AS `halfyear`, `platform`,
83                         ANY_VALUE(`network`) AS `network`, MAX(`version`) AS `version`
84                         FROM `gserver` WHERE NOT `failed` AND `detection-method` != ? AND NOT `network` IN (?, ?) GROUP BY `platform`", GServer::DETECT_MANUAL, Protocol::PHANTOM, Protocol::FEED);
85                 while ($gserver = DBA::fetch($gservers)) {
86                         $total    += $gserver['total'];
87                         $users    += $gserver['users'];
88                         $month    += $gserver['month'];
89                         $halfyear += $gserver['halfyear'];
90                         $posts    += $gserver['posts'];
91
92                         $versionCounts = [];
93                         $versions = DBA::p("SELECT COUNT(*) AS `total`, `version` FROM `gserver`
94                                 WHERE NOT `failed` AND `platform` = ? AND `detection-method` != ? AND NOT `network` IN (?, ?)
95                                 GROUP BY `version` ORDER BY `version`", $gserver['platform'], GServer::DETECT_MANUAL, Protocol::PHANTOM, Protocol::FEED);
96                         while ($version = DBA::fetch($versions)) {
97                                 $version['version'] = str_replace(["\n", "\r", "\t"], " ", $version['version']);
98
99                                 if (in_array($gserver['platform'], ['Red Matrix', 'redmatrix', 'red'])) {
100                                         $version['version'] = 'Red ' . $version['version'];
101                                 } elseif (in_array($gserver['platform'], ['osada', 'mistpark', 'roadhouse', 'zap'])) {
102                                         $version['version'] = $gserver['platform'] . ' ' . $version['version'];
103                                 } elseif (in_array($gserver['platform'], ['activityrelay', 'pub-relay', 'selective-relay', 'aoderelay'])) {
104                                         $version['version'] = $gserver['platform'] . '-' . $version['version'];
105                                 }
106
107                                 $versionCounts[] = $version;
108                         }
109                         DBA::close($versions);
110
111                         $platform = $gserver['platform'] = strtolower($gserver['platform']);
112
113                         if ($platform == 'friendika') {
114                                 $platform = 'friendica';
115                         } elseif (in_array($platform, ['red matrix', 'redmatrix', 'red'])) {
116                                 $platform = 'hubzilla';
117                         } elseif (in_array($platform, ['mistpark', 'osada', 'roadhouse', 'zap'])) {
118                                 $platform = 'mistpark';
119                         } elseif(stristr($platform, 'pleroma')) {
120                                 $platform = 'pleroma';
121                         } elseif(stristr($platform, 'statusnet')) {
122                                 $platform = 'gnusocial';
123                         } elseif(stristr($platform, 'wordpress')) {
124                                 $platform = 'wordpress';
125                         } elseif (in_array($platform, ['activityrelay', 'pub-relay', 'selective-relay', 'aoderelay'])) {
126                                 $platform = 'relay';
127                         } elseif (!in_array($platform, $platforms)) {
128                                 $platform = 'other';
129                         }
130
131                         if ($platform != $gserver['platform']) {
132                                 if ($platform == 'other') {
133                                         $versionCounts = $counts[$platform][1] ?? [];
134                                         $versionCounts[] = ['version' => $gserver['platform'] ?: DI::l10n()->t('unknown'), 'total' => $gserver['total']];
135                                         $gserver['version'] = '';
136                                 } else {
137                                         $versionCounts = array_merge($versionCounts, $counts[$platform][1] ?? []);
138                                 }
139
140                                 $gserver['platform']  = $platform;
141                                 $gserver['total']    += $counts[$platform][0]['total'] ?? 0;
142                                 $gserver['users']    += $counts[$platform][0]['users'] ?? 0;
143                                 $gserver['month']    += $counts[$platform][0]['month'] ?? 0;
144                                 $gserver['halfyear'] += $counts[$platform][0]['halfyear'] ?? 0;
145                                 $gserver['posts']    += $counts[$platform][0]['posts'] ?? 0;
146                         }
147
148                         if ($platform == 'friendica') {
149                                 $versionCounts = self::reformaFriendicaVersions($versionCounts);
150                         } elseif ($platform == 'pleroma') {
151                                 $versionCounts = self::reformaPleromaVersions($versionCounts);
152                         } elseif ($platform == 'diaspora') {
153                                 $versionCounts = self::reformaDiasporaVersions($versionCounts);
154                         } elseif ($platform == 'relay') {
155                                 $versionCounts = self::reformatRelayVersions($versionCounts);
156                         } elseif (in_array($platform, ['funkwhale', 'mastodon', 'mobilizon', 'misskey'])) {
157                                 $versionCounts = self::removeVersionSuffixes($versionCounts);
158                         }
159
160                         if (!in_array($platform, ['other', 'relay', 'mistpark'])) {
161                                 $versionCounts = self::sortVersion($versionCounts);
162                         } else {
163                                 ksort($versionCounts);
164                         }
165
166                         $gserver['platform']    = $systems[$platform]['name'];
167                         $gserver['monthlbl']    = DI::l10n()->t('%d active users last month', $gserver['month']);
168                         $gserver['halfyearlbl'] = DI::l10n()->t('%d active users last six month', $gserver['halfyear']);
169                         $gserver['userslbl']    = DI::l10n()->t('%d registered users', $gserver['users']);
170                         $gserver['postslbl']    = DI::l10n()->t('%d local posts', $gserver['posts']);
171
172                         if (($gserver['users'] > 0) && ($gserver['posts'] > 0)) {
173                                 $gserver['postsuserlbl'] = DI::l10n()->t('%d posts per user', $gserver['posts'] / $gserver['users']);
174                         } else {
175                                 $gserver['postsuserlbl'] = '';
176                         }
177
178                         $counts[$platform] = [$gserver, $versionCounts, str_replace([' ', '%', '.'], '', $platform), $systems[$platform]['color']];
179                 }
180                 DBA::close($gserver);
181
182                 // some helpful text
183                 $intro = DI::l10n()->t('This page offers you some numbers to the known part of the federated social network your Friendica node is part of. These numbers are not complete but only reflect the part of the network your node is aware of.');
184
185                 // load the template, replace the macros and return the page content
186                 $t = Renderer::getMarkupTemplate('admin/federation.tpl');
187                 return Renderer::replaceMacros($t, [
188                         '$title' => DI::l10n()->t('Administration'),
189                         '$page' => DI::l10n()->t('Federation Statistics'),
190                         '$intro' => $intro,
191                         '$counts' => $counts,
192                         '$version' => FRIENDICA_VERSION,
193                         '$legendtext' => DI::l10n()->t('Currently this node is aware of %d nodes (%d active users last month, %d active users last six month, %d registered users in total) from the following platforms:', $total, $month, $halfyear, $users),
194                 ]);
195         }
196
197         /**
198          * early friendica versions have the format x.x.xxxx where xxxx is the
199          * DB version stamp; those should be operated out and versions be combined
200          *
201          * @param array $versionCounts list of version numbers
202          * @return array with cleaned version numbers
203          */
204         private static function reformaFriendicaVersions(array $versionCounts)
205         {
206                 $newV = [];
207                 $newVv = [];
208                 foreach ($versionCounts as $vv) {
209                         $newVC = $vv['total'];
210                         $newVV = $vv['version'];
211                         $lastDot = strrpos($newVV, '.');
212                         $firstDash = strpos($newVV, '-');
213                         $len = strlen($newVV) - 1;
214                         if (($lastDot == $len - 4) && (!strrpos($newVV, '-rc') == $len - 3) && (!$firstDash == $len - 1)) {
215                                 $newVV = substr($newVV, 0, $lastDot);
216                         }
217                         if (isset($newV[$newVV])) {
218                                 $newV[$newVV] += $newVC;
219                         } else {
220                                 $newV[$newVV] = $newVC;
221                         }
222                 }
223                 foreach ($newV as $key => $value) {
224                         array_push($newVv, ['total' => $value, 'version' => $key]);
225                 }
226                 $versionCounts = $newVv;
227
228                 return $versionCounts;
229         }
230
231         /**
232          * in the DB the Diaspora versions have the format x.x.x.x-xx the last
233          * part (-xx) should be removed to clean up the versions from the "head
234          * commit" information and combined into a single entry for x.x.x.x
235          *
236          * @param array $versionCounts list of version numbers
237          * @return array with cleaned version numbers
238          */
239         private static function reformaDiasporaVersions(array $versionCounts)
240         {
241                 $newV = [];
242                 $newVv = [];
243                 foreach ($versionCounts as $vv) {
244                         $newVC = $vv['total'];
245                         $newVV = $vv['version'];
246                         $posDash = strpos($newVV, '-');
247                         if ($posDash) {
248                                 $newVV = substr($newVV, 0, $posDash);
249                         }
250                         if (isset($newV[$newVV])) {
251                                 $newV[$newVV] += $newVC;
252                         } else {
253                                 $newV[$newVV] = $newVC;
254                         }
255                 }
256                 foreach ($newV as $key => $value) {
257                         array_push($newVv, ['total' => $value, 'version' => $key]);
258                 }
259                 $versionCounts = $newVv;
260
261                 return $versionCounts;
262         }
263
264         /**
265          * Clean up Pleroma version numbers
266          *
267          * @param array $versionCounts list of version numbers
268          * @return array with cleaned version numbers
269          */
270         private static function reformaPleromaVersions(array $versionCounts)
271         {
272                 $compacted = [];
273                 foreach ($versionCounts as $key => $value) {
274                         $version = $versionCounts[$key]['version'];
275                         $parts = explode(' ', trim($version));
276                         do {
277                                 $part = array_pop($parts);
278                         } while (!empty($parts) && ((strlen($part) >= 40) || (strlen($part) <= 3)));
279                         // only take the x.x.x part of the version, not the "release" after the dash
280                         if (!empty($part) && strpos($part, '-')) {
281                                 $part = explode('-', $part)[0];
282                         }
283                         if (!empty($part)) {
284                                 if (empty($compacted[$part])) {
285                                         $compacted[$part] = $versionCounts[$key]['total'];
286                                 } else {
287                                         $compacted[$part] += $versionCounts[$key]['total'];
288                                 }
289                         }
290                 }
291
292                 $versionCounts = [];
293                 foreach ($compacted as $version => $pl_total) {
294                         $versionCounts[] = ['version' => $version, 'total' => $pl_total];
295                 }
296
297                 return $versionCounts;
298         }
299
300         /**
301          * Clean up version numbers
302          *
303          * @param array $versionCounts list of version numbers
304          * @return array with cleaned version numbers
305          */
306         private static function removeVersionSuffixes(array $versionCounts)
307         {
308                 $compacted = [];
309                 foreach ($versionCounts as $key => $value) {
310                         $version = $versionCounts[$key]['version'];
311
312                         foreach ([' ', '+', '-', '#', '_', '~'] as $delimiter) {
313                                 $parts = explode($delimiter, trim($version));
314                                 $version = array_shift($parts);
315                         }
316
317                         if (empty($compacted[$version])) {
318                                 $compacted[$version] = $versionCounts[$key]['total'];
319                         } else {
320                                 $compacted[$version] += $versionCounts[$key]['total'];
321                         }
322                 }
323
324                 $versionCounts = [];
325                 foreach ($compacted as $version => $pl_total) {
326                         $versionCounts[] = ['version' => $version, 'total' => $pl_total];
327                 }
328
329                 return $versionCounts;
330         }
331
332         /**
333          * Clean up relay version numbers
334          *
335          * @param array $versionCounts list of version numbers
336          * @return array with cleaned version numbers
337          */
338         private static function reformatRelayVersions(array $versionCounts)
339         {
340                 $compacted = [];
341                 foreach ($versionCounts as $key => $value) {
342                         $version = $versionCounts[$key]['version'];
343
344                         $parts = explode(' ', trim($version));
345                         $version = array_shift($parts);
346
347                         if (empty($compacted[$version])) {
348                                 $compacted[$version] = $versionCounts[$key]['total'];
349                         } else {
350                                 $compacted[$version] += $versionCounts[$key]['total'];
351                         }
352                 }
353
354                 $versionCounts = [];
355                 foreach ($compacted as $version => $pl_total) {
356                         $versionCounts[] = ['version' => $version, 'total' => $pl_total];
357                 }
358
359                 return $versionCounts;
360         }
361
362         /**
363          * Reformat, sort and compact version numbers
364          *
365          * @param array $versionCounts list of version numbers
366          * @return array with reformatted version numbers
367          */
368         private static function sortVersion(array $versionCounts)
369         {
370                 //
371                 // clean up version numbers
372                 //
373                 // some platforms do not provide version information, add a unkown there
374                 // to the version string for the displayed list.
375                 foreach ($versionCounts as $key => $value) {
376                         if ($versionCounts[$key]['version'] == '') {
377                                 $versionCounts[$key] = ['total' => $versionCounts[$key]['total'], 'version' => DI::l10n()->t('unknown')];
378                         }
379                 }
380
381                 // Assure that the versions are sorted correctly
382                 $v2 = [];
383                 $versions = [];
384                 foreach ($versionCounts as $vv) {
385                         $version = trim(strip_tags($vv["version"]));
386                         $v2[$version] = $vv;
387                         $versions[] = $version;
388                 }
389
390                 usort($versions, 'version_compare');
391
392                 $versionCounts = [];
393                 foreach ($versions as $version) {
394                         $versionCounts[] = $v2[$version];
395                 }
396
397                 return $versionCounts;
398         }
399 }