2011-08-26 20:12:34 +00:00
|
|
|
<?php
|
|
|
|
|
2020-12-03 22:05:42 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Api;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiResult;
|
|
|
|
use FormatJson;
|
|
|
|
use LogicException;
|
2020-09-18 14:49:13 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
2020-01-11 17:05:30 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\VariableGenerator\RCVariableGenerator;
|
2021-01-02 13:41:31 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\AbuseFilterVariableHolder;
|
2020-12-03 22:05:42 +00:00
|
|
|
use RecentChange;
|
2020-01-11 17:05:30 +00:00
|
|
|
|
2020-12-03 22:05:42 +00:00
|
|
|
class CheckMatch extends ApiBase {
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
2020-12-03 08:49:56 +00:00
|
|
|
* @inheritDoc
|
2018-04-04 21:14:25 +00:00
|
|
|
*/
|
2011-08-26 20:12:34 +00:00
|
|
|
public function execute() {
|
2020-09-18 14:49:13 +00:00
|
|
|
$afPermManager = AbuseFilterServices::getPermissionManager();
|
2011-08-26 20:12:34 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$this->requireOnlyOneParameter( $params, 'vars', 'rcid', 'logid' );
|
|
|
|
|
|
|
|
// "Anti-DoS"
|
2020-09-18 14:49:13 +00:00
|
|
|
if ( !$afPermManager->canViewPrivateFilters( $this->getUser() ) ) {
|
2017-08-07 23:35:21 +00:00
|
|
|
$this->dieWithError( 'apierror-abusefilter-canttest', 'permissiondenied' );
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
|
2013-10-15 12:28:39 +00:00
|
|
|
$vars = null;
|
2011-08-26 20:12:34 +00:00
|
|
|
if ( $params['vars'] ) {
|
2014-07-30 00:50:30 +00:00
|
|
|
$pairs = FormatJson::decode( $params['vars'], true );
|
2019-02-24 14:55:19 +00:00
|
|
|
$vars = AbuseFilterVariableHolder::newFromArray( $pairs );
|
2011-08-26 20:12:34 +00:00
|
|
|
} elseif ( $params['rcid'] ) {
|
2020-05-28 18:09:17 +00:00
|
|
|
$rc = RecentChange::newFromId( $params['rcid'] );
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2020-05-28 18:09:17 +00:00
|
|
|
if ( !$rc ) {
|
2017-08-07 23:35:21 +00:00
|
|
|
$this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] );
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 16:39:57 +00:00
|
|
|
$vars = new AbuseFilterVariableHolder();
|
2020-05-28 18:09:17 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
|
2020-09-20 22:07:57 +00:00
|
|
|
$varGenerator = new RCVariableGenerator( $vars, $rc, $this->getUser() );
|
2019-06-25 16:39:57 +00:00
|
|
|
$vars = $varGenerator->getVars();
|
2011-08-26 20:12:34 +00:00
|
|
|
} elseif ( $params['logid'] ) {
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2011-08-26 23:52:46 +00:00
|
|
|
$row = $dbr->selectRow(
|
|
|
|
'abuse_filter_log',
|
2018-02-05 10:48:58 +00:00
|
|
|
'afl_var_dump',
|
2017-06-15 14:23:16 +00:00
|
|
|
[ 'afl_id' => $params['logid'] ],
|
2011-08-26 23:52:46 +00:00
|
|
|
__METHOD__
|
2011-08-26 20:12:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$row ) {
|
2017-08-07 23:35:21 +00:00
|
|
|
$this->dieWithError( [ 'apierror-abusefilter-nosuchlogid', $params['logid'] ], 'nosuchlogid' );
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 14:52:05 +00:00
|
|
|
$vars = AbuseFilterServices::getVariablesBlobStore()->loadVarDump( $row->afl_var_dump );
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
2020-09-19 22:30:14 +00:00
|
|
|
if ( $vars === null ) {
|
|
|
|
throw new LogicException( 'Impossible.' );
|
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2019-12-16 16:19:48 +00:00
|
|
|
$parser = AbuseFilterServices::getParserFactory()->newParser();
|
2020-12-18 16:53:36 +00:00
|
|
|
if ( $parser->checkSyntax( $params['filter'] )->getResult() !== true ) {
|
2017-08-07 23:35:21 +00:00
|
|
|
$this->dieWithError( 'apierror-abusefilter-badsyntax', 'badsyntax' );
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 22:30:14 +00:00
|
|
|
$parser->setVariables( $vars );
|
2017-06-15 14:23:16 +00:00
|
|
|
$result = [
|
2016-10-27 14:10:31 +00:00
|
|
|
ApiResult::META_BC_BOOLS => [ 'result' ],
|
2019-09-15 15:48:13 +00:00
|
|
|
'result' => $parser->checkConditions( $params['filter'] )->getResult(),
|
2017-06-15 14:23:16 +00:00
|
|
|
];
|
2015-05-18 14:30:02 +00:00
|
|
|
|
2011-08-26 20:12:34 +00:00
|
|
|
$this->getResult()->addValue(
|
|
|
|
null,
|
|
|
|
$this->getModuleName(),
|
2015-05-18 14:30:02 +00:00
|
|
|
$result
|
2011-08-26 20:12:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
2020-12-03 22:05:42 +00:00
|
|
|
* @see ApiBase::getAllowedParams
|
2018-04-04 21:14:25 +00:00
|
|
|
*/
|
2011-08-26 20:12:34 +00:00
|
|
|
public function getAllowedParams() {
|
2017-06-15 14:23:16 +00:00
|
|
|
return [
|
|
|
|
'filter' => [
|
2011-08-26 20:12:34 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
2011-08-26 20:12:34 +00:00
|
|
|
'vars' => null,
|
2017-06-15 14:23:16 +00:00
|
|
|
'rcid' => [
|
2011-08-26 20:12:34 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer'
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
'logid' => [
|
2011-08-26 20:12:34 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer'
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
];
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 16:25:22 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @return array
|
2020-12-03 22:05:42 +00:00
|
|
|
* @see ApiBase::getExamplesMessages()
|
2014-10-28 16:25:22 +00:00
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
2017-06-15 14:23:16 +00:00
|
|
|
return [
|
2014-10-28 16:25:22 +00:00
|
|
|
'action=abusefiltercheckmatch&filter=!("autoconfirmed"%20in%20user_groups)&rcid=15'
|
|
|
|
=> 'apihelp-abusefiltercheckmatch-example-1',
|
2017-06-15 14:23:16 +00:00
|
|
|
];
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
}
|