2009-01-28 23:54:41 +00:00
|
|
|
<?php
|
|
|
|
|
2020-01-11 17:05:30 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\VariableGenerator\RCVariableGenerator;
|
|
|
|
|
2009-01-29 22:44:31 +00:00
|
|
|
class AbuseFilterViewTestBatch extends AbuseFilterView {
|
2018-11-08 14:34:32 +00:00
|
|
|
/**
|
|
|
|
* @var int The limit of changes to test, hard coded for now
|
|
|
|
*/
|
2015-09-28 18:03:35 +00:00
|
|
|
protected static $mChangeLimit = 100;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-11-08 14:34:32 +00:00
|
|
|
/**
|
|
|
|
* @var bool Whether to show changes that don't trigger the specified pattern
|
|
|
|
*/
|
|
|
|
public $mShowNegative;
|
|
|
|
/**
|
|
|
|
* @var string The start time of the lookup period
|
|
|
|
*/
|
|
|
|
public $mTestPeriodStart;
|
|
|
|
/**
|
|
|
|
* @var string The end time of the lookup period
|
|
|
|
*/
|
|
|
|
public $mTestPeriodEnd;
|
|
|
|
/**
|
|
|
|
* @var string The page of which edits we're interested in
|
|
|
|
*/
|
|
|
|
public $mTestPage;
|
|
|
|
/**
|
|
|
|
* @var string The user whose actions we want to test
|
|
|
|
*/
|
|
|
|
public $mTestUser;
|
|
|
|
/**
|
|
|
|
* @var bool Whether to exclude bot edits
|
|
|
|
*/
|
|
|
|
public $mExcludeBots;
|
|
|
|
/**
|
|
|
|
* @var string The action (performed by the user) we want to search for
|
|
|
|
*/
|
|
|
|
public $mTestAction;
|
2019-12-16 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @var string The text of the rule to test changes against
|
|
|
|
*/
|
|
|
|
private $testPattern;
|
2013-10-15 13:22:05 +00:00
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* Shows the page
|
|
|
|
*/
|
|
|
|
public function show() {
|
2011-11-16 05:34:24 +00:00
|
|
|
$out = $this->getOutput();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2019-08-27 09:40:01 +00:00
|
|
|
if ( !AbuseFilter::canViewPrivate( $this->getUser() ) ) {
|
2018-05-04 19:56:45 +00:00
|
|
|
$out->addWikiMsg( 'abusefilter-mustviewprivateoredit' );
|
2009-03-31 15:13:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-01-28 23:54:41 +00:00
|
|
|
|
|
|
|
$this->loadParameters();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
$out->setPageTitle( $this->msg( 'abusefilter-test' ) );
|
2011-11-16 05:34:24 +00:00
|
|
|
$out->addWikiMsg( 'abusefilter-test-intro', self::$mChangeLimit );
|
2018-03-15 17:22:37 +00:00
|
|
|
$out->enableOOUI();
|
2009-01-28 23:54:41 +00:00
|
|
|
|
|
|
|
$output = '';
|
2010-02-13 14:10:36 +00:00
|
|
|
$output .=
|
2018-04-08 15:57:23 +00:00
|
|
|
$this->buildEditBox(
|
2019-12-16 11:17:49 +00:00
|
|
|
$this->testPattern,
|
2018-03-15 17:22:37 +00:00
|
|
|
true,
|
2018-05-04 19:56:45 +00:00
|
|
|
true,
|
|
|
|
false
|
2018-03-15 17:22:37 +00:00
|
|
|
) . "\n";
|
2009-01-29 22:44:31 +00:00
|
|
|
|
2018-12-09 13:33:30 +00:00
|
|
|
$output .= $this->buildFilterLoader();
|
2018-03-15 17:22:37 +00:00
|
|
|
$output = Xml::tags( 'div', [ 'id' => 'mw-abusefilter-test-editor' ], $output );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-03-15 17:22:37 +00:00
|
|
|
$RCMaxAge = $this->getConfig()->get( 'RCMaxAge' );
|
|
|
|
$min = wfTimestamp( TS_ISO_8601, time() - $RCMaxAge );
|
|
|
|
$max = wfTimestampNow();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-03-15 17:22:37 +00:00
|
|
|
// Search form
|
|
|
|
$formFields = [];
|
2018-05-01 09:27:00 +00:00
|
|
|
$formFields['wpTestAction'] = [
|
|
|
|
'name' => 'wpTestAction',
|
|
|
|
'type' => 'select',
|
|
|
|
'label-message' => 'abusefilter-test-action',
|
|
|
|
'options' => [
|
|
|
|
$this->msg( 'abusefilter-test-search-type-all' )->text() => 0,
|
|
|
|
$this->msg( 'abusefilter-test-search-type-edit' )->text() => 'edit',
|
|
|
|
$this->msg( 'abusefilter-test-search-type-move' )->text() => 'move',
|
|
|
|
$this->msg( 'abusefilter-test-search-type-delete' )->text() => 'delete',
|
2018-07-11 23:54:48 +00:00
|
|
|
$this->msg( 'abusefilter-test-search-type-createaccount' )->text() => 'createaccount',
|
|
|
|
$this->msg( 'abusefilter-test-search-type-upload' )->text() => 'upload'
|
2018-05-01 09:27:00 +00:00
|
|
|
]
|
|
|
|
];
|
2018-03-15 17:22:37 +00:00
|
|
|
$formFields['wpTestUser'] = [
|
|
|
|
'name' => 'wpTestUser',
|
|
|
|
'type' => 'user',
|
|
|
|
'ipallowed' => true,
|
|
|
|
'label-message' => 'abusefilter-test-user',
|
|
|
|
'default' => $this->mTestUser
|
|
|
|
];
|
2018-05-07 15:25:40 +00:00
|
|
|
$formFields['wpExcludeBots'] = [
|
|
|
|
'name' => 'wpExcludeBots',
|
|
|
|
'type' => 'check',
|
|
|
|
'label-message' => 'abusefilter-test-nobots',
|
|
|
|
'default' => $this->mExcludeBots
|
|
|
|
];
|
2018-03-15 17:22:37 +00:00
|
|
|
$formFields['wpTestPeriodStart'] = [
|
|
|
|
'name' => 'wpTestPeriodStart',
|
|
|
|
'type' => 'datetime',
|
|
|
|
'label-message' => 'abusefilter-test-period-start',
|
|
|
|
'default' => $this->mTestPeriodStart,
|
|
|
|
'min' => $min,
|
|
|
|
'max' => $max
|
|
|
|
];
|
|
|
|
$formFields['wpTestPeriodEnd'] = [
|
|
|
|
'name' => 'wpTestPeriodEnd',
|
|
|
|
'type' => 'datetime',
|
|
|
|
'label-message' => 'abusefilter-test-period-end',
|
|
|
|
'default' => $this->mTestPeriodEnd,
|
|
|
|
'min' => $min,
|
|
|
|
'max' => $max
|
|
|
|
];
|
|
|
|
$formFields['wpTestPage'] = [
|
|
|
|
'name' => 'wpTestPage',
|
|
|
|
'type' => 'title',
|
|
|
|
'label-message' => 'abusefilter-test-page',
|
|
|
|
'default' => $this->mTestPage,
|
2018-05-10 20:20:05 +00:00
|
|
|
'creatable' => true,
|
|
|
|
'required' => false
|
2018-03-15 17:22:37 +00:00
|
|
|
];
|
|
|
|
$formFields['wpShowNegative'] = [
|
|
|
|
'name' => 'wpShowNegative',
|
|
|
|
'type' => 'check',
|
|
|
|
'label-message' => 'abusefilter-test-shownegative',
|
|
|
|
'selected' => $this->mShowNegative
|
|
|
|
];
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2018-03-15 17:22:37 +00:00
|
|
|
$htmlForm = HTMLForm::factory( 'ooui', $formFields, $this->getContext() )
|
|
|
|
->addHiddenField( 'title', $this->getTitle( 'test' )->getPrefixedDBkey() )
|
|
|
|
->setId( 'wpFilterForm' )
|
|
|
|
->setWrapperLegendMsg( 'abusefilter-list-options' )
|
|
|
|
->setAction( $this->getTitle( 'test' )->getLocalURL() )
|
|
|
|
->setSubmitTextMsg( 'abusefilter-test-submit' )
|
|
|
|
->setMethod( 'post' )
|
2018-04-29 17:52:45 +00:00
|
|
|
->prepareForm()
|
|
|
|
->getHTML( true );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2018-03-15 17:22:37 +00:00
|
|
|
$output = Xml::fieldset( $this->msg( 'abusefilter-test-legend' )->text(), $output . $htmlForm );
|
2011-11-16 05:34:24 +00:00
|
|
|
$out->addHTML( $output );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2011-11-16 05:34:24 +00:00
|
|
|
if ( $this->getRequest()->wasPosted() ) {
|
2009-01-28 23:54:41 +00:00
|
|
|
$this->doTest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 08:19:09 +00:00
|
|
|
/**
|
2018-10-03 12:02:00 +00:00
|
|
|
* Loads the revisions and checks the given syntax against them
|
2017-07-13 08:19:09 +00:00
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function doTest() {
|
2009-01-28 23:54:41 +00:00
|
|
|
// Quick syntax check.
|
2011-11-16 05:34:24 +00:00
|
|
|
$out = $this->getOutput();
|
2020-09-19 22:30:14 +00:00
|
|
|
$parser = AbuseFilter::getDefaultParser();
|
|
|
|
|
|
|
|
$validSyntax = $parser->checkSyntax( $this->testPattern );
|
|
|
|
if ( $validSyntax !== true ) {
|
2011-11-16 05:34:24 +00:00
|
|
|
$out->addWikiMsg( 'abusefilter-test-syntaxerr' );
|
2009-01-28 23:54:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2009-01-29 22:44:31 +00:00
|
|
|
|
2017-07-13 08:19:09 +00:00
|
|
|
$conds = [];
|
2018-03-09 21:23:38 +00:00
|
|
|
|
|
|
|
if ( (string)$this->mTestUser !== '' ) {
|
|
|
|
$conds[] = ActorMigration::newMigration()->getWhere(
|
|
|
|
$dbr, 'rc_user', User::newFromName( $this->mTestUser, false )
|
|
|
|
)['conds'];
|
|
|
|
}
|
2009-01-29 22:44:31 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $this->mTestPeriodStart ) {
|
|
|
|
$conds[] = 'rc_timestamp >= ' .
|
2009-02-07 09:34:11 +00:00
|
|
|
$dbr->addQuotes( $dbr->timestamp( strtotime( $this->mTestPeriodStart ) ) );
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $this->mTestPeriodEnd ) {
|
|
|
|
$conds[] = 'rc_timestamp <= ' .
|
2009-02-07 09:34:11 +00:00
|
|
|
$dbr->addQuotes( $dbr->timestamp( strtotime( $this->mTestPeriodEnd ) ) );
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $this->mTestPage ) {
|
2009-04-23 04:30:17 +00:00
|
|
|
$title = Title::newFromText( $this->mTestPage );
|
2014-01-04 03:03:59 +00:00
|
|
|
if ( $title instanceof Title ) {
|
|
|
|
$conds['rc_namespace'] = $title->getNamespace();
|
|
|
|
$conds['rc_title'] = $title->getDBkey();
|
|
|
|
} else {
|
|
|
|
$out->addWikiMsg( 'abusefilter-test-badtitle' );
|
|
|
|
return;
|
|
|
|
}
|
2009-04-23 04:30:17 +00:00
|
|
|
}
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2018-05-07 15:25:40 +00:00
|
|
|
if ( $this->mExcludeBots ) {
|
|
|
|
$conds['rc_bot'] = 0;
|
|
|
|
}
|
2017-07-13 08:19:09 +00:00
|
|
|
|
2018-08-26 08:34:42 +00:00
|
|
|
$action = $this->mTestAction !== '0' ? $this->mTestAction : false;
|
2018-09-24 12:34:53 +00:00
|
|
|
$conds[] = $this->buildTestConditions( $dbr, $action );
|
|
|
|
|
2009-01-28 23:54:41 +00:00
|
|
|
// Get our ChangesList
|
2019-12-16 11:17:49 +00:00
|
|
|
$changesList = new AbuseFilterChangesList( $this->getSkin(), $this->testPattern );
|
2009-01-28 23:54:41 +00:00
|
|
|
$output = $changesList->beginRecentChangesList();
|
|
|
|
|
2017-10-12 19:44:48 +00:00
|
|
|
$rcQuery = RecentChange::getQueryInfo();
|
2009-10-07 13:57:06 +00:00
|
|
|
$res = $dbr->select(
|
2017-10-12 19:44:48 +00:00
|
|
|
$rcQuery['tables'],
|
|
|
|
$rcQuery['fields'],
|
2018-05-07 15:25:40 +00:00
|
|
|
$conds,
|
2010-02-13 14:10:36 +00:00
|
|
|
__METHOD__,
|
2017-10-12 19:44:48 +00:00
|
|
|
[ 'LIMIT' => self::$mChangeLimit, 'ORDER BY' => 'rc_timestamp desc' ],
|
|
|
|
$rcQuery['joins']
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2009-01-28 23:54:41 +00:00
|
|
|
|
|
|
|
$counter = 1;
|
|
|
|
|
2020-09-20 22:07:57 +00:00
|
|
|
$contextUser = $this->getUser();
|
2020-09-19 22:30:14 +00:00
|
|
|
$parser->toggleConditionLimit( false );
|
2015-09-28 18:03:35 +00:00
|
|
|
foreach ( $res as $row ) {
|
2019-06-25 16:39:57 +00:00
|
|
|
$vars = new AbuseFilterVariableHolder();
|
2020-05-28 18:09:17 +00:00
|
|
|
$rc = RecentChange::newFromRow( $row );
|
2020-09-20 22:07:57 +00:00
|
|
|
$varGenerator = new RCVariableGenerator( $vars, $rc, $contextUser );
|
2019-06-25 16:39:57 +00:00
|
|
|
$vars = $varGenerator->getVars();
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( !$vars ) {
|
2009-01-28 23:54:41 +00:00
|
|
|
continue;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2020-09-19 22:30:14 +00:00
|
|
|
$parser->setVariables( $vars );
|
|
|
|
$result = $parser->checkConditions( $this->testPattern );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $result || $this->mShowNegative ) {
|
2009-01-30 00:54:20 +00:00
|
|
|
// Stash result in RC item
|
2019-02-24 05:29:18 +00:00
|
|
|
// @phan-suppress-next-line PhanUndeclaredProperty not a big deal
|
2009-01-30 00:54:20 +00:00
|
|
|
$rc->filterResult = $result;
|
2009-01-28 23:54:41 +00:00
|
|
|
$rc->counter = $counter++;
|
|
|
|
$output .= $changesList->recentChangesLine( $rc, false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= $changesList->endRecentChangesList();
|
|
|
|
|
2011-11-16 05:34:24 +00:00
|
|
|
$out->addHTML( $output );
|
2009-01-28 23:54:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* Loads parameters from request
|
|
|
|
*/
|
|
|
|
public function loadParameters() {
|
2011-11-16 05:34:24 +00:00
|
|
|
$request = $this->getRequest();
|
|
|
|
|
2019-12-16 11:17:49 +00:00
|
|
|
$this->testPattern = $request->getText( 'wpFilterRules' );
|
2011-11-16 05:34:24 +00:00
|
|
|
$this->mShowNegative = $request->getBool( 'wpShowNegative' );
|
|
|
|
$testUsername = $request->getText( 'wpTestUser' );
|
|
|
|
$this->mTestPeriodEnd = $request->getText( 'wpTestPeriodEnd' );
|
|
|
|
$this->mTestPeriodStart = $request->getText( 'wpTestPeriodStart' );
|
|
|
|
$this->mTestPage = $request->getText( 'wpTestPage' );
|
2018-05-07 15:25:40 +00:00
|
|
|
$this->mExcludeBots = $request->getBool( 'wpExcludeBots' );
|
2018-05-01 09:27:00 +00:00
|
|
|
$this->mTestAction = $request->getText( 'wpTestAction' );
|
2009-01-30 19:19:03 +00:00
|
|
|
|
2019-12-16 11:17:49 +00:00
|
|
|
if ( !$this->testPattern
|
2009-10-07 13:57:06 +00:00
|
|
|
&& count( $this->mParams ) > 1
|
2017-07-08 18:49:13 +00:00
|
|
|
&& is_numeric( $this->mParams[1] )
|
|
|
|
) {
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2019-12-16 11:17:49 +00:00
|
|
|
$this->testPattern = $dbr->selectField( 'abuse_filter',
|
2010-02-13 14:10:36 +00:00
|
|
|
'af_pattern',
|
2017-06-15 14:23:16 +00:00
|
|
|
[ 'af_id' => $this->mParams[1] ],
|
2009-10-07 13:57:06 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2009-01-30 19:19:03 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-25 02:33:09 +00:00
|
|
|
// Normalise username
|
2017-09-20 14:14:02 +00:00
|
|
|
$userTitle = Title::newFromText( $testUsername, NS_USER );
|
|
|
|
$this->mTestUser = $userTitle ? $userTitle->getText() : null;
|
2009-01-28 23:54:41 +00:00
|
|
|
}
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|