3 namespace Friendica\Module\Admin;
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;
11 class Federation extends BaseAdminModule
13 public static function content(array $parameters = [])
15 parent::content($parameters);
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"
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
39 $platforms = array_keys($systems);
42 foreach ($platforms as $platform) {
43 $counts[$platform] = [];
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'];
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;
64 DBA::close($versions);
66 $platform = $gserver['platform'];
68 if ($platform == 'Friendika') {
69 $platform = 'Friendica';
70 } elseif (in_array($platform, ['Red Matrix', 'redmatrix'])) {
72 } elseif(stristr($platform, 'pleroma')) {
73 $platform = 'pleroma';
74 } elseif(stristr($platform, 'wordpress')) {
75 $platform = 'wordpress';
76 } elseif (!in_array($platform, $platforms)) {
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'] = '';
86 $versionCounts = array_merge($versionCounts, $counts[$platform][1] ?? []);
89 $gserver['platform'] = $platform;
90 $gserver['total'] += $counts[$platform][0]['total'] ?? 0;
91 $gserver['users'] += $counts[$platform][0]['users'] ?? 0;
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);
102 $versionCounts = self::sortVersion($versionCounts);
104 $gserver['platform'] = $systems[$platform]['name'];
106 $counts[$platform] = [$gserver, $versionCounts, str_replace([' ', '%'], '', $platform), $systems[$platform]['color']];
108 DBA::close($gserver);
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.');
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'),
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),
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
132 * @param array $versionCounts list of version numbers
133 * @return array with cleaned version numbers
135 private static function reformaFriendicaVersions(array $versionCounts)
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);
147 if (isset($newV[$newVV])) {
148 $newV[$newVV] += $newVC;
150 $newV[$newVV] = $newVC;
153 foreach ($newV as $key => $value) {
154 array_push($newVv, ['total' => $value, 'version' => $key]);
156 $versionCounts = $newVv;
158 return $versionCounts;
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
166 * @param array $versionCounts list of version numbers
167 * @return array with cleaned version numbers
169 private static function reformaDiasporaVersions(array $versionCounts)
173 foreach ($versionCounts as $vv) {
174 $newVC = $vv['total'];
175 $newVV = $vv['version'];
176 $posDash = strpos($newVV, '-');
178 $newVV = substr($newVV, 0, $posDash);
180 if (isset($newV[$newVV])) {
181 $newV[$newVV] += $newVC;
183 $newV[$newVV] = $newVC;
186 foreach ($newV as $key => $value) {
187 array_push($newVv, ['total' => $value, 'version' => $key]);
189 $versionCounts = $newVv;
191 return $versionCounts;
195 * Clean up Pleroma version numbers
197 * @param array $versionCounts list of version numbers
198 * @return array with cleaned version numbers
200 private static function reformaPleromaVersions(array $versionCounts)
203 foreach ($versionCounts as $key => $value) {
204 $version = $versionCounts[$key]['version'];
205 $parts = explode(' ', trim($version));
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];
214 if (empty($compacted[$part])) {
215 $compacted[$part] = $versionCounts[$key]['total'];
217 $compacted[$part] += $versionCounts[$key]['total'];
223 foreach ($compacted as $version => $pl_total) {
224 $versionCounts[] = ['version' => $version, 'total' => $pl_total];
227 return $versionCounts;
231 * Reformat, sort and compact version numbers
233 * @param array $versionCounts list of version numbers
234 * @return array with reformatted version numbers
236 private static function sortVersion(array $versionCounts)
239 // clean up version numbers
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')];
249 // Assure that the versions are sorted correctly
252 foreach ($versionCounts as $vv) {
253 $version = trim(strip_tags($vv["version"]));
255 $versions[] = $version;
258 usort($versions, 'version_compare');
261 foreach ($versions as $version) {
262 $versionCounts[] = $v2[$version];
265 return $versionCounts;