From f476708f3a9e69bba6da6d879916589aa978eb56 Mon Sep 17 00:00:00 2001 From: t3nma Date: Tue, 4 Aug 2020 05:02:06 +0100 Subject: [PATCH] [TheFreeNetwork] Add fix_duplicates script --- .../TheFreeNetwork/scripts/fix_duplicates.php | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 modules/TheFreeNetwork/scripts/fix_duplicates.php diff --git a/modules/TheFreeNetwork/scripts/fix_duplicates.php b/modules/TheFreeNetwork/scripts/fix_duplicates.php new file mode 100644 index 0000000000..6a9df2ed0c --- /dev/null +++ b/modules/TheFreeNetwork/scripts/fix_duplicates.php @@ -0,0 +1,111 @@ +#!/usr/bin/env php +. + +/** + * Script that removes duplicated profiles inter and intra + * federation protocols. + * + * @package GNUsocial + * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ + +define('INSTALLDIR', dirname(__DIR__, 3)); +define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public'); + +$longoptions = []; +$shortoptions = ''; + +$helptext = << $profile_class) { + fix_duplicates($profile_class, $seen); + } +} + +function fix_duplicates(string $profile_class, array &$seen): void +{ + $db = new $profile_class(); + $db->selectAdd('profile_id'); + $db->selectAdd('uri'); + $db->whereAdd('profile_id IS NOT NULL'); // ignore groups + + if (!$db->find()) { + return; + } + + $seen_local = []; + + while ($db->fetch()) { + $id = $db->profile_id; + $uri = $db->uri; + + // have we seen this profile before? + if (array_key_exists($uri, $seen)) { + // yes, mantain previous + if ($seen[$uri] !== $id) { + printfv("Deleting Profile with id = {$id}"); + $profile = Profile::getKV('id', $id); + $profile->delete(); // will propagate to federation profile classes + } else { + printfv("Deleting {$profile_class} with id = {$id}"); + $profile = $profile_class::getKV('profile_id', $id); + $profile->delete(); + } + } elseif (array_key_exists($uri, $seen_local)) { + // yes, mantain current + if ($seen_local[$uri] !== $id) { + printfv("Deleting Profile with id = {$id}"); + $profile = Profile::getKV('id', $seen_local[$uri]); + $profile->delete(); // will propagate to federation profile classes + } else { + printfv("Deleting {$profile_class} with id = {$seen_local[$uri]}"); + $profile = $profile_class::getKV('profile_id', $seen_local[$uri]); + $profile->delete(); + } + + $seen_local[$uri] = $id; + } else { + // no, save it + $seen_local[$uri] = $id; + } + } + + // save valid local profiles + $seen = array_merge($seen, $seen_local); +} + +run();