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 {
|
2009-10-07 13:57:06 +00:00
|
|
|
public function __construct( $query, $moduleName ) {
|
|
|
|
parent::__construct( $query, $moduleName, 'afl' );
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
2011-11-16 05:34:24 +00:00
|
|
|
$user = $this->getUser();
|
2015-04-17 15:10:27 +00:00
|
|
|
$errors = $this->getTitle()->getUserPermissionsErrors(
|
|
|
|
'abusefilter-log', $user, true, array( 'ns-specialprotected' ) );
|
2012-12-07 22:17:09 +00:00
|
|
|
if ( count( $errors ) ) {
|
|
|
|
$this->dieUsageMsg( $errors[0] );
|
|
|
|
return;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
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_ip = isset( $prop['ip'] );
|
|
|
|
$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'] );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2011-11-16 05:34:24 +00:00
|
|
|
if ( $fld_ip && !$user->isAllowed( 'abusefilter-private' ) ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->dieUsage( 'You don\'t have permission to view IP addresses', 'permissiondenied' );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2011-11-16 05:34:24 +00:00
|
|
|
if ( $fld_details && !$user->isAllowed( 'abusefilter-log-detail' ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
$this->dieUsage(
|
|
|
|
'You don\'t have permission to view detailed abuse log entries',
|
|
|
|
'permissiondenied'
|
|
|
|
);
|
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'] ) ) {
|
|
|
|
$params['filter'] = array( $params['filter'] );
|
|
|
|
}
|
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 ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
$this->dieUsage(
|
|
|
|
'You don\'t have permission to view log entries for private filters',
|
|
|
|
'permissiondenied'
|
|
|
|
);
|
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 );
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->addFieldsIf( 'afl_ip', $fld_ip );
|
|
|
|
$this->addFieldsIf( array( 'afl_namespace', 'afl_title' ), $fld_title );
|
|
|
|
$this->addFieldsIf( 'afl_action', $fld_action );
|
|
|
|
$this->addFieldsIf( 'afl_var_dump', $fld_details );
|
|
|
|
$this->addFieldsIf( 'afl_actions', $fld_result );
|
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' );
|
|
|
|
$this->addJoinConds( array( 'abuse_filter' => array( '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 );
|
|
|
|
|
|
|
|
$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();
|
|
|
|
} elseif( IP::isIPAddress( $params['user'] ) ) {
|
|
|
|
// 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(
|
|
|
|
array(
|
|
|
|
'afl_user' => $userId,
|
|
|
|
'afl_user_text' => $params['user']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
$this->addWhereIf( array( 'afl_filter' => $params['filter'] ), isset( $params['filter'] ) );
|
2011-11-16 05:34:24 +00:00
|
|
|
$this->addWhereIf( $notDeletedCond, !SpecialAbuseLog::canSeeHidden( $user ) );
|
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 ) ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$this->dieUsageMsg( array( 'invalidtitle', $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;
|
|
|
|
}
|
2012-12-14 11:17:24 +00:00
|
|
|
if ( SpecialAbuseLog::isHidden( $row ) &&
|
2012-02-10 23:41:05 +00:00
|
|
|
!SpecialAbuseLog::canSeeHidden( $user )
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-17 15:18:13 +00:00
|
|
|
$canSeeDetails = SpecialAbuseLog::canSeeDetails( $row->afl_filter );
|
|
|
|
|
2009-03-28 23:40:27 +00:00
|
|
|
$entry = array();
|
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 );
|
2012-12-11 00:58:24 +00:00
|
|
|
$entry['filter_id'] = '';
|
2013-07-17 15:18:13 +00:00
|
|
|
if ( $canSeeDetails ) {
|
2013-07-09 22:49:19 +00:00
|
|
|
$entry['filter_id'] = $row->afl_filter;
|
2012-12-11 00:58:24 +00:00
|
|
|
}
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $fld_filter ) {
|
2009-03-28 23:40:27 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
if ( $fld_ip ) {
|
2009-03-29 04:29:15 +00:00
|
|
|
$entry['ip'] = $row->afl_ip;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
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 ) ) {
|
|
|
|
$entry['revid'] = '';
|
2013-07-17 15:18:13 +00:00
|
|
|
if ( $canSeeDetails ) {
|
2012-12-14 11:17:24 +00:00
|
|
|
$entry['revid'] = $row->afl_rev_id;
|
|
|
|
}
|
|
|
|
}
|
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 ) {
|
2012-12-18 22:02:16 +00:00
|
|
|
$entry['details'] = array();
|
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
|
|
|
|
2010-06-25 20:04:55 +00:00
|
|
|
if ( $fld_hidden ) {
|
2012-02-10 23:41:05 +00:00
|
|
|
$val = SpecialAbuseLog::isHidden( $row );
|
|
|
|
if ( $val ) {
|
|
|
|
$entry['hidden'] = $val;
|
|
|
|
}
|
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 ) {
|
|
|
|
$fit = $result->addValue( array( '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
|
|
|
}
|
2014-12-30 22:20:55 +00:00
|
|
|
if ( defined( 'ApiResult::META_CONTENT' ) ) {
|
2015-04-20 18:41:28 +00:00
|
|
|
$result->addIndexedTagName( array( 'query', $this->getModuleName() ), 'item' );
|
2014-12-30 22:20:55 +00:00
|
|
|
} else {
|
|
|
|
$result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
|
|
|
|
}
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2009-10-07 13:57:06 +00:00
|
|
|
return array(
|
2009-03-28 23:40:27 +00:00
|
|
|
'start' => array(
|
2009-10-07 13:57:06 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'timestamp'
|
2009-03-28 23:40:27 +00:00
|
|
|
),
|
|
|
|
'end' => array(
|
2009-10-07 13:57:06 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'timestamp'
|
2009-03-28 23:40:27 +00:00
|
|
|
),
|
|
|
|
'dir' => array(
|
2009-10-07 13:57:06 +00:00
|
|
|
ApiBase::PARAM_TYPE => array(
|
2009-03-28 23:40:27 +00:00
|
|
|
'newer',
|
|
|
|
'older'
|
|
|
|
),
|
2014-10-28 16:25:22 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'older',
|
2015-09-28 18:03:35 +00:00
|
|
|
/** @todo Once support for MediaWiki < 1.25 is dropped,
|
|
|
|
* just use ApiBase::PARAM_HELP_MSG directly
|
|
|
|
*/
|
2014-10-28 16:25:22 +00:00
|
|
|
constant( 'ApiBase::PARAM_HELP_MSG' ) ?: '' => 'api-help-param-direction',
|
2009-03-28 23:40:27 +00:00
|
|
|
),
|
|
|
|
'user' => null,
|
|
|
|
'title' => null,
|
2012-12-07 00:01:22 +00:00
|
|
|
'filter' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true
|
|
|
|
),
|
2009-03-28 23:40:27 +00:00
|
|
|
'limit' => array(
|
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
|
2009-03-28 23:40:27 +00:00
|
|
|
),
|
|
|
|
'prop' => array(
|
2012-12-14 11:17:24 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'ids|user|title|action|result|timestamp|hidden|revid',
|
2009-10-07 13:57:06 +00:00
|
|
|
ApiBase::PARAM_TYPE => array(
|
|
|
|
'ids',
|
|
|
|
'filter',
|
|
|
|
'user',
|
|
|
|
'ip',
|
|
|
|
'title',
|
|
|
|
'action',
|
|
|
|
'details',
|
|
|
|
'result',
|
|
|
|
'timestamp',
|
2010-06-25 20:04:55 +00:00
|
|
|
'hidden',
|
2012-12-14 11:17:24 +00:00
|
|
|
'revid',
|
2009-10-07 13:57:06 +00:00
|
|
|
),
|
|
|
|
ApiBase::PARAM_ISMULTI => true
|
2009-03-28 23:40:27 +00:00
|
|
|
)
|
|
|
|
);
|
2010-02-13 14:10:36 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2014-10-28 16:25:22 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since MediaWiki core 1.25
|
|
|
|
*/
|
2009-03-28 23:40:27 +00:00
|
|
|
public function getParamDescription() {
|
2009-10-07 13:57:06 +00:00
|
|
|
return array(
|
2009-03-28 23:40:27 +00:00
|
|
|
'start' => 'The timestamp to start enumerating from',
|
|
|
|
'end' => 'The timestamp to stop enumerating at',
|
|
|
|
'dir' => 'The direction in which to enumerate',
|
2009-03-29 00:12:40 +00:00
|
|
|
'title' => 'Show only entries occurring on a given page.',
|
|
|
|
'user' => 'Show only entries done by a given user or IP address.',
|
2010-02-13 14:10:36 +00:00
|
|
|
'filter' => 'Show only entries that were caught by a given filter ID',
|
2009-03-28 23:40:27 +00:00
|
|
|
'limit' => 'The maximum amount of entries to list',
|
|
|
|
'prop' => 'Which properties to get',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-28 16:25:22 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since MediaWiki core 1.25
|
|
|
|
*/
|
2009-03-28 23:40:27 +00:00
|
|
|
public function getDescription() {
|
|
|
|
return 'Show events that were caught by one of the abuse filters.';
|
|
|
|
}
|
2010-08-19 21:12:09 +00:00
|
|
|
|
2014-10-28 16:25:22 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since MediaWiki core 1.25
|
|
|
|
*/
|
2011-08-26 20:12:34 +00:00
|
|
|
public function getExamples() {
|
2009-10-07 13:57:06 +00:00
|
|
|
return array(
|
|
|
|
'api.php?action=query&list=abuselog',
|
2009-03-28 23:40:27 +00:00
|
|
|
'api.php?action=query&list=abuselog&afltitle=API'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-28 16:25:22 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
|
|
|
return array(
|
|
|
|
'action=query&list=abuselog'
|
|
|
|
=> 'apihelp-query+abuselog-example-1',
|
|
|
|
'action=query&list=abuselog&afltitle=API'
|
|
|
|
=> 'apihelp-query+abuselog-example-2',
|
|
|
|
);
|
2009-03-28 23:40:27 +00:00
|
|
|
}
|
|
|
|
}
|