Debian Codegraph @

extensions/AbuseFilter/includes/Api/QueryAbuseFilters.php

raw symbols SCIP
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 MediaWiki\Api\ApiBase;
22 use MediaWiki\Api\ApiQuery;
23 use MediaWiki\Api\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 if ( $this->afPermManager->canSeeLogDetailsForFilter( $this->getAuthority(), $filter ) ) {
170 $entry['hits'] = intval( $row->af_hit_count );
171 }
172 }
173 if (
174 $fld_comments &&
175 ( !FilterUtils::isHidden( $row->af_hidden ) || $showhidden ) &&
176 ( !FilterUtils::isProtected( $row->af_hidden ) || $showProtected )
177 ) {
178 $entry['comments'] = $row->af_comments;
179 }
180 if ( $fld_user ) {
181 $entry['lasteditor'] = $row->af_user_text;
182 }
183 if ( $fld_time ) {
184 $ts = new MWTimestamp( $row->af_timestamp );
185 $entry['lastedittime'] = $ts->getTimestamp( TS_ISO_8601 );
186 }
187 if ( $fld_private && FilterUtils::isHidden( $row->af_hidden ) ) {
188 $entry['private'] = '';
189 }
190 if ( $fld_protected && FilterUtils::isProtected( $row->af_hidden ) ) {
191 $entry['protected'] = '';
192 }
193 if ( $fld_status ) {
194 if ( $row->af_enabled ) {
195 $entry['enabled'] = '';
196 }
197 if ( $row->af_deleted ) {
198 $entry['deleted'] = '';
199 }
200 }
201 if ( $entry ) {
202 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
203 if ( !$fit ) {
204 $this->setContinueEnumParameter( 'startid', $filterId );
205 break;
206 }
207 }
208 }
209 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'filter' );
210 }
211
212 /**
213 * @codeCoverageIgnore Merely declarative
214 * @inheritDoc
215 */
216 public function getAllowedParams() {
217 return [
218 'startid' => [
219 ParamValidator::PARAM_TYPE => 'integer'
220 ],
221 'endid' => [
222 ParamValidator::PARAM_TYPE => 'integer',
223 ],
224 'dir' => [
225 ParamValidator::PARAM_TYPE => [
226 'older',
227 'newer'
228 ],
229 ParamValidator::PARAM_DEFAULT => 'newer',
230 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
231 ],
232 'show' => [
233 ParamValidator::PARAM_ISMULTI => true,
234 ParamValidator::PARAM_TYPE => [
235 'enabled',
236 '!enabled',
237 'deleted',
238 '!deleted',
239 'private',
240 '!private',
241 'protected',
242 '!protected',
243 ],
244 ],
245 'limit' => [
246 ParamValidator::PARAM_DEFAULT => 10,
247 ParamValidator::PARAM_TYPE => 'limit',
248 IntegerDef::PARAM_MIN => 1,
249 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
250 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
251 ],
252 'prop' => [
253 ParamValidator::PARAM_DEFAULT => 'id|description|actions|status',
254 ParamValidator::PARAM_TYPE => [
255 'id',
256 'description',
257 'pattern',
258 'actions',
259 'hits',
260 'comments',
261 'lasteditor',
262 'lastedittime',
263 'status',
264 'private',
265 'protected',
266 ],
267 ParamValidator::PARAM_ISMULTI => true
268 ]
269 ];
270 }
271
272 /**
273 * @codeCoverageIgnore Merely declarative
274 * @inheritDoc
275 */
276 protected function getExamplesMessages() {
277 return [
278 'action=query&list=abusefilters&abfshow=enabled|!private'
279 => 'apihelp-query+abusefilters-example-1',
280 'action=query&list=abusefilters&abfprop=id|description|pattern'
281 => 'apihelp-query+abusefilters-example-2',
282 ];
283 }
284 }