Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 107 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
QueryAbuseFilters | |
0.00% |
0 / 107 |
|
0.00% |
0 / 2 |
1560 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 105 |
|
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 | |
19 | namespace MediaWiki\Extension\AbuseFilter\Api; |
20 | |
21 | use ApiBase; |
22 | use ApiQuery; |
23 | use ApiQueryBase; |
24 | use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager; |
25 | use MediaWiki\Extension\AbuseFilter\Filter\Flags; |
26 | use MediaWiki\Extension\AbuseFilter\FilterUtils; |
27 | use MediaWiki\Utils\MWTimestamp; |
28 | use Wikimedia\ParamValidator\ParamValidator; |
29 | use 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 | */ |
40 | class QueryAbuseFilters extends ApiQueryBase { |
41 | |
42 | /** @var AbuseFilterPermissionManager */ |
43 | private $afPermManager; |
44 | |
45 | /** |
46 | * @param ApiQuery $query |
47 | * @param string $moduleName |
48 | * @param AbuseFilterPermissionManager $afPermManager |
49 | */ |
50 | public function __construct( |
51 | ApiQuery $query, |
52 | $moduleName, |
53 | AbuseFilterPermissionManager $afPermManager |
54 | ) { |
55 | parent::__construct( $query, $moduleName, 'abf' ); |
56 | $this->afPermManager = $afPermManager; |
57 | } |
58 | |
59 | /** |
60 | * @inheritDoc |
61 | */ |
62 | public function execute() { |
63 | $this->checkUserRightsAny( 'abusefilter-view' ); |
64 | |
65 | $params = $this->extractRequestParams(); |
66 | |
67 | $prop = array_fill_keys( $params['prop'], true ); |
68 | $fld_id = isset( $prop['id'] ); |
69 | $fld_desc = isset( $prop['description'] ); |
70 | $fld_pattern = isset( $prop['pattern'] ); |
71 | $fld_actions = isset( $prop['actions'] ); |
72 | $fld_hits = isset( $prop['hits'] ); |
73 | $fld_comments = isset( $prop['comments'] ); |
74 | $fld_user = isset( $prop['lasteditor'] ); |
75 | $fld_time = isset( $prop['lastedittime'] ); |
76 | $fld_status = isset( $prop['status'] ); |
77 | $fld_private = isset( $prop['private'] ); |
78 | $fld_protected = isset( $prop['protected'] ); |
79 | |
80 | $result = $this->getResult(); |
81 | |
82 | $this->addTables( 'abuse_filter' ); |
83 | |
84 | $this->addFields( 'af_id' ); |
85 | $this->addFields( 'af_hidden' ); |
86 | $this->addFieldsIf( 'af_hit_count', $fld_hits ); |
87 | $this->addFieldsIf( 'af_enabled', $fld_status ); |
88 | $this->addFieldsIf( 'af_deleted', $fld_status ); |
89 | $this->addFieldsIf( 'af_public_comments', $fld_desc ); |
90 | $this->addFieldsIf( 'af_pattern', $fld_pattern ); |
91 | $this->addFieldsIf( 'af_actions', $fld_actions ); |
92 | $this->addFieldsIf( 'af_comments', $fld_comments ); |
93 | if ( $fld_user ) { |
94 | $this->addTables( 'actor' ); |
95 | $this->addFields( [ 'af_user_text' => 'actor_name' ] ); |
96 | $this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id = af_actor' ] ] ); |
97 | } |
98 | $this->addFieldsIf( 'af_timestamp', $fld_time ); |
99 | |
100 | $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
101 | |
102 | $this->addWhereRange( 'af_id', $params['dir'], $params['startid'], $params['endid'] ); |
103 | |
104 | if ( $params['show'] !== null ) { |
105 | $show = array_fill_keys( $params['show'], true ); |
106 | |
107 | /* Check for conflicting parameters. */ |
108 | if ( ( isset( $show['enabled'] ) && isset( $show['!enabled'] ) ) |
109 | || ( isset( $show['deleted'] ) && isset( $show['!deleted'] ) ) |
110 | || ( isset( $show['private'] ) && isset( $show['!private'] ) ) |
111 | ) { |
112 | $this->dieWithError( 'apierror-show' ); |
113 | } |
114 | |
115 | $dbr = $this->getDb(); |
116 | $this->addWhereIf( $dbr->expr( 'af_enabled', '=', 0 ), isset( $show['!enabled'] ) ); |
117 | $this->addWhereIf( $dbr->expr( 'af_enabled', '!=', 0 ), isset( $show['enabled'] ) ); |
118 | $this->addWhereIf( $dbr->expr( 'af_deleted', '=', 0 ), isset( $show['!deleted'] ) ); |
119 | $this->addWhereIf( $dbr->expr( 'af_deleted', '!=', 0 ), isset( $show['deleted'] ) ); |
120 | $this->addWhereIf( |
121 | $dbr->bitAnd( 'af_hidden', Flags::FILTER_HIDDEN ) . ' = 0', |
122 | isset( $show['!private'] ) |
123 | ); |
124 | $this->addWhereIf( |
125 | $dbr->bitAnd( 'af_hidden', Flags::FILTER_HIDDEN ) . ' != 0', |
126 | isset( $show['private'] ) |
127 | ); |
128 | $this->addWhereIf( |
129 | $dbr->bitAnd( 'af_hidden', Flags::FILTER_USES_PROTECTED_VARS ) . ' != 0', |
130 | isset( $show['!protected'] ) |
131 | ); |
132 | $this->addWhereIf( |
133 | $dbr->bitAnd( 'af_hidden', Flags::FILTER_USES_PROTECTED_VARS ) . ' = 0', |
134 | isset( $show['!protected'] ) |
135 | ); |
136 | } |
137 | |
138 | $res = $this->select( __METHOD__ ); |
139 | |
140 | $showhidden = $this->afPermManager->canViewPrivateFilters( $this->getAuthority() ); |
141 | $showProtected = $this->afPermManager->canViewProtectedVariables( $this->getAuthority() ); |
142 | |
143 | $count = 0; |
144 | foreach ( $res as $row ) { |
145 | $filterId = intval( $row->af_id ); |
146 | if ( ++$count > $params['limit'] ) { |
147 | // We've had enough |
148 | $this->setContinueEnumParameter( 'startid', $filterId ); |
149 | break; |
150 | } |
151 | $entry = []; |
152 | if ( $fld_id ) { |
153 | $entry['id'] = $filterId; |
154 | } |
155 | if ( $fld_desc ) { |
156 | $entry['description'] = $row->af_public_comments; |
157 | } |
158 | if ( |
159 | $fld_pattern && |
160 | ( !FilterUtils::isHidden( $row->af_hidden ) || $showhidden ) && |
161 | ( !FilterUtils::isProtected( $row->af_hidden ) || $showProtected ) |
162 | ) { |
163 | $entry['pattern'] = $row->af_pattern; |
164 | } |
165 | if ( $fld_actions ) { |
166 | $entry['actions'] = $row->af_actions; |
167 | } |
168 | if ( $fld_hits ) { |
169 | $entry['hits'] = intval( $row->af_hit_count ); |
170 | } |
171 | if ( |
172 | $fld_comments && |
173 | ( !FilterUtils::isHidden( $row->af_hidden ) || $showhidden ) && |
174 | ( !FilterUtils::isProtected( $row->af_hidden ) || $showProtected ) |
175 | ) { |
176 | $entry['comments'] = $row->af_comments; |
177 | } |
178 | if ( $fld_user ) { |
179 | $entry['lasteditor'] = $row->af_user_text; |
180 | } |
181 | if ( $fld_time ) { |
182 | $ts = new MWTimestamp( $row->af_timestamp ); |
183 | $entry['lastedittime'] = $ts->getTimestamp( TS_ISO_8601 ); |
184 | } |
185 | if ( $fld_private && FilterUtils::isHidden( $row->af_hidden ) ) { |
186 | $entry['private'] = ''; |
187 | } |
188 | if ( $fld_protected && FilterUtils::isProtected( $row->af_hidden ) ) { |
189 | $entry['protected'] = ''; |
190 | } |
191 | if ( $fld_status ) { |
192 | if ( $row->af_enabled ) { |
193 | $entry['enabled'] = ''; |
194 | } |
195 | if ( $row->af_deleted ) { |
196 | $entry['deleted'] = ''; |
197 | } |
198 | } |
199 | if ( $entry ) { |
200 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry ); |
201 | if ( !$fit ) { |
202 | $this->setContinueEnumParameter( 'startid', $filterId ); |
203 | break; |
204 | } |
205 | } |
206 | } |
207 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'filter' ); |
208 | } |
209 | |
210 | /** |
211 | * @codeCoverageIgnore Merely declarative |
212 | * @inheritDoc |
213 | */ |
214 | public function getAllowedParams() { |
215 | return [ |
216 | 'startid' => [ |
217 | ParamValidator::PARAM_TYPE => 'integer' |
218 | ], |
219 | 'endid' => [ |
220 | ParamValidator::PARAM_TYPE => 'integer', |
221 | ], |
222 | 'dir' => [ |
223 | ParamValidator::PARAM_TYPE => [ |
224 | 'older', |
225 | 'newer' |
226 | ], |
227 | ParamValidator::PARAM_DEFAULT => 'newer', |
228 | ApiBase::PARAM_HELP_MSG => 'api-help-param-direction', |
229 | ], |
230 | 'show' => [ |
231 | ParamValidator::PARAM_ISMULTI => true, |
232 | ParamValidator::PARAM_TYPE => [ |
233 | 'enabled', |
234 | '!enabled', |
235 | 'deleted', |
236 | '!deleted', |
237 | 'private', |
238 | '!private', |
239 | 'protected', |
240 | '!protected', |
241 | ], |
242 | ], |
243 | 'limit' => [ |
244 | ParamValidator::PARAM_DEFAULT => 10, |
245 | ParamValidator::PARAM_TYPE => 'limit', |
246 | IntegerDef::PARAM_MIN => 1, |
247 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
248 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
249 | ], |
250 | 'prop' => [ |
251 | ParamValidator::PARAM_DEFAULT => 'id|description|actions|status', |
252 | ParamValidator::PARAM_TYPE => [ |
253 | 'id', |
254 | 'description', |
255 | 'pattern', |
256 | 'actions', |
257 | 'hits', |
258 | 'comments', |
259 | 'lasteditor', |
260 | 'lastedittime', |
261 | 'status', |
262 | 'private', |
263 | 'protected', |
264 | ], |
265 | ParamValidator::PARAM_ISMULTI => true |
266 | ] |
267 | ]; |
268 | } |
269 | |
270 | /** |
271 | * @codeCoverageIgnore Merely declarative |
272 | * @inheritDoc |
273 | */ |
274 | protected function getExamplesMessages() { |
275 | return [ |
276 | 'action=query&list=abusefilters&abfshow=enabled|!private' |
277 | => 'apihelp-query+abusefilters-example-1', |
278 | 'action=query&list=abusefilters&abfprop=id|description|pattern' |
279 | => 'apihelp-query+abusefilters-example-2', |
280 | ]; |
281 | } |
282 | } |