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