fix_duplicates.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * Script that removes duplicated profiles inter and intra
  19. * federation protocols.
  20. *
  21. * @package GNUsocial
  22. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. define('INSTALLDIR', dirname(__DIR__, 3));
  26. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  27. $longoptions = [];
  28. $shortoptions = '';
  29. $helptext = <<<END_OF_HELP
  30. fix_duplicates.php [options]
  31. remove duplicated profiles inter and intra federation protocols
  32. END_OF_HELP;
  33. require_once INSTALLDIR . '/scripts/commandline.inc';
  34. /**
  35. * Remote profiles are inspected from the most to the least
  36. * preferred according to the protocols they belong and age.
  37. * Invariants:
  38. * - `seen_local` array: The most recent profile inside of a certain protocol are kept
  39. * - global `seen` array: The most relevant profile (if there were duplicates, the first protocol of the list is the one to have its profile maintained) are kept
  40. * These two variables make it easy to satisfy a policy of maintaining
  41. * only the profiles that are either the most relevant or the newest
  42. * ones intra-protocol wise.
  43. */
  44. function run(): void
  45. {
  46. $protocols = common_config('TheFreeNetworkModule', 'protocols');
  47. $seen = [];
  48. foreach ($protocols as $protocol => $profile_class) {
  49. fix_duplicates($profile_class, $seen);
  50. }
  51. }
  52. function fix_duplicates(string $profile_class, array &$seen): void
  53. {
  54. $protocol_profile = new $profile_class();
  55. $protocol_profile->selectAdd();
  56. $protocol_profile->selectAdd('profile_id');
  57. $protocol_profile->selectAdd('uri');
  58. $protocol_profile->whereAdd('profile_id IS NOT NULL'); // ignore groups
  59. if (!$protocol_profile->find()) {
  60. // This protocol wasn't used apparently
  61. return;
  62. }
  63. $seen_local = [];
  64. while ($protocol_profile->fetch()) {
  65. $id = $protocol_profile->profile_id;
  66. $uri = $protocol_profile->uri;
  67. // Have we seen this profile before?
  68. if (array_key_exists($uri, $seen)) {
  69. try {
  70. // Was it on a previous protocol? Keep the highest preference protocol's one
  71. if ($seen[$uri] !== $id) {
  72. printfnq("Deleting Profile with id = {$id}\n");
  73. $profile = Profile::getByID($id);
  74. $profile->delete();
  75. } else {
  76. printfnq("Deleting {$profile_class} with id = {$id}\n");
  77. $protocol_profile->delete();
  78. }
  79. } catch (Exception $e) {
  80. // Let it go
  81. printfnq('FWIW: ' . $e->getMessage() . "\n");
  82. }
  83. } elseif (array_key_exists($uri, $seen_local)) {
  84. try {
  85. // Was it in this protocol? Delete the older record.
  86. if ($seen_local[$uri] !== $id) {
  87. printfnq("Deleting Profile with id = {$seen_local[$uri]}\n");
  88. $profile = Profile::getByID($seen_local[$uri]);
  89. $profile->delete();
  90. } else {
  91. printfnq("Deleting {$profile_class} with id = {$seen_local[$uri]}\n");
  92. $profile = $profile_class::getKV('profile_id', $seen_local[$uri]);
  93. $profile->delete();
  94. }
  95. } catch (Exception $e) {
  96. // Let it go
  97. printfnq('FWIW: ' . $e->getMessage() . "\n");
  98. }
  99. // Update the profile id for this URI.
  100. $seen_local[$uri] = $id;
  101. } else {
  102. // It's the first time we see this profile _inside_ this protocol!
  103. $seen_local[$uri] = $id;
  104. }
  105. }
  106. $protocol_profile->free();
  107. unset($protocol_profile);
  108. // Merge the findings inside this protocol with the global seen to be used on the next protocol of the list.
  109. $seen = array_merge($seen, $seen_local);
  110. }
  111. run();