14require_once __DIR__ .
'/Maintenance.php';
23 parent::__construct();
25 $this->
addDescription(
'Pass through all users and purge options for
26 users that have not logged in for certain time and have below certain edits' );
28 $this->
addOption(
'login-age',
'How many years since last login. Default: 5',
false,
true );
29 $this->
addOption(
'max-editcount',
'How many edit or more to skip deletion. Default: 100',
false,
true );
30 $this->
addOption(
'dry',
'Do not carry out the deletion' );
31 $this->
addArg(
'option name',
'Only delete this option',
false );
36 $option = $this->
getArg( 0 );
39 $maxUserId = $dbr->newSelectQueryBuilder()
40 ->select(
'MAX(user_id)' )
42 ->caller( __METHOD__ )
45 $duration =
'P' . (int)$this->
getOption(
'login-age', 5 ) .
'Y';
46 $cutoff = $dbr->timestamp( (
new DateTime() )->sub(
new DateInterval( $duration ) )->getTimestamp() );
47 for ( $min = 0; $min <= $maxUserId; $min += $this->
getBatchSize() ) {
48 $userIdsToClean = $dbr->newSelectQueryBuilder()
51 ->where( $dbr->expr(
'user_id',
'>=', $min ) )
52 ->where( $dbr->expr(
'user_id',
'<', $min + $this->getBatchSize() ) )
53 ->where( $dbr->expr(
'user_touched',
'<', $cutoff ) )
54 ->where( $dbr->expr(
'user_editcount',
'<', (
int)$this->getOption(
'max-editcount', 100 ) ) )
55 ->caller( __METHOD__ )->fetchFieldValues();
56 if ( $userIdsToClean === [] ) {
61 $this->
output(
'The script will clean up user_properties for the following users ' .
62 json_encode( $userIdsToClean ) .
"\n" );
67 $deleteQueryBuilder = $dbw->newDeleteQueryBuilder()
68 ->deleteFrom(
'user_properties' )
69 ->where( [
'up_user' => $userIdsToClean ] );
71 $deleteQueryBuilder->where( [
'up_property' => $option ] );
73 $deleteQueryBuilder->caller( __METHOD__ )->execute();
76 $cleaned += count( $userIdsToClean );
77 $this->
output(
'Cleaned up user_properties for users up to ' . ( $min + $this->
getBatchSize() ) .
78 ' and ' . $cleaned .
" users have been cleared.\n" );
86require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
execute()
Do the actual work.
__construct()
Default constructor.