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