2011-08-26 20:12:34 +00:00
|
|
|
<?php
|
|
|
|
|
2020-12-03 22:05:42 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Api;
|
|
|
|
|
|
|
|
use AbuseFilter;
|
|
|
|
use AbuseFilterVariableHolder;
|
|
|
|
use ApiBase;
|
|
|
|
use ApiResult;
|
2020-09-18 14:49:13 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
2020-09-19 22:30:14 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGenerator;
|
2020-12-03 22:05:42 +00:00
|
|
|
use Status;
|
2020-09-19 22:30:14 +00:00
|
|
|
|
2020-12-03 22:05:42 +00:00
|
|
|
class EvalExpression 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();
|
2019-11-16 13:22:46 +00:00
|
|
|
// "Anti-DoS"
|
2020-09-18 14:49:13 +00:00
|
|
|
if ( !$afPermManager->canViewPrivateFilters( $this->getUser() ) ) {
|
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() ) {
|
|
|
|
$this->dieWithError( $status->getErrors()[0] );
|
|
|
|
} else {
|
|
|
|
$res = $status->getValue();
|
|
|
|
$res = $params['prettyprint'] ? AbuseFilter::formatVar( $res ) : $res;
|
|
|
|
$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 {
|
2019-12-16 16:19:48 +00:00
|
|
|
$parser = AbuseFilterServices::getParserFactory()->newParser();
|
2020-09-19 22:30:14 +00:00
|
|
|
if ( $parser->checkSyntax( $expr ) !== true ) {
|
|
|
|
return Status::newFatal( 'abusefilter-tools-syntax-error' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$vars = new AbuseFilterVariableHolder();
|
|
|
|
// Generic vars are the only ones available
|
|
|
|
$generator = new VariableGenerator( $vars );
|
|
|
|
$vars = $generator->addGenericVars()->getVariableHolder();
|
|
|
|
$vars->setVar( 'timestamp', wfTimestamp( TS_UNIX ) );
|
|
|
|
$parser->setVariables( $vars );
|
|
|
|
|
|
|
|
return Status::newGood( $parser->evaluateExpression( $expr ) );
|
|
|
|
}
|
|
|
|
|
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 [
|
|
|
|
'expression' => [
|
2011-08-26 20:12:34 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
2018-03-26 18:41:20 +00:00
|
|
|
'prettyprint' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean'
|
|
|
|
]
|
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=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
|
|
|
}
|
|
|
|
}
|