mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-24 14:13:54 +00:00
6081bf90c4
So we can use DI in all generators. Some improvements were deliberately omitted, e.g. injecting more services and relaxing User/Title to UserIdentity/LinkTarget, and they'll be included in a subsequent commit. Depends-On: I1f351071ef2b0b7c80e91407a9c3bb17be293044 Depends-On: Ie71740fac35a86f8fe03023080ae8ca08671243d Depends-On: I589a0e1c2c5891070ab82cd5adfd9cedec19e67d Change-Id: I92ef0abd5e45b672e6f297a71b3c2c345d56f136
85 lines
2.2 KiB
PHP
85 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Api;
|
|
|
|
use ApiBase;
|
|
use ApiResult;
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariablesFormatter;
|
|
use Status;
|
|
|
|
class EvalExpression extends ApiBase {
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function execute() {
|
|
$afPermManager = AbuseFilterServices::getPermissionManager();
|
|
// "Anti-DoS"
|
|
if ( !$afPermManager->canViewPrivateFilters( $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 ) );
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @see ApiBase::getAllowedParams()
|
|
*/
|
|
public function getAllowedParams() {
|
|
return [
|
|
'expression' => [
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
],
|
|
'prettyprint' => [
|
|
ApiBase::PARAM_TYPE => 'boolean'
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @see ApiBase::getExamplesMessages()
|
|
*/
|
|
protected function getExamplesMessages() {
|
|
return [
|
|
'action=abusefilterevalexpression&expression=lcase("FOO")'
|
|
=> 'apihelp-abusefilterevalexpression-example-1',
|
|
'action=abusefilterevalexpression&expression=lcase("FOO")&prettyprint=1'
|
|
=> 'apihelp-abusefilterevalexpression-example-2',
|
|
];
|
|
}
|
|
}
|