Skip to content
Snippets Groups Projects
Commit 01e310d8 authored by Fran Garcia-Linares's avatar Fran Garcia-Linares
Browse files

Delete users not in D7.

parent 40b025c9
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
namespace Drupal\drupalorg_migrate\Commands;
use Drupal\Core\Database\Database;
use Drush\Commands\DrushCommands as BaseDrushCommands;
/**
......@@ -18,7 +19,106 @@ class DrushCommands extends BaseDrushCommands {
* @usage drush drupalorg_migrate:sync-users
*/
public function syncUsers() {
dd('hi');
$db_connection = Database::getConnection();
$d7_connection = Database::getConnection('default', 'migrate');
$count_users_d7 = $d7_connection->select('users')->countQuery()->execute()->fetchField();
$count_users = $db_connection->select('users')->countQuery()->execute()->fetchField();
if ($count_users !== $count_users_d7) {
// Mismatch of users between Drupal 7 and modern Drupal.
$this->logger()->warning(\dt('Users counts are different in D7 and the new system. @count (new) vs @count_d7 (D7).', [
'@count' => $count_users,
'@count_d7' => $count_users_d7,
]));
// Memory usage around 22MB for 10000.
$length = 10000;
$offset = 0;
$extra_users = [];
// Users are migrated from D7 to D10+, so any user present on D10+ that
// is not in D7, means that the user account was removed.
$user_ids = $this->getNextBatch($offset, $length);
$this->io()->progressStart($count_users);
while (!empty($user_ids)) {
$user_ids_not_in_d7 = $this->userIdsNotInD7($user_ids);
$extra_users = array_merge($extra_users, $user_ids_not_in_d7);
$this->io()->progressAdvance($length);
$offset += $length;
$user_ids = $this->getNextBatch($offset, $length);
}
$this->io()->progressFinish();
if (empty($extra_users)) {
$this->logger()->success('Users are in sync');
}
else {
$this->logger()->warning(\dt('Users are NOT in sync. @count user(s) will be deleted.', [
'@count' => count($extra_users),
]));
foreach ($extra_users as $extra_user_uid) {
// No need for extra logging as the user_cancel function does it.
user_cancel([], $extra_user_uid, 'user_cancel_reassign');
// Since user_cancel() is not invoked via Form API, batch processing
// needs to be invoked manually.
$batch =& batch_get();
// Mark this batch as non-progressive to bypass the progress bar and
// redirect.
$batch['progressive'] = FALSE;
// Batch it the drush way.
// batch_process();
drush_backend_batch_process();
}
}
}
else {
$this->logger()->success('Users are in sync');
}
}
/**
* Get a batch of user ids to check.
*
* @param int $offset
* Offset of the records to get.
* @param int $length
* Length of the records to get.
*
* @return array
* Array of user ids.
*/
protected function getNextBatch($offset, $length) {
$connection = Database::getConnection();
return $connection
->select('users', 'u')
->fields('u', ['uid'])
->range($offset, $length)
->orderBy('u.uid')
->execute()
->fetchCol();
}
/**
* Check which user ids are not in D7.
*
* @param array $uids
* List of user ids to check
*
* @return array
* Array of user ids not present in D7.
*/
protected function userIdsNotInD7($uids) {
$d7_connection = Database::getConnection('default', 'migrate');
$matched_uids = $d7_connection
->select('users', 'u')
->fields('u', ['uid'])
->condition('u.uid', $uids, 'IN')
->orderBy('u.uid')
->execute()
->fetchCol();
return array_diff($uids, $matched_uids);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment