Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.29% covered (warning)
89.29%
25 / 28
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckSyntax
89.29% covered (warning)
89.29%
25 / 28
50.00% covered (danger)
50.00%
1 / 2
10.12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
88.89% covered (warning)
88.89%
24 / 27
0.00% covered (danger)
0.00%
0 / 1
7.07
 getAllowedParams
n/a
0 / 0
n/a
0 / 0
1
 getExamplesMessages
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Api;
4
5use MediaWiki\Api\ApiBase;
6use MediaWiki\Api\ApiMain;
7use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
8use MediaWiki\Extension\AbuseFilter\Parser\Exception\UserVisibleException;
9use MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerFactory;
10use Wikimedia\ParamValidator\ParamValidator;
11
12class CheckSyntax extends ApiBase {
13
14    public function __construct(
15        ApiMain $main,
16        string $action,
17        private readonly RuleCheckerFactory $ruleCheckerFactory,
18        private readonly AbuseFilterPermissionManager $afPermManager
19    ) {
20        parent::__construct( $main, $action );
21    }
22
23    /**
24     * @inheritDoc
25     */
26    public function execute() {
27        // "Anti-DoS"
28        if ( !$this->afPermManager->canUseTestTools( $this->getAuthority() )
29            && !$this->afPermManager->canEdit( $this->getAuthority() )
30        ) {
31            $this->dieWithError( 'apierror-abusefilter-cantcheck', 'permissiondenied' );
32        }
33
34        $params = $this->extractRequestParams();
35        $result = $this->ruleCheckerFactory->newRuleChecker()->checkSyntax( $params['filter'] );
36
37        $r = [];
38        $warnings = [];
39        foreach ( $result->getWarnings() as $warning ) {
40            $warnings[] = [
41                'message' => $this->msg( $warning->getMessageObj() )->text(),
42                'character' => $warning->getPosition()
43            ];
44        }
45        if ( $warnings ) {
46            $r['warnings'] = $warnings;
47        }
48
49        if ( $result->isValid() ) {
50            // Everything went better than expected :)
51            $r['status'] = 'ok';
52        } else {
53            $excep = $result->getException();
54            if ( !( $excep instanceof UserVisibleException ) ) {
55                throw new \UnexpectedValueException(
56                    'Non-user-visible exception returned from syntax check'
57                );
58            }
59            $r = [
60                'status' => 'error',
61                'message' => $this->msg( $excep->getMessageObj() )->text(),
62                'character' => $excep->getPosition(),
63            ];
64        }
65
66        $this->getResult()->addValue( null, $this->getModuleName(), $r );
67    }
68
69    /**
70     * @codeCoverageIgnore Merely declarative
71     * @inheritDoc
72     */
73    public function getAllowedParams() {
74        return [
75            'filter' => [
76                ParamValidator::PARAM_REQUIRED => true,
77            ],
78        ];
79    }
80
81    /**
82     * @codeCoverageIgnore Merely declarative
83     * @inheritDoc
84     */
85    protected function getExamplesMessages() {
86        return [
87            'action=abusefilterchecksyntax&filter="foo"'
88                => 'apihelp-abusefilterchecksyntax-example-1',
89            'action=abusefilterchecksyntax&filter="bar"%20bad_variable'
90                => 'apihelp-abusefilterchecksyntax-example-2',
91        ];
92    }
93}