mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-27 17:51:09 +00:00
2321e645f4
Opt out users who are active (according to Special:ActiveUsers) and have at least 100 edits, unless they have already opted in by enabling the beta feature. To run the script, use the new run.php wrapper: php maintenance/run.php MediaWiki.Extension.DiscussionTools.Maintenance.NewTopicOptOutActiveUsers Bug: T317375 Change-Id: I953c4c9d00a60b00a621049b119ba96c9682004d
120 lines
3.6 KiB
PHP
120 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Maintenance;
|
|
|
|
use IDatabase;
|
|
use Maintenance;
|
|
use MediaWiki\Extension\DiscussionTools\Hooks\HookUtils;
|
|
|
|
class NewTopicOptOutActiveUsers extends Maintenance {
|
|
|
|
private IDatabase $dbw;
|
|
|
|
public function __construct() {
|
|
parent::__construct();
|
|
$this->requireExtension( 'DiscussionTools' );
|
|
$this->addDescription( 'Opt out active users from the new topic tool' );
|
|
$this->addOption( 'dry-run', 'Output information, do not save changes' );
|
|
$this->addOption( 'save', 'Save the changes to the database' );
|
|
$this->setBatchSize( 100 );
|
|
}
|
|
|
|
public function execute() {
|
|
if ( $this->hasOption( 'dry-run' ) ) {
|
|
$save = false;
|
|
$this->output( "Dry run:\n" );
|
|
} elseif ( $this->hasOption( 'save' ) ) {
|
|
$save = true;
|
|
$this->output( "CHANGING PREFERENCES!\n" );
|
|
$this->countDown( 5 );
|
|
} else {
|
|
$this->error( "Please provide '--dry-run' or '--save' option" );
|
|
return;
|
|
}
|
|
|
|
$this->dbw = $this->getDB( DB_PRIMARY );
|
|
|
|
$userRows = $this->dbw->newSelectQueryBuilder()
|
|
->caller( __METHOD__ )
|
|
->table( 'querycachetwo' )
|
|
->where( [
|
|
'qcc_type' => 'activeusers',
|
|
'qcc_namespace' => NS_USER,
|
|
] )
|
|
->join( 'user', null, 'qcc_title=user_name' )
|
|
->where( [ 'user_editcount >= 100' ] )
|
|
->fields( [ 'user_id', 'user_name' ] )
|
|
->fetchResultSet();
|
|
|
|
$count = count( $userRows );
|
|
$countUpdated = 0;
|
|
$this->output( "Found $count active users with enough edits\n" );
|
|
|
|
foreach ( $userRows as $i => $row ) {
|
|
$skipReason = $this->skipReason( $row->user_id );
|
|
if ( $skipReason ) {
|
|
$this->output( "Won't update '$row->user_name' because: $skipReason\n" );
|
|
} else {
|
|
$this->output( "Will update '$row->user_name'\n" );
|
|
$countUpdated++;
|
|
if ( $save ) {
|
|
$this->updatePrefs( $row->user_id );
|
|
if ( $countUpdated % $this->getBatchSize() === 0 ) {
|
|
$this->waitForReplication();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( $save ) {
|
|
$this->output( "Updated $countUpdated out of $count users\n" );
|
|
} else {
|
|
$this->output( "Would update $countUpdated out of $count users\n" );
|
|
}
|
|
}
|
|
|
|
private function skipReason( int $userId ): ?string {
|
|
// We can't use UserOptionsLookup here, because we're not interested in the default options,
|
|
// but only in the options actually stored in the database.
|
|
|
|
// We're not looking at global preferences, because if the user has set them, then they will
|
|
// override our local preferences anyway.
|
|
|
|
// Check that the user has not already set their preference for new topic tool to any value
|
|
$foundRow = $this->dbw->newSelectQueryBuilder()
|
|
->caller( __METHOD__ )
|
|
->table( 'user_properties' )
|
|
->where( [ 'up_user' => $userId, 'up_property' => 'discussiontools-' . HookUtils::NEWTOPICTOOL ] )
|
|
->field( '1' )
|
|
->fetchField();
|
|
if ( $foundRow ) {
|
|
return HookUtils::NEWTOPICTOOL;
|
|
}
|
|
|
|
// Check that the user has not already opted into the beta feature
|
|
$foundRow = $this->dbw->newSelectQueryBuilder()
|
|
->caller( __METHOD__ )
|
|
->table( 'user_properties' )
|
|
->where( [ 'up_user' => $userId, 'up_property' => 'discussiontools-betaenable', 'up_value' => 1 ] )
|
|
->field( '1' )
|
|
->fetchField();
|
|
if ( $foundRow ) {
|
|
return 'betaenable';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function updatePrefs( int $userId ): void {
|
|
// We can't use UserOptionsManager here, because we want to store the preference
|
|
// in the database even if it's identical to the current default
|
|
// (this script is only used when we're about to change the default).
|
|
$this->dbw->insert(
|
|
'user_properties',
|
|
[ 'up_user' => $userId, 'up_property' => 'discussiontools-' . HookUtils::NEWTOPICTOOL, 'up_value' => 0 ],
|
|
__METHOD__
|
|
);
|
|
}
|
|
|
|
}
|