3 namespace Friendica\Module\Admin;
5 use Friendica\Core\Renderer;
6 use Friendica\Database\DBA;
8 use Friendica\Module\BaseAdminModule;
10 class Federation extends BaseAdminModule
12 public static function content(array $parameters = [])
14 parent::content($parameters);
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"
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
36 $platforms = array_keys($systems);
39 foreach ($platforms as $platform) {
40 $counts[$platform] = [];
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'];
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']);
60 if (in_array($gserver['platform'], ['Red Matrix', 'redmatrix', 'red'])) {
61 $version['version'] = 'Red ' . $version['version'];
64 $versionCounts[] = $version;
66 DBA::close($versions);
68 $platform = $gserver['platform'] = strtolower($gserver['platform']);
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)) {
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'] = '';
90 $versionCounts = array_merge($versionCounts, $counts[$platform][1] ?? []);
93 $gserver['platform'] = $platform;
94 $gserver['total'] += $counts[$platform][0]['total'] ?? 0;
95 $gserver['users'] += $counts[$platform][0]['users'] ?? 0;
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);
106 $versionCounts = self::sortVersion($versionCounts);
108 $gserver['platform'] = $systems[$platform]['name'];
110 $counts[$platform] = [$gserver, $versionCounts, str_replace([' ', '%'], '', $platform), $systems[$platform]['color']];
112 DBA::close($gserver);
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.');
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'),
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),
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
136 * @param array $versionCounts list of version numbers
137 * @return array with cleaned version numbers
139 private static function reformaFriendicaVersions(array $versionCounts)
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);
151 if (isset($newV[$newVV])) {
152 $newV[$newVV] += $newVC;
154 $newV[$newVV] = $newVC;
157 foreach ($newV as $key => $value) {
158 array_push($newVv, ['total' => $value, 'version' => $key]);
160 $versionCounts = $newVv;
162 return $versionCounts;
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
170 * @param array $versionCounts list of version numbers
171 * @return array with cleaned version numbers
173 private static function reformaDiasporaVersions(array $versionCounts)
177 foreach ($versionCounts as $vv) {
178 $newVC = $vv['total'];
179 $newVV = $vv['version'];
180 $posDash = strpos($newVV, '-');
182 $newVV = substr($newVV, 0, $posDash);
184 if (isset($newV[$newVV])) {
185 $newV[$newVV] += $newVC;
187 $newV[$newVV] = $newVC;
190 foreach ($newV as $key => $value) {
191 array_push($newVv, ['total' => $value, 'version' => $key]);
193 $versionCounts = $newVv;
195 return $versionCounts;
199 * Clean up Pleroma version numbers
201 * @param array $versionCounts list of version numbers
202 * @return array with cleaned version numbers
204 private static function reformaPleromaVersions(array $versionCounts)
207 foreach ($versionCounts as $key => $value) {
208 $version = $versionCounts[$key]['version'];
209 $parts = explode(' ', trim($version));
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];
218 if (empty($compacted[$part])) {
219 $compacted[$part] = $versionCounts[$key]['total'];
221 $compacted[$part] += $versionCounts[$key]['total'];
227 foreach ($compacted as $version => $pl_total) {
228 $versionCounts[] = ['version' => $version, 'total' => $pl_total];
231 return $versionCounts;
235 * Reformat, sort and compact version numbers
237 * @param array $versionCounts list of version numbers
238 * @return array with reformatted version numbers
240 private static function sortVersion(array $versionCounts)
243 // clean up version numbers
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')];
253 // Assure that the versions are sorted correctly
256 foreach ($versionCounts as $vv) {
257 $version = trim(strip_tags($vv["version"]));
259 $versions[] = $version;
262 usort($versions, 'version_compare');
265 foreach ($versions as $version) {
266 $versionCounts[] = $v2[$version];
269 return $versionCounts;