2011-08-26 20:12:34 +00:00
|
|
|
<?php
|
|
|
|
|
2020-12-03 22:05:42 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Api;
|
|
|
|
|
2024-10-19 19:55:40 +00:00
|
|
|
use MediaWiki\Api\ApiBase;
|
|
|
|
use MediaWiki\Api\ApiMain;
|
|
|
|
use MediaWiki\Api\ApiResult;
|
2021-03-25 03:48:03 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
|
2021-09-01 11:53:38 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerFactory;
|
2021-04-04 18:57:49 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGeneratorFactory;
|
2021-01-02 13:41:31 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariablesFormatter;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2022-04-03 23:23:52 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2020-09-19 22:30:14 +00:00
|
|
|
|
2020-12-03 22:05:42 +00:00
|
|
|
class EvalExpression extends ApiBase {
|
2021-03-25 03:48:03 +00:00
|
|
|
|
2021-09-01 11:53:38 +00:00
|
|
|
/** @var RuleCheckerFactory */
|
|
|
|
private $ruleCheckerFactory;
|
2021-04-04 18:57:49 +00:00
|
|
|
|
2021-03-25 03:48:03 +00:00
|
|
|
/** @var AbuseFilterPermissionManager */
|
|
|
|
private $afPermManager;
|
|
|
|
|
2021-04-04 18:57:49 +00:00
|
|
|
/** @var VariableGeneratorFactory */
|
|
|
|
private $afVariableGeneratorFactory;
|
|
|
|
|
2021-03-25 03:48:03 +00:00
|
|
|
/**
|
|
|
|
* @param ApiMain $main
|
|
|
|
* @param string $action
|
2021-09-01 11:53:38 +00:00
|
|
|
* @param RuleCheckerFactory $ruleCheckerFactory
|
2021-03-25 03:48:03 +00:00
|
|
|
* @param AbuseFilterPermissionManager $afPermManager
|
2021-04-04 18:57:49 +00:00
|
|
|
* @param VariableGeneratorFactory $afVariableGeneratorFactory
|
2021-03-25 03:48:03 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
|
|
|
$action,
|
2021-09-01 11:53:38 +00:00
|
|
|
RuleCheckerFactory $ruleCheckerFactory,
|
2021-04-04 18:57:49 +00:00
|
|
|
AbuseFilterPermissionManager $afPermManager,
|
|
|
|
VariableGeneratorFactory $afVariableGeneratorFactory
|
2021-03-25 03:48:03 +00:00
|
|
|
) {
|
|
|
|
parent::__construct( $main, $action );
|
2021-09-01 11:53:38 +00:00
|
|
|
$this->ruleCheckerFactory = $ruleCheckerFactory;
|
2021-03-25 03:48:03 +00:00
|
|
|
$this->afPermManager = $afPermManager;
|
2021-04-04 18:57:49 +00:00
|
|
|
$this->afVariableGeneratorFactory = $afVariableGeneratorFactory;
|
2021-03-25 03:48:03 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2019-11-16 13:22:46 +00:00
|
|
|
// "Anti-DoS"
|
2022-07-02 13:35:00 +00:00
|
|
|
if ( !$this->afPermManager->canUseTestTools( $this->getAuthority() ) ) {
|
2019-11-16 13:22:46 +00:00
|
|
|
$this->dieWithError( 'apierror-abusefilter-canteval', 'permissiondenied' );
|
|
|
|
}
|
|
|
|
|
2011-08-26 20:12:34 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
2020-09-19 22:30:14 +00:00
|
|
|
$status = $this->evaluateExpression( $params['expression'] );
|
2018-03-26 18:41:20 +00:00
|
|
|
if ( !$status->isGood() ) {
|
2024-04-12 20:48:59 +00:00
|
|
|
$this->dieStatus( $status );
|
2018-03-26 18:41:20 +00:00
|
|
|
} else {
|
|
|
|
$res = $status->getValue();
|
2020-12-31 13:29:00 +00:00
|
|
|
$res = $params['prettyprint'] ? VariablesFormatter::formatVar( $res ) : $res;
|
2018-03-26 18:41:20 +00:00
|
|
|
$this->getResult()->addValue(
|
|
|
|
null,
|
|
|
|
$this->getModuleName(),
|
|
|
|
ApiResult::addMetadataToResultVars( [ 'result' => $res ] )
|
|
|
|
);
|
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 22:30:14 +00:00
|
|
|
/**
|
|
|
|
* @param string $expr
|
|
|
|
* @return Status
|
|
|
|
*/
|
2020-12-03 22:05:42 +00:00
|
|
|
private function evaluateExpression( string $expr ): Status {
|
2021-09-01 11:53:38 +00:00
|
|
|
$ruleChecker = $this->ruleCheckerFactory->newRuleChecker();
|
2021-09-06 20:40:36 +00:00
|
|
|
if ( !$ruleChecker->checkSyntax( $expr )->isValid() ) {
|
2020-09-19 22:30:14 +00:00
|
|
|
return Status::newFatal( 'abusefilter-tools-syntax-error' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic vars are the only ones available
|
2021-04-04 18:57:49 +00:00
|
|
|
$generator = $this->afVariableGeneratorFactory->newGenerator();
|
2020-09-19 22:30:14 +00:00
|
|
|
$vars = $generator->addGenericVars()->getVariableHolder();
|
2021-09-01 11:53:38 +00:00
|
|
|
$ruleChecker->setVariables( $vars );
|
2020-09-19 22:30:14 +00:00
|
|
|
|
2021-09-01 11:53:38 +00:00
|
|
|
return Status::newGood( $ruleChecker->evaluateExpression( $expr ) );
|
2020-09-19 22:30:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
2021-01-17 11:54:43 +00:00
|
|
|
* @codeCoverageIgnore Merely declarative
|
|
|
|
* @inheritDoc
|
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 [
|
|
|
|
'expression' => [
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
2018-03-26 18:41:20 +00:00
|
|
|
'prettyprint' => [
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'boolean'
|
2018-03-26 18:41:20 +00:00
|
|
|
]
|
2017-06-15 14:23:16 +00:00
|
|
|
];
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 16:25:22 +00:00
|
|
|
/**
|
2021-01-17 11:54:43 +00:00
|
|
|
* @codeCoverageIgnore Merely declarative
|
|
|
|
* @inheritDoc
|
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=abusefilterevalexpression&expression=lcase("FOO")'
|
|
|
|
=> 'apihelp-abusefilterevalexpression-example-1',
|
2018-03-26 18:41:20 +00:00
|
|
|
'action=abusefilterevalexpression&expression=lcase("FOO")&prettyprint=1'
|
|
|
|
=> 'apihelp-abusefilterevalexpression-example-2',
|
2017-06-15 14:23:16 +00:00
|
|
|
];
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
}
|