]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Federation.php
Remove DI::app() dependency again
[friendica.git] / src / Module / Admin / Federation.php
1 <?php
2
3 namespace Friendica\Module\Admin;
4
5 use Friendica\Core\Renderer;
6 use Friendica\Database\DBA;
7 use Friendica\DI;
8 use Friendica\Module\BaseAdmin;
9
10 class Federation extends BaseAdmin
11 {
12         public static function content(array $parameters = [])
13         {
14                 parent::content($parameters);
15
16                 // get counts on active federation systems this node is knowing
17                 // We list the more common systems by name. The rest is counted as "other"
18                 $systems = [
19                         'friendica'   => ['name' => 'Friendica', 'color' => '#ffc018'], // orange from the logo
20                         'diaspora'    => ['name' => 'Diaspora', 'color' => '#a1a1a1'], // logo is black and white, makes a gray
21                         'funkwhale'   => ['name' => 'Funkwhale', 'color' => '#4082B4'], // From the homepage
22                         'gnusocial'   => ['name' => 'GNU Social/Statusnet', 'color' => '#a22430'], // dark red from the logo
23                         'hubzilla'    => ['name' => 'Hubzilla/Red Matrix', 'color' => '#43488a'], // blue from the logo
24                         'mastodon'    => ['name' => 'Mastodon', 'color' => '#1a9df9'], // blue from the Mastodon logo
25                         'misskey'     => ['name' => 'Misskey', 'color' => '#ccfefd'], // Font color of the homepage
26                         'peertube'    => ['name' => 'Peertube', 'color' => '#ffad5c'], // One of the logo colors
27                         'pixelfed'    => ['name' => 'Pixelfed', 'color' => '#11da47'], // One of the logo colors
28                         'pleroma'     => ['name' => 'Pleroma', 'color' => '#E46F0F'], // Orange from the text that is used on Pleroma instances
29                         'plume'       => ['name' => 'Plume', 'color' => '#7765e3'], // From the homepage
30                         'socialhome'  => ['name' => 'SocialHome', 'color' => '#52056b'], // lilac from the Django Image used at the Socialhome homepage
31                         'wordpress'   => ['name' => 'WordPress', 'color' => '#016087'], // Background color of the homepage
32                         'writefreely' => ['name' => 'WriteFreely', 'color' => '#292929'], // Font color of the homepage
33                         'other'       => ['name' => DI::l10n()->t('Other'), 'color' => '#F1007E'], // ActivityPub main color
34                 ];
35
36                 $platforms = array_keys($systems);
37
38                 $counts = [];
39                 foreach ($platforms as $platform) {
40                         $counts[$platform] = [];
41                 }
42
43                 $total = 0;
44                 $users = 0;
45
46                 $gservers = DBA::p("SELECT COUNT(*) AS `total`, SUM(`registered-users`) AS `users`, `platform`,
47                         ANY_VALUE(`network`) AS `network`, MAX(`version`) AS `version`
48                         FROM `gserver` WHERE `last_contact` >= `last_failure` GROUP BY `platform`");
49                 while ($gserver = DBA::fetch($gservers)) {
50                         $total += $gserver['total'];
51                         $users += $gserver['users'];
52
53                         $versionCounts = [];
54                         $versions = DBA::p("SELECT COUNT(*) AS `total`, `version` FROM `gserver`
55                                 WHERE `last_contact` >= `last_failure` AND `platform` = ?
56                                 GROUP BY `version` ORDER BY `version`", $gserver['platform']);
57                         while ($version = DBA::fetch($versions)) {
58                                 $version['version'] = str_replace(["\n", "\r", "\t"], " ", $version['version']);
59
60                                 if (in_array($gserver['platform'], ['Red Matrix', 'redmatrix', 'red'])) {
61                                         $version['version'] = 'Red ' . $version['version'];
62                                 }
63
64                                 $versionCounts[] = $version;
65                         }
66                         DBA::close($versions);
67
68                         $platform = $gserver['platform'] = strtolower($gserver['platform']);
69
70                         if ($platform == 'friendika') {
71                                 $platform = 'friendica';
72                         } elseif (in_array($platform, ['red matrix', 'redmatrix', 'red'])) {
73                                 $platform = 'hubzilla';
74                         } elseif(stristr($platform, 'pleroma')) {
75                                 $platform = 'pleroma';
76                         } elseif(stristr($platform, 'statusnet')) {
77                                 $platform = 'gnusocial';
78                         } elseif(stristr($platform, 'wordpress')) {
79                                 $platform = 'wordpress';
80                         } elseif (!in_array($platform, $platforms)) {
81                                 $platform = 'other';
82                         }
83
84                         if ($platform != $gserver['platform']) {
85                                 if ($platform == 'other') {
86                                         $versionCounts = $counts[$platform][1] ?? [];
87                                         $versionCounts[] = ['version' => $gserver['platform'] ?: DI::l10n()->t('unknown'), 'total' => $gserver['total']];
88                                         $gserver['version'] = '';
89                                 } else {
90                                         $versionCounts = array_merge($versionCounts, $counts[$platform][1] ?? []);
91                                 }
92
93                                 $gserver['platform'] = $platform;
94                                 $gserver['total'] += $counts[$platform][0]['total'] ?? 0;
95                                 $gserver['users'] += $counts[$platform][0]['users'] ?? 0;
96                         }
97
98                         if ($platform == 'friendica') {
99                                 $versionCounts = self::reformaFriendicaVersions($versionCounts);
100                         } elseif ($platform == 'pleroma') {
101                                 $versionCounts = self::reformaPleromaVersions($versionCounts);
102                         } elseif ($platform == 'diaspora') {
103                                 $versionCounts = self::reformaDiasporaVersions($versionCounts);
104                         }
105
106                         $versionCounts = self::sortVersion($versionCounts);
107
108                         $gserver['platform'] = $systems[$platform]['name'];
109
110                         $counts[$platform] = [$gserver, $versionCounts, str_replace([' ', '%'], '', $platform), $systems[$platform]['color']];
111                 }
112                 DBA::close($gserver);
113
114                 // some helpful text
115                 $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.');
116                 $hint = DI::l10n()->t('The <em>Auto Discovered Contact Directory</em> feature is not enabled, it will improve the data displayed here.');
117
118                 // load the template, replace the macros and return the page content
119                 $t = Renderer::getMarkupTemplate('admin/federation.tpl');
120                 return Renderer::replaceMacros($t, [
121                         '$title' => DI::l10n()->t('Administration'),
122                         '$page' => DI::l10n()->t('Federation Statistics'),
123                         '$intro' => $intro,
124                         '$hint' => $hint,
125                         '$autoactive' => DI::config()->get('system', 'poco_completion'),
126                         '$counts' => $counts,
127                         '$version' => FRIENDICA_VERSION,
128                         '$legendtext' => DI::l10n()->t('Currently this node is aware of %d nodes with %d registered users from the following platforms:', $total, $users),
129                 ]);
130         }
131
132         /**
133          * early friendica versions have the format x.x.xxxx where xxxx is the
134          * DB version stamp; those should be operated out and versions be combined
135          *
136          * @param array $versionCounts list of version numbers
137          * @return array with cleaned version numbers
138          */
139         private static function reformaFriendicaVersions(array $versionCounts)
140         {
141                 $newV = [];
142                 $newVv = [];
143                 foreach ($versionCounts as $vv) {
144                         $newVC = $vv['total'];
145                         $newVV = $vv['version'];
146                         $lastDot = strrpos($newVV, '.');
147                         $len = strlen($newVV) - 1;
148                         if (($lastDot == $len - 4) && (!strrpos($newVV, '-rc') == $len - 3)) {
149                                 $newVV = substr($newVV, 0, $lastDot);
150                         }
151                         if (isset($newV[$newVV])) {
152                                 $newV[$newVV] += $newVC;
153                         } else {
154                                 $newV[$newVV] = $newVC;
155                         }
156                 }
157                 foreach ($newV as $key => $value) {
158                         array_push($newVv, ['total' => $value, 'version' => $key]);
159                 }
160                 $versionCounts = $newVv;
161
162                 return $versionCounts;
163         }
164
165         /**
166          * in the DB the Diaspora versions have the format x.x.x.x-xx the last
167          * part (-xx) should be removed to clean up the versions from the "head
168          * commit" information and combined into a single entry for x.x.x.x
169          *
170          * @param array $versionCounts list of version numbers
171          * @return array with cleaned version numbers
172          */
173         private static function reformaDiasporaVersions(array $versionCounts)
174         {
175                 $newV = [];
176                 $newVv = [];
177                 foreach ($versionCounts as $vv) {
178                         $newVC = $vv['total'];
179                         $newVV = $vv['version'];
180                         $posDash = strpos($newVV, '-');
181                         if ($posDash) {
182                                 $newVV = substr($newVV, 0, $posDash);
183                         }
184                         if (isset($newV[$newVV])) {
185                                 $newV[$newVV] += $newVC;
186                         } else {
187                                 $newV[$newVV] = $newVC;
188                         }
189                 }
190                 foreach ($newV as $key => $value) {
191                         array_push($newVv, ['total' => $value, 'version' => $key]);
192                 }
193                 $versionCounts = $newVv;
194
195                 return $versionCounts;
196         }
197
198         /**
199          * Clean up Pleroma version numbers
200          *
201          * @param array $versionCounts list of version numbers
202          * @return array with cleaned version numbers
203          */
204         private static function reformaPleromaVersions(array $versionCounts)
205         {
206                 $compacted = [];
207                 foreach ($versionCounts as $key => $value) {
208                         $version = $versionCounts[$key]['version'];
209                         $parts = explode(' ', trim($version));
210                         do {
211                                 $part = array_pop($parts);
212                         } while (!empty($parts) && ((strlen($part) >= 40) || (strlen($part) <= 3)));
213                         // only take the x.x.x part of the version, not the "release" after the dash
214                         if (!empty($part) && strpos($part, '-')) {
215                                 $part = explode('-', $part)[0];
216                         }
217                         if (!empty($part)) {
218                                 if (empty($compacted[$part])) {
219                                         $compacted[$part] = $versionCounts[$key]['total'];
220                                 } else {
221                                         $compacted[$part] += $versionCounts[$key]['total'];
222                                 }
223                         }
224                 }
225
226                 $versionCounts = [];
227                 foreach ($compacted as $version => $pl_total) {
228                         $versionCounts[] = ['version' => $version, 'total' => $pl_total];
229                 }
230
231                 return $versionCounts;
232         }
233
234         /**
235          * Reformat, sort and compact version numbers
236          *
237          * @param array $versionCounts list of version numbers
238          * @return array with reformatted version numbers
239          */
240         private static function sortVersion(array $versionCounts)
241         {
242                 //
243                 // clean up version numbers
244                 //
245                 // some platforms do not provide version information, add a unkown there
246                 // to the version string for the displayed list.
247                 foreach ($versionCounts as $key => $value) {
248                         if ($versionCounts[$key]['version'] == '') {
249                                 $versionCounts[$key] = ['total' => $versionCounts[$key]['total'], 'version' => DI::l10n()->t('unknown')];
250                         }
251                 }
252
253                 // Assure that the versions are sorted correctly
254                 $v2 = [];
255                 $versions = [];
256                 foreach ($versionCounts as $vv) {
257                         $version = trim(strip_tags($vv["version"]));
258                         $v2[$version] = $vv;
259                         $versions[] = $version;
260                 }
261
262                 usort($versions, 'version_compare');
263
264                 $versionCounts = [];
265                 foreach ($versions as $version) {
266                         $versionCounts[] = $v2[$version];
267                 }
268
269                 return $versionCounts;
270         }
271 }