2020-10-21 14:18:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
|
|
|
use BagOStuff;
|
|
|
|
use ManualLogEntry;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Title\TitleValue;
|
2020-10-21 14:18:08 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class responsible for storing and retrieving blockautopromote status
|
|
|
|
*/
|
|
|
|
class BlockAutopromoteStore {
|
|
|
|
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterBlockAutopromoteStore';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var BagOStuff
|
|
|
|
*/
|
|
|
|
private $store;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
2020-10-25 15:30:43 +00:00
|
|
|
/** @var FilterUser */
|
|
|
|
private $filterUser;
|
|
|
|
|
2020-10-21 14:18:08 +00:00
|
|
|
/**
|
|
|
|
* @param BagOStuff $store
|
|
|
|
* @param LoggerInterface $logger
|
2020-10-25 15:30:43 +00:00
|
|
|
* @param FilterUser $filterUser
|
2020-10-21 14:18:08 +00:00
|
|
|
*/
|
2020-10-25 15:30:43 +00:00
|
|
|
public function __construct( BagOStuff $store, LoggerInterface $logger, FilterUser $filterUser ) {
|
2020-10-21 14:18:08 +00:00
|
|
|
$this->store = $store;
|
|
|
|
$this->logger = $logger;
|
2020-10-25 15:30:43 +00:00
|
|
|
$this->filterUser = $filterUser;
|
2020-10-21 14:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the autopromotion block status for the given user
|
|
|
|
*
|
|
|
|
* @param UserIdentity $target
|
|
|
|
* @return int
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getAutoPromoteBlockStatus( UserIdentity $target ): int {
|
2020-10-21 14:18:08 +00:00
|
|
|
return (int)$this->store->get( $this->getAutoPromoteBlockKey( $target ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks autopromotion for the given user
|
|
|
|
*
|
|
|
|
* @param UserIdentity $target
|
|
|
|
* @param string $msg The message to show in the log
|
|
|
|
* @param int $duration Duration for which autopromotion is blocked, in seconds
|
|
|
|
* @return bool True on success, false on failure
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function blockAutoPromote( UserIdentity $target, string $msg, int $duration ): bool {
|
2020-10-21 14:18:08 +00:00
|
|
|
if ( !$this->store->set(
|
|
|
|
$this->getAutoPromoteBlockKey( $target ),
|
|
|
|
1,
|
|
|
|
$duration
|
|
|
|
) ) {
|
|
|
|
// Failed to set key
|
|
|
|
$this->logger->warning(
|
|
|
|
'Failed to block autopromotion for {target}. Error: {error}',
|
|
|
|
[
|
|
|
|
'target' => $target->getName(),
|
|
|
|
'error' => $this->store->getLastError(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$logEntry = new ManualLogEntry( 'rights', 'blockautopromote' );
|
2022-03-04 18:04:08 +00:00
|
|
|
$logEntry->setPerformer( $this->filterUser->getUserIdentity() );
|
2020-10-21 14:18:08 +00:00
|
|
|
$logEntry->setTarget( new TitleValue( NS_USER, $target->getName() ) );
|
|
|
|
|
|
|
|
$logEntry->setParameters( [
|
|
|
|
'7::duration' => $duration,
|
|
|
|
// These parameters are unused in our message, but some parts of the code check for them
|
|
|
|
'4::oldgroups' => [],
|
|
|
|
'5::newgroups' => []
|
|
|
|
] );
|
|
|
|
$logEntry->setComment( $msg );
|
2021-01-16 14:52:09 +00:00
|
|
|
if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
|
|
|
|
// FIXME Remove this check once ManualLogEntry is servicified (T253717)
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
$logEntry->publish( $logEntry->insert() );
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
2020-10-21 14:18:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblocks autopromotion for the given user
|
|
|
|
*
|
|
|
|
* @param UserIdentity $target
|
|
|
|
* @param UserIdentity $performer
|
|
|
|
* @param string $msg The message to show in the log
|
|
|
|
* @return bool True on success, false on failure
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function unblockAutopromote( UserIdentity $target, UserIdentity $performer, string $msg ): bool {
|
2020-10-21 14:18:08 +00:00
|
|
|
// Immediately expire (delete) the key, failing if it does not exist
|
|
|
|
$expireAt = time() - BagOStuff::TTL_HOUR;
|
|
|
|
if ( !$this->store->changeTTL(
|
|
|
|
$this->getAutoPromoteBlockKey( $target ),
|
|
|
|
$expireAt
|
|
|
|
) ) {
|
|
|
|
// Key did not exist to begin with; nothing to do
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$logEntry = new ManualLogEntry( 'rights', 'restoreautopromote' );
|
|
|
|
$logEntry->setTarget( new TitleValue( NS_USER, $target->getName() ) );
|
|
|
|
$logEntry->setComment( $msg );
|
|
|
|
// These parameters are unused in our message, but some parts of the code check for them
|
|
|
|
$logEntry->setParameters( [
|
|
|
|
'4::oldgroups' => [],
|
|
|
|
'5::newgroups' => []
|
|
|
|
] );
|
|
|
|
$logEntry->setPerformer( $performer );
|
2021-01-16 14:52:09 +00:00
|
|
|
if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
|
|
|
|
// FIXME Remove this check once ManualLogEntry is servicified (T253717)
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
$logEntry->publish( $logEntry->insert() );
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
2020-10-21 14:18:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UserIdentity $target
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function getAutoPromoteBlockKey( UserIdentity $target ): string {
|
2020-10-21 14:18:08 +00:00
|
|
|
return $this->store->makeKey( 'abusefilter', 'block-autopromote', $target->getId() );
|
|
|
|
}
|
|
|
|
}
|