Use DBA::fetchFirst instead of DBA::p for aggregate query in Module\Admin\Federation

This commit is contained in:
Hypolite Petovan 2019-04-28 09:48:26 -04:00
parent 451a06a597
commit 2a035b9b2f

View file

@ -1,198 +1,195 @@
<?php <?php
namespace Friendica\Module\Admin; namespace Friendica\Module\Admin;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Module\BaseAdminModule; use Friendica\Module\BaseAdminModule;
class Federation extends BaseAdminModule class Federation extends BaseAdminModule
{ {
public static function content() public static function content()
{ {
parent::content(); parent::content();
// get counts on active friendica, diaspora, redmatrix, hubzilla, gnu // get counts on active friendica, diaspora, redmatrix, hubzilla, gnu
// social and statusnet nodes this node is knowing // social and statusnet nodes this node is knowing
// //
// We are looking for the following platforms in the DB, "Red" should find // We are looking for the following platforms in the DB, "Red" should find
// all variants of that platform ID string as the q() function is stripping // all variants of that platform ID string as the q() function is stripping
// off one % two of them are needed in the query // off one % two of them are needed in the query
// Add more platforms if you like, when one returns 0 known nodes it is not // Add more platforms if you like, when one returns 0 known nodes it is not
// displayed on the stats page. // displayed on the stats page.
$platforms = ['Friendi%%a', 'Diaspora', '%%red%%', 'Hubzilla', 'BlaBlaNet', 'GNU Social', 'StatusNet', 'Mastodon', 'Pleroma', 'socialhome', 'ganggo']; $platforms = ['Friendi%%a', 'Diaspora', '%%red%%', 'Hubzilla', 'BlaBlaNet', 'GNU Social', 'StatusNet', 'Mastodon', 'Pleroma', 'socialhome', 'ganggo'];
$colors = [ $colors = [
'Friendi%%a' => '#ffc018', // orange from the logo 'Friendi%%a' => '#ffc018', // orange from the logo
'Diaspora' => '#a1a1a1', // logo is black and white, makes a gray 'Diaspora' => '#a1a1a1', // logo is black and white, makes a gray
'%%red%%' => '#c50001', // fire red from the logo '%%red%%' => '#c50001', // fire red from the logo
'Hubzilla' => '#43488a', // blue from the logo 'Hubzilla' => '#43488a', // blue from the logo
'BlaBlaNet' => '#3B5998', // blue from the navbar at blablanet-dot-com 'BlaBlaNet' => '#3B5998', // blue from the navbar at blablanet-dot-com
'GNU Social' => '#a22430', // dark red from the logo 'GNU Social' => '#a22430', // dark red from the logo
'StatusNet' => '#789240', // the green from the logo (red and blue have already others 'StatusNet' => '#789240', // the green from the logo (red and blue have already others
'Mastodon' => '#1a9df9', // blue from the Mastodon logo 'Mastodon' => '#1a9df9', // blue from the Mastodon logo
'Pleroma' => '#E46F0F', // Orange from the text that is used on Pleroma instances 'Pleroma' => '#E46F0F', // Orange from the text that is used on Pleroma instances
'socialhome' => '#52056b', // lilac from the Django Image used at the Socialhome homepage 'socialhome' => '#52056b', // lilac from the Django Image used at the Socialhome homepage
'ganggo' => '#69d7e2', // from the favicon 'ganggo' => '#69d7e2', // from the favicon
]; ];
$counts = []; $counts = [];
$total = 0; $total = 0;
$users = 0; $users = 0;
foreach ($platforms as $platform) { foreach ($platforms as $platform) {
// get a total count for the platform, the name and version of the // get a total count for the platform, the name and version of the
// highest version and the protocol tpe // highest version and the protocol tpe
$platformCountStmt = DBA::p('SELECT $platformCount = DBA::fetchFirst('SELECT
COUNT(*) AS `total`, COUNT(*) AS `total`,
SUM(`registered-users`) AS `users`, SUM(`registered-users`) AS `users`,
ANY_VALUE(`platform`) AS `platform`, ANY_VALUE(`platform`) AS `platform`,
ANY_VALUE(`network`) AS `network`, ANY_VALUE(`network`) AS `network`,
MAX(`version`) AS `version` FROM `gserver` MAX(`version`) AS `version` FROM `gserver`
WHERE `platform` LIKE ? WHERE `platform` LIKE ?
AND `last_contact` >= `last_failure` AND `last_contact` >= `last_failure`
ORDER BY `version` ASC', $platform); ORDER BY `version` ASC', $platform);
$platformCount = DBA::fetch($platformCountStmt); $total += $platformCount['total'];
$total += $platformCount['total']; $users += $platformCount['users'];
$users += $platformCount['users'];
// what versions for that platform do we know at all?
DBA::close($platformCountStmt); // again only the active nodes
$versionCountsStmt = DBA::p('SELECT
// what versions for that platform do we know at all? COUNT(*) AS `total`,
// again only the active nodes `version` FROM `gserver`
$versionCountsStmt = DBA::p('SELECT WHERE `last_contact` >= `last_failure`
COUNT(*) AS `total`, AND `platform` LIKE ?
`version` FROM `gserver` GROUP BY `version`
WHERE `last_contact` >= `last_failure` ORDER BY `version`;', $platform);
AND `platform` LIKE ? $versionCounts = DBA::toArray($versionCountsStmt);
GROUP BY `version`
ORDER BY `version`;', $platform); //
$versionCounts = DBA::toArray($versionCountsStmt); // clean up version numbers
//
// // some platforms do not provide version information, add a unkown there
// clean up version numbers // to the version string for the displayed list.
// foreach ($versionCounts as $key => $value) {
// some platforms do not provide version information, add a unkown there if ($versionCounts[$key]['version'] == '') {
// to the version string for the displayed list. $versionCounts[$key] = ['total' => $versionCounts[$key]['total'], 'version' => L10n::t('unknown')];
foreach ($versionCounts as $key => $value) { }
if ($versionCounts[$key]['version'] == '') { }
$versionCounts[$key] = ['total' => $versionCounts[$key]['total'], 'version' => L10n::t('unknown')];
} // Reformat and compact version numbers
} if ($platform == 'Pleroma') {
$compacted = [];
// Reformat and compact version numbers foreach ($versionCounts as $key => $value) {
if ($platform == 'Pleroma') { $version = $versionCounts[$key]['version'];
$compacted = []; $parts = explode(' ', trim($version));
foreach ($versionCounts as $key => $value) { do {
$version = $versionCounts[$key]['version']; $part = array_pop($parts);
$parts = explode(' ', trim($version)); } while (!empty($parts) && ((strlen($part) >= 40) || (strlen($part) <= 3)));
do { // only take the x.x.x part of the version, not the "release" after the dash
$part = array_pop($parts); $part = array_shift(explode('-', $part));
} while (!empty($parts) && ((strlen($part) >= 40) || (strlen($part) <= 3))); if (!empty($part)) {
// only take the x.x.x part of the version, not the "release" after the dash if (empty($compacted[$part])) {
$part = array_shift(explode('-', $part)); $compacted[$part] = $versionCounts[$key]['total'];
if (!empty($part)) { } else {
if (empty($compacted[$part])) { $compacted[$part] += $versionCounts[$key]['total'];
$compacted[$part] = $versionCounts[$key]['total']; }
} else { }
$compacted[$part] += $versionCounts[$key]['total']; }
}
} $versionCounts = [];
} foreach ($compacted as $version => $pl_total) {
$versionCounts[] = ['version' => $version, 'total' => $pl_total];
$versionCounts = []; }
foreach ($compacted as $version => $pl_total) { }
$versionCounts[] = ['version' => $version, 'total' => $pl_total];
} // in the DB the Diaspora versions have the format x.x.x.x-xx the last
} // part (-xx) should be removed to clean up the versions from the "head
// commit" information and combined into a single entry for x.x.x.x
// in the DB the Diaspora versions have the format x.x.x.x-xx the last if ($platform == 'Diaspora') {
// part (-xx) should be removed to clean up the versions from the "head $newV = [];
// commit" information and combined into a single entry for x.x.x.x $newVv = [];
if ($platform == 'Diaspora') { foreach ($versionCounts as $vv) {
$newV = []; $newVC = $vv['total'];
$newVv = []; $newVV = $vv['version'];
foreach ($versionCounts as $vv) { $posDash = strpos($newVV, '-');
$newVC = $vv['total']; if ($posDash) {
$newVV = $vv['version']; $newVV = substr($newVV, 0, $posDash);
$posDash = strpos($newVV, '-'); }
if ($posDash) { if (isset($newV[$newVV])) {
$newVV = substr($newVV, 0, $posDash); $newV[$newVV] += $newVC;
} } else {
if (isset($newV[$newVV])) { $newV[$newVV] = $newVC;
$newV[$newVV] += $newVC; }
} else { }
$newV[$newVV] = $newVC; foreach ($newV as $key => $value) {
} array_push($newVv, ['total' => $value, 'version' => $key]);
} }
foreach ($newV as $key => $value) { $versionCounts = $newVv;
array_push($newVv, ['total' => $value, 'version' => $key]); }
}
$versionCounts = $newVv; // early friendica versions have the format x.x.xxxx where xxxx is the
} // DB version stamp; those should be operated out and versions be
// conbined
// early friendica versions have the format x.x.xxxx where xxxx is the if ($platform == 'Friendi%%a') {
// DB version stamp; those should be operated out and versions be $newV = [];
// conbined $newVv = [];
if ($platform == 'Friendi%%a') { foreach ($versionCounts as $vv) {
$newV = []; $newVC = $vv['total'];
$newVv = []; $newVV = $vv['version'];
foreach ($versionCounts as $vv) { $lastDot = strrpos($newVV, '.');
$newVC = $vv['total']; $len = strlen($newVV) - 1;
$newVV = $vv['version']; if (($lastDot == $len - 4) && (!strrpos($newVV, '-rc') == $len - 3)) {
$lastDot = strrpos($newVV, '.'); $newVV = substr($newVV, 0, $lastDot);
$len = strlen($newVV) - 1; }
if (($lastDot == $len - 4) && (!strrpos($newVV, '-rc') == $len - 3)) { if (isset($newV[$newVV])) {
$newVV = substr($newVV, 0, $lastDot); $newV[$newVV] += $newVC;
} } else {
if (isset($newV[$newVV])) { $newV[$newVV] = $newVC;
$newV[$newVV] += $newVC; }
} else { }
$newV[$newVV] = $newVC; foreach ($newV as $key => $value) {
} array_push($newVv, ['total' => $value, 'version' => $key]);
} }
foreach ($newV as $key => $value) { $versionCounts = $newVv;
array_push($newVv, ['total' => $value, 'version' => $key]); }
}
$versionCounts = $newVv; // Assure that the versions are sorted correctly
} $v2 = [];
$versions = [];
// Assure that the versions are sorted correctly foreach ($versionCounts as $vv) {
$v2 = []; $version = trim(strip_tags($vv["version"]));
$versions = []; $v2[$version] = $vv;
foreach ($versionCounts as $vv) { $versions[] = $version;
$version = trim(strip_tags($vv["version"])); }
$v2[$version] = $vv;
$versions[] = $version; usort($versions, 'version_compare');
}
$versionCounts = [];
usort($versions, 'version_compare'); foreach ($versions as $version) {
$versionCounts[] = $v2[$version];
$versionCounts = []; }
foreach ($versions as $version) {
$versionCounts[] = $v2[$version]; // the 3rd array item is needed for the JavaScript graphs as JS does
} // not like some characters in the names of variables...
$counts[$platform] = [$platformCount, $versionCounts, str_replace([' ', '%'], '', $platform), $colors[$platform]];
// the 3rd array item is needed for the JavaScript graphs as JS does }
// not like some characters in the names of variables...
$counts[$platform] = [$platformCount, $versionCounts, str_replace([' ', '%'], '', $platform), $colors[$platform]]; // some helpful text
} $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.');
$hint = L10n::t('The <em>Auto Discovered Contact Directory</em> feature is not enabled, it will improve the data displayed here.');
// some helpful text
$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.'); // load the template, replace the macros and return the page content
$hint = L10n::t('The <em>Auto Discovered Contact Directory</em> feature is not enabled, it will improve the data displayed here.'); $t = Renderer::getMarkupTemplate('admin/federation.tpl');
return Renderer::replaceMacros($t, [
// load the template, replace the macros and return the page content '$title' => L10n::t('Administration'),
$t = Renderer::getMarkupTemplate('admin/federation.tpl'); '$page' => L10n::t('Federation Statistics'),
return Renderer::replaceMacros($t, [ '$intro' => $intro,
'$title' => L10n::t('Administration'), '$hint' => $hint,
'$page' => L10n::t('Federation Statistics'), '$autoactive' => Config::get('system', 'poco_completion'),
'$intro' => $intro, '$counts' => $counts,
'$hint' => $hint, '$version' => FRIENDICA_VERSION,
'$autoactive' => Config::get('system', 'poco_completion'), '$legendtext' => L10n::t('Currently this node is aware of %d nodes with %d registered users from the following platforms:', $total, $users),
'$counts' => $counts, ]);
'$version' => FRIENDICA_VERSION, }
'$legendtext' => L10n::t('Currently this node is aware of %d nodes with %d registered users from the following platforms:', $total, $users), }
]);
}
}