mediawiki-extensions-AbuseF.../includes/BlockAutopromoteStore.php
Matěj Suchánek 1445d5962a Introduce BlockAutopromoteStore service
This service is responsible for the blockautopromote feature:
(un)block autopromotion and check status.

The patch mostly moves code from static methods to the new class
and relaxes type hints (e.g. from User to UserIdentity).

Change-Id: I79a72377881cf06717931cd09af12f3b8e5f3e3f
2020-10-24 12:31:44 +00:00

131 lines
3.4 KiB
PHP

<?php
namespace MediaWiki\Extension\AbuseFilter;
use AbuseFilter;
use BagOStuff;
use ManualLogEntry;
use MediaWiki\User\UserIdentity;
use Psr\Log\LoggerInterface;
use TitleValue;
/**
* Class responsible for storing and retrieving blockautopromote status
*/
class BlockAutopromoteStore {
public const SERVICE_NAME = 'AbuseFilterBlockAutopromoteStore';
/**
* @var BagOStuff
*/
private $store;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param BagOStuff $store
* @param LoggerInterface $logger
*/
public function __construct( BagOStuff $store, LoggerInterface $logger ) {
$this->store = $store;
$this->logger = $logger;
}
/**
* Gets the autopromotion block status for the given user
*
* @param UserIdentity $target
* @return int
*/
public function getAutoPromoteBlockStatus( UserIdentity $target ) : int {
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
*/
public function blockAutoPromote( UserIdentity $target, string $msg, int $duration ) : bool {
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' );
$performer = AbuseFilter::getFilterUser();
$logEntry->setPerformer( $performer );
$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 );
$logEntry->publish( $logEntry->insert() );
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
*/
public function unblockAutopromote( UserIdentity $target, UserIdentity $performer, string $msg ) : bool {
// 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 );
$logEntry->publish( $logEntry->insert() );
return true;
}
/**
* @param UserIdentity $target
* @return string
*/
private function getAutoPromoteBlockKey( UserIdentity $target ) : string {
return $this->store->makeKey( 'abusefilter', 'block-autopromote', $target->getId() );
}
}