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;
|
2021-03-25 03:48:03 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
|
2021-08-29 22:58:17 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\Exception\UserVisibleException;
|
2021-09-01 11:53:38 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerFactory;
|
2022-04-03 23:23:52 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2020-09-18 14:49:13 +00:00
|
|
|
|
2020-12-03 22:05:42 +00:00
|
|
|
class CheckSyntax extends ApiBase {
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2024-10-25 17:02:04 +00:00
|
|
|
private RuleCheckerFactory $ruleCheckerFactory;
|
|
|
|
private AbuseFilterPermissionManager $afPermManager;
|
2021-04-04 18:57:49 +00:00
|
|
|
|
2021-03-25 03:48:03 +00:00
|
|
|
public function __construct(
|
|
|
|
ApiMain $main,
|
2024-10-25 17:02:04 +00:00
|
|
|
string $action,
|
2021-09-01 11:53:38 +00:00
|
|
|
RuleCheckerFactory $ruleCheckerFactory,
|
2021-03-25 03:48:03 +00:00
|
|
|
AbuseFilterPermissionManager $afPermManager
|
|
|
|
) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
// "Anti-DoS"
|
2022-07-02 13:35:00 +00:00
|
|
|
if ( !$this->afPermManager->canUseTestTools( $this->getAuthority() )
|
|
|
|
&& !$this->afPermManager->canEdit( $this->getAuthority() )
|
2021-02-19 17:50:38 +00:00
|
|
|
) {
|
2017-08-07 23:35:21 +00:00
|
|
|
$this->dieWithError( 'apierror-abusefilter-cantcheck', 'permissiondenied' );
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
|
2011-08-26 23:52:46 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2021-09-01 11:53:38 +00:00
|
|
|
$result = $this->ruleCheckerFactory->newRuleChecker()->checkSyntax( $params['filter'] );
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2017-06-15 14:23:16 +00:00
|
|
|
$r = [];
|
2020-12-18 16:53:36 +00:00
|
|
|
$warnings = [];
|
|
|
|
foreach ( $result->getWarnings() as $warning ) {
|
|
|
|
$warnings[] = [
|
2021-01-10 19:26:01 +00:00
|
|
|
'message' => $this->msg( $warning->getMessageObj() )->text(),
|
2020-12-18 16:53:36 +00:00
|
|
|
'character' => $warning->getPosition()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if ( $warnings ) {
|
|
|
|
$r['warnings'] = $warnings;
|
|
|
|
}
|
|
|
|
|
2021-09-06 20:40:36 +00:00
|
|
|
if ( $result->isValid() ) {
|
2011-08-26 20:12:34 +00:00
|
|
|
// Everything went better than expected :)
|
|
|
|
$r['status'] = 'ok';
|
|
|
|
} else {
|
2020-12-18 16:53:36 +00:00
|
|
|
// TODO: Improve the type here.
|
2021-08-29 22:58:17 +00:00
|
|
|
/** @var UserVisibleException $excep */
|
2020-12-18 16:53:36 +00:00
|
|
|
$excep = $result->getException();
|
2021-08-29 22:58:17 +00:00
|
|
|
'@phan-var UserVisibleException $excep';
|
2017-06-15 14:23:16 +00:00
|
|
|
$r = [
|
2011-08-26 20:12:34 +00:00
|
|
|
'status' => 'error',
|
2021-01-10 19:26:01 +00:00
|
|
|
'message' => $this->msg( $excep->getMessageObj() )->text(),
|
2020-12-18 16:53:36 +00:00
|
|
|
'character' => $excep->getPosition(),
|
2017-06-15 14:23:16 +00:00
|
|
|
];
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $r );
|
|
|
|
}
|
|
|
|
|
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 [
|
|
|
|
'filter' => [
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
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=abusefilterchecksyntax&filter="foo"'
|
|
|
|
=> 'apihelp-abusefilterchecksyntax-example-1',
|
|
|
|
'action=abusefilterchecksyntax&filter="bar"%20bad_variable'
|
|
|
|
=> 'apihelp-abusefilterchecksyntax-example-2',
|
2017-06-15 14:23:16 +00:00
|
|
|
];
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
2012-01-13 21:36:51 +00:00
|
|
|
}
|