123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- define('INSTALLDIR', dirname(__DIR__, 3));
- define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
- $longoptions = [];
- $shortoptions = '';
- $helptext = <<<END_OF_HELP
- fix_duplicates.php [options]
- remove duplicated profiles inter and intra federation protocols
- END_OF_HELP;
- require_once INSTALLDIR . '/scripts/commandline.inc';
- function run(): void
- {
- $protocols = common_config('TheFreeNetworkModule', 'protocols');
- $seen = [];
- foreach ($protocols as $protocol => $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');
- if (!$protocol_profile->find()) {
-
- return;
- }
- $seen_local = [];
- while ($protocol_profile->fetch()) {
- $id = $protocol_profile->profile_id;
- $uri = $protocol_profile->uri;
-
- if (array_key_exists($uri, $seen)) {
- try {
-
- 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();
- }
- } catch (Exception $e) {
-
- printfnq('FWIW: ' . $e->getMessage() . "\n");
- }
- } elseif (array_key_exists($uri, $seen_local)) {
- try {
-
- 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();
- }
- } catch (Exception $e) {
-
- printfnq('FWIW: ' . $e->getMessage() . "\n");
- }
-
- $seen_local[$uri] = $id;
- } else {
-
- $seen_local[$uri] = $id;
- }
- }
- $protocol_profile->free();
- unset($protocol_profile);
-
- $seen = array_merge($seen, $seen_local);
- }
- run();
|