<commit_before><?php
/**
 * Script to remove users with duplicate email accounts.
 *
 * to run:
 * drush --script-path=../scripts/ php-script remove-duplicate-emails.php
 */

$dupes = array_keys(db_query('SELECT mail FROM users GROUP BY mail HAVING COUNT(mail) > 1')->fetchAllKeyed());
$removed = 0;

foreach ($dupes as $mail) {
  // Load all users with that duped email address, with the most recently accessed first.
  $users = db_query('SELECT uid FROM users WHERE mail = :mail ORDER BY access DESC', [':mail' => $mail]);
  $canonical_uid = 0;

  foreach ($users as $key => $user) {
    $user = user_load($user->uid);

    if ($key == 0) {
      print 'Keeping ' . $user->uid . ' for ' . $user->mail . '.' . PHP_EOL;
      $canonical_uid = $user->uid;
      continue;
    }

    // Set the new email for the deactivated user.
    $new_email = 'duplicate-' . $canonical_uid . '-' . $key . '@dosomething.invalid';
    print ' - Removing ' . $user->uid . ' (' . $user->mail . ' --> ' . $new_email . ')' . PHP_EOL;
    user_save($user, ['mail' => $new_email, 'status' => 0]);
    $removed++;

    // Finally, make a note if that user has a Northstar profile, so we can clean that up.
    $northstar_id = $user->field_northstar_id[LANGUAGE_NONE][0]['value'];
    if ($northstar_id !== 'NONE') {
      print '   ** User ' . $user->uid . ' has a Northstar profile: ' . $northstar_id;
    }
  }

  print PHP_EOL;
}

print '[✔] Renamed & deactivated ' . $removed . ' users.' . PHP_EOL;<commit_msg>Save IDs in a table so we can mass-delete them!<commit_after><?php
/**
 * Script to remove users with duplicate email accounts.
 *
 * to run:
 * drush --script-path=../scripts/ php-script remove-duplicate-emails.php
 */

$dupes = array_keys(db_query('SELECT mail FROM users GROUP BY mail HAVING COUNT(mail) > 1')->fetchAllKeyed());
$removed = 0;


// Watch out, because we're gonna make a database table. Yee-haw!
db_query('
  CREATE TABLE IF NOT EXISTS `dosomething_northstar_delete_queue` (
    `uid` int(11) unsigned NOT NULL,
    `northstar_id` varchar(32) DEFAULT NULL,
    PRIMARY KEY (`uid`))
');

foreach ($dupes as $mail) {
  // Load all users with that duped email address, with the most recently accessed first.
  $users = db_query('SELECT uid FROM users WHERE mail = :mail ORDER BY access DESC', [':mail' => $mail]);
  $canonical_uid = 0;

  foreach ($users as $key => $user) {
    $user = user_load($user->uid);

    if ($key == 0) {
      print 'Keeping ' . $user->uid . ' for ' . $user->mail . '.' . PHP_EOL;
      $canonical_uid = $user->uid;
      continue;
    }

    // Set the new email for the deactivated user.
    $new_email = 'duplicate-' . $canonical_uid . '-' . $key . '@dosomething.invalid';
    print ' - Removing ' . $user->uid . ' (' . $user->mail . ' --> ' . $new_email . ')' . PHP_EOL;
    user_save($user, ['mail' => $new_email, 'status' => 0]);
    $removed++;

    // Finally, make a note if that user has a Northstar profile, so we can clean that up.
    $northstar_id = $user->field_northstar_id[LANGUAGE_NONE][0]['value'];
    if ($northstar_id !== 'NONE') {
      db_insert('dosomething_northstar_delete_queue')->fields(['uid' => $user->uid, 'northstar_id' => $northstar_id])->execute();
      print '   ** User ' . $user->uid . ' has a Northstar profile: ' . $northstar_id . PHP_EOL;
    }
  }

  print PHP_EOL;
}

print '[✔] Renamed & deactivated ' . $removed . ' users.' . PHP_EOL;
{'fraction_non_alphanumeric': 0.1695957820738137, 'fraction_numerical': 0.0046865846514352666, 'mean_word_length': 3.0414201183431953, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4134086', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1148, 'n_words': 333}