mediawiki-extensions-AbuseF.../includes/Api/CheckMatch.php
Daimona Eaytoy 3e0c30ff92 Allow the parsers to return extra info
This is achieved by creating a new ParserStatus class. Aside from the
result of parse(), it contains whether the cache was warm. This can be
used to differentiate profiling data as part of T231112.

Another use case is returning non-fatal warnings (T269770).

Change-Id: Ifcbda861ce1a44bbe9bffba5b83cd9ef338a8dba
2020-12-11 15:03:23 +00:00

111 lines
2.8 KiB
PHP

<?php
namespace MediaWiki\Extension\AbuseFilter\Api;
use AbuseFilter;
use AbuseFilterVariableHolder;
use ApiBase;
use ApiResult;
use FormatJson;
use LogicException;
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use MediaWiki\Extension\AbuseFilter\VariableGenerator\RCVariableGenerator;
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 = AbuseFilterVariableHolder::newFromArray( $pairs );
} elseif ( $params['rcid'] ) {
$rc = RecentChange::newFromId( $params['rcid'] );
if ( !$rc ) {
$this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
}
$vars = new AbuseFilterVariableHolder();
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
$varGenerator = new RCVariableGenerator( $vars, $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 = AbuseFilter::loadVarDump( $row->afl_var_dump );
}
if ( $vars === null ) {
throw new LogicException( 'Impossible.' );
}
$parser = AbuseFilterServices::getParserFactory()->newParser();
if ( $parser->checkSyntax( $params['filter'] ) !== 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',
];
}
}