#!/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 { $protocol_profile = new $profile_class(); $protocol_profile->selectAdd(); $protocol_profile->selectAdd('profile_id'); $protocol_profile->selectAdd('uri'); $protocol_profile->whereAdd('profile_id IS NOT NULL'); // ignore groups if (!$protocol_profile->find()) { // This protocol wasn't used apparently return; } $seen_local = []; while ($protocol_profile->fetch()) { $id = $protocol_profile->profile_id; $uri = $protocol_profile->uri; // Have we seen this profile before? if (array_key_exists($uri, $seen)) { // Was it on a previous protocol? Keep the highest preference protocol's one if ($seen[$uri] !== $id) { printfnq("Deleting Profile with id = {$id}\n"); $profile = Profile::getByID($id); $profile->delete(); } else { printfnq("Deleting {$profile_class} with id = {$id}\n"); $protocol_profile->delete(); } } elseif (array_key_exists($uri, $seen_local)) { // Was it in this protocol? Delete the older record. if ($seen_local[$uri] !== $id) { printfnq("Deleting Profile with id = {$seen_local[$uri]}\n"); $profile = Profile::getByID($seen_local[$uri]); $profile->delete(); } else { printfnq("Deleting {$profile_class} with id = {$seen_local[$uri]}\n"); $profile = $profile_class::getKV('profile_id', $seen_local[$uri]); $profile->delete(); } // Update the profile id for this URI. $seen_local[$uri] = $id; } else { // It's the first time we see this profile _inside_ this protocol! $seen_local[$uri] = $id; } } // Merge the findings inside this protocol with the global seen to be used on the next protocol of the list. $seen = array_merge($seen, $seen_local); } run();