123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
- $longoptions = array('dry-run', 'start=', 'end=');
- $helptext = <<<END_OF_USERROLE_HELP
- fixup_deletions.php [options]
- Finds notices posted by deleted users and cleans them up.
- Stray incompletely deleted items cause various fun problems!
- --dry-run look but don't touch
- --start=N start looking at profile_id N instead of 1
- --end=N end looking at profile_id N instead of the max
- END_OF_USERROLE_HELP;
- require_once INSTALLDIR.'/scripts/commandline.inc';
- function get_max_profile_id()
- {
- $query = 'SELECT MAX(profile_id) AS id FROM notice';
- $profile = new Profile();
- $profile->query($query);
- if ($profile->fetch()) {
- return intval($profile->id);
- } else {
- die("Something went awry; could not look up max used profile_id.");
- }
- }
- function get_missing_profiles($start, $end)
- {
- $query = sprintf("SELECT id FROM profile WHERE id BETWEEN %d AND %d",
- $start, $end);
- $profile = new Profile();
- $profile->query($query);
- $all = range($start, $end);
- $known = array();
- while ($row = $profile->fetch()) {
- $known[] = intval($profile->id);
- }
- unset($profile);
- $missing = array_diff($all, $known);
- return $missing;
- }
- function cleanup_missing_profile($profile_id, $dry)
- {
- $notice = new Notice();
- $notice->profile_id = $profile_id;
- $notice->find();
- if ($notice->N == 0) {
- return;
- }
- $s = ($notice->N == 1) ? '' : 's';
- print "Deleted profile $profile_id has $notice->N stray notice$s:\n";
- while ($notice->fetch()) {
- print " notice $notice->id";
- if ($dry) {
- print " (skipped; dry run)\n";
- } else {
- $victim = clone($notice);
- try {
- $victim->delete();
- print " (deleted)\n";
- } catch (Exception $e) {
- print " FAILED: ";
- print $e->getMessage();
- print "\n";
- }
- }
- }
- }
- $dry = have_option('dry-run');
- $max_profile_id = get_max_profile_id();
- $chunk = 1000;
- if (have_option('start')) {
- $begin = intval(get_option_value('start'));
- } else {
- $begin = 1;
- }
- if (have_option('end')) {
- $final = min($max_profile_id, intval(get_option_value('end')));
- } else {
- $final = $max_profile_id;
- }
- if ($begin < 1) {
- die("Silly human, you can't begin before profile number 1!\n");
- }
- if ($final < $begin) {
- die("Silly human, you can't end at $final if it's before $begin!\n");
- }
- for ($start = $begin; $start <= $final; $start += $chunk) {
- $end = min($start + $chunk - 1, $final);
- print "Checking for missing profiles between id $start and $end";
- if ($dry) {
- print " (dry run)";
- }
- print "...\n";
- $missing = get_missing_profiles($start, $end);
- foreach ($missing as $profile_id) {
- cleanup_missing_profile($profile_id, $dry);
- }
- }
- echo "done.\n";
|