mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-25 06:26:03 +00:00
4720c97530
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its purpose should be to hold static functions intended as generic utility functions (e.g. to format messages, determine whether a filter is global etc.), but we actually use it for all methods related to running filters. This patch creates a new class, AbuseFilterRunner, containing all such methods, which have been made non-static. This leads to several improvements (also for related methods and the parser), and opens the way to further improve the code. Aside from making the code prettier, less global and easier to test, this patch could also produce a performance improvement, although I don't have tools to measure that. Also note that many public methods have been removed, and almost any of them has been made protected; a couple of them (the ones used from outside) are left for back-compat, and will be removed in the future. Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
64 lines
1.9 KiB
PHP
64 lines
1.9 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Auth\AbstractPreAuthenticationProvider;
|
|
use MediaWiki\Auth\AuthenticationRequest;
|
|
|
|
class AbuseFilterPreAuthenticationProvider extends AbstractPreAuthenticationProvider {
|
|
/**
|
|
* @param User $user
|
|
* @param User $creator
|
|
* @param AuthenticationRequest[] $reqs
|
|
* @return StatusValue
|
|
*/
|
|
public function testForAccountCreation( $user, $creator, array $reqs ) {
|
|
return $this->testUser( $user, $creator, false );
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
* @param bool|string $autocreate
|
|
* @param array $options
|
|
* @return StatusValue
|
|
*/
|
|
public function testUserForCreation( $user, $autocreate, array $options = [] ) {
|
|
// if this is not an autocreation, testForAccountCreation already handled it
|
|
if ( $autocreate ) {
|
|
return $this->testUser( $user, $user, true );
|
|
}
|
|
return StatusValue::newGood();
|
|
}
|
|
|
|
/**
|
|
* @param User $user The user being created or autocreated
|
|
* @param User $creator The user who caused $user to be created (or $user itself on autocreation)
|
|
* @param bool $autocreate Is this an autocreation?
|
|
* @return StatusValue
|
|
*/
|
|
protected function testUser( $user, $creator, $autocreate ) {
|
|
if ( $user->getName() === wfMessage( 'abusefilter-blocker' )->inContentLanguage()->text() ) {
|
|
return StatusValue::newFatal( 'abusefilter-accountreserved' );
|
|
}
|
|
|
|
$vars = new AbuseFilterVariableHolder;
|
|
|
|
// generateUserVars records $creator->getName() which would be the IP for unregistered users
|
|
if ( $creator->isLoggedIn() ) {
|
|
$vars->addHolders( AbuseFilter::generateUserVars( $creator ) );
|
|
}
|
|
|
|
$vars->setVar( 'action', $autocreate ? 'autocreateaccount' : 'createaccount' );
|
|
$vars->setVar( 'accountname', $user->getName() );
|
|
|
|
// pass creator in explicitly to prevent recording the current user on autocreation - T135360
|
|
$runner = new AbuseFilterRunner(
|
|
$creator,
|
|
SpecialPage::getTitleFor( 'Userlogin' ),
|
|
$vars,
|
|
'default'
|
|
);
|
|
$status = $runner->run();
|
|
|
|
return $status->getStatusValue();
|
|
}
|
|
}
|