Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 85
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 / 85
0.00% covered (danger)
0.00%
0 / 2
1122
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 / 83
0.00% covered (danger)
0.00%
0 / 1
930
 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 ApiBase;
22use ApiQuery;
23use ApiQueryBase;
24use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
25use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
26use MediaWiki\Utils\MWTimestamp;
27use Wikimedia\ParamValidator\ParamValidator;
28use Wikimedia\ParamValidator\TypeDef\IntegerDef;
29
30/**
31 * Query module to list abuse filter details.
32 *
33 * @copyright 2009 Alex Z. <mrzmanwiki AT gmail DOT com>
34 * Based mostly on code by Bryan Tong Minh and Roan Kattouw
35 *
36 * @ingroup API
37 * @ingroup Extensions
38 */
39class QueryAbuseFilters extends ApiQueryBase {
40
41    /** @var AbuseFilterPermissionManager */
42    private $afPermManager;
43
44    /**
45     * @param ApiQuery $query
46     * @param string $moduleName
47     * @param AbuseFilterPermissionManager $afPermManager
48     */
49    public function __construct(
50        ApiQuery $query,
51        $moduleName,
52        AbuseFilterPermissionManager $afPermManager
53    ) {
54        parent::__construct( $query, $moduleName, 'abf' );
55        $this->afPermManager = $afPermManager;
56    }
57
58    /**
59     * @inheritDoc
60     */
61    public function execute() {
62        $this->checkUserRightsAny( 'abusefilter-view' );
63
64        $params = $this->extractRequestParams();
65
66        $prop = array_fill_keys( $params['prop'], true );
67        $fld_id = isset( $prop['id'] );
68        $fld_desc = isset( $prop['description'] );
69        $fld_pattern = isset( $prop['pattern'] );
70        $fld_actions = isset( $prop['actions'] );
71        $fld_hits = isset( $prop['hits'] );
72        $fld_comments = isset( $prop['comments'] );
73        $fld_user = isset( $prop['lasteditor'] );
74        $fld_time = isset( $prop['lastedittime'] );
75        $fld_status = isset( $prop['status'] );
76        $fld_private = isset( $prop['private'] );
77
78        $result = $this->getResult();
79
80        $this->addTables( 'abuse_filter' );
81
82        $this->addFields( 'af_id' );
83        $this->addFields( 'af_hidden' );
84        $this->addFieldsIf( 'af_hit_count', $fld_hits );
85        $this->addFieldsIf( 'af_enabled', $fld_status );
86        $this->addFieldsIf( 'af_deleted', $fld_status );
87        $this->addFieldsIf( 'af_public_comments', $fld_desc );
88        $this->addFieldsIf( 'af_pattern', $fld_pattern );
89        $this->addFieldsIf( 'af_actions', $fld_actions );
90        $this->addFieldsIf( 'af_comments', $fld_comments );
91        if ( $fld_user ) {
92            $actorQuery = AbuseFilterServices::getActorMigration()->getJoin( 'af_user' );
93            $this->addTables( $actorQuery['tables'] );
94            $this->addFields( [ 'af_user_text' => $actorQuery['fields']['af_user_text'] ] );
95            $this->addJoinConds( $actorQuery['joins'] );
96        }
97        $this->addFieldsIf( 'af_timestamp', $fld_time );
98
99        $this->addOption( 'LIMIT', $params['limit'] + 1 );
100
101        $this->addWhereRange( 'af_id', $params['dir'], $params['startid'], $params['endid'] );
102
103        if ( $params['show'] !== null ) {
104            $show = array_fill_keys( $params['show'], true );
105
106            /* Check for conflicting parameters. */
107            if ( ( isset( $show['enabled'] ) && isset( $show['!enabled'] ) )
108                || ( isset( $show['deleted'] ) && isset( $show['!deleted'] ) )
109                || ( isset( $show['private'] ) && isset( $show['!private'] ) )
110            ) {
111                $this->dieWithError( 'apierror-show' );
112            }
113
114            $this->addWhereIf( 'af_enabled = 0', isset( $show['!enabled'] ) );
115            $this->addWhereIf( 'af_enabled != 0', isset( $show['enabled'] ) );
116            $this->addWhereIf( 'af_deleted = 0', isset( $show['!deleted'] ) );
117            $this->addWhereIf( 'af_deleted != 0', isset( $show['deleted'] ) );
118            $this->addWhereIf( 'af_hidden = 0', isset( $show['!private'] ) );
119            $this->addWhereIf( 'af_hidden != 0', isset( $show['private'] ) );
120        }
121
122        $res = $this->select( __METHOD__ );
123
124        $showhidden = $this->afPermManager->canViewPrivateFilters( $this->getAuthority() );
125
126        $count = 0;
127        foreach ( $res as $row ) {
128            $filterId = intval( $row->af_id );
129            if ( ++$count > $params['limit'] ) {
130                // We've had enough
131                $this->setContinueEnumParameter( 'startid', $filterId );
132                break;
133            }
134            $entry = [];
135            if ( $fld_id ) {
136                $entry['id'] = $filterId;
137            }
138            if ( $fld_desc ) {
139                $entry['description'] = $row->af_public_comments;
140            }
141            if ( $fld_pattern && ( !$row->af_hidden || $showhidden ) ) {
142                $entry['pattern'] = $row->af_pattern;
143            }
144            if ( $fld_actions ) {
145                $entry['actions'] = $row->af_actions;
146            }
147            if ( $fld_hits ) {
148                $entry['hits'] = intval( $row->af_hit_count );
149            }
150            if ( $fld_comments && ( !$row->af_hidden || $showhidden ) ) {
151                $entry['comments'] = $row->af_comments;
152            }
153            if ( $fld_user ) {
154                $entry['lasteditor'] = $row->af_user_text;
155            }
156            if ( $fld_time ) {
157                $ts = new MWTimestamp( $row->af_timestamp );
158                $entry['lastedittime'] = $ts->getTimestamp( TS_ISO_8601 );
159            }
160            if ( $fld_private && $row->af_hidden ) {
161                $entry['private'] = '';
162            }
163            if ( $fld_status ) {
164                if ( $row->af_enabled ) {
165                    $entry['enabled'] = '';
166                }
167                if ( $row->af_deleted ) {
168                    $entry['deleted'] = '';
169                }
170            }
171            if ( $entry ) {
172                $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
173                if ( !$fit ) {
174                    $this->setContinueEnumParameter( 'startid', $filterId );
175                    break;
176                }
177            }
178        }
179        $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'filter' );
180    }
181
182    /**
183     * @codeCoverageIgnore Merely declarative
184     * @inheritDoc
185     */
186    public function getAllowedParams() {
187        return [
188            'startid' => [
189                ParamValidator::PARAM_TYPE => 'integer'
190            ],
191            'endid' => [
192                ParamValidator::PARAM_TYPE => 'integer',
193            ],
194            'dir' => [
195                ParamValidator::PARAM_TYPE => [
196                    'older',
197                    'newer'
198                ],
199                ParamValidator::PARAM_DEFAULT => 'newer',
200                ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
201            ],
202            'show' => [
203                ParamValidator::PARAM_ISMULTI => true,
204                ParamValidator::PARAM_TYPE => [
205                    'enabled',
206                    '!enabled',
207                    'deleted',
208                    '!deleted',
209                    'private',
210                    '!private',
211                ],
212            ],
213            'limit' => [
214                ParamValidator::PARAM_DEFAULT => 10,
215                ParamValidator::PARAM_TYPE => 'limit',
216                IntegerDef::PARAM_MIN => 1,
217                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
218                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
219            ],
220            'prop' => [
221                ParamValidator::PARAM_DEFAULT => 'id|description|actions|status',
222                ParamValidator::PARAM_TYPE => [
223                    'id',
224                    'description',
225                    'pattern',
226                    'actions',
227                    'hits',
228                    'comments',
229                    'lasteditor',
230                    'lastedittime',
231                    'status',
232                    'private',
233                ],
234                ParamValidator::PARAM_ISMULTI => true
235            ]
236        ];
237    }
238
239    /**
240     * @codeCoverageIgnore Merely declarative
241     * @inheritDoc
242     */
243    protected function getExamplesMessages() {
244        return [
245            'action=query&list=abusefilters&abfshow=enabled|!private'
246                => 'apihelp-query+abusefilters-example-1',
247            'action=query&list=abusefilters&abfprop=id|description|pattern'
248                => 'apihelp-query+abusefilters-example-2',
249        ];
250    }
251}