Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 107
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryAbuseFilters
0.00% covered (danger)
0.00%
0 / 107
0.00% covered (danger)
0.00%
0 / 2
1560
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 105
0.00% covered (danger)
0.00%
0 / 1
1332
 getAllowedParams
n/a
0 / 0
n/a
0 / 0
1
 getExamplesMessages
n/a
0 / 0
n/a
0 / 0
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19namespace MediaWiki\Extension\AbuseFilter\Api;
20
21use MediaWiki\Api\ApiBase;
22use MediaWiki\Api\ApiQuery;
23use MediaWiki\Api\ApiQueryBase;
24use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
25use MediaWiki\Extension\AbuseFilter\Filter\Flags;
26use MediaWiki\Extension\AbuseFilter\FilterUtils;
27use MediaWiki\Utils\MWTimestamp;
28use Wikimedia\ParamValidator\ParamValidator;
29use Wikimedia\ParamValidator\TypeDef\IntegerDef;
30
31/**
32 * Query module to list abuse filter details.
33 *
34 * @copyright 2009 Alex Z. <mrzmanwiki AT gmail DOT com>
35 * Based mostly on code by Bryan Tong Minh and Roan Kattouw
36 *
37 * @ingroup API
38 * @ingroup Extensions
39 */
40class QueryAbuseFilters extends ApiQueryBase {
41
42    private AbuseFilterPermissionManager $afPermManager;
43
44    public function __construct(
45        ApiQuery $query,
46        string $moduleName,
47        AbuseFilterPermissionManager $afPermManager
48    ) {
49        parent::__construct( $query, $moduleName, 'abf' );
50        $this->afPermManager = $afPermManager;
51    }
52
53    /**
54     * @inheritDoc
55     */
56    public function execute() {
57        $this->checkUserRightsAny( 'abusefilter-view' );
58
59        $params = $this->extractRequestParams();
60
61        $prop = array_fill_keys( $params['prop'], true );
62        $fld_id = isset( $prop['id'] );
63        $fld_desc = isset( $prop['description'] );
64        $fld_pattern = isset( $prop['pattern'] );
65        $fld_actions = isset( $prop['actions'] );
66        $fld_hits = isset( $prop['hits'] );
67        $fld_comments = isset( $prop['comments'] );
68        $fld_user = isset( $prop['lasteditor'] );
69        $fld_time = isset( $prop['lastedittime'] );
70        $fld_status = isset( $prop['status'] );
71        $fld_private = isset( $prop['private'] );
72        $fld_protected = isset( $prop['protected'] );
73
74        $result = $this->getResult();
75
76        $this->addTables( 'abuse_filter' );
77
78        $this->addFields( 'af_id' );
79        $this->addFields( 'af_hidden' );
80        $this->addFieldsIf( 'af_hit_count', $fld_hits );
81        $this->addFieldsIf( 'af_enabled', $fld_status );
82        $this->addFieldsIf( 'af_deleted', $fld_status );
83        $this->addFieldsIf( 'af_public_comments', $fld_desc );
84        $this->addFieldsIf( 'af_pattern', $fld_pattern );
85        $this->addFieldsIf( 'af_actions', $fld_actions );
86        $this->addFieldsIf( 'af_comments', $fld_comments );
87        if ( $fld_user ) {
88            $this->addTables( 'actor' );
89            $this->addFields( [ 'af_user_text' => 'actor_name' ] );
90            $this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id = af_actor' ] ] );
91        }
92        $this->addFieldsIf( 'af_timestamp', $fld_time );
93
94        $this->addOption( 'LIMIT', $params['limit'] + 1 );
95
96        $this->addWhereRange( 'af_id', $params['dir'], $params['startid'], $params['endid'] );
97
98        if ( $params['show'] !== null ) {
99            $show = array_fill_keys( $params['show'], true );
100
101            /* Check for conflicting parameters. */
102            if ( ( isset( $show['enabled'] ) && isset( $show['!enabled'] ) )
103                || ( isset( $show['deleted'] ) && isset( $show['!deleted'] ) )
104                || ( isset( $show['private'] ) && isset( $show['!private'] ) )
105            ) {
106                $this->dieWithError( 'apierror-show' );
107            }
108
109            $dbr = $this->getDb();
110            $this->addWhereIf( $dbr->expr( 'af_enabled', '=', 0 ), isset( $show['!enabled'] ) );
111            $this->addWhereIf( $dbr->expr( 'af_enabled', '!=', 0 ), isset( $show['enabled'] ) );
112            $this->addWhereIf( $dbr->expr( 'af_deleted', '=', 0 ), isset( $show['!deleted'] ) );
113            $this->addWhereIf( $dbr->expr( 'af_deleted', '!=', 0 ), isset( $show['deleted'] ) );
114            $this->addWhereIf(
115                $dbr->bitAnd( 'af_hidden', Flags::FILTER_HIDDEN ) . ' = 0',
116                isset( $show['!private'] )
117            );
118            $this->addWhereIf(
119                $dbr->bitAnd( 'af_hidden', Flags::FILTER_HIDDEN ) . ' != 0',
120                isset( $show['private'] )
121            );
122            $this->addWhereIf(
123                $dbr->bitAnd( 'af_hidden', Flags::FILTER_USES_PROTECTED_VARS ) . ' != 0',
124                isset( $show['!protected'] )
125            );
126            $this->addWhereIf(
127                $dbr->bitAnd( 'af_hidden', Flags::FILTER_USES_PROTECTED_VARS ) . ' = 0',
128                isset( $show['!protected'] )
129            );
130        }
131
132        $res = $this->select( __METHOD__ );
133
134        $showhidden = $this->afPermManager->canViewPrivateFilters( $this->getAuthority() );
135        $showProtected = $this->afPermManager->canViewProtectedVariables( $this->getAuthority() );
136
137        $count = 0;
138        foreach ( $res as $row ) {
139            $filterId = intval( $row->af_id );
140            if ( ++$count > $params['limit'] ) {
141                // We've had enough
142                $this->setContinueEnumParameter( 'startid', $filterId );
143                break;
144            }
145            $entry = [];
146            if ( $fld_id ) {
147                $entry['id'] = $filterId;
148            }
149            if ( $fld_desc ) {
150                $entry['description'] = $row->af_public_comments;
151            }
152            if (
153                $fld_pattern &&
154                ( !FilterUtils::isHidden( $row->af_hidden ) || $showhidden ) &&
155                ( !FilterUtils::isProtected( $row->af_hidden ) || $showProtected )
156            ) {
157                $entry['pattern'] = $row->af_pattern;
158            }
159            if ( $fld_actions ) {
160                $entry['actions'] = $row->af_actions;
161            }
162            if ( $fld_hits ) {
163                $entry['hits'] = intval( $row->af_hit_count );
164            }
165            if (
166                $fld_comments &&
167                ( !FilterUtils::isHidden( $row->af_hidden ) || $showhidden ) &&
168                ( !FilterUtils::isProtected( $row->af_hidden ) || $showProtected )
169            ) {
170                $entry['comments'] = $row->af_comments;
171            }
172            if ( $fld_user ) {
173                $entry['lasteditor'] = $row->af_user_text;
174            }
175            if ( $fld_time ) {
176                $ts = new MWTimestamp( $row->af_timestamp );
177                $entry['lastedittime'] = $ts->getTimestamp( TS_ISO_8601 );
178            }
179            if ( $fld_private && FilterUtils::isHidden( $row->af_hidden ) ) {
180                $entry['private'] = '';
181            }
182            if ( $fld_protected && FilterUtils::isProtected( $row->af_hidden ) ) {
183                $entry['protected'] = '';
184            }
185            if ( $fld_status ) {
186                if ( $row->af_enabled ) {
187                    $entry['enabled'] = '';
188                }
189                if ( $row->af_deleted ) {
190                    $entry['deleted'] = '';
191                }
192            }
193            if ( $entry ) {
194                $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
195                if ( !$fit ) {
196                    $this->setContinueEnumParameter( 'startid', $filterId );
197                    break;
198                }
199            }
200        }
201        $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'filter' );
202    }
203
204    /**
205     * @codeCoverageIgnore Merely declarative
206     * @inheritDoc
207     */
208    public function getAllowedParams() {
209        return [
210            'startid' => [
211                ParamValidator::PARAM_TYPE => 'integer'
212            ],
213            'endid' => [
214                ParamValidator::PARAM_TYPE => 'integer',
215            ],
216            'dir' => [
217                ParamValidator::PARAM_TYPE => [
218                    'older',
219                    'newer'
220                ],
221                ParamValidator::PARAM_DEFAULT => 'newer',
222                ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
223            ],
224            'show' => [
225                ParamValidator::PARAM_ISMULTI => true,
226                ParamValidator::PARAM_TYPE => [
227                    'enabled',
228                    '!enabled',
229                    'deleted',
230                    '!deleted',
231                    'private',
232                    '!private',
233                    'protected',
234                    '!protected',
235                ],
236            ],
237            'limit' => [
238                ParamValidator::PARAM_DEFAULT => 10,
239                ParamValidator::PARAM_TYPE => 'limit',
240                IntegerDef::PARAM_MIN => 1,
241                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
242                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
243            ],
244            'prop' => [
245                ParamValidator::PARAM_DEFAULT => 'id|description|actions|status',
246                ParamValidator::PARAM_TYPE => [
247                    'id',
248                    'description',
249                    'pattern',
250                    'actions',
251                    'hits',
252                    'comments',
253                    'lasteditor',
254                    'lastedittime',
255                    'status',
256                    'private',
257                    'protected',
258                ],
259                ParamValidator::PARAM_ISMULTI => true
260            ]
261        ];
262    }
263
264    /**
265     * @codeCoverageIgnore Merely declarative
266     * @inheritDoc
267     */
268    protected function getExamplesMessages() {
269        return [
270            'action=query&list=abusefilters&abfshow=enabled|!private'
271                => 'apihelp-query+abusefilters-example-1',
272            'action=query&list=abusefilters&abfprop=id|description|pattern'
273                => 'apihelp-query+abusefilters-example-2',
274        ];
275    }
276}