mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-15 02:03:53 +00:00
1bd0b02441
Some of these api modules still retrieve other services statically, this patch is focused just on injecting the permission manager and setting up DI Bug: T259960 Change-Id: Ic5196f230d68604fdf321f705377a1e6e1e2efca
104 lines
2.6 KiB
PHP
104 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Api;
|
|
|
|
use ApiBase;
|
|
use ApiMain;
|
|
use ApiResult;
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariablesFormatter;
|
|
use Status;
|
|
|
|
class EvalExpression extends ApiBase {
|
|
|
|
/** @var AbuseFilterPermissionManager */
|
|
private $afPermManager;
|
|
|
|
/**
|
|
* @param ApiMain $main
|
|
* @param string $action
|
|
* @param AbuseFilterPermissionManager $afPermManager
|
|
*/
|
|
public function __construct(
|
|
ApiMain $main,
|
|
$action,
|
|
AbuseFilterPermissionManager $afPermManager
|
|
) {
|
|
parent::__construct( $main, $action );
|
|
$this->afPermManager = $afPermManager;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function execute() {
|
|
// "Anti-DoS"
|
|
if ( !$this->afPermManager->canUseTestTools( $this->getUser() ) ) {
|
|
$this->dieWithError( 'apierror-abusefilter-canteval', 'permissiondenied' );
|
|
}
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$status = $this->evaluateExpression( $params['expression'] );
|
|
if ( !$status->isGood() ) {
|
|
$this->dieWithError( $status->getErrors()[0] );
|
|
} else {
|
|
$res = $status->getValue();
|
|
$res = $params['prettyprint'] ? VariablesFormatter::formatVar( $res ) : $res;
|
|
$this->getResult()->addValue(
|
|
null,
|
|
$this->getModuleName(),
|
|
ApiResult::addMetadataToResultVars( [ 'result' => $res ] )
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $expr
|
|
* @return Status
|
|
*/
|
|
private function evaluateExpression( string $expr ): Status {
|
|
$parser = AbuseFilterServices::getParserFactory()->newParser();
|
|
if ( $parser->checkSyntax( $expr )->getResult() !== true ) {
|
|
return Status::newFatal( 'abusefilter-tools-syntax-error' );
|
|
}
|
|
|
|
// Generic vars are the only ones available
|
|
$generator = AbuseFilterServices::getVariableGeneratorFactory()->newGenerator();
|
|
$vars = $generator->addGenericVars()->getVariableHolder();
|
|
$vars->setVar( 'timestamp', wfTimestamp( TS_UNIX ) );
|
|
$parser->setVariables( $vars );
|
|
|
|
return Status::newGood( $parser->evaluateExpression( $expr ) );
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore Merely declarative
|
|
* @inheritDoc
|
|
*/
|
|
public function getAllowedParams() {
|
|
return [
|
|
'expression' => [
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
],
|
|
'prettyprint' => [
|
|
ApiBase::PARAM_TYPE => 'boolean'
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @codeCoverageIgnore Merely declarative
|
|
* @inheritDoc
|
|
*/
|
|
protected function getExamplesMessages() {
|
|
return [
|
|
'action=abusefilterevalexpression&expression=lcase("FOO")'
|
|
=> 'apihelp-abusefilterevalexpression-example-1',
|
|
'action=abusefilterevalexpression&expression=lcase("FOO")&prettyprint=1'
|
|
=> 'apihelp-abusefilterevalexpression-example-2',
|
|
];
|
|
}
|
|
}
|