Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupalorg_migrate
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupalorg_migrate
Commits
01e310d8
Commit
01e310d8
authored
2 months ago
by
Fran Garcia-Linares
Browse files
Options
Downloads
Patches
Plain Diff
Delete users not in D7.
parent
40b025c9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Commands/DrushCommands.php
+101
-1
101 additions, 1 deletion
src/Commands/DrushCommands.php
with
101 additions
and
1 deletion
src/Commands/DrushCommands.php
+
101
−
1
View file @
01e310d8
...
...
@@ -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
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment