| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiMain; |
| 7 |
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager; |
| 8 |
use MediaWiki\Extension\AbuseFilter\Parser\Exception\UserVisibleException; |
| 9 |
use MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerFactory; |
| 10 |
use Wikimedia\ParamValidator\ParamValidator; |
| 11 |
|
| 12 |
class CheckSyntax extends ApiBase { |
| 13 |
|
| 14 |
|
| 15 |
private $ruleCheckerFactory; |
| 16 |
|
| 17 |
|
| 18 |
private $afPermManager; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct( |
| 27 |
ApiMain $main, |
| 28 |
$action, |
| 29 |
RuleCheckerFactory $ruleCheckerFactory, |
| 30 |
AbuseFilterPermissionManager $afPermManager |
| 31 |
) { |
| 32 |
parent::__construct( $main, $action ); |
| 33 |
$this->ruleCheckerFactory = $ruleCheckerFactory; |
| 34 |
$this->afPermManager = $afPermManager; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function execute() { |
| 41 |
|
| 42 |
if ( !$this->afPermManager->canUseTestTools( $this->getAuthority() ) |
| 43 |
&& !$this->afPermManager->canEdit( $this->getAuthority() ) |
| 44 |
) { |
| 45 |
$this->dieWithError( 'apierror-abusefilter-cantcheck', 'permissiondenied' ); |
| 46 |
} |
| 47 |
|
| 48 |
$params = $this->extractRequestParams(); |
| 49 |
$result = $this->ruleCheckerFactory->newRuleChecker()->checkSyntax( $params['filter'] ); |
| 50 |
|
| 51 |
$r = []; |
| 52 |
$warnings = []; |
| 53 |
foreach ( $result->getWarnings() as $warning ) { |
| 54 |
$warnings[] = [ |
| 55 |
'message' => $this->msg( $warning->getMessageObj() )->text(), |
| 56 |
'character' => $warning->getPosition() |
| 57 |
]; |
| 58 |
} |
| 59 |
if ( $warnings ) { |
| 60 |
$r['warnings'] = $warnings; |
| 61 |
} |
| 62 |
|
| 63 |
if ( $result->isValid() ) { |
| 64 |
|
| 65 |
$r['status'] = 'ok'; |
| 66 |
} else { |
| 67 |
|
| 68 |
|
| 69 |
$excep = $result->getException(); |
| 70 |
'@phan-var UserVisibleException $excep'; |
| 71 |
$r = [ |
| 72 |
'status' => 'error', |
| 73 |
'message' => $this->msg( $excep->getMessageObj() )->text(), |
| 74 |
'character' => $excep->getPosition(), |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
$this->getResult()->addValue( null, $this->getModuleName(), $r ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function getAllowedParams() { |
| 86 |
return [ |
| 87 |
'filter' => [ |
| 88 |
ParamValidator::PARAM_REQUIRED => true, |
| 89 |
], |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
protected function getExamplesMessages() { |
| 98 |
return [ |
| 99 |
'action=abusefilterchecksyntax&filter="foo"' |
| 100 |
=> 'apihelp-abusefilterchecksyntax-example-1', |
| 101 |
'action=abusefilterchecksyntax&filter="bar"%20bad_variable' |
| 102 |
=> 'apihelp-abusefilterchecksyntax-example-2', |
| 103 |
]; |
| 104 |
} |
| 105 |
} |