mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-15 18:19:38 +00:00
5e609eb537
This is just a temporary location for these two methods. Since they're used a lot, having them in the AbuseFilter class means that the dependency graph is unnecessarily complicated. Thus, since these methods aren't doing much, they were moved to a dedicated class. Future todo is finding an appropriate location, that might be either as part of another service, or keep them in a Utilities class, perhaps a single class with all util methods, rather than a specific class. Change-Id: I52cc47a6b9a387cd1e68c5127f6598a4c43ca428
333 lines
9 KiB
PHP
333 lines
9 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter\View;
|
|
|
|
use AbuseFilter;
|
|
use AbuseFilterChangesList;
|
|
use AbuseFilterVariableHolder;
|
|
use ChangesList;
|
|
use HTMLForm;
|
|
use IContextSource;
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
|
|
use MediaWiki\Extension\AbuseFilter\EditBoxBuilderFactory;
|
|
use MediaWiki\Extension\AbuseFilter\FilterLookup;
|
|
use MediaWiki\Extension\AbuseFilter\GlobalNameUtils;
|
|
use MediaWiki\Extension\AbuseFilter\Pager\AbuseFilterExaminePager;
|
|
use MediaWiki\Extension\AbuseFilter\VariableGenerator\RCVariableGenerator;
|
|
use MediaWiki\Linker\LinkRenderer;
|
|
use MediaWiki\Revision\RevisionLookup;
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
use OOUI;
|
|
use RecentChange;
|
|
use SpecialAbuseLog;
|
|
use Title;
|
|
use Xml;
|
|
|
|
class AbuseFilterViewExamine extends AbuseFilterView {
|
|
/**
|
|
* @var string The user whose entries we're examinating
|
|
*/
|
|
public $mSearchUser;
|
|
/**
|
|
* @var string The start time of the search period
|
|
*/
|
|
public $mSearchPeriodStart;
|
|
/**
|
|
* @var string The end time of the search period
|
|
*/
|
|
public $mSearchPeriodEnd;
|
|
/**
|
|
* @var string The ID of the filter we're examinating
|
|
*/
|
|
public $mTestFilter;
|
|
/**
|
|
* @var RevisionLookup
|
|
*/
|
|
private $revisionLookup;
|
|
/**
|
|
* @var FilterLookup
|
|
*/
|
|
private $filterLookup;
|
|
/**
|
|
* @var EditBoxBuilderFactory
|
|
*/
|
|
private $boxBuilderFactory;
|
|
|
|
/**
|
|
* @param RevisionLookup $revisionLookup
|
|
* @param AbuseFilterPermissionManager $afPermManager
|
|
* @param FilterLookup $filterLookup
|
|
* @param EditBoxBuilderFactory $boxBuilderFactory
|
|
* @param IContextSource $context
|
|
* @param LinkRenderer $linkRenderer
|
|
* @param string $basePageName
|
|
* @param array $params
|
|
*/
|
|
public function __construct(
|
|
RevisionLookup $revisionLookup,
|
|
AbuseFilterPermissionManager $afPermManager,
|
|
FilterLookup $filterLookup,
|
|
EditBoxBuilderFactory $boxBuilderFactory,
|
|
IContextSource $context,
|
|
LinkRenderer $linkRenderer,
|
|
string $basePageName,
|
|
array $params
|
|
) {
|
|
parent::__construct( $afPermManager, $context, $linkRenderer, $basePageName, $params );
|
|
$this->revisionLookup = $revisionLookup;
|
|
$this->filterLookup = $filterLookup;
|
|
$this->boxBuilderFactory = $boxBuilderFactory;
|
|
}
|
|
|
|
/**
|
|
* Shows the page
|
|
*/
|
|
public function show() {
|
|
$out = $this->getOutput();
|
|
$out->setPageTitle( $this->msg( 'abusefilter-examine' ) );
|
|
$out->addHelpLink( 'Extension:AbuseFilter/Rules format' );
|
|
$out->addWikiMsg( 'abusefilter-examine-intro' );
|
|
|
|
$this->loadParameters();
|
|
|
|
// Check if we've got a subpage
|
|
if ( count( $this->mParams ) > 1 && is_numeric( $this->mParams[1] ) ) {
|
|
$this->showExaminerForRC( $this->mParams[1] );
|
|
} elseif ( count( $this->mParams ) > 2
|
|
&& $this->mParams[1] === 'log'
|
|
&& is_numeric( $this->mParams[2] )
|
|
) {
|
|
$this->showExaminerForLogEntry( $this->mParams[2] );
|
|
} else {
|
|
$this->showSearch();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Shows the search form
|
|
*/
|
|
public function showSearch() {
|
|
$RCMaxAge = $this->getConfig()->get( 'RCMaxAge' );
|
|
$min = wfTimestamp( TS_ISO_8601, time() - $RCMaxAge );
|
|
$max = wfTimestampNow();
|
|
$formDescriptor = [
|
|
'SearchUser' => [
|
|
'label-message' => 'abusefilter-test-user',
|
|
'type' => 'user',
|
|
'ipallowed' => true,
|
|
'default' => $this->mSearchUser,
|
|
],
|
|
'SearchPeriodStart' => [
|
|
'label-message' => 'abusefilter-test-period-start',
|
|
'type' => 'datetime',
|
|
'default' => $this->mSearchPeriodStart,
|
|
'min' => $min,
|
|
'max' => $max,
|
|
],
|
|
'SearchPeriodEnd' => [
|
|
'label-message' => 'abusefilter-test-period-end',
|
|
'type' => 'datetime',
|
|
'default' => $this->mSearchPeriodEnd,
|
|
'min' => $min,
|
|
'max' => $max,
|
|
],
|
|
];
|
|
$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
|
|
$htmlForm->setWrapperLegendMsg( 'abusefilter-examine-legend' )
|
|
->setSubmitTextMsg( 'abusefilter-examine-submit' )
|
|
->setFormIdentifier( 'examine-select-date' )
|
|
->setSubmitCallback( [ $this, 'showResults' ] )
|
|
->showAlways();
|
|
}
|
|
|
|
/**
|
|
* Show search results, called as submit callback by HTMLForm
|
|
* @param array $formData
|
|
* @param HTMLForm $form
|
|
* @return bool
|
|
*/
|
|
public function showResults( array $formData, HTMLForm $form ) : bool {
|
|
$changesList = new AbuseFilterChangesList( $this->getSkin(), $this->mTestFilter );
|
|
$pager = new AbuseFilterExaminePager( $this, $changesList );
|
|
|
|
$output = $changesList->beginRecentChangesList()
|
|
. $pager->getNavigationBar()
|
|
. $pager->getBody()
|
|
. $pager->getNavigationBar()
|
|
. $changesList->endRecentChangesList();
|
|
|
|
$form->addPostText( $output );
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param int $rcid
|
|
*/
|
|
public function showExaminerForRC( $rcid ) {
|
|
// Get data
|
|
$rc = RecentChange::newFromId( $rcid );
|
|
$out = $this->getOutput();
|
|
if ( !$rc ) {
|
|
$out->addWikiMsg( 'abusefilter-examine-notfound' );
|
|
return;
|
|
}
|
|
|
|
if ( !ChangesList::userCan( $rc, RevisionRecord::SUPPRESSED_ALL ) ) {
|
|
$out->addWikiMsg( 'abusefilter-log-details-hidden-implicit' );
|
|
return;
|
|
}
|
|
|
|
$vars = new AbuseFilterVariableHolder();
|
|
$varGenerator = new RCVariableGenerator( $vars, $rc, $this->getUser() );
|
|
$vars = $varGenerator->getVars();
|
|
$out->addJsConfigVars( [
|
|
'wgAbuseFilterVariables' => $vars ? $vars->dumpAllVars( true ) : [],
|
|
'abuseFilterExamine' => [ 'type' => 'rc', 'id' => $rcid ]
|
|
] );
|
|
|
|
$this->showExaminer( $vars );
|
|
}
|
|
|
|
/**
|
|
* @param int $logid
|
|
*/
|
|
public function showExaminerForLogEntry( $logid ) {
|
|
$aflFilterMigrationStage = $this->getConfig()->get( 'AbuseFilterAflFilterMigrationStage' );
|
|
// Get data
|
|
$dbr = wfGetDB( DB_REPLICA );
|
|
$user = $this->getUser();
|
|
$out = $this->getOutput();
|
|
|
|
$fields = [
|
|
'afl_deleted',
|
|
'afl_var_dump',
|
|
'afl_rev_id'
|
|
];
|
|
|
|
if ( $aflFilterMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
|
|
$fields[] = 'afl_filter_id';
|
|
$fields[] = 'afl_global';
|
|
} else {
|
|
// SCHEMA_COMPAT_READ_OLD
|
|
$fields[] = 'afl_filter';
|
|
}
|
|
|
|
$row = $dbr->selectRow(
|
|
'abuse_filter_log',
|
|
$fields,
|
|
[ 'afl_id' => $logid ],
|
|
__METHOD__
|
|
);
|
|
|
|
if ( !$row ) {
|
|
$out->addWikiMsg( 'abusefilter-examine-notfound' );
|
|
return;
|
|
}
|
|
|
|
if ( $aflFilterMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
|
|
$filterID = $row->afl_filter_id;
|
|
$isGlobal = $row->afl_global;
|
|
} else {
|
|
// SCHEMA_COMPAT_READ_OLD
|
|
[ $filterID, $isGlobal ] = GlobalNameUtils::splitGlobalName( $row->afl_filter );
|
|
}
|
|
$isHidden = $this->filterLookup->getFilter( $filterID, $isGlobal )->isHidden();
|
|
if ( !$this->afPermManager->canSeeLogDetailsForFilter( $user, $isHidden ) ) {
|
|
$out->addWikiMsg( 'abusefilter-log-cannot-see-details' );
|
|
return;
|
|
}
|
|
|
|
if ( $row->afl_deleted && !$this->afPermManager->canSeeHiddenLogEntries( $user ) ) {
|
|
$out->addWikiMsg( 'abusefilter-log-details-hidden' );
|
|
return;
|
|
}
|
|
|
|
if ( SpecialAbuseLog::isHidden( $row ) === 'implicit' ) {
|
|
$revRec = $this->revisionLookup->getRevisionById( (int)$row->afl_rev_id );
|
|
if ( !AbuseFilter::userCanViewRev( $revRec, $user ) ) {
|
|
$out->addWikiMsg( 'abusefilter-log-details-hidden-implicit' );
|
|
return;
|
|
}
|
|
}
|
|
$vars = AbuseFilter::loadVarDump( $row->afl_var_dump );
|
|
$out->addJsConfigVars( [
|
|
'wgAbuseFilterVariables' => $vars->dumpAllVars( true ),
|
|
'abuseFilterExamine' => [ 'type' => 'log', 'id' => $logid ]
|
|
] );
|
|
$this->showExaminer( $vars );
|
|
}
|
|
|
|
/**
|
|
* @param AbuseFilterVariableHolder|null $vars
|
|
*/
|
|
public function showExaminer( ?AbuseFilterVariableHolder $vars ) {
|
|
$output = $this->getOutput();
|
|
$output->enableOOUI();
|
|
|
|
if ( !$vars ) {
|
|
$output->addWikiMsg( 'abusefilter-examine-incompatible' );
|
|
return;
|
|
}
|
|
|
|
$vars = $vars->exportAllVars();
|
|
|
|
$html = '';
|
|
|
|
$output->addModules( 'ext.abuseFilter.examine' );
|
|
|
|
// Add test bit
|
|
if ( $this->afPermManager->canViewPrivateFilters( $this->getUser() ) ) {
|
|
$boxBuilder = $this->boxBuilderFactory->newEditBoxBuilder(
|
|
$this,
|
|
$this->getUser(),
|
|
$output
|
|
);
|
|
|
|
$tester = Xml::tags( 'h2', null, $this->msg( 'abusefilter-examine-test' )->parse() );
|
|
$tester .= $boxBuilder->buildEditBox( $this->mTestFilter, false, false, false );
|
|
$tester .= $this->buildFilterLoader();
|
|
$html .= Xml::tags( 'div', [ 'id' => 'mw-abusefilter-examine-editor' ], $tester );
|
|
$html .= Xml::tags( 'p',
|
|
null,
|
|
new OOUI\ButtonInputWidget(
|
|
[
|
|
'label' => $this->msg( 'abusefilter-examine-test-button' )->text(),
|
|
'id' => 'mw-abusefilter-examine-test'
|
|
]
|
|
) .
|
|
Xml::element( 'div',
|
|
[
|
|
'id' => 'mw-abusefilter-syntaxresult',
|
|
'style' => 'display: none;'
|
|
], ' '
|
|
)
|
|
);
|
|
}
|
|
|
|
// Variable dump
|
|
$html .= Xml::tags(
|
|
'h2',
|
|
null,
|
|
$this->msg( 'abusefilter-examine-vars' )->parse()
|
|
);
|
|
$html .= AbuseFilter::buildVarDumpTable( $vars, $this->getContext() );
|
|
|
|
$output->addHTML( $html );
|
|
}
|
|
|
|
/**
|
|
* Loads parameters from request
|
|
*/
|
|
public function loadParameters() {
|
|
$request = $this->getRequest();
|
|
$this->mSearchPeriodStart = $request->getText( 'wpSearchPeriodStart' );
|
|
$this->mSearchPeriodEnd = $request->getText( 'wpSearchPeriodEnd' );
|
|
$this->mTestFilter = $request->getText( 'testfilter' );
|
|
|
|
// Normalise username
|
|
$searchUsername = $request->getText( 'wpSearchUser' );
|
|
$userTitle = Title::newFromText( $searchUsername, NS_USER );
|
|
$this->mSearchUser = $userTitle ? $userTitle->getText() : '';
|
|
}
|
|
}
|