fixup-shadow.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2010 StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
  21. $longoptions = array('dry-run');
  22. $helptext = <<<END_OF_USERROLE_HELP
  23. fixup_shadow.php [options]
  24. Patches up stray ostatus_profile entries with corrupted shadow entries
  25. for local users and groups.
  26. --dry-run look but don't touch
  27. END_OF_USERROLE_HELP;
  28. require_once INSTALLDIR.'/scripts/commandline.inc';
  29. $dry = have_option('dry-run');
  30. // Look for user.uri matches... These may not match up with the current
  31. // URL schema if the site has changed names.
  32. echo "Checking for bogus ostatus_profile entries matching user.uri...\n";
  33. $user = new User();
  34. $oprofile = new Ostatus_profile();
  35. $user->joinAdd($oprofile, 'INNER', 'oprofile', 'uri');
  36. $user->find();
  37. $count = $user->N;
  38. echo "Found $count...\n";
  39. while ($user->fetch()) {
  40. $uri = $user->getUri();
  41. echo "user $user->id ($user->nickname) hidden by $uri";
  42. if ($dry) {
  43. echo " - skipping\n";
  44. } else {
  45. echo " - removing bogus ostatus_profile entry...";
  46. $evil = Ostatus_profile::getKV('uri', $uri);
  47. $evil->delete();
  48. echo " ok\n";
  49. }
  50. }
  51. echo "\n";
  52. // Also try user_group.uri matches for local groups.
  53. // Not all group entries will have this filled out, though, as it's new!
  54. echo "Checking for bogus ostatus_profile entries matching local user_group.uri...\n";
  55. $group = new User_group();
  56. $group->joinAdd(array('uri', 'ostatus_profile:uri'));
  57. $group->joinAdd(array('id', 'local_group:group_id'));
  58. $group->find();
  59. $count = $group->N;
  60. echo "Found $count...\n";
  61. while ($group->fetch()) {
  62. $uri = $group->getUri();
  63. echo "group $group->id ($group->nickname) hidden by $uri";
  64. if ($dry) {
  65. echo " - skipping\n";
  66. } else {
  67. echo " - removing bogus ostatus_profile entry...";
  68. $evil = Ostatus_profile::getKV('uri', $uri);
  69. $evil->delete();
  70. echo " ok\n";
  71. }
  72. }
  73. echo "\n";
  74. // And there may be user_group entries remaining where we've already killed
  75. // the ostatus_profile. These were "harmless" until our lookup started actually
  76. // using the uri field, at which point we can clearly see it breaks stuff.
  77. echo "Checking for leftover bogus user_group.uri entries obscuring local_group entries...\n";
  78. $group = new User_group();
  79. $group->joinAdd(array('id', 'local_group:group_id'), 'LEFT');
  80. $group->whereAdd('group_id IS NULL');
  81. $marker = mt_rand(31337, 31337000);
  82. $groupTemplate = common_local_url('groupbyid', array('id' => $marker));
  83. $encGroup = $group->escape($groupTemplate, true);
  84. $encGroup = str_replace($marker, '%', $encGroup);
  85. echo " LIKE '$encGroup'\n";
  86. $group->whereAdd("uri LIKE '$encGroup'");
  87. $group->find();
  88. $count = $group->N;
  89. echo "Found $count...\n";
  90. while ($group->fetch()) {
  91. $uri = $group->getUri();
  92. if (preg_match('!/group/(\d+)/id!', $uri, $matches)) {
  93. $id = intval($matches[1]);
  94. $local = Local_group::getKV('group_id', $id);
  95. if ($local) {
  96. $nick = $local->nickname;
  97. } else {
  98. $nick = '<deleted>';
  99. }
  100. echo "local group $id ($local->nickname) hidden by $uri (bogus group id $group->id)";
  101. if ($dry) {
  102. echo " - skipping\n";
  103. } else {
  104. echo " - removing bogus user_group entry...";
  105. $evil = User_group::getKV('id', $group->id);
  106. $evil->delete();
  107. echo " ok\n";
  108. }
  109. }
  110. }
  111. echo "\n";
  112. // Fallback?
  113. echo "Checking for bogus profiles blocking local users/groups by URI pattern match...\n";
  114. $oprofile = new Ostatus_profile();
  115. $marker = mt_rand(31337, 31337000);
  116. $profileTemplate = common_local_url('userbyid', array('id' => $marker));
  117. $encProfile = $oprofile->escape($profileTemplate, true);
  118. $encProfile = str_replace($marker, '%', $encProfile);
  119. echo " LIKE '$encProfile'\n";
  120. $groupTemplate = common_local_url('groupbyid', array('id' => $marker));
  121. $encGroup = $oprofile->escape($groupTemplate, true);
  122. $encGroup = str_replace($marker, '%', $encGroup);
  123. echo " LIKE '$encGroup'\n";
  124. $sql = "SELECT * FROM ostatus_profile WHERE uri LIKE '%s' OR uri LIKE '%s'";
  125. $oprofile->query(sprintf($sql, $encProfile, $encGroup));
  126. $count = $oprofile->N;
  127. echo "Found $count...\n";
  128. while ($oprofile->fetch()) {
  129. $uri = $oprofile->getUri();
  130. if (preg_match('!/group/(\d+)/id!', $oprofile->getUri(), $matches)) {
  131. $id = intval($matches[1]);
  132. $group = Local_group::getKV('group_id', $id);
  133. if ($group) {
  134. $nick = $group->nickname;
  135. } else {
  136. $nick = '<deleted>';
  137. }
  138. echo "group $id ($nick) hidden by $uri";
  139. } else if (preg_match('!/user/(\d+)!', $uri, $matches)) {
  140. $id = intval($matches[1]);
  141. $user = User::getKV('id', $id);
  142. if ($user) {
  143. $nick = $user->nickname;
  144. } else {
  145. $nick = '<deleted>';
  146. }
  147. echo "user $id ($nick) hidden by $uri";
  148. } else {
  149. echo "$uri matched query, but we don't recognize it.\n";
  150. continue;
  151. }
  152. if ($dry) {
  153. echo " - skipping\n";
  154. } else {
  155. echo " - removing bogus ostatus_profile entry...";
  156. $evil = clone($oprofile);
  157. $evil->delete();
  158. echo " ok\n";
  159. }
  160. }
  161. if ($count && $dry) {
  162. echo "NO CHANGES MADE -- To delete the bogus entries, run again without --dry-run option.\n";
  163. } else {
  164. echo "done.\n";
  165. }