Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Block users by blocked emails #141

Merged
merged 2 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/dependencies
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CategoryTree
Disambiguator
SpamBlacklist
Wikibase
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Versions and bullets are arranged chronologically from latest to oldest.

- Dropped SpecialOrderedWhatLinksHere because of the hardness of the maintaining. Please upvote https://phabricator.wikimedia.org/T4306 if you still want to the functionality.
- Dropped `$UnifiedExtensionForFemiwikiSoftDefaultOptions` configuration variable and the related feature.
- Added `$wgUnifiedExtensionForFemiwikiBlockByEmail` configuration variable (default is true) which provides various email blocking funtionalities.

## Previous Releases

Expand Down
11 changes: 10 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "UnifiedExtensionForFemiwiki",
"version": "3.0.1",
"version": "4.1.0",
"author": "[https://femiwiki.com/ Femiwiki Team]",
"url": "https://github.com/femiwiki/unifiedExtensionForFemiwiki",
"descriptionmsg": "unifiedextensionforfemiwiki-desc",
Expand Down Expand Up @@ -34,10 +34,15 @@
"DefaultOptions": {
"class": "MediaWiki\\Extension\\UnifiedExtensionForFemiwiki\\HookHandlers\\DefaultOptions",
"services": ["MainConfig", "UserOptionsManager"]
},
"SpamEmail": {
"class": "MediaWiki\\Extension\\UnifiedExtensionForFemiwiki\\HookHandlers\\SpamEmail",
"services": ["MainConfig", "DBLoadBalancer", "DatabaseBlockStore", "MainWANObjectCache"]
}
},
"Hooks": {
"HtmlPageLinkRendererBegin": "main",
"isValidEmailAddr": "SpamEmail",
"LinkerMakeExternalLink": "main",
"OutputPageParserOutput": "RelatedArticles",
"SidebarBeforeOutput": "main",
Expand All @@ -63,6 +68,10 @@
"UnifiedExtensionForFemiwikiRelatedArticlesTargetNamespaces": {
"value": [],
"description": "An array of namespaces which can be a target for RelatedArticles. Empty array means allowing all namespaces."
},
"UnifiedExtensionForFemiwikiBlockByEmail": {
"value": true,
"description": "Block emails used by block users"
}
},
"TestAutoloadNamespaces": {
Expand Down
99 changes: 99 additions & 0 deletions includes/HookHandlers/SpamEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace MediaWiki\Extension\UnifiedExtensionForFemiwiki\HookHandlers;

use Config;
use MediaWiki\Block\DatabaseBlockStore;
use MediaWiki\Extension\SpamBlacklist\BaseBlacklist;
use MediaWiki\User\User;
use WANObjectCache;
use Wikimedia\AtEase\AtEase;
use Wikimedia\Rdbms\ILoadBalancer;

class SpamEmail implements
\MediaWiki\Hook\IsValidEmailAddrHook
{

/** @var Config */
private $config;

/** @var ILoadBalancer */
private $loadBalancer;

/** @var DatabaseBlockStore */
private $databaseBlockStore;

/** @var WANObjectCache */
private $wanCache;

/**
* @param Config $config
* @param ILoadBalancer $loadBalancer
* @param DatabaseBlockStore $databaseBlockStore
* @param WANObjectCache $wanCache
*
*/
public function __construct(
Config $config,
ILoadBalancer $loadBalancer,
DatabaseBlockStore $databaseBlockStore,
WANObjectCache $wanCache
) {
$this->config = $config;
$this->loadBalancer = $loadBalancer;
$this->databaseBlockStore = $databaseBlockStore;
$this->wanCache = $wanCache;
}

/** @inheritDoc */
public function onIsValidEmailAddr( $addr, &$result ) {
if ( !$this->config->get( 'UnifiedExtensionForFemiwikiBlockByEmail' ) ) {
return true;
}

// Check againt MediaWiki:Email-blacklist
$denylist = BaseBlacklist::getEmailBlacklist()->getBlacklists();
foreach ( $denylist as $regex ) {
AtEase::suppressWarnings();
$match = preg_match( $regex, $addr );
AtEase::restoreWarnings();
if ( $match ) {
return false;
}
}

// Check email addresses of block users
if ( version_compare( '1.42', MW_VERSION, '<=' ) ) {
$emails = array_filter( array_unique( array_map(
static fn( $block ) => User::newFromIdentity( $block->getBlocker() )->getEmail()
$this->databaseBlockStore->newListFromConds( [ 'bt_user IS NOT NULL' ] ),
) ) );
} else {
$loadBalancer = $this->loadBalancer;
$emails = $this->wanCache->getWithSetCallback(
$this->wanCache->makeKey( 'unified-femiwiki-extension-blocked-email' ),
WANObjectCache::TTL_HOUR,
static function ( $old, &$ttl, array &$setOpts ) use ( $loadBalancer ) {
$dbr = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
return $dbr->newSelectQueryBuilder()
->distinct()
->fields( [ 'user_email' ] )
->tables( [ 'ipblocks' ] )
->join( 'user', 'user_id = ipb_user' )
->conds( [
'ipb_user IS NOT NULL',
'user_email != ' . $dbr->addQuotes( '' ),
'ipb_expiry > ' . $dbr->addQuotes( $dbr->timestamp() ),
] )
->fetchFieldValues();
}
);
if ( !is_array( $emails ) ) {
$emails = [];
}
}
if ( in_array( $addr, $emails ) ) {
return false;
}
}
}
Loading