Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
26 / 26 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
CheckSyntax | |
100.00% |
26 / 26 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
6 | |||
getAllowedParams | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
getExamplesMessages | n/a |
0 / 0 |
n/a |
0 / 0 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\AbuseFilter\Api; |
4 | |
5 | use ApiBase; |
6 | use 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 | /** @var RuleCheckerFactory */ |
15 | private $ruleCheckerFactory; |
16 | |
17 | /** @var AbuseFilterPermissionManager */ |
18 | private $afPermManager; |
19 | |
20 | /** |
21 | * @param ApiMain $main |
22 | * @param string $action |
23 | * @param RuleCheckerFactory $ruleCheckerFactory |
24 | * @param AbuseFilterPermissionManager $afPermManager |
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 | * @inheritDoc |
39 | */ |
40 | public function execute() { |
41 | // "Anti-DoS" |
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 | // Everything went better than expected :) |
65 | $r['status'] = 'ok'; |
66 | } else { |
67 | // TODO: Improve the type here. |
68 | /** @var UserVisibleException $excep */ |
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 | * @codeCoverageIgnore Merely declarative |
83 | * @inheritDoc |
84 | */ |
85 | public function getAllowedParams() { |
86 | return [ |
87 | 'filter' => [ |
88 | ParamValidator::PARAM_REQUIRED => true, |
89 | ], |
90 | ]; |
91 | } |
92 | |
93 | /** |
94 | * @codeCoverageIgnore Merely declarative |
95 | * @inheritDoc |
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 | } |