2009-03-28 23:40:27 +00:00
|
|
|
<?php
|
2009-10-07 13:57:06 +00:00
|
|
|
/**
|
2009-03-28 23:40:27 +00:00
|
|
|
* Created on Mar 28, 2009
|
|
|
|
*
|
|
|
|
* AbuseFilter extension
|
|
|
|
*
|
2010-08-19 21:12:09 +00:00
|
|
|
* Copyright © 2008 Alex Z. mrzmanwiki AT gmail DOT com
|
2009-03-28 23:40:27 +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-28 23:40:27 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query module to list abuse log entries.
|
|
|
|
*
|
|
|
|
* @ingroup API
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
class ApiQueryAbuseLog extends ApiQueryBase {
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @param ApiQuery $query
|
|
|
|
* @param string $moduleName
|
|
|
|
*/
|
2018-10-17 05:15:21 +00:00
|
|
|
public function __construct( ApiQuery $query, $moduleName ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
parent::__construct( $query, $moduleName, 'afl' );
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
2019-01-11 16:34:08 +00:00
|
|
|
* @inheritDoc
|
2018-04-04 21:14:25 +00:00
|
|
|
*/
|
2009-03-28 23:40:27 +00:00
|
|
|
public function execute() {
|
2019-01-11 16:34:08 +00:00
|
|
|
// Same check as in SpecialAbuseLog
|
|
|
|
$this->checkUserRightsAny( 'abusefilter-log' );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2019-01-11 16:34:08 +00:00
|
|
|
$user = $this->getUser();
|
2009-03-28 23:40:27 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$prop = array_flip( $params['prop'] );
|
|
|
|
$fld_ids = isset( $prop['ids'] );
|
|
|
|
$fld_filter = isset( $prop['filter'] );
|
|
|
|
$fld_user = isset( $prop['user'] );
|
|
|
|
$fld_title = isset( $prop['title'] );
|
|
|
|
$fld_action = isset( $prop['action'] );
|
|
|
|
$fld_details = isset( $prop['details'] );
|
|
|
|
$fld_result = isset( $prop['result'] );
|
|
|
|
$fld_timestamp = isset( $prop['timestamp'] );
|
2010-06-25 20:04:55 +00:00
|
|
|
$fld_hidden = isset( $prop['hidden'] );
|
2012-12-14 11:17:24 +00:00
|
|
|
$fld_revid = isset( $prop['revid'] );
|
2018-03-10 21:47:33 +00:00
|
|
|
$isCentral = $this->getConfig()->get( 'AbuseFilterIsCentral' );
|
|
|
|
$fld_wiki = $isCentral && isset( $prop['wiki'] );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-08-08 12:03:56 +00:00
|
|
|
if ( $fld_details ) {
|
|
|
|
$this->checkUserRightsAny( 'abusefilter-log-detail' );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2012-12-11 00:58:24 +00:00
|
|
|
// Match permissions for viewing events on private filters to SpecialAbuseLog (bug 42814)
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( $params['filter'] &&
|
|
|
|
!( AbuseFilterView::canViewPrivate() || $user->isAllowed( 'abusefilter-log-private' ) )
|
|
|
|
) {
|
2012-12-07 00:01:22 +00:00
|
|
|
// A specific filter parameter is set but the user isn't allowed to view all filters
|
|
|
|
if ( !is_array( $params['filter'] ) ) {
|
2017-06-15 14:23:16 +00:00
|
|
|
$params['filter'] = [ $params['filter'] ];
|
2012-12-07 00:01:22 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
foreach ( $params['filter'] as $filter ) {
|
2012-12-07 00:01:22 +00:00
|
|
|
if ( AbuseFilter::filterHidden( $filter ) ) {
|
2017-08-08 12:03:56 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-permissiondenied', $this->msg( 'action-abusefilter-log-private' ) ]
|
|
|
|
);
|
2012-12-07 00:01:22 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-11 00:58:24 +00:00
|
|
|
}
|
2009-03-28 23:40:27 +00:00
|
|
|
|
|
|
|
$result = $this->getResult();
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->addTables( 'abuse_filter_log' );
|
|
|
|
$this->addFields( 'afl_timestamp' );
|
2012-12-14 11:17:24 +00:00
|
|
|
$this->addFields( 'afl_rev_id' );
|
|
|
|
$this->addFields( 'afl_deleted' );
|
|
|
|
$this->addFields( 'afl_filter' );
|
|
|
|
$this->addFieldsIf( 'afl_id', $fld_ids );
|
2013-06-26 22:58:36 +00:00
|
|
|
$this->addFieldsIf( 'afl_user_text', $fld_user );
|
2017-06-15 14:23:16 +00:00
|
|
|
$this->addFieldsIf( [ 'afl_namespace', 'afl_title' ], $fld_title );
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->addFieldsIf( 'afl_action', $fld_action );
|
|
|
|
$this->addFieldsIf( 'afl_var_dump', $fld_details );
|
|
|
|
$this->addFieldsIf( 'afl_actions', $fld_result );
|
2018-02-03 15:39:08 +00:00
|
|
|
$this->addFieldsIf( 'afl_wiki', $fld_wiki );
|
2010-08-19 21:12:09 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $fld_filter ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->addTables( 'abuse_filter' );
|
|
|
|
$this->addFields( 'af_public_comments' );
|
2017-06-15 14:23:16 +00:00
|
|
|
$this->addJoinConds( [ 'abuse_filter' => [ 'LEFT JOIN',
|
|
|
|
'af_id=afl_filter' ] ] );
|
2009-03-30 12:19:41 +00:00
|
|
|
}
|
2009-03-28 23:40:27 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->addOption( 'LIMIT', $params['limit'] + 1 );
|
|
|
|
|
2018-07-14 12:26:42 +00:00
|
|
|
$this->addWhereIf( [ 'afl_id' => $params['logid'] ], isset( $params['logid'] ) );
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->addWhereRange( 'afl_timestamp', $params['dir'], $params['start'], $params['end'] );
|
2012-12-11 00:58:24 +00:00
|
|
|
|
2010-08-28 12:11:11 +00:00
|
|
|
$db = $this->getDB();
|
2013-07-17 15:18:13 +00:00
|
|
|
$notDeletedCond = SpecialAbuseLog::getNotDeletedCond( $db );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-03-25 23:28:26 +00:00
|
|
|
if ( isset( $params['user'] ) ) {
|
|
|
|
$u = User::newFromName( $params['user'] );
|
|
|
|
if ( $u ) {
|
|
|
|
// Username normalisation
|
|
|
|
$params['user'] = $u->getName();
|
|
|
|
$userId = $u->getId();
|
2017-06-13 09:59:10 +00:00
|
|
|
} elseif ( IP::isIPAddress( $params['user'] ) ) {
|
2013-03-25 23:28:26 +00:00
|
|
|
// It's an IP, sanitize it
|
|
|
|
$params['user'] = IP::sanitizeIP( $params['user'] );
|
|
|
|
$userId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $userId ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
// Only add the WHERE for user in case it's either a valid user
|
|
|
|
// (but not necessary an existing one) or an IP.
|
2013-03-25 23:28:26 +00:00
|
|
|
$this->addWhere(
|
2017-06-15 14:23:16 +00:00
|
|
|
[
|
2013-03-25 23:28:26 +00:00
|
|
|
'afl_user' => $userId,
|
|
|
|
'afl_user_text' => $params['user']
|
2017-06-15 14:23:16 +00:00
|
|
|
]
|
2013-03-25 23:28:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 14:23:16 +00:00
|
|
|
$this->addWhereIf( [ 'afl_filter' => $params['filter'] ], isset( $params['filter'] ) );
|
2018-04-29 17:52:45 +00:00
|
|
|
$this->addWhereIf( $notDeletedCond, !SpecialAbuseLog::canSeeHidden() );
|
2018-02-09 20:00:07 +00:00
|
|
|
if ( isset( $params['wiki'] ) ) {
|
|
|
|
// 'wiki' won't be set if $wgAbuseFilterIsCentral = false
|
2018-03-10 21:47:33 +00:00
|
|
|
$this->addWhereIf( [ 'afl_wiki' => $params['wiki'] ], $isCentral );
|
2018-02-09 20:00:07 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-28 23:40:27 +00:00
|
|
|
$title = $params['title'];
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !is_null( $title ) ) {
|
|
|
|
$titleObj = Title::newFromText( $title );
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( is_null( $titleObj ) ) {
|
2017-08-07 23:35:21 +00:00
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->addWhereFld( 'afl_namespace', $titleObj->getNamespace() );
|
|
|
|
$this->addWhereFld( 'afl_title', $titleObj->getDBkey() );
|
|
|
|
}
|
|
|
|
$res = $this->select( __METHOD__ );
|
2009-03-28 23:40:27 +00:00
|
|
|
|
|
|
|
$count = 0;
|
2011-02-10 17:25:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( ++$count > $params['limit'] ) {
|
2009-03-28 23:40:27 +00:00
|
|
|
// We've had enough
|
2012-09-02 11:07:02 +00:00
|
|
|
$ts = new MWTimestamp( $row->afl_timestamp );
|
|
|
|
$this->setContinueEnumParameter( 'start', $ts->getTimestamp( TS_ISO_8601 ) );
|
2009-03-28 23:40:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-10-16 21:04:20 +00:00
|
|
|
$hidden = SpecialAbuseLog::isHidden( $row );
|
|
|
|
if ( $hidden === true && !SpecialAbuseLog::canSeeHidden() ) {
|
2012-02-10 23:41:05 +00:00
|
|
|
continue;
|
2018-10-16 21:04:20 +00:00
|
|
|
} elseif ( $hidden === 'implicit' ) {
|
|
|
|
$rev = Revision::newFromId( $row->afl_rev_id );
|
|
|
|
if ( !$rev->userCan( Revision::SUPPRESSED_ALL, $user ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-10 23:41:05 +00:00
|
|
|
}
|
2013-07-17 15:18:13 +00:00
|
|
|
$canSeeDetails = SpecialAbuseLog::canSeeDetails( $row->afl_filter );
|
|
|
|
|
2017-06-15 14:23:16 +00:00
|
|
|
$entry = [];
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $fld_ids ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$entry['id'] = intval( $row->afl_id );
|
2018-07-14 12:26:42 +00:00
|
|
|
$entry['filter_id'] = $canSeeDetails ? $row->afl_filter : '';
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $fld_filter ) {
|
2016-12-26 09:49:26 +00:00
|
|
|
$globalIndex = AbuseFilter::decodeGlobalName( $row->afl_filter );
|
|
|
|
if ( $globalIndex ) {
|
|
|
|
$entry['filter'] = AbuseFilter::getGlobalFilterDescription( $globalIndex );
|
|
|
|
} else {
|
|
|
|
$entry['filter'] = $row->af_public_comments;
|
|
|
|
}
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_user ) {
|
2013-06-26 22:58:36 +00:00
|
|
|
$entry['user'] = $row->afl_user_text;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2018-02-03 15:39:08 +00:00
|
|
|
if ( $fld_wiki ) {
|
|
|
|
$entry['wiki'] = $row->afl_wiki;
|
|
|
|
}
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $fld_title ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$title = Title::makeTitle( $row->afl_namespace, $row->afl_title );
|
|
|
|
ApiQueryBase::addTitleInfo( $entry, $title );
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $fld_action ) {
|
2009-03-28 23:40:27 +00:00
|
|
|
$entry['action'] = $row->afl_action;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
|
|
|
if ( $fld_result ) {
|
2009-03-28 23:40:27 +00:00
|
|
|
$entry['result'] = $row->afl_actions;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2012-12-14 11:17:24 +00:00
|
|
|
if ( $fld_revid && !is_null( $row->afl_rev_id ) ) {
|
2018-07-14 12:26:42 +00:00
|
|
|
$entry['revid'] = $canSeeDetails ? $row->afl_rev_id : '';
|
2012-12-14 11:17:24 +00:00
|
|
|
}
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $fld_timestamp ) {
|
2012-09-02 11:07:02 +00:00
|
|
|
$ts = new MWTimestamp( $row->afl_timestamp );
|
|
|
|
$entry['timestamp'] = $ts->getTimestamp( TS_ISO_8601 );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $fld_details ) {
|
2017-06-15 14:23:16 +00:00
|
|
|
$entry['details'] = [];
|
2013-07-17 15:18:13 +00:00
|
|
|
if ( $canSeeDetails ) {
|
2012-12-18 22:02:16 +00:00
|
|
|
$vars = AbuseFilter::loadVarDump( $row->afl_var_dump );
|
|
|
|
if ( $vars instanceof AbuseFilterVariableHolder ) {
|
|
|
|
$entry['details'] = $vars->exportAllVars();
|
|
|
|
} else {
|
|
|
|
$entry['details'] = array_change_key_case( $vars, CASE_LOWER );
|
|
|
|
}
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-19 21:12:09 +00:00
|
|
|
|
2018-10-16 21:04:20 +00:00
|
|
|
if ( $fld_hidden && $hidden ) {
|
|
|
|
$entry['hidden'] = $hidden;
|
2010-06-25 20:04:55 +00:00
|
|
|
}
|
2010-08-19 21:12:09 +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 ) {
|
2012-09-02 11:07:02 +00:00
|
|
|
$ts = new MWTimestamp( $row->afl_timestamp );
|
|
|
|
$this->setContinueEnumParameter( 'start', $ts->getTimestamp( TS_ISO_8601 ) );
|
2009-03-30 12:19:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
2017-06-15 14:23:16 +00:00
|
|
|
$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @see ApiQueryBase::getAllowedParams()
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-03-28 23:40:27 +00:00
|
|
|
public function getAllowedParams() {
|
2018-02-03 15:39:08 +00:00
|
|
|
$params = [
|
2018-07-14 12:26:42 +00:00
|
|
|
'logid' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'integer'
|
|
|
|
],
|
2017-06-15 14:23:16 +00:00
|
|
|
'start' => [
|
2009-10-07 13:57:06 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'timestamp'
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
'end' => [
|
2009-10-07 13:57:06 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'timestamp'
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
'dir' => [
|
|
|
|
ApiBase::PARAM_TYPE => [
|
2009-03-28 23:40:27 +00:00
|
|
|
'newer',
|
|
|
|
'older'
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
2014-10-28 16:25:22 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'older',
|
2016-09-20 18:39:11 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
2009-03-28 23:40:27 +00:00
|
|
|
'user' => null,
|
|
|
|
'title' => null,
|
2017-06-15 14:23:16 +00:00
|
|
|
'filter' => [
|
2018-02-03 15:39:08 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
2012-12-07 00:01:22 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
'limit' => [
|
2009-10-07 13:57:06 +00:00
|
|
|
ApiBase::PARAM_DFLT => 10,
|
|
|
|
ApiBase::PARAM_TYPE => 'limit',
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
|
|
|
|
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
|
|
|
'prop' => [
|
2012-12-14 11:17:24 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'ids|user|title|action|result|timestamp|hidden|revid',
|
2017-06-15 14:23:16 +00:00
|
|
|
ApiBase::PARAM_TYPE => [
|
2009-10-07 13:57:06 +00:00
|
|
|
'ids',
|
|
|
|
'filter',
|
|
|
|
'user',
|
|
|
|
'title',
|
|
|
|
'action',
|
|
|
|
'details',
|
|
|
|
'result',
|
|
|
|
'timestamp',
|
2010-06-25 20:04:55 +00:00
|
|
|
'hidden',
|
2012-12-14 11:17:24 +00:00
|
|
|
'revid',
|
2017-06-15 14:23:16 +00:00
|
|
|
],
|
2009-10-07 13:57:06 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true
|
2017-06-15 14:23:16 +00:00
|
|
|
]
|
|
|
|
];
|
2018-03-10 21:47:33 +00:00
|
|
|
if ( $this->getConfig()->get( 'AbuseFilterIsCentral' ) ) {
|
2018-02-03 15:39:08 +00:00
|
|
|
$params['wiki'] = [
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
];
|
|
|
|
$params['prop'][ApiBase::PARAM_DFLT] .= '|wiki';
|
|
|
|
$params['prop'][ApiBase::PARAM_TYPE][] = 'wiki';
|
|
|
|
}
|
|
|
|
return $params;
|
2010-02-13 14:10:36 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2014-10-28 16:25:22 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
2017-10-06 18:52:31 +00:00
|
|
|
* @return array
|
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=abuselog'
|
|
|
|
=> 'apihelp-query+abuselog-example-1',
|
|
|
|
'action=query&list=abuselog&afltitle=API'
|
|
|
|
=> 'apihelp-query+abuselog-example-2',
|
2017-06-15 14:23:16 +00:00
|
|
|
];
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
|
|
|
}
|