2011-08-26 20:12:34 +00:00
|
|
|
<?php
|
|
|
|
|
2020-12-03 22:05:42 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Api;
|
|
|
|
|
|
|
|
use ApiBase;
|
2021-03-25 03:48:03 +00:00
|
|
|
use ApiMain;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
|
2020-09-18 14:49:13 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
2020-12-18 16:53:36 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\AFPUserVisibleException;
|
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
|
|
|
|
2021-03-25 03:48:03 +00:00
|
|
|
/** @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;
|
|
|
|
}
|
|
|
|
|
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"
|
2021-03-25 03:48:03 +00:00
|
|
|
if ( !$this->afPermManager->canUseTestTools( $this->getUser() )
|
|
|
|
&& !$this->afPermManager->canEdit( $this->getUser() )
|
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();
|
2020-12-03 22:05:42 +00:00
|
|
|
$result = AbuseFilterServices::getParserFactory()->newParser()->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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $result->getResult() === true ) {
|
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.
|
|
|
|
/** @var AFPUserVisibleException $excep */
|
|
|
|
$excep = $result->getException();
|
|
|
|
'@phan-var AFPUserVisibleException $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' => [
|
2011-08-26 20:12:34 +00:00
|
|
|
ApiBase::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
|
|
|
}
|