2009-01-28 23:54:41 +00:00
|
|
|
<?php
|
|
|
|
|
2020-11-29 10:53:29 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\View;
|
|
|
|
|
2021-02-08 12:34:18 +00:00
|
|
|
use LogEventsList;
|
|
|
|
use LogPage;
|
2024-06-12 18:01:35 +00:00
|
|
|
use MediaWiki\Context\IContextSource;
|
2021-01-03 11:12:16 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterChangesList;
|
2020-10-23 14:19:02 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
|
2021-02-01 15:47:46 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\EditBox\EditBoxBuilderFactory;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\EditBox\EditBoxField;
|
2021-09-01 11:53:38 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerFactory;
|
2021-01-03 13:10:20 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGeneratorFactory;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2024-06-12 18:01:35 +00:00
|
|
|
use MediaWiki\HTMLForm\HTMLForm;
|
2020-10-23 14:19:02 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2021-02-08 12:34:18 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2023-08-19 17:49:36 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2020-11-29 10:53:29 +00:00
|
|
|
use RecentChange;
|
2023-02-26 12:51:08 +00:00
|
|
|
use Wikimedia\Rdbms\LBFactory;
|
2024-04-30 18:21:20 +00:00
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2020-01-11 17:05:30 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private static $mChangeLimit = 100;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2023-02-26 12:51:08 +00:00
|
|
|
/**
|
|
|
|
* @var LBFactory
|
|
|
|
*/
|
|
|
|
private $lbFactory;
|
2019-12-16 11:17:49 +00:00
|
|
|
/**
|
|
|
|
* @var string The text of the rule to test changes against
|
|
|
|
*/
|
|
|
|
private $testPattern;
|
2020-10-23 14:19:02 +00:00
|
|
|
/**
|
|
|
|
* @var EditBoxBuilderFactory
|
|
|
|
*/
|
|
|
|
private $boxBuilderFactory;
|
|
|
|
/**
|
2021-09-01 11:53:38 +00:00
|
|
|
* @var RuleCheckerFactory
|
2020-10-23 14:19:02 +00:00
|
|
|
*/
|
2021-09-01 11:53:38 +00:00
|
|
|
private $ruleCheckerFactory;
|
2021-01-03 13:10:20 +00:00
|
|
|
/**
|
|
|
|
* @var VariableGeneratorFactory
|
|
|
|
*/
|
|
|
|
private $varGeneratorFactory;
|
2020-10-23 14:19:02 +00:00
|
|
|
|
|
|
|
/**
|
2023-02-26 12:51:08 +00:00
|
|
|
* @param LBFactory $lbFactory
|
2020-10-23 14:19:02 +00:00
|
|
|
* @param AbuseFilterPermissionManager $afPermManager
|
|
|
|
* @param EditBoxBuilderFactory $boxBuilderFactory
|
2021-09-01 11:53:38 +00:00
|
|
|
* @param RuleCheckerFactory $ruleCheckerFactory
|
2021-01-03 13:10:20 +00:00
|
|
|
* @param VariableGeneratorFactory $varGeneratorFactory
|
2020-10-23 14:19:02 +00:00
|
|
|
* @param IContextSource $context
|
|
|
|
* @param LinkRenderer $linkRenderer
|
|
|
|
* @param string $basePageName
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2023-02-26 12:51:08 +00:00
|
|
|
LBFactory $lbFactory,
|
2020-10-23 14:19:02 +00:00
|
|
|
AbuseFilterPermissionManager $afPermManager,
|
|
|
|
EditBoxBuilderFactory $boxBuilderFactory,
|
2021-09-01 11:53:38 +00:00
|
|
|
RuleCheckerFactory $ruleCheckerFactory,
|
2021-01-03 13:10:20 +00:00
|
|
|
VariableGeneratorFactory $varGeneratorFactory,
|
2020-10-23 14:19:02 +00:00
|
|
|
IContextSource $context,
|
|
|
|
LinkRenderer $linkRenderer,
|
|
|
|
string $basePageName,
|
|
|
|
array $params
|
|
|
|
) {
|
|
|
|
parent::__construct( $afPermManager, $context, $linkRenderer, $basePageName, $params );
|
2023-02-26 12:51:08 +00:00
|
|
|
$this->lbFactory = $lbFactory;
|
2020-10-23 14:19:02 +00:00
|
|
|
$this->boxBuilderFactory = $boxBuilderFactory;
|
2021-09-01 11:53:38 +00:00
|
|
|
$this->ruleCheckerFactory = $ruleCheckerFactory;
|
2021-01-03 13:10:20 +00:00
|
|
|
$this->varGeneratorFactory = $varGeneratorFactory;
|
2020-10-23 14:19:02 +00:00
|
|
|
}
|
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
|
|
|
|
2022-07-02 13:35:00 +00:00
|
|
|
if ( !$this->afPermManager->canUseTestTools( $this->getAuthority() ) ) {
|
2021-02-19 17:50:38 +00:00
|
|
|
// TODO: the message still refers to the old rights
|
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
|
|
|
|
2023-09-22 20:43:01 +00:00
|
|
|
$out->setPageTitleMsg( $this->msg( 'abusefilter-test' ) );
|
2020-10-03 13:05:20 +00:00
|
|
|
$out->addHelpLink( 'Extension:AbuseFilter/Rules format' );
|
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
|
|
|
|
2022-07-02 13:35:00 +00:00
|
|
|
$boxBuilder = $this->boxBuilderFactory->newEditBoxBuilder( $this, $this->getAuthority(), $out );
|
2020-11-24 12:16:41 +00:00
|
|
|
|
2021-02-01 15:47:46 +00:00
|
|
|
$rulesFields = [
|
|
|
|
'rules' => [
|
|
|
|
'section' => 'abusefilter-test-rules-section',
|
|
|
|
'class' => EditBoxField::class,
|
|
|
|
'html' => $boxBuilder->buildEditBox(
|
|
|
|
$this->testPattern,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
) . $this->buildFilterLoader()
|
|
|
|
]
|
|
|
|
];
|
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
|
|
|
|
2021-04-16 11:24:39 +00:00
|
|
|
$optionsFields = [
|
|
|
|
'TestAction' => [
|
|
|
|
'type' => 'select',
|
|
|
|
'label-message' => 'abusefilter-test-action',
|
|
|
|
'options-messages' => [
|
|
|
|
'abusefilter-test-search-type-all' => '0',
|
|
|
|
'abusefilter-test-search-type-edit' => 'edit',
|
|
|
|
'abusefilter-test-search-type-move' => 'move',
|
|
|
|
'abusefilter-test-search-type-delete' => 'delete',
|
|
|
|
'abusefilter-test-search-type-createaccount' => 'createaccount',
|
|
|
|
'abusefilter-test-search-type-upload' => 'upload'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'TestUser' => [
|
|
|
|
'type' => 'user',
|
|
|
|
'exists' => true,
|
|
|
|
'ipallowed' => true,
|
|
|
|
'required' => false,
|
|
|
|
'label-message' => 'abusefilter-test-user',
|
|
|
|
],
|
|
|
|
'ExcludeBots' => [
|
|
|
|
'type' => 'check',
|
|
|
|
'label-message' => 'abusefilter-test-nobots',
|
|
|
|
],
|
|
|
|
'TestPeriodStart' => [
|
|
|
|
'type' => 'datetime',
|
|
|
|
'label-message' => 'abusefilter-test-period-start',
|
|
|
|
'min' => $min,
|
|
|
|
'max' => $max,
|
|
|
|
],
|
|
|
|
'TestPeriodEnd' => [
|
|
|
|
'type' => 'datetime',
|
|
|
|
'label-message' => 'abusefilter-test-period-end',
|
|
|
|
'min' => $min,
|
|
|
|
'max' => $max,
|
|
|
|
],
|
|
|
|
'TestPage' => [
|
|
|
|
'type' => 'title',
|
|
|
|
'label-message' => 'abusefilter-test-page',
|
|
|
|
'creatable' => true,
|
|
|
|
'required' => false,
|
|
|
|
],
|
|
|
|
'ShowNegative' => [
|
|
|
|
'type' => 'check',
|
|
|
|
'label-message' => 'abusefilter-test-shownegative',
|
|
|
|
],
|
2018-03-15 17:22:37 +00:00
|
|
|
];
|
2021-04-30 18:54:17 +00:00
|
|
|
array_walk( $optionsFields, static function ( &$el ) {
|
2021-02-01 15:47:46 +00:00
|
|
|
$el['section'] = 'abusefilter-test-options-section';
|
|
|
|
} );
|
|
|
|
$allFields = array_merge( $rulesFields, $optionsFields );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2021-02-01 15:47:46 +00:00
|
|
|
HTMLForm::factory( 'ooui', $allFields, $this->getContext() )
|
2021-07-02 10:10:30 +00:00
|
|
|
->setTitle( $this->getTitle( 'test' ) )
|
2018-03-15 17:22:37 +00:00
|
|
|
->setId( 'wpFilterForm' )
|
2021-02-01 15:47:46 +00:00
|
|
|
->setWrapperLegendMsg( 'abusefilter-test-legend' )
|
2018-03-15 17:22:37 +00:00
|
|
|
->setSubmitTextMsg( 'abusefilter-test-submit' )
|
2021-04-16 11:24:39 +00:00
|
|
|
->setSubmitCallback( [ $this, 'doTest' ] )
|
|
|
|
->showAlways();
|
2009-01-28 23:54:41 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-04-16 11:24:39 +00:00
|
|
|
* @param array $formData
|
|
|
|
* @param HTMLForm $form
|
|
|
|
* @return bool
|
2017-07-13 08:19:09 +00:00
|
|
|
*/
|
2021-04-16 11:24:39 +00:00
|
|
|
public function doTest( array $formData, HTMLForm $form ): bool {
|
2009-01-28 23:54:41 +00:00
|
|
|
// Quick syntax check.
|
2021-09-01 11:53:38 +00:00
|
|
|
$ruleChecker = $this->ruleCheckerFactory->newRuleChecker();
|
2020-09-19 22:30:14 +00:00
|
|
|
|
2021-09-06 20:40:36 +00:00
|
|
|
if ( !$ruleChecker->checkSyntax( $this->testPattern )->isValid() ) {
|
2021-04-16 11:24:39 +00:00
|
|
|
$form->addPreHtml(
|
2022-06-30 07:40:47 +00:00
|
|
|
Html::errorBox( $this->msg( 'abusefilter-test-syntaxerr' )->parse() )
|
|
|
|
);
|
2021-04-16 11:24:39 +00:00
|
|
|
return true;
|
2009-01-28 23:54:41 +00:00
|
|
|
}
|
2009-01-29 22:44:31 +00:00
|
|
|
|
2023-02-26 12:51:08 +00:00
|
|
|
$dbr = $this->lbFactory->getReplicaDatabase();
|
2021-04-19 05:18:47 +00:00
|
|
|
$rcQuery = RecentChange::getQueryInfo();
|
2017-07-13 08:19:09 +00:00
|
|
|
$conds = [];
|
2018-03-09 21:23:38 +00:00
|
|
|
|
2021-04-16 11:24:39 +00:00
|
|
|
// Normalise username
|
|
|
|
$userTitle = Title::newFromText( $formData['TestUser'], NS_USER );
|
|
|
|
$testUser = $userTitle ? $userTitle->getText() : '';
|
|
|
|
if ( $testUser !== '' ) {
|
|
|
|
$conds[$rcQuery['fields']['rc_user_text']] = $testUser;
|
2018-03-09 21:23:38 +00:00
|
|
|
}
|
2009-01-29 22:44:31 +00:00
|
|
|
|
2021-04-16 11:24:39 +00:00
|
|
|
$startTS = strtotime( $formData['TestPeriodStart'] );
|
|
|
|
if ( $startTS ) {
|
2024-04-30 18:21:20 +00:00
|
|
|
$conds[] = $dbr->expr( 'rc_timestamp', '>=', $dbr->timestamp( $startTS ) );
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|
2021-04-16 11:24:39 +00:00
|
|
|
$endTS = strtotime( $formData['TestPeriodEnd'] );
|
|
|
|
if ( $endTS ) {
|
2024-04-30 18:21:20 +00:00
|
|
|
$conds[] = $dbr->expr( 'rc_timestamp', '<=', $dbr->timestamp( $endTS ) );
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|
2021-04-16 11:24:39 +00:00
|
|
|
if ( $formData['TestPage'] !== '' ) {
|
|
|
|
// The form validates the input for us, so this shouldn't throw.
|
|
|
|
$title = Title::newFromTextThrow( $formData['TestPage'] );
|
2022-06-29 10:19:24 +00:00
|
|
|
$conds['rc_namespace'] = $title->getNamespace();
|
|
|
|
$conds['rc_title'] = $title->getDBkey();
|
2009-04-23 04:30:17 +00:00
|
|
|
}
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2021-04-16 11:24:39 +00:00
|
|
|
if ( $formData['ExcludeBots'] ) {
|
2018-05-07 15:25:40 +00:00
|
|
|
$conds['rc_bot'] = 0;
|
|
|
|
}
|
2017-07-13 08:19:09 +00:00
|
|
|
|
2021-04-16 11:24:39 +00:00
|
|
|
$action = $formData['TestAction'] !== '0' ? $formData['TestAction'] : false;
|
2018-09-24 12:34:53 +00:00
|
|
|
$conds[] = $this->buildTestConditions( $dbr, $action );
|
2021-03-11 10:50:44 +00:00
|
|
|
$conds = array_merge( $conds, $this->buildVisibilityConditions( $dbr, $this->getAuthority() ) );
|
2018-09-24 12:34:53 +00:00
|
|
|
|
2024-04-30 18:21:20 +00:00
|
|
|
$res = $dbr->newSelectQueryBuilder()
|
|
|
|
->tables( $rcQuery['tables'] )
|
|
|
|
->fields( $rcQuery['fields'] )
|
|
|
|
->conds( $conds )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->limit( self::$mChangeLimit )
|
|
|
|
->orderBy( 'rc_timestamp', SelectQueryBuilder::SORT_DESC )
|
|
|
|
->joinConds( $rcQuery['joins'] )
|
|
|
|
->fetchResultSet();
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2021-01-31 16:45:12 +00:00
|
|
|
// Get our ChangesList
|
2021-09-25 08:28:50 +00:00
|
|
|
$changesList = new AbuseFilterChangesList( $this->getContext(), $this->testPattern );
|
2021-01-31 16:45:12 +00:00
|
|
|
// Note, we're initializing some rows that will later be discarded. Hopefully this won't have any overhead.
|
|
|
|
$changesList->initChangesListRows( $res );
|
|
|
|
$output = $changesList->beginRecentChangesList();
|
|
|
|
|
2009-01-28 23:54:41 +00:00
|
|
|
$counter = 1;
|
|
|
|
|
2020-09-20 22:07:57 +00:00
|
|
|
$contextUser = $this->getUser();
|
2021-09-01 11:53:38 +00:00
|
|
|
$ruleChecker->toggleConditionLimit( false );
|
2015-09-28 18:03:35 +00:00
|
|
|
foreach ( $res as $row ) {
|
2020-05-28 18:09:17 +00:00
|
|
|
$rc = RecentChange::newFromRow( $row );
|
2021-04-16 11:24:39 +00:00
|
|
|
if ( !$formData['ShowNegative'] ) {
|
2021-02-08 12:34:18 +00:00
|
|
|
$type = (int)$rc->getAttribute( 'rc_type' );
|
2021-04-16 11:24:39 +00:00
|
|
|
$deletedValue = (int)$rc->getAttribute( 'rc_deleted' );
|
2021-02-08 12:34:18 +00:00
|
|
|
if (
|
|
|
|
(
|
|
|
|
$type === RC_LOG &&
|
|
|
|
!LogEventsList::userCanBitfield(
|
|
|
|
$deletedValue,
|
|
|
|
LogPage::SUPPRESSED_ACTION | LogPage::SUPPRESSED_USER,
|
|
|
|
$contextUser
|
|
|
|
)
|
|
|
|
) || (
|
|
|
|
$type !== RC_LOG &&
|
|
|
|
!RevisionRecord::userCanBitfield( $deletedValue, RevisionRecord::SUPPRESSED_ALL, $contextUser )
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// If the RC is deleted, the user can't see it, and we're only showing matches,
|
2021-04-16 11:24:39 +00:00
|
|
|
// always skip this row. If ShowNegative is true, we can still show the row
|
2021-02-08 12:34:18 +00:00
|
|
|
// because we won't tell whether it matches the given filter.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 13:10:20 +00:00
|
|
|
$varGenerator = $this->varGeneratorFactory->newRCGenerator( $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
|
|
|
|
2021-09-01 11:53:38 +00:00
|
|
|
$ruleChecker->setVariables( $vars );
|
|
|
|
$result = $ruleChecker->checkConditions( $this->testPattern )->getResult();
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2021-04-16 11:24:39 +00:00
|
|
|
if ( $result || $formData['ShowNegative'] ) {
|
2009-01-30 00:54:20 +00:00
|
|
|
// Stash result in RC item
|
|
|
|
$rc->filterResult = $result;
|
2009-01-28 23:54:41 +00:00
|
|
|
$rc->counter = $counter++;
|
|
|
|
$output .= $changesList->recentChangesLine( $rc, false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= $changesList->endRecentChangesList();
|
|
|
|
|
2021-04-16 11:24:39 +00:00
|
|
|
$form->addPostHtml( $output );
|
|
|
|
|
|
|
|
return true;
|
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' );
|
2009-01-30 19:19:03 +00:00
|
|
|
|
2022-06-29 10:19:24 +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] )
|
|
|
|
) {
|
2023-02-26 12:51:08 +00:00
|
|
|
$dbr = $this->lbFactory->getReplicaDatabase();
|
2024-04-30 18:21:20 +00:00
|
|
|
$pattern = $dbr->newSelectQueryBuilder()
|
|
|
|
->select( 'af_pattern' )
|
|
|
|
->from( 'abuse_filter' )
|
|
|
|
->where( [ 'af_id' => intval( $this->mParams[1] ) ] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchField();
|
2022-06-29 10:19:24 +00:00
|
|
|
if ( $pattern !== false ) {
|
|
|
|
$this->testPattern = $pattern;
|
|
|
|
}
|
2009-01-30 19:19:03 +00:00
|
|
|
}
|
2009-01-28 23:54:41 +00:00
|
|
|
}
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|