Merge "Api: inject more abuse filter services"

This commit is contained in:
jenkins-bot 2021-04-18 09:15:17 +00:00 committed by Gerrit Code Review
commit 19bc3dcf9b
6 changed files with 116 additions and 22 deletions

View file

@ -123,20 +123,31 @@
"abusefilterchecksyntax": {
"class": "MediaWiki\\Extension\\AbuseFilter\\Api\\CheckSyntax",
"services": [
"AbuseFilterParserFactory",
"AbuseFilterPermissionManager"
]
},
"abusefilterevalexpression": {
"class": "MediaWiki\\Extension\\AbuseFilter\\Api\\EvalExpression",
"services": [
"AbuseFilterPermissionManager"
"AbuseFilterParserFactory",
"AbuseFilterPermissionManager",
"AbuseFilterVariableGeneratorFactory"
]
},
"abusefilterunblockautopromote": {
"class": "MediaWiki\\Extension\\AbuseFilter\\Api\\UnblockAutopromote",
"services": [
"AbuseFilterBlockAutopromoteStore"
]
},
"abusefilterunblockautopromote": "MediaWiki\\Extension\\AbuseFilter\\Api\\UnblockAutopromote",
"abusefiltercheckmatch": {
"class": "MediaWiki\\Extension\\AbuseFilter\\Api\\CheckMatch",
"services": [
"AbuseFilterPermissionManager"
"AbuseFilterParserFactory",
"AbuseFilterPermissionManager",
"AbuseFilterVariablesBlobStore",
"AbuseFilterVariableGeneratorFactory"
]
},
"abuselogprivatedetails": {
@ -150,7 +161,10 @@
"abuselog": {
"class": "MediaWiki\\Extension\\AbuseFilter\\Api\\QueryAbuseLog",
"services": [
"AbuseFilterPermissionManager"
"AbuseFilterFilterLookup",
"AbuseFilterPermissionManager",
"AbuseFilterVariablesBlobStore",
"AbuseFilterVariablesManager"
]
},
"abusefilters": {

View file

@ -10,29 +10,49 @@ use LogEventsList;
use LogicException;
use LogPage;
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseLog;
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGeneratorFactory;
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
use MediaWiki\Extension\AbuseFilter\Variables\VariablesBlobStore;
use MediaWiki\Revision\RevisionRecord;
use RecentChange;
class CheckMatch extends ApiBase {
/** @var ParserFactory */
private $afParserFactory;
/** @var AbuseFilterPermissionManager */
private $afPermManager;
/** @var VariablesBlobStore */
private $afVariablesBlobStore;
/** @var VariableGeneratorFactory */
private $afVariableGeneratorFactory;
/**
* @param ApiMain $main
* @param string $action
* @param ParserFactory $afParserFactory
* @param AbuseFilterPermissionManager $afPermManager
* @param VariablesBlobStore $afVariablesBlobStore
* @param VariableGeneratorFactory $afVariableGeneratorFactory
*/
public function __construct(
ApiMain $main,
$action,
AbuseFilterPermissionManager $afPermManager
ParserFactory $afParserFactory,
AbuseFilterPermissionManager $afPermManager,
VariablesBlobStore $afVariablesBlobStore,
VariableGeneratorFactory $afVariableGeneratorFactory
) {
parent::__construct( $main, $action );
$this->afParserFactory = $afParserFactory;
$this->afPermManager = $afPermManager;
$this->afVariablesBlobStore = $afVariablesBlobStore;
$this->afVariableGeneratorFactory = $afVariableGeneratorFactory;
}
/**
@ -79,9 +99,10 @@ class CheckMatch extends ApiBase {
}
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
$varGenerator = AbuseFilterServices::getVariableGeneratorFactory()->newRCGenerator( $rc, $user );
$varGenerator = $this->afVariableGeneratorFactory->newRCGenerator( $rc, $user );
$vars = $varGenerator->getVars();
} elseif ( $params['logid'] ) {
// TODO inject
$dbr = wfGetDB( DB_REPLICA );
$row = $dbr->selectRow(
'abuse_filter_log',
@ -100,13 +121,13 @@ class CheckMatch extends ApiBase {
$this->dieWithError( 'apierror-permissiondenied-generic', 'deletedabuselog' );
}
$vars = AbuseFilterServices::getVariablesBlobStore()->loadVarDump( $row->afl_var_dump );
$vars = $this->afVariablesBlobStore->loadVarDump( $row->afl_var_dump );
}
if ( $vars === null ) {
throw new LogicException( 'Impossible.' );
}
$parser = AbuseFilterServices::getParserFactory()->newParser( $vars );
$parser = $this->afParserFactory->newParser( $vars );
if ( $parser->checkSyntax( $params['filter'] )->getResult() !== true ) {
$this->dieWithError( 'apierror-abusefilter-badsyntax', 'badsyntax' );
}

View file

@ -5,25 +5,31 @@ namespace MediaWiki\Extension\AbuseFilter\Api;
use ApiBase;
use ApiMain;
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use MediaWiki\Extension\AbuseFilter\Parser\AFPUserVisibleException;
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
class CheckSyntax extends ApiBase {
/** @var ParserFactory */
private $afParserFactory;
/** @var AbuseFilterPermissionManager */
private $afPermManager;
/**
* @param ApiMain $main
* @param string $action
* @param ParserFactory $afParserFactory
* @param AbuseFilterPermissionManager $afPermManager
*/
public function __construct(
ApiMain $main,
$action,
ParserFactory $afParserFactory,
AbuseFilterPermissionManager $afPermManager
) {
parent::__construct( $main, $action );
$this->afParserFactory = $afParserFactory;
$this->afPermManager = $afPermManager;
}
@ -39,7 +45,7 @@ class CheckSyntax extends ApiBase {
}
$params = $this->extractRequestParams();
$result = AbuseFilterServices::getParserFactory()->newParser()->checkSyntax( $params['filter'] );
$result = $this->afParserFactory->newParser()->checkSyntax( $params['filter'] );
$r = [];
$warnings = [];

View file

@ -6,27 +6,40 @@ use ApiBase;
use ApiMain;
use ApiResult;
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGeneratorFactory;
use MediaWiki\Extension\AbuseFilter\Variables\VariablesFormatter;
use Status;
class EvalExpression extends ApiBase {
/** @var ParserFactory */
private $afParserFactory;
/** @var AbuseFilterPermissionManager */
private $afPermManager;
/** @var VariableGeneratorFactory */
private $afVariableGeneratorFactory;
/**
* @param ApiMain $main
* @param string $action
* @param ParserFactory $afParserFactory
* @param AbuseFilterPermissionManager $afPermManager
* @param VariableGeneratorFactory $afVariableGeneratorFactory
*/
public function __construct(
ApiMain $main,
$action,
AbuseFilterPermissionManager $afPermManager
ParserFactory $afParserFactory,
AbuseFilterPermissionManager $afPermManager,
VariableGeneratorFactory $afVariableGeneratorFactory
) {
parent::__construct( $main, $action );
$this->afParserFactory = $afParserFactory;
$this->afPermManager = $afPermManager;
$this->afVariableGeneratorFactory = $afVariableGeneratorFactory;
}
/**
@ -59,13 +72,13 @@ class EvalExpression extends ApiBase {
* @return Status
*/
private function evaluateExpression( string $expr ): Status {
$parser = AbuseFilterServices::getParserFactory()->newParser();
$parser = $this->afParserFactory->newParser();
if ( $parser->checkSyntax( $expr )->getResult() !== true ) {
return Status::newFatal( 'abusefilter-tools-syntax-error' );
}
// Generic vars are the only ones available
$generator = AbuseFilterServices::getVariableGeneratorFactory()->newGenerator();
$generator = $this->afVariableGeneratorFactory->newGenerator();
$vars = $generator->addGenericVars()->getVariableHolder();
$vars->setVar( 'timestamp', wfTimestamp( TS_UNIX ) );
$parser->setVariables( $vars );

View file

@ -31,11 +31,13 @@ use ApiQueryBase;
use InvalidArgumentException;
use MediaWiki\Extension\AbuseFilter\AbuseFilter;
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use MediaWiki\Extension\AbuseFilter\CentralDBNotAvailableException;
use MediaWiki\Extension\AbuseFilter\Filter\FilterNotFoundException;
use MediaWiki\Extension\AbuseFilter\FilterLookup;
use MediaWiki\Extension\AbuseFilter\GlobalNameUtils;
use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseLog;
use MediaWiki\Extension\AbuseFilter\Variables\VariablesBlobStore;
use MediaWiki\Extension\AbuseFilter\Variables\VariablesManager;
use MediaWiki\MediaWikiServices;
use MWTimestamp;
use Title;
@ -50,28 +52,46 @@ use Wikimedia\IPUtils;
*/
class QueryAbuseLog extends ApiQueryBase {
/** @var FilterLookup */
private $afFilterLookup;
/** @var AbuseFilterPermissionManager */
private $afPermManager;
/** @var VariablesBlobStore */
private $afVariablesBlobStore;
/** @var VariablesManager */
private $afVariablesManager;
/**
* @param ApiQuery $query
* @param string $moduleName
* @param FilterLookup $afFilterLookup
* @param AbuseFilterPermissionManager $afPermManager
* @param VariablesBlobStore $afVariablesBlobStore
* @param VariablesManager $afVariablesManager
*/
public function __construct(
ApiQuery $query,
$moduleName,
AbuseFilterPermissionManager $afPermManager
FilterLookup $afFilterLookup,
AbuseFilterPermissionManager $afPermManager,
VariablesBlobStore $afVariablesBlobStore,
VariablesManager $afVariablesManager
) {
parent::__construct( $query, $moduleName, 'afl' );
$this->afFilterLookup = $afFilterLookup;
$this->afPermManager = $afPermManager;
$this->afVariablesBlobStore = $afVariablesBlobStore;
$this->afVariablesManager = $afVariablesManager;
}
/**
* @inheritDoc
*/
public function execute() {
$lookup = AbuseFilterServices::getFilterLookup();
$lookup = $this->afFilterLookup;
$aflFilterMigrationStage = $this->getConfig()->get( 'AbuseFilterAflFilterMigrationStage' );
// Same check as in SpecialAbuseLog
@ -274,6 +294,7 @@ class QueryAbuseLog extends ApiQueryBase {
continue;
}
if ( $hidden === 'implicit' ) {
// TODO inject
$revRec = MediaWikiServices::getInstance()
->getRevisionLookup()
->getRevisionById( (int)$row->afl_rev_id );
@ -332,8 +353,8 @@ class QueryAbuseLog extends ApiQueryBase {
if ( $fld_details ) {
$entry['details'] = [];
if ( $canSeeDetails ) {
$vars = AbuseFilterServices::getVariablesBlobStore()->loadVarDump( $row->afl_var_dump );
$varManager = AbuseFilterServices::getVariablesManager();
$vars = $this->afVariablesBlobStore->loadVarDump( $row->afl_var_dump );
$varManager = $this->afVariablesManager;
$entry['details'] = $varManager->exportAllVars( $vars );
}
}

View file

@ -3,10 +3,29 @@
namespace MediaWiki\Extension\AbuseFilter\Api;
use ApiBase;
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use ApiMain;
use MediaWiki\Extension\AbuseFilter\BlockAutopromoteStore;
use User;
class UnblockAutopromote extends ApiBase {
/** @var BlockAutopromoteStore */
private $afBlockAutopromoteStore;
/**
* @param ApiMain $main
* @param string $action
* @param BlockAutopromoteStore $afBlockAutopromoteStore
*/
public function __construct(
ApiMain $main,
$action,
BlockAutopromoteStore $afBlockAutopromoteStore
) {
parent::__construct( $main, $action );
$this->afBlockAutopromoteStore = $afBlockAutopromoteStore;
}
/**
* @inheritDoc
*/
@ -30,7 +49,7 @@ class UnblockAutopromote extends ApiBase {
}
$msg = $this->msg( 'abusefilter-tools-restoreautopromote' )->inContentLanguage()->text();
$blockAutopromoteStore = AbuseFilterServices::getBlockAutopromoteStore();
$blockAutopromoteStore = $this->afBlockAutopromoteStore;
$res = $blockAutopromoteStore->unblockAutopromote( $target, $this->getUser(), $msg );
if ( $res === false ) {