mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-24 22:15:26 +00:00
6081bf90c4
So we can use DI in all generators. Some improvements were deliberately omitted, e.g. injecting more services and relaxing User/Title to UserIdentity/LinkTarget, and they'll be included in a subsequent commit. Depends-On: I1f351071ef2b0b7c80e91407a9c3bb17be293044 Depends-On: Ie71740fac35a86f8fe03023080ae8ca08671243d Depends-On: I589a0e1c2c5891070ab82cd5adfd9cedec19e67d Change-Id: I92ef0abd5e45b672e6f297a71b3c2c345d56f136
108 lines
2.7 KiB
PHP
108 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Api;
|
|
|
|
use ApiBase;
|
|
use ApiResult;
|
|
use FormatJson;
|
|
use LogicException;
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
|
|
use RecentChange;
|
|
|
|
class CheckMatch extends ApiBase {
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function execute() {
|
|
$afPermManager = AbuseFilterServices::getPermissionManager();
|
|
$params = $this->extractRequestParams();
|
|
$this->requireOnlyOneParameter( $params, 'vars', 'rcid', 'logid' );
|
|
|
|
// "Anti-DoS"
|
|
if ( !$afPermManager->canViewPrivateFilters( $this->getUser() ) ) {
|
|
$this->dieWithError( 'apierror-abusefilter-canttest', 'permissiondenied' );
|
|
}
|
|
|
|
$vars = null;
|
|
if ( $params['vars'] ) {
|
|
$pairs = FormatJson::decode( $params['vars'], true );
|
|
$vars = VariableHolder::newFromArray( $pairs );
|
|
} elseif ( $params['rcid'] ) {
|
|
$rc = RecentChange::newFromId( $params['rcid'] );
|
|
|
|
if ( !$rc ) {
|
|
$this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
|
|
}
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
|
|
$varGenerator = AbuseFilterServices::getVariableGeneratorFactory()->newRCGenerator( $rc, $this->getUser() );
|
|
$vars = $varGenerator->getVars();
|
|
} elseif ( $params['logid'] ) {
|
|
$dbr = wfGetDB( DB_REPLICA );
|
|
$row = $dbr->selectRow(
|
|
'abuse_filter_log',
|
|
'afl_var_dump',
|
|
[ 'afl_id' => $params['logid'] ],
|
|
__METHOD__
|
|
);
|
|
|
|
if ( !$row ) {
|
|
$this->dieWithError( [ 'apierror-abusefilter-nosuchlogid', $params['logid'] ], 'nosuchlogid' );
|
|
}
|
|
|
|
$vars = AbuseFilterServices::getVariablesBlobStore()->loadVarDump( $row->afl_var_dump );
|
|
}
|
|
if ( $vars === null ) {
|
|
throw new LogicException( 'Impossible.' );
|
|
}
|
|
|
|
$parser = AbuseFilterServices::getParserFactory()->newParser();
|
|
if ( $parser->checkSyntax( $params['filter'] )->getResult() !== true ) {
|
|
$this->dieWithError( 'apierror-abusefilter-badsyntax', 'badsyntax' );
|
|
}
|
|
|
|
$parser->setVariables( $vars );
|
|
$result = [
|
|
ApiResult::META_BC_BOOLS => [ 'result' ],
|
|
'result' => $parser->checkConditions( $params['filter'] )->getResult(),
|
|
];
|
|
|
|
$this->getResult()->addValue(
|
|
null,
|
|
$this->getModuleName(),
|
|
$result
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @see ApiBase::getAllowedParams
|
|
*/
|
|
public function getAllowedParams() {
|
|
return [
|
|
'filter' => [
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
],
|
|
'vars' => null,
|
|
'rcid' => [
|
|
ApiBase::PARAM_TYPE => 'integer'
|
|
],
|
|
'logid' => [
|
|
ApiBase::PARAM_TYPE => 'integer'
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @see ApiBase::getExamplesMessages()
|
|
*/
|
|
protected function getExamplesMessages() {
|
|
return [
|
|
'action=abusefiltercheckmatch&filter=!("autoconfirmed"%20in%20user_groups)&rcid=15'
|
|
=> 'apihelp-abusefiltercheckmatch-example-1',
|
|
];
|
|
}
|
|
}
|