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/Statusnet', 'color' => '#a22430'], // dark red from the logo
24 'hubzilla' => ['name' => 'Hubzilla/Red Matrix', '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 'socialhome' => ['name' => 'SocialHome', 'color' => '#52056b'], // lilac from the Django Image used at the Socialhome homepage
32 'wordpress' => ['name' => 'WordPress', 'color' => '#016087'], // Background color of the homepage
33 'writefreely' => ['name' => 'WriteFreely', 'color' => '#292929'], // Font color of the homepage
34 'other' => ['name' => L10n::t('Other'), 'color' => '#F1007E'], // ActivityPub main color
37 $platforms = array_keys($systems);
40 foreach ($platforms as $platform) {
41 $counts[$platform] = [];
47 $gservers = DBA::p("SELECT COUNT(*) AS `total`, SUM(`registered-users`) AS `users`, `platform`,
48 ANY_VALUE(`network`) AS `network`, MAX(`version`) AS `version`
49 FROM `gserver` WHERE `last_contact` >= `last_failure` GROUP BY `platform`");
50 while ($gserver = DBA::fetch($gservers)) {
51 $total += $gserver['total'];
52 $users += $gserver['users'];
55 $versions = DBA::p("SELECT COUNT(*) AS `total`, `version` FROM `gserver`
56 WHERE `last_contact` >= `last_failure` AND `platform` = ?
57 GROUP BY `version` ORDER BY `version`", $gserver['platform']);
58 while ($version = DBA::fetch($versions)) {
59 $version['version'] = str_replace(["\n", "\r", "\t"], " ", $version['version']);
61 if (in_array($gserver['platform'], ['Red Matrix', 'redmatrix', 'red'])) {
62 $version['version'] = 'Red ' . $version['version'];
65 $versionCounts[] = $version;
67 DBA::close($versions);
69 $platform = $gserver['platform'];
71 if ($platform == 'Friendika') {
72 $platform = 'Friendica';
73 } elseif (in_array($platform, ['Red Matrix', 'redmatrix', 'red'])) {
74 $platform = 'hubzilla';
75 } elseif(stristr($platform, 'pleroma')) {
76 $platform = 'pleroma';
77 } elseif(stristr($platform, 'statusnet')) {
78 $platform = 'gnusocial';
79 } elseif(stristr($platform, 'wordpress')) {
80 $platform = 'wordpress';
81 } elseif (!in_array($platform, $platforms)) {
85 if ($platform != $gserver['platform']) {
86 if ($platform == 'other') {
87 $versionCounts = $counts[$platform][1] ?? [];
88 $versionCounts[] = ['version' => $gserver['platform'] ?: L10n::t('unknown'), 'total' => $gserver['total']];
89 $gserver['version'] = '';
91 $versionCounts = array_merge($versionCounts, $counts[$platform][1] ?? []);
94 $gserver['platform'] = $platform;
95 $gserver['total'] += $counts[$platform][0]['total'] ?? 0;
96 $gserver['users'] += $counts[$platform][0]['users'] ?? 0;
99 if ($platform == 'Friendica') {
100 $versionCounts = self::reformaFriendicaVersions($versionCounts);
101 } elseif ($platform == 'pleroma') {
102 $versionCounts = self::reformaPleromaVersions($versionCounts);
103 } elseif ($platform == 'diaspora') {
104 $versionCounts = self::reformaDiasporaVersions($versionCounts);
107 $versionCounts = self::sortVersion($versionCounts);
109 $gserver['platform'] = $systems[$platform]['name'];
111 $counts[$platform] = [$gserver, $versionCounts, str_replace([' ', '%'], '', $platform), $systems[$platform]['color']];
113 DBA::close($gserver);
116 $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.');
117 $hint = L10n::t('The <em>Auto Discovered Contact Directory</em> feature is not enabled, it will improve the data displayed here.');
119 // load the template, replace the macros and return the page content
120 $t = Renderer::getMarkupTemplate('admin/federation.tpl');
121 return Renderer::replaceMacros($t, [
122 '$title' => L10n::t('Administration'),
123 '$page' => L10n::t('Federation Statistics'),
126 '$autoactive' => Config::get('system', 'poco_completion'),
127 '$counts' => $counts,
128 '$version' => FRIENDICA_VERSION,
129 '$legendtext' => L10n::t('Currently this node is aware of %d nodes with %d registered users from the following platforms:', $total, $users),
134 * early friendica versions have the format x.x.xxxx where xxxx is the
135 * DB version stamp; those should be operated out and versions be combined
137 * @param array $versionCounts list of version numbers
138 * @return array with cleaned version numbers
140 private static function reformaFriendicaVersions(array $versionCounts)
144 foreach ($versionCounts as $vv) {
145 $newVC = $vv['total'];
146 $newVV = $vv['version'];
147 $lastDot = strrpos($newVV, '.');
148 $len = strlen($newVV) - 1;
149 if (($lastDot == $len - 4) && (!strrpos($newVV, '-rc') == $len - 3)) {
150 $newVV = substr($newVV, 0, $lastDot);
152 if (isset($newV[$newVV])) {
153 $newV[$newVV] += $newVC;
155 $newV[$newVV] = $newVC;
158 foreach ($newV as $key => $value) {
159 array_push($newVv, ['total' => $value, 'version' => $key]);
161 $versionCounts = $newVv;
163 return $versionCounts;
167 * in the DB the Diaspora versions have the format x.x.x.x-xx the last
168 * part (-xx) should be removed to clean up the versions from the "head
169 * commit" information and combined into a single entry for x.x.x.x
171 * @param array $versionCounts list of version numbers
172 * @return array with cleaned version numbers
174 private static function reformaDiasporaVersions(array $versionCounts)
178 foreach ($versionCounts as $vv) {
179 $newVC = $vv['total'];
180 $newVV = $vv['version'];
181 $posDash = strpos($newVV, '-');
183 $newVV = substr($newVV, 0, $posDash);
185 if (isset($newV[$newVV])) {
186 $newV[$newVV] += $newVC;
188 $newV[$newVV] = $newVC;
191 foreach ($newV as $key => $value) {
192 array_push($newVv, ['total' => $value, 'version' => $key]);
194 $versionCounts = $newVv;
196 return $versionCounts;
200 * Clean up Pleroma version numbers
202 * @param array $versionCounts list of version numbers
203 * @return array with cleaned version numbers
205 private static function reformaPleromaVersions(array $versionCounts)
208 foreach ($versionCounts as $key => $value) {
209 $version = $versionCounts[$key]['version'];
210 $parts = explode(' ', trim($version));
212 $part = array_pop($parts);
213 } while (!empty($parts) && ((strlen($part) >= 40) || (strlen($part) <= 3)));
214 // only take the x.x.x part of the version, not the "release" after the dash
215 if (!empty($part) && strpos($part, '-')) {
216 $part = explode('-', $part)[0];
219 if (empty($compacted[$part])) {
220 $compacted[$part] = $versionCounts[$key]['total'];
222 $compacted[$part] += $versionCounts[$key]['total'];
228 foreach ($compacted as $version => $pl_total) {
229 $versionCounts[] = ['version' => $version, 'total' => $pl_total];
232 return $versionCounts;
236 * Reformat, sort and compact version numbers
238 * @param array $versionCounts list of version numbers
239 * @return array with reformatted version numbers
241 private static function sortVersion(array $versionCounts)
244 // clean up version numbers
246 // some platforms do not provide version information, add a unkown there
247 // to the version string for the displayed list.
248 foreach ($versionCounts as $key => $value) {
249 if ($versionCounts[$key]['version'] == '') {
250 $versionCounts[$key] = ['total' => $versionCounts[$key]['total'], 'version' => L10n::t('unknown')];
254 // Assure that the versions are sorted correctly
257 foreach ($versionCounts as $vv) {
258 $version = trim(strip_tags($vv["version"]));
260 $versions[] = $version;
263 usort($versions, 'version_compare');
266 foreach ($versions as $version) {
267 $versionCounts[] = $v2[$version];
270 return $versionCounts;