+
+ /**
+ * early friendica versions have the format x.x.xxxx where xxxx is the
+ * DB version stamp; those should be operated out and versions be combined
+ *
+ * @param array $versionCounts list of version numbers
+ * @return array with cleaned version numbers
+ */
+ private static function reformaFriendicaVersions(array $versionCounts)
+ {
+ $newV = [];
+ $newVv = [];
+ foreach ($versionCounts as $vv) {
+ $newVC = $vv['total'];
+ $newVV = $vv['version'];
+ $lastDot = strrpos($newVV, '.');
+ $firstDash = strpos($newVV, '-');
+ $len = strlen($newVV) - 1;
+ if (($lastDot == $len - 4) && (!strrpos($newVV, '-rc') == $len - 3) && (!$firstDash == $len - 1)) {
+ $newVV = substr($newVV, 0, $lastDot);
+ }
+ if (isset($newV[$newVV])) {
+ $newV[$newVV] += $newVC;
+ } else {
+ $newV[$newVV] = $newVC;
+ }
+ }
+ foreach ($newV as $key => $value) {
+ array_push($newVv, ['total' => $value, 'version' => $key]);
+ }
+ $versionCounts = $newVv;
+
+ return $versionCounts;
+ }
+
+ /**
+ * 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
+ *
+ * @param array $versionCounts list of version numbers
+ * @return array with cleaned version numbers
+ */
+ private static function reformaDiasporaVersions(array $versionCounts)
+ {
+ $newV = [];
+ $newVv = [];
+ foreach ($versionCounts as $vv) {
+ $newVC = $vv['total'];
+ $newVV = $vv['version'];
+ $posDash = strpos($newVV, '-');
+ if ($posDash) {
+ $newVV = substr($newVV, 0, $posDash);
+ }
+ if (isset($newV[$newVV])) {
+ $newV[$newVV] += $newVC;
+ } else {
+ $newV[$newVV] = $newVC;
+ }
+ }
+ foreach ($newV as $key => $value) {
+ array_push($newVv, ['total' => $value, 'version' => $key]);
+ }
+ $versionCounts = $newVv;
+
+ return $versionCounts;
+ }
+
+ /**
+ * Clean up Pleroma version numbers
+ *
+ * @param array $versionCounts list of version numbers
+ * @return array with cleaned version numbers
+ */
+ private static function reformaPleromaVersions(array $versionCounts)
+ {
+ $compacted = [];
+ foreach ($versionCounts as $key => $value) {
+ $version = $versionCounts[$key]['version'];
+ $parts = explode(' ', trim($version));
+ do {
+ $part = array_pop($parts);
+ } while (!empty($parts) && ((strlen($part) >= 40) || (strlen($part) <= 3)));
+ // only take the x.x.x part of the version, not the "release" after the dash
+ if (!empty($part) && strpos($part, '-')) {
+ $part = explode('-', $part)[0];
+ }
+ if (!empty($part)) {
+ if (empty($compacted[$part])) {
+ $compacted[$part] = $versionCounts[$key]['total'];
+ } else {
+ $compacted[$part] += $versionCounts[$key]['total'];
+ }
+ }
+ }
+
+ $versionCounts = [];
+ foreach ($compacted as $version => $pl_total) {
+ $versionCounts[] = ['version' => $version, 'total' => $pl_total];
+ }
+
+ return $versionCounts;
+ }
+
+ /**
+ * Reformat, sort and compact version numbers
+ *
+ * @param array $versionCounts list of version numbers
+ * @return array with reformatted version numbers
+ */
+ private static function sortVersion(array $versionCounts)
+ {
+ //
+ // clean up version numbers
+ //
+ // some platforms do not provide version information, add a unkown there
+ // to the version string for the displayed list.
+ foreach ($versionCounts as $key => $value) {
+ if ($versionCounts[$key]['version'] == '') {
+ $versionCounts[$key] = ['total' => $versionCounts[$key]['total'], 'version' => DI::l10n()->t('unknown')];
+ }
+ }
+
+ // Assure that the versions are sorted correctly
+ $v2 = [];
+ $versions = [];
+ foreach ($versionCounts as $vv) {
+ $version = trim(strip_tags($vv["version"]));
+ $v2[$version] = $vv;
+ $versions[] = $version;
+ }
+
+ usort($versions, 'version_compare');
+
+ $versionCounts = [];
+ foreach ($versions as $version) {
+ $versionCounts[] = $v2[$version];
+ }
+
+ return $versionCounts;
+ }