2009-03-29 04:23:20 +00:00
|
|
|
<?php
|
2009-10-07 13:57:06 +00:00
|
|
|
/**
|
2009-03-29 04:23:20 +00:00
|
|
|
* Created on Mar 29, 2009
|
|
|
|
*
|
|
|
|
* AbuseFilter extension
|
|
|
|
*
|
2010-08-19 21:12:09 +00:00
|
|
|
* Copyright © 2008 Alex Z. mrzmanwiki AT gmail DOT com
|
2009-03-29 04:23:20 +00:00
|
|
|
* Based mostly on code by Bryan Tong Minh and Roan Kattouw
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:45:17 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-03-29 04:23:20 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
2020-12-03 22:05:42 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Api;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiQuery;
|
|
|
|
use ApiQueryBase;
|
2021-03-25 03:48:03 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
|
2020-12-03 22:05:42 +00:00
|
|
|
use MWTimestamp;
|
2022-04-03 23:23:52 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2022-04-03 20:13:20 +00:00
|
|
|
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
|
2020-09-18 14:49:13 +00:00
|
|
|
|
2009-03-29 04:23:20 +00:00
|
|
|
/**
|
|
|
|
* Query module to list abuse filter details.
|
|
|
|
*
|
|
|
|
* @ingroup API
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
2020-12-03 22:05:42 +00:00
|
|
|
class QueryAbuseFilters extends ApiQueryBase {
|
2021-03-25 03:48:03 +00:00
|
|
|
|
|
|
|
/** @var AbuseFilterPermissionManager */
|
|
|
|
private $afPermManager;
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @param ApiQuery $query
|
|
|
|
* @param string $moduleName
|
2021-03-25 03:48:03 +00:00
|
|
|
* @param AbuseFilterPermissionManager $afPermManager
|
2018-04-04 21:14:25 +00:00
|
|
|
*/
|
2021-03-25 03:48:03 +00:00
|
|
|
public function __construct(
|
|
|
|
ApiQuery $query,
|
|
|
|
$moduleName,
|
|
|
|
AbuseFilterPermissionManager $afPermManager
|
|
|
|
) {
|
2009-10-07 13:57:06 +00:00
|
|
|
parent::__construct( $query, $moduleName, 'abf' );
|
2021-03-25 03:48:03 +00:00
|
|
|
$this->afPermManager = $afPermManager;
|
2009-03-29 04:23:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
2020-12-03 08:49:56 +00:00
|
|
|
* @inheritDoc
|
2018-04-04 21:14:25 +00:00
|
|
|
*/
|
2009-03-29 04:23:20 +00:00
|
|
|
public function execute() {
|
2017-08-07 23:35:21 +00:00
|
|
|
$this->checkUserRightsAny( 'abusefilter-view' );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-29 04:23:20 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
2021-07-10 07:57:12 +00:00
|
|
|
$prop = array_fill_keys( $params['prop'], true );
|
2009-10-07 13:57:06 +00:00
|
|
|
$fld_id = isset( $prop['id'] );
|
|
|
|
$fld_desc = isset( $prop['description'] );
|
|
|
|
$fld_pattern = isset( $prop['pattern'] );
|
|
|
|
$fld_actions = isset( $prop['actions'] );
|
|
|
|
$fld_hits = isset( $prop['hits'] );
|
|
|
|
$fld_comments = isset( $prop['comments'] );
|
|
|
|
$fld_user = isset( $prop['lasteditor'] );
|
|
|
|
$fld_time = isset( $prop['lastedittime'] );
|
|
|
|
$fld_status = isset( $prop['status'] );
|
|
|
|
$fld_private = isset( $prop['private'] );
|
|
|
|
|
2009-03-29 04:23:20 +00:00
|
|
|
$result = $this->getResult();
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->addTables( 'abuse_filter' );
|
|
|
|
|
|
|
|
$this->addFields( 'af_id' );
|
|
|
|
$this->addFields( 'af_hidden' );
|
|
|
|
$this->addFieldsIf( 'af_hit_count', $fld_hits );
|
|
|
|
$this->addFieldsIf( 'af_enabled', $fld_status );
|
|
|
|
$this->addFieldsIf( 'af_deleted', $fld_status );
|
|
|
|
$this->addFieldsIf( 'af_public_comments', $fld_desc );
|
|
|
|
$this->addFieldsIf( 'af_pattern', $fld_pattern );
|
|
|
|
$this->addFieldsIf( 'af_actions', $fld_actions );
|
|
|
|
$this->addFieldsIf( 'af_comments', $fld_comments );
|
2013-06-26 22:58:36 +00:00
|
|
|
$this->addFieldsIf( 'af_user_text', $fld_user );
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->addFieldsIf( 'af_timestamp', $fld_time );
|
|
|
|
|
|
|
|
$this->addOption( 'LIMIT', $params['limit'] + 1 );
|
|
|
|
|
|
|
|
$this->addWhereRange( 'af_id', $params['dir'], $params['startid'], $params['endid'] );
|
|
|
|
|
2020-01-21 07:38:52 +00:00
|
|
|
if ( $params['show'] !== null ) {
|
2021-07-10 07:57:12 +00:00
|
|
|
$show = array_fill_keys( $params['show'], true );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-29 04:23:20 +00:00
|
|
|
/* Check for conflicting parameters. */
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( ( isset( $show['enabled'] ) && isset( $show['!enabled'] ) )
|
2016-11-03 19:16:55 +00:00
|
|
|
|| ( isset( $show['deleted'] ) && isset( $show['!deleted'] ) )
|
|
|
|
|| ( isset( $show['private'] ) && isset( $show['!private'] ) )
|
|
|
|
) {
|
2017-08-07 23:35:21 +00:00
|
|
|
$this->dieWithError( 'apierror-show' );
|
2009-03-29 04:23:20 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$this->addWhereIf( 'af_enabled = 0', isset( $show['!enabled'] ) );
|
|
|
|
$this->addWhereIf( 'af_enabled != 0', isset( $show['enabled'] ) );
|
|
|
|
$this->addWhereIf( 'af_deleted = 0', isset( $show['!deleted'] ) );
|
|
|
|
$this->addWhereIf( 'af_deleted != 0', isset( $show['deleted'] ) );
|
|
|
|
$this->addWhereIf( 'af_hidden = 0', isset( $show['!private'] ) );
|
|
|
|
$this->addWhereIf( 'af_hidden != 0', isset( $show['private'] ) );
|
2009-03-29 04:23:20 +00:00
|
|
|
}
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$res = $this->select( __METHOD__ );
|
|
|
|
|
2022-07-02 13:35:00 +00:00
|
|
|
$showhidden = $this->afPermManager->canViewPrivateFilters( $this->getAuthority() );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-29 04:23:20 +00:00
|
|
|
$count = 0;
|
2011-02-10 17:25:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
2019-12-01 07:58:42 +00:00
|
|
|
$filterId = intval( $row->af_id );
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( ++$count > $params['limit'] ) {
|
2009-03-29 04:23:20 +00:00
|
|
|
// We've had enough
|
2019-12-01 07:58:42 +00:00
|
|
|
$this->setContinueEnumParameter( 'startid', $filterId );
|
2009-03-29 04:23:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-06-15 14:23:16 +00:00
|
|
|
$entry = [];
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $fld_id ) {
|
2019-12-01 07:58:42 +00:00
|
|
|
$entry['id'] = $filterId;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_desc ) {
|
2009-03-29 04:23:20 +00:00
|
|
|
$entry['description'] = $row->af_public_comments;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_pattern && ( !$row->af_hidden || $showhidden ) ) {
|
2009-03-29 04:23:20 +00:00
|
|
|
$entry['pattern'] = $row->af_pattern;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_actions ) {
|
2009-03-29 04:23:20 +00:00
|
|
|
$entry['actions'] = $row->af_actions;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_hits ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$entry['hits'] = intval( $row->af_hit_count );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_comments && ( !$row->af_hidden || $showhidden ) ) {
|
2009-03-29 04:23:20 +00:00
|
|
|
$entry['comments'] = $row->af_comments;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_user ) {
|
2013-06-26 22:58:36 +00:00
|
|
|
$entry['lasteditor'] = $row->af_user_text;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_time ) {
|
2012-09-02 11:07:02 +00:00
|
|
|
$ts = new MWTimestamp( $row->af_timestamp );
|
|
|
|
$entry['lastedittime'] = $ts->getTimestamp( TS_ISO_8601 );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_private && $row->af_hidden ) {
|
2009-03-29 04:23:20 +00:00
|
|
|
$entry['private'] = '';
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $fld_status ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $row->af_enabled ) {
|
2009-03-29 04:23:20 +00:00
|
|
|
$entry['enabled'] = '';
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $row->af_deleted ) {
|
2009-03-29 04:23:20 +00:00
|
|
|
$entry['deleted'] = '';
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-03-29 04:23:20 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $entry ) {
|
2017-06-15 14:23:16 +00:00
|
|
|
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( !$fit ) {
|
2019-12-01 07:58:42 +00:00
|
|
|
$this->setContinueEnumParameter( 'startid', $filterId );
|
2009-03-30 12:19:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-03-29 04:23:20 +00:00
|
|
|
}
|
2017-06-15 14:23:16 +00:00
|
|
|
$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'filter' );
|
2009-03-29 04:23:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
2021-01-17 11:54:43 +00:00
|
|
|
* @codeCoverageIgnore Merely declarative
|
|
|
|
* @inheritDoc
|
2018-04-04 21:14:25 +00:00
|
|
|
*/
|
2009-03-29 04:23:20 +00:00
|
|
|
public function getAllowedParams() {
|
2017-06-15 14:23:16 +00:00
|
|
|
return [
|
|
|
|
'startid' => [
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer'
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
'endid' => [
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
'dir' => [
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_TYPE => [
|
2009-03-29 04:23:20 +00:00
|
|
|
'older',
|
|
|
|
'newer'
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => 'newer',
|
2016-09-20 18:39:11 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
'show' => [
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
|
|
|
ParamValidator::PARAM_TYPE => [
|
2009-03-29 04:23:20 +00:00
|
|
|
'enabled',
|
|
|
|
'!enabled',
|
|
|
|
'deleted',
|
|
|
|
'!deleted',
|
|
|
|
'private',
|
|
|
|
'!private',
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'limit' => [
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => 10,
|
|
|
|
ParamValidator::PARAM_TYPE => 'limit',
|
2022-04-03 20:13:20 +00:00
|
|
|
IntegerDef::PARAM_MIN => 1,
|
|
|
|
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
|
|
|
|
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
'prop' => [
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => 'id|description|actions|status',
|
|
|
|
ParamValidator::PARAM_TYPE => [
|
2009-10-07 13:57:06 +00:00
|
|
|
'id',
|
|
|
|
'description',
|
|
|
|
'pattern',
|
|
|
|
'actions',
|
|
|
|
'hits',
|
|
|
|
'comments',
|
|
|
|
'lasteditor',
|
|
|
|
'lastedittime',
|
|
|
|
'status',
|
|
|
|
'private',
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
2022-04-03 23:23:52 +00:00
|
|
|
ParamValidator::PARAM_ISMULTI => true
|
2017-06-15 14:23:16 +00:00
|
|
|
]
|
|
|
|
];
|
2009-10-07 13:57:06 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 16:25:22 +00:00
|
|
|
/**
|
2021-01-17 11:54:43 +00:00
|
|
|
* @codeCoverageIgnore Merely declarative
|
|
|
|
* @inheritDoc
|
2014-10-28 16:25:22 +00:00
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
2017-06-15 14:23:16 +00:00
|
|
|
return [
|
2014-10-28 16:25:22 +00:00
|
|
|
'action=query&list=abusefilters&abfshow=enabled|!private'
|
|
|
|
=> 'apihelp-query+abusefilters-example-1',
|
|
|
|
'action=query&list=abusefilters&abfprop=id|description|pattern'
|
|
|
|
=> 'apihelp-query+abusefilters-example-2',
|
2017-06-15 14:23:16 +00:00
|
|
|
];
|
2009-03-29 04:23:20 +00:00
|
|
|
}
|
|
|
|
}
|