2020-09-20 11:30:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
2018-03-08 21:16:25 +00:00
|
|
|
use ActorMigrationBase;
|
2020-09-20 11:30:59 +00:00
|
|
|
use DBAccessObjectUtils;
|
|
|
|
use IDBAccessObject;
|
2021-01-07 23:42:58 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\ClosestFilterVersionNotFoundException;
|
2021-01-07 20:10:52 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\ExistingFilter;
|
2020-09-20 11:30:59 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\FilterNotFoundException;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\FilterVersionNotFoundException;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\Flags;
|
2021-01-07 20:10:52 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\HistoryFilter;
|
2020-09-20 11:30:59 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\LastEditInfo;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\Specs;
|
|
|
|
use stdClass;
|
|
|
|
use WANObjectCache;
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides read access to the filters stored in the database.
|
2022-09-29 16:54:36 +00:00
|
|
|
*
|
2021-01-07 23:42:58 +00:00
|
|
|
* @todo Cache exceptions
|
2020-09-20 11:30:59 +00:00
|
|
|
*/
|
|
|
|
class FilterLookup implements IDBAccessObject {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterFilterLookup';
|
|
|
|
|
2021-01-07 23:42:58 +00:00
|
|
|
// Used in getClosestVersion
|
|
|
|
public const DIR_PREV = 'prev';
|
|
|
|
public const DIR_NEXT = 'next';
|
|
|
|
|
2020-09-20 11:30:59 +00:00
|
|
|
/**
|
2021-01-07 20:10:52 +00:00
|
|
|
* @var ExistingFilter[] Individual filters cache. Keys can be integer IDs, or global names
|
2020-09-20 11:30:59 +00:00
|
|
|
*/
|
|
|
|
private $cache = [];
|
|
|
|
|
|
|
|
/**
|
2021-01-07 20:10:52 +00:00
|
|
|
* @var ExistingFilter[][][] Cache of all active filters in each group. This is not related to
|
2020-09-20 11:30:59 +00:00
|
|
|
* the individual cache, and is replicated in WAN cache. The structure is
|
|
|
|
* [ local|global => [ group => [ ID => filter ] ] ]
|
|
|
|
* where the cache for each group has the same format as $this->cache
|
|
|
|
* Note that the keys are also in the form 'global-ID' for filters in 'global', although redundant.
|
|
|
|
*/
|
|
|
|
private $groupCache = [ 'local' => [], 'global' => [] ];
|
|
|
|
|
2021-01-07 20:10:52 +00:00
|
|
|
/** @var HistoryFilter[] */
|
2020-09-20 11:30:59 +00:00
|
|
|
private $historyCache = [];
|
|
|
|
|
2021-01-04 13:46:34 +00:00
|
|
|
/** @var int[] */
|
|
|
|
private $firstVersionCache = [];
|
|
|
|
|
2021-01-07 23:42:58 +00:00
|
|
|
/** @var int[] */
|
|
|
|
private $lastVersionCache = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int[][] [ filter => [ historyID => [ prev, next ] ] ]
|
|
|
|
* @phan-var array<int,array<int,array{prev?:int,next?:int}>>
|
|
|
|
*/
|
|
|
|
private $closestVersionsCache = [];
|
|
|
|
|
2020-09-20 11:30:59 +00:00
|
|
|
/** @var ILoadBalancer */
|
|
|
|
private $loadBalancer;
|
|
|
|
|
|
|
|
/** @var WANObjectCache */
|
|
|
|
private $wanCache;
|
|
|
|
|
|
|
|
/** @var CentralDBManager */
|
|
|
|
private $centralDBManager;
|
|
|
|
|
2018-03-08 21:16:25 +00:00
|
|
|
/** @var ActorMigrationBase */
|
|
|
|
private $actorMigration;
|
|
|
|
|
2020-09-20 11:30:59 +00:00
|
|
|
/**
|
|
|
|
* @param ILoadBalancer $loadBalancer
|
|
|
|
* @param WANObjectCache $cache
|
|
|
|
* @param CentralDBManager $centralDBManager
|
2018-03-08 21:16:25 +00:00
|
|
|
* @param ActorMigrationBase $actorMigration
|
2020-09-20 11:30:59 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ILoadBalancer $loadBalancer,
|
|
|
|
WANObjectCache $cache,
|
2018-03-08 21:16:25 +00:00
|
|
|
CentralDBManager $centralDBManager,
|
|
|
|
ActorMigrationBase $actorMigration
|
2020-09-20 11:30:59 +00:00
|
|
|
) {
|
|
|
|
$this->loadBalancer = $loadBalancer;
|
|
|
|
$this->wanCache = $cache;
|
|
|
|
$this->centralDBManager = $centralDBManager;
|
2018-03-08 21:16:25 +00:00
|
|
|
$this->actorMigration = $actorMigration;
|
2020-09-20 11:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $filterID
|
|
|
|
* @param bool $global
|
|
|
|
* @param int $flags One of the self::READ_* constants
|
2021-01-07 20:10:52 +00:00
|
|
|
* @return ExistingFilter
|
2020-09-20 11:30:59 +00:00
|
|
|
* @throws FilterNotFoundException if the filter doesn't exist
|
|
|
|
* @throws CentralDBNotAvailableException
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getFilter( int $filterID, bool $global, int $flags = self::READ_NORMAL ): ExistingFilter {
|
2020-09-20 11:30:59 +00:00
|
|
|
$cacheKey = $this->getCacheKey( $filterID, $global );
|
|
|
|
if ( $flags !== self::READ_NORMAL || !isset( $this->cache[$cacheKey] ) ) {
|
|
|
|
[ $dbIndex, $dbOptions ] = DBAccessObjectUtils::getDBOptions( $flags );
|
|
|
|
$dbr = $this->getDBConnection( $dbIndex, $global );
|
2018-03-08 21:16:25 +00:00
|
|
|
$query = $this->getAbuseFilterQueryInfo();
|
2020-09-20 11:30:59 +00:00
|
|
|
|
|
|
|
$row = $dbr->selectRow(
|
2018-03-08 21:16:25 +00:00
|
|
|
$query['tables'],
|
|
|
|
$query['fields'],
|
2020-09-20 11:30:59 +00:00
|
|
|
[ 'af_id' => $filterID ],
|
|
|
|
__METHOD__,
|
2018-03-08 21:16:25 +00:00
|
|
|
$dbOptions,
|
|
|
|
$query['joins']
|
2020-09-20 11:30:59 +00:00
|
|
|
);
|
|
|
|
if ( !$row ) {
|
|
|
|
throw new FilterNotFoundException( $filterID, $global );
|
|
|
|
}
|
|
|
|
$fname = __METHOD__;
|
2021-07-21 18:51:12 +00:00
|
|
|
$getActionsCB = function () use ( $dbr, $fname, $row ): array {
|
2020-09-20 11:30:59 +00:00
|
|
|
return $this->getActionsFromDB( $dbr, $fname, $row->af_id );
|
|
|
|
};
|
|
|
|
$this->cache[$cacheKey] = $this->filterFromRow( $row, $getActionsCB );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->cache[$cacheKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all filters that are active (and not deleted) and in the given group
|
|
|
|
* @param string $group
|
|
|
|
* @param bool $global
|
|
|
|
* @param int $flags
|
2021-01-07 20:10:52 +00:00
|
|
|
* @return ExistingFilter[]
|
2020-09-20 11:30:59 +00:00
|
|
|
* @throws CentralDBNotAvailableException
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getAllActiveFiltersInGroup( string $group, bool $global, int $flags = self::READ_NORMAL ): array {
|
2020-09-20 11:30:59 +00:00
|
|
|
$domainKey = $global ? 'global' : 'local';
|
|
|
|
if ( $flags !== self::READ_NORMAL || !isset( $this->groupCache[$domainKey][$group] ) ) {
|
|
|
|
if ( $global ) {
|
|
|
|
$globalRulesKey = $this->getGlobalRulesKey( $group );
|
|
|
|
$ret = $this->wanCache->getWithSetCallback(
|
|
|
|
$globalRulesKey,
|
|
|
|
WANObjectCache::TTL_WEEK,
|
|
|
|
function () use ( $group, $global, $flags ) {
|
|
|
|
return $this->getAllActiveFiltersInGroupFromDB( $group, $global, $flags );
|
|
|
|
},
|
|
|
|
[
|
|
|
|
'checkKeys' => [ $globalRulesKey ],
|
|
|
|
'lockTSE' => 300,
|
2021-01-17 22:55:11 +00:00
|
|
|
'version' => 3
|
2020-09-20 11:30:59 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$ret = $this->getAllActiveFiltersInGroupFromDB( $group, $global, $flags );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->groupCache[$domainKey][$group] = [];
|
|
|
|
foreach ( $ret as $key => $filter ) {
|
|
|
|
$this->groupCache[$domainKey][$group][$key] = $filter;
|
|
|
|
$this->cache[$key] = $filter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->groupCache[$domainKey][$group];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $group
|
|
|
|
* @param bool $global
|
|
|
|
* @param int $flags
|
2021-01-07 20:10:52 +00:00
|
|
|
* @return ExistingFilter[]
|
2020-09-20 11:30:59 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function getAllActiveFiltersInGroupFromDB( string $group, bool $global, int $flags ): array {
|
2020-09-20 11:30:59 +00:00
|
|
|
[ $dbIndex, $dbOptions ] = DBAccessObjectUtils::getDBOptions( $flags );
|
|
|
|
$dbr = $this->getDBConnection( $dbIndex, $global );
|
|
|
|
|
2018-03-08 21:16:25 +00:00
|
|
|
$query = $this->getAbuseFilterQueryInfo();
|
2020-09-20 11:30:59 +00:00
|
|
|
$where = [
|
|
|
|
'af_enabled' => 1,
|
|
|
|
'af_deleted' => 0,
|
|
|
|
'af_group' => $group,
|
|
|
|
];
|
|
|
|
if ( $global ) {
|
|
|
|
$where['af_global'] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note, excluding individually cached filter now wouldn't help much, so take it as
|
|
|
|
// an occasion to refresh the cache later
|
|
|
|
$rows = $dbr->select(
|
2018-03-08 21:16:25 +00:00
|
|
|
$query['tables'],
|
|
|
|
$query['fields'],
|
2020-09-20 11:30:59 +00:00
|
|
|
$where,
|
|
|
|
__METHOD__,
|
2018-03-08 21:16:25 +00:00
|
|
|
$dbOptions,
|
|
|
|
$query['joins']
|
2020-09-20 11:30:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$fname = __METHOD__;
|
|
|
|
$ret = [];
|
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
$filterKey = $this->getCacheKey( $row->af_id, $global );
|
2021-07-21 18:51:12 +00:00
|
|
|
$getActionsCB = function () use ( $dbr, $fname, $row ): array {
|
2020-09-20 11:30:59 +00:00
|
|
|
return $this->getActionsFromDB( $dbr, $fname, $row->af_id );
|
|
|
|
};
|
|
|
|
$ret[$filterKey] = $this->filterFromRow(
|
|
|
|
$row,
|
|
|
|
// Don't pass a closure if global, as this is going to be serialized when caching
|
|
|
|
$global ? $getActionsCB() : $getActionsCB
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $dbIndex
|
|
|
|
* @param bool $global
|
|
|
|
* @return IDatabase
|
|
|
|
* @throws CentralDBNotAvailableException
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function getDBConnection( int $dbIndex, bool $global ): IDatabase {
|
2020-09-20 11:30:59 +00:00
|
|
|
if ( $global ) {
|
|
|
|
return $this->centralDBManager->getConnection( $dbIndex );
|
|
|
|
} else {
|
|
|
|
return $this->loadBalancer->getConnectionRef( $dbIndex );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IDatabase $db
|
|
|
|
* @param string $fname
|
|
|
|
* @param int $id
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function getActionsFromDB( IDatabase $db, string $fname, int $id ): array {
|
2020-09-20 11:30:59 +00:00
|
|
|
$res = $db->select(
|
|
|
|
'abuse_filter_action',
|
|
|
|
[ 'afa_consequence', 'afa_parameters' ],
|
|
|
|
[ 'afa_filter' => $id ],
|
|
|
|
$fname
|
|
|
|
);
|
|
|
|
|
|
|
|
$actions = [];
|
|
|
|
foreach ( $res as $actionRow ) {
|
2022-05-22 12:47:34 +00:00
|
|
|
$actions[$actionRow->afa_consequence] = $actionRow->afa_parameters !== ''
|
|
|
|
? explode( "\n", $actionRow->afa_parameters )
|
|
|
|
: [];
|
2020-09-20 11:30:59 +00:00
|
|
|
}
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an old version of the given (local) filter, with its actions
|
|
|
|
*
|
|
|
|
* @param int $version Unique identifier of the version
|
|
|
|
* @param int $flags
|
2021-01-07 20:10:52 +00:00
|
|
|
* @return HistoryFilter
|
2020-09-20 11:30:59 +00:00
|
|
|
* @throws FilterVersionNotFoundException if the version doesn't exist
|
|
|
|
*/
|
|
|
|
public function getFilterVersion(
|
|
|
|
int $version,
|
|
|
|
int $flags = self::READ_NORMAL
|
2021-07-21 18:51:12 +00:00
|
|
|
): HistoryFilter {
|
2020-09-20 11:30:59 +00:00
|
|
|
if ( $flags !== self::READ_NORMAL || !isset( $this->historyCache[$version] ) ) {
|
|
|
|
[ $dbIndex, $dbOptions ] = DBAccessObjectUtils::getDBOptions( $flags );
|
|
|
|
$dbr = $this->loadBalancer->getConnectionRef( $dbIndex );
|
2018-03-08 21:16:25 +00:00
|
|
|
$query = $this->getAbuseFilterHistoryQueryInfo();
|
2020-09-20 11:30:59 +00:00
|
|
|
|
|
|
|
$row = $dbr->selectRow(
|
2018-03-08 21:16:25 +00:00
|
|
|
$query['tables'],
|
|
|
|
$query['fields'],
|
2020-09-20 11:30:59 +00:00
|
|
|
[ 'afh_id' => $version ],
|
|
|
|
__METHOD__,
|
2018-03-08 21:16:25 +00:00
|
|
|
$dbOptions,
|
|
|
|
$query['joins']
|
2020-09-20 11:30:59 +00:00
|
|
|
);
|
|
|
|
if ( !$row ) {
|
|
|
|
throw new FilterVersionNotFoundException( $version );
|
|
|
|
}
|
2021-01-07 20:10:52 +00:00
|
|
|
$this->historyCache[$version] = $this->filterFromHistoryRow( $row );
|
2020-09-20 11:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->historyCache[$version];
|
|
|
|
}
|
|
|
|
|
2021-01-07 23:42:58 +00:00
|
|
|
/**
|
|
|
|
* @param int $filterID
|
|
|
|
* @return HistoryFilter
|
|
|
|
* @throws FilterNotFoundException If the filter doesn't exist
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getLastHistoryVersion( int $filterID ): HistoryFilter {
|
2021-01-07 23:42:58 +00:00
|
|
|
if ( !isset( $this->lastVersionCache[$filterID] ) ) {
|
|
|
|
$dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
|
2018-03-08 21:16:25 +00:00
|
|
|
$query = $this->getAbuseFilterHistoryQueryInfo();
|
2021-01-07 23:42:58 +00:00
|
|
|
$row = $dbr->selectRow(
|
2018-03-08 21:16:25 +00:00
|
|
|
$query['tables'],
|
|
|
|
$query['fields'],
|
2021-01-07 23:42:58 +00:00
|
|
|
[ 'afh_filter' => $filterID ],
|
|
|
|
__METHOD__,
|
2018-03-08 21:16:25 +00:00
|
|
|
[ 'ORDER BY' => 'afh_id DESC' ],
|
|
|
|
$query['joins']
|
2021-01-07 23:42:58 +00:00
|
|
|
);
|
|
|
|
if ( !$row ) {
|
|
|
|
throw new FilterNotFoundException( $filterID, false );
|
|
|
|
}
|
|
|
|
$filterObj = $this->filterFromHistoryRow( $row );
|
|
|
|
$this->lastVersionCache[$filterID] = $filterObj->getHistoryID();
|
|
|
|
$this->historyCache[$filterObj->getHistoryID()] = $filterObj;
|
|
|
|
}
|
|
|
|
return $this->historyCache[ $this->lastVersionCache[$filterID] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $historyID
|
|
|
|
* @param int $filterID
|
|
|
|
* @param string $direction self::DIR_PREV or self::DIR_NEXT
|
|
|
|
* @return HistoryFilter
|
|
|
|
* @throws ClosestFilterVersionNotFoundException
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getClosestVersion( int $historyID, int $filterID, string $direction ): HistoryFilter {
|
2021-01-07 23:42:58 +00:00
|
|
|
if ( !isset( $this->closestVersionsCache[$filterID][$historyID][$direction] ) ) {
|
|
|
|
$comparison = $direction === self::DIR_PREV ? '<' : '>';
|
|
|
|
$order = $direction === self::DIR_PREV ? 'DESC' : 'ASC';
|
|
|
|
$dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
|
2018-03-08 21:16:25 +00:00
|
|
|
$query = $this->getAbuseFilterHistoryQueryInfo();
|
2021-01-07 23:42:58 +00:00
|
|
|
$row = $dbr->selectRow(
|
2018-03-08 21:16:25 +00:00
|
|
|
$query['tables'],
|
|
|
|
$query['fields'],
|
2021-01-07 23:42:58 +00:00
|
|
|
[
|
|
|
|
'afh_filter' => $filterID,
|
|
|
|
"afh_id $comparison" . $dbr->addQuotes( $historyID ),
|
|
|
|
],
|
|
|
|
__METHOD__,
|
2018-03-08 21:16:25 +00:00
|
|
|
[ 'ORDER BY' => "afh_timestamp $order" ],
|
|
|
|
$query['joins']
|
2021-01-07 23:42:58 +00:00
|
|
|
);
|
|
|
|
if ( !$row ) {
|
|
|
|
throw new ClosestFilterVersionNotFoundException( $filterID, $historyID );
|
|
|
|
}
|
|
|
|
$filterObj = $this->filterFromHistoryRow( $row );
|
|
|
|
$this->closestVersionsCache[$filterID][$historyID][$direction] = $filterObj->getHistoryID();
|
|
|
|
$this->historyCache[$filterObj->getHistoryID()] = $filterObj;
|
|
|
|
}
|
|
|
|
$histID = $this->closestVersionsCache[$filterID][$historyID][$direction];
|
|
|
|
return $this->historyCache[$histID];
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:46:34 +00:00
|
|
|
/**
|
|
|
|
* Get the history ID of the first change to a given filter
|
|
|
|
*
|
|
|
|
* @param int $filterID
|
|
|
|
* @return int
|
2021-01-07 23:42:58 +00:00
|
|
|
* @throws FilterNotFoundException
|
2021-01-04 13:46:34 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function getFirstFilterVersionID( int $filterID ): int {
|
2021-01-04 13:46:34 +00:00
|
|
|
if ( !isset( $this->firstVersionCache[$filterID] ) ) {
|
|
|
|
$dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
|
2021-01-07 23:42:58 +00:00
|
|
|
$historyID = $dbr->selectField(
|
2021-01-04 13:46:34 +00:00
|
|
|
'abuse_filter_history',
|
|
|
|
'MIN(afh_id)',
|
|
|
|
[ 'afh_filter' => $filterID ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
2021-01-07 23:42:58 +00:00
|
|
|
if ( $historyID === false ) {
|
|
|
|
throw new FilterNotFoundException( $filterID, false );
|
|
|
|
}
|
|
|
|
$this->firstVersionCache[$filterID] = (int)$historyID;
|
2021-01-04 13:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->firstVersionCache[$filterID];
|
|
|
|
}
|
|
|
|
|
2020-09-20 11:30:59 +00:00
|
|
|
/**
|
|
|
|
* Resets the internal cache of Filter objects
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function clearLocalCache(): void {
|
2020-09-20 11:30:59 +00:00
|
|
|
$this->cache = [];
|
|
|
|
$this->groupCache = [ 'local' => [], 'global' => [] ];
|
|
|
|
$this->historyCache = [];
|
2021-01-07 23:42:58 +00:00
|
|
|
$this->firstVersionCache = [];
|
|
|
|
$this->lastVersionCache = [];
|
|
|
|
$this->closestVersionsCache = [];
|
2020-09-20 11:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purge the shared cache of global filters in the given group.
|
|
|
|
* @note This doesn't purge the local cache
|
|
|
|
* @param string $group
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function purgeGroupWANCache( string $group ): void {
|
2020-09-20 11:30:59 +00:00
|
|
|
$this->wanCache->touchCheckKey( $this->getGlobalRulesKey( $group ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function getGlobalRulesKey( string $group ): string {
|
2020-09-20 11:30:59 +00:00
|
|
|
if ( !$this->centralDBManager->filterIsCentral() ) {
|
|
|
|
return $this->wanCache->makeGlobalKey(
|
|
|
|
'abusefilter',
|
|
|
|
'rules',
|
|
|
|
$this->centralDBManager->getCentralDBName(),
|
|
|
|
$group
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->wanCache->makeKey( 'abusefilter', 'rules', $group );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-07 20:10:52 +00:00
|
|
|
* Note: this is private because no external caller should access DB rows directly.
|
2020-09-20 11:30:59 +00:00
|
|
|
* @param stdClass $row
|
2021-01-07 20:10:52 +00:00
|
|
|
* @return HistoryFilter
|
2020-09-20 11:30:59 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function filterFromHistoryRow( stdClass $row ): HistoryFilter {
|
2020-09-20 11:30:59 +00:00
|
|
|
$actionsRaw = unserialize( $row->afh_actions );
|
2021-01-07 20:10:52 +00:00
|
|
|
$actions = is_array( $actionsRaw ) ? $actionsRaw : [];
|
|
|
|
$flags = $row->afh_flags ? explode( ',', $row->afh_flags ) : [];
|
|
|
|
return new HistoryFilter(
|
|
|
|
new Specs(
|
|
|
|
trim( $row->afh_pattern ),
|
|
|
|
$row->afh_comments,
|
|
|
|
// FIXME: Make the DB field NOT NULL (T263324)
|
|
|
|
(string)$row->afh_public_comments,
|
|
|
|
array_keys( $actions ),
|
|
|
|
// FIXME Make the field NOT NULL and add default (T263324)
|
|
|
|
$row->afh_group ?? 'default'
|
|
|
|
),
|
|
|
|
new Flags(
|
|
|
|
in_array( 'enabled', $flags, true ),
|
|
|
|
in_array( 'deleted', $flags, true ),
|
|
|
|
in_array( 'hidden', $flags, true ),
|
|
|
|
in_array( 'global', $flags, true )
|
|
|
|
),
|
|
|
|
$actions,
|
|
|
|
new LastEditInfo(
|
|
|
|
(int)$row->afh_user,
|
|
|
|
$row->afh_user_text,
|
|
|
|
$row->afh_timestamp
|
|
|
|
),
|
|
|
|
(int)$row->afh_filter,
|
|
|
|
$row->afh_id
|
|
|
|
);
|
2020-09-20 11:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note: this is private because no external caller should access DB rows directly.
|
|
|
|
* @param stdClass $row
|
|
|
|
* @param array[]|callable $actions
|
2021-01-07 20:10:52 +00:00
|
|
|
* @return ExistingFilter
|
2020-09-20 11:30:59 +00:00
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function filterFromRow( stdClass $row, $actions ): ExistingFilter {
|
2021-01-07 20:10:52 +00:00
|
|
|
return new ExistingFilter(
|
2020-09-20 11:30:59 +00:00
|
|
|
new Specs(
|
|
|
|
trim( $row->af_pattern ),
|
|
|
|
// FIXME: Make the DB fields for these NOT NULL (T263324)
|
|
|
|
(string)$row->af_comments,
|
|
|
|
(string)$row->af_public_comments,
|
|
|
|
$row->af_actions !== '' ? explode( ',', $row->af_actions ) : [],
|
|
|
|
$row->af_group
|
|
|
|
),
|
|
|
|
new Flags(
|
|
|
|
(bool)$row->af_enabled,
|
|
|
|
(bool)$row->af_deleted,
|
|
|
|
(bool)$row->af_hidden,
|
|
|
|
(bool)$row->af_global
|
|
|
|
),
|
|
|
|
$actions,
|
|
|
|
new LastEditInfo(
|
|
|
|
(int)$row->af_user,
|
|
|
|
$row->af_user_text,
|
|
|
|
$row->af_timestamp
|
|
|
|
),
|
|
|
|
(int)$row->af_id,
|
|
|
|
isset( $row->af_hit_count ) ? (int)$row->af_hit_count : null,
|
|
|
|
isset( $row->af_throttled ) ? (bool)$row->af_throttled : null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-08 21:16:25 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getAbuseFilterQueryInfo(): array {
|
|
|
|
$actorQuery = $this->actorMigration->getJoin( 'af_user' );
|
|
|
|
return [
|
|
|
|
'tables' => [ 'abuse_filter' ] + $actorQuery['tables'],
|
|
|
|
'fields' => [
|
|
|
|
'af_id',
|
|
|
|
'af_pattern',
|
|
|
|
'af_timestamp',
|
|
|
|
'af_enabled',
|
|
|
|
'af_comments',
|
|
|
|
'af_public_comments',
|
|
|
|
'af_hidden',
|
|
|
|
'af_hit_count',
|
|
|
|
'af_throttled',
|
|
|
|
'af_deleted',
|
|
|
|
'af_actions',
|
|
|
|
'af_global',
|
|
|
|
'af_group'
|
|
|
|
] + $actorQuery['fields'],
|
|
|
|
'joins' => $actorQuery['joins']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getAbuseFilterHistoryQueryInfo(): array {
|
|
|
|
$actorQuery = $this->actorMigration->getJoin( 'afh_user' );
|
|
|
|
return [
|
|
|
|
'tables' => [ 'abuse_filter_history' ] + $actorQuery['tables'],
|
|
|
|
'fields' => [
|
|
|
|
'afh_id',
|
|
|
|
'afh_pattern',
|
|
|
|
'afh_timestamp',
|
|
|
|
'afh_filter',
|
|
|
|
'afh_comments',
|
|
|
|
'afh_public_comments',
|
|
|
|
'afh_flags',
|
|
|
|
'afh_actions',
|
|
|
|
'afh_group'
|
|
|
|
] + $actorQuery['fields'],
|
|
|
|
'joins' => $actorQuery['joins']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-09-20 11:30:59 +00:00
|
|
|
/**
|
|
|
|
* @param int $filterID
|
|
|
|
* @param bool $global
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function getCacheKey( int $filterID, bool $global ): string {
|
2020-12-02 22:47:40 +00:00
|
|
|
return GlobalNameUtils::buildGlobalName( $filterID, $global );
|
2020-09-20 11:30:59 +00:00
|
|
|
}
|
|
|
|
}
|