]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Federation.php
Added parameters
[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($parameters)
14         {
15                 parent::content($parameters);
16
17                 // get counts on active friendica, diaspora, redmatrix, hubzilla, gnu
18                 // social and statusnet nodes this node is knowing
19                 //
20                 // We are looking for the following platforms in the DB, "Red" should find
21                 // all variants of that platform ID string as the q() function is stripping
22                 // off one % two of them are needed in the query
23                 // Add more platforms if you like, when one returns 0 known nodes it is not
24                 // displayed on the stats page.
25                 $platforms = ['Friendi%%a', 'Diaspora', '%%red%%', 'Hubzilla', 'BlaBlaNet', 'GNU Social', 'StatusNet', 'Mastodon', 'Pleroma', 'socialhome', 'ganggo'];
26                 $colors = [
27                         'Friendi%%a' => '#ffc018', // orange from the logo
28                         'Diaspora'   => '#a1a1a1', // logo is black and white, makes a gray
29                         '%%red%%'    => '#c50001', // fire red from the logo
30                         'Hubzilla'   => '#43488a', // blue from the logo
31                         'BlaBlaNet'  => '#3B5998', // blue from the navbar at blablanet-dot-com
32                         'GNU Social' => '#a22430', // dark red from the logo
33                         'StatusNet'  => '#789240', // the green from the logo (red and blue have already others
34                         'Mastodon'   => '#1a9df9', // blue from the Mastodon logo
35                         'Pleroma'    => '#E46F0F', // Orange from the text that is used on Pleroma instances
36                         'socialhome' => '#52056b', // lilac from the Django Image used at the Socialhome homepage
37                         'ganggo'     => '#69d7e2', // from the favicon
38                 ];
39                 $counts = [];
40                 $total = 0;
41                 $users = 0;
42
43                 foreach ($platforms as $platform) {
44                         // get a total count for the platform, the name and version of the
45                         // highest version and the protocol tpe
46                         $platformCount = DBA::fetchFirst('SELECT
47                         COUNT(*) AS `total`,
48                         SUM(`registered-users`) AS `users`,
49                         ANY_VALUE(`platform`) AS `platform`,
50                                 ANY_VALUE(`network`) AS `network`,
51                         MAX(`version`) AS `version` FROM `gserver`
52                                 WHERE `platform` LIKE ?
53                                 AND `last_contact` >= `last_failure`
54                                 ORDER BY `version` ASC', $platform);
55                         $total += $platformCount['total'];
56                         $users += $platformCount['users'];
57
58                         // what versions for that platform do we know at all?
59                         // again only the active nodes
60                         $versionCountsStmt = DBA::p('SELECT
61                         COUNT(*) AS `total`,
62                         `version` FROM `gserver`
63                                 WHERE `last_contact` >= `last_failure`
64                                 AND `platform` LIKE ?
65                                 GROUP BY `version`
66                                 ORDER BY `version`;', $platform);
67                         $versionCounts = DBA::toArray($versionCountsStmt);
68
69                         //
70                         // clean up version numbers
71                         //
72                         // some platforms do not provide version information, add a unkown there
73                         // to the version string for the displayed list.
74                         foreach ($versionCounts as $key => $value) {
75                                 if ($versionCounts[$key]['version'] == '') {
76                                         $versionCounts[$key] = ['total' => $versionCounts[$key]['total'], 'version' => L10n::t('unknown')];
77                                 }
78                         }
79
80                         // Reformat and compact version numbers
81                         if ($platform == 'Pleroma') {
82                                 $compacted = [];
83                                 foreach ($versionCounts as $key => $value) {
84                                         $version = $versionCounts[$key]['version'];
85                                         $parts = explode(' ', trim($version));
86                                         do {
87                                                 $part = array_pop($parts);
88                                         } while (!empty($parts) && ((strlen($part) >= 40) || (strlen($part) <= 3)));
89                                         // only take the x.x.x part of the version, not the "release" after the dash
90                                         if (!empty($part) && strpos($part, '-')) {
91                                                 $part = explode('-', $part)[0];
92                                         }
93                                         if (!empty($part)) {
94                                                 if (empty($compacted[$part])) {
95                                                         $compacted[$part] = $versionCounts[$key]['total'];
96                                                 } else {
97                                                         $compacted[$part] += $versionCounts[$key]['total'];
98                                                 }
99                                         }
100                                 }
101
102                                 $versionCounts = [];
103                                 foreach ($compacted as $version => $pl_total) {
104                                         $versionCounts[] = ['version' => $version, 'total' => $pl_total];
105                                 }
106                         }
107
108                         // in the DB the Diaspora versions have the format x.x.x.x-xx the last
109                         // part (-xx) should be removed to clean up the versions from the "head
110                         // commit" information and combined into a single entry for x.x.x.x
111                         if ($platform == 'Diaspora') {
112                                 $newV = [];
113                                 $newVv = [];
114                                 foreach ($versionCounts as $vv) {
115                                         $newVC = $vv['total'];
116                                         $newVV = $vv['version'];
117                                         $posDash = strpos($newVV, '-');
118                                         if ($posDash) {
119                                                 $newVV = substr($newVV, 0, $posDash);
120                                         }
121                                         if (isset($newV[$newVV])) {
122                                                 $newV[$newVV] += $newVC;
123                                         } else {
124                                                 $newV[$newVV] = $newVC;
125                                         }
126                                 }
127                                 foreach ($newV as $key => $value) {
128                                         array_push($newVv, ['total' => $value, 'version' => $key]);
129                                 }
130                                 $versionCounts = $newVv;
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
135                         // conbined
136                         if ($platform == 'Friendi%%a') {
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
159                         // Assure that the versions are sorted correctly
160                         $v2 = [];
161                         $versions = [];
162                         foreach ($versionCounts as $vv) {
163                                 $version = trim(strip_tags($vv["version"]));
164                                 $v2[$version] = $vv;
165                                 $versions[] = $version;
166                         }
167
168                         usort($versions, 'version_compare');
169
170                         $versionCounts = [];
171                         foreach ($versions as $version) {
172                                 $versionCounts[] = $v2[$version];
173                         }
174
175                         // the 3rd array item is needed for the JavaScript graphs as JS does
176                         // not like some characters in the names of variables...
177                         $counts[$platform] = [$platformCount, $versionCounts, str_replace([' ', '%'], '', $platform), $colors[$platform]];
178                 }
179
180                 // some helpful text
181                 $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.');
182                 $hint = L10n::t('The <em>Auto Discovered Contact Directory</em> feature is not enabled, it will improve the data displayed here.');
183
184                 // load the template, replace the macros and return the page content
185                 $t = Renderer::getMarkupTemplate('admin/federation.tpl');
186                 return Renderer::replaceMacros($t, [
187                         '$title' => L10n::t('Administration'),
188                         '$page' => L10n::t('Federation Statistics'),
189                         '$intro' => $intro,
190                         '$hint' => $hint,
191                         '$autoactive' => Config::get('system', 'poco_completion'),
192                         '$counts' => $counts,
193                         '$version' => FRIENDICA_VERSION,
194                         '$legendtext' => L10n::t('Currently this node is aware of %d nodes with %d registered users from the following platforms:', $total, $users),
195                 ]);
196         }
197 }