2020-09-08 13:22:01 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-02 13:49:41 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Maintenance;
|
|
|
|
|
2024-08-22 21:01:16 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2024-10-19 19:55:40 +00:00
|
|
|
use MediaWiki\Maintenance\Maintenance;
|
2024-08-22 21:01:16 +00:00
|
|
|
use Wikimedia\Rdbms\IExpression;
|
|
|
|
use Wikimedia\Rdbms\LikeValue;
|
2021-01-02 13:49:41 +00:00
|
|
|
|
2021-01-17 11:54:43 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2022-09-29 16:54:36 +00:00
|
|
|
$IP = getenv( 'MW_INSTALL_PATH' );
|
|
|
|
if ( $IP === false ) {
|
2020-09-08 13:22:01 +00:00
|
|
|
$IP = __DIR__ . '/../../..';
|
|
|
|
}
|
|
|
|
require_once "$IP/maintenance/Maintenance.php";
|
2021-01-17 11:54:43 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-09-08 13:22:01 +00:00
|
|
|
|
|
|
|
class SearchFilters extends Maintenance {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2024-08-22 21:01:16 +00:00
|
|
|
$this->addDescription(
|
2024-11-25 13:12:20 +00:00
|
|
|
'Find all filters matching a regular expression pattern and/or that have a given ' .
|
|
|
|
'consequence and/or privacy level'
|
2024-08-22 21:01:16 +00:00
|
|
|
);
|
|
|
|
$this->addOption( 'pattern', 'Regular expression pattern', false, true );
|
|
|
|
$this->addOption( 'consequence', 'The consequence that the filter should have', false, true );
|
2024-11-25 13:12:20 +00:00
|
|
|
$this->addOption(
|
|
|
|
'privacy',
|
|
|
|
'The privacy level that the filter should include (a constant from Flags)',
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
2020-09-08 13:22:01 +00:00
|
|
|
|
|
|
|
$this->requireExtension( 'Abuse Filter' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Maintenance:execute()
|
|
|
|
*/
|
|
|
|
public function execute() {
|
2024-08-22 21:01:16 +00:00
|
|
|
global $wgConf;
|
2020-09-08 13:22:01 +00:00
|
|
|
|
2024-08-22 21:01:16 +00:00
|
|
|
if ( $this->getConfig()->get( MainConfigNames::DBtype ) !== 'mysql' ) {
|
2020-09-08 13:22:01 +00:00
|
|
|
$this->fatalError( 'This maintenance script only works with MySQL databases' );
|
|
|
|
}
|
|
|
|
|
2024-11-25 13:12:20 +00:00
|
|
|
if (
|
|
|
|
!$this->getOption( 'pattern' ) &&
|
|
|
|
!$this->getOption( 'consequence' ) &&
|
|
|
|
$this->getOption( 'privacy' ) === null
|
|
|
|
) {
|
|
|
|
$this->fatalError( 'One of --consequence, --pattern or --privacy should be specified.' );
|
2020-09-08 13:22:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-22 21:01:16 +00:00
|
|
|
$this->output( "wiki\tfilter\n" );
|
|
|
|
|
2020-09-08 13:22:01 +00:00
|
|
|
if ( count( $wgConf->wikis ) > 0 ) {
|
|
|
|
foreach ( $wgConf->wikis as $dbname ) {
|
|
|
|
$this->getMatchingFilters( $dbname );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->getMatchingFilters();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|false $dbname Name of database, or false if the wiki is not part of a wikifarm
|
|
|
|
*/
|
2023-06-23 10:30:44 +00:00
|
|
|
private function getMatchingFilters( $dbname = false ) {
|
2023-03-03 17:32:00 +00:00
|
|
|
$dbr = $this->getDB( DB_REPLICA, [], $dbname );
|
2020-09-08 13:22:01 +00:00
|
|
|
$pattern = $dbr->addQuotes( $this->getOption( 'pattern' ) );
|
2024-08-22 21:01:16 +00:00
|
|
|
$consequence = $this->getOption( 'consequence' );
|
2024-11-25 13:12:20 +00:00
|
|
|
$privacy = $this->getOption( 'privacy' );
|
2020-09-08 13:22:01 +00:00
|
|
|
|
2024-09-11 13:20:11 +00:00
|
|
|
if ( $dbr->tableExists( 'abuse_filter', __METHOD__ ) ) {
|
2024-08-22 21:01:16 +00:00
|
|
|
$queryBuilder = $dbr->newSelectQueryBuilder()
|
2024-04-29 20:15:35 +00:00
|
|
|
->select( [ 'dbname' => 'DATABASE()', 'af_id' ] )
|
2024-08-22 21:01:16 +00:00
|
|
|
->from( 'abuse_filter' );
|
2024-08-27 10:19:56 +00:00
|
|
|
if ( $this->getOption( 'pattern' ) ) {
|
2024-08-22 21:01:16 +00:00
|
|
|
$queryBuilder->where( "af_pattern RLIKE $pattern" );
|
|
|
|
}
|
|
|
|
if ( $consequence ) {
|
|
|
|
$queryBuilder->where( $dbr->expr(
|
|
|
|
'af_actions',
|
|
|
|
IExpression::LIKE,
|
|
|
|
new LikeValue( $dbr->anyString(), $consequence, $dbr->anyString() )
|
|
|
|
) );
|
|
|
|
}
|
2024-11-25 13:12:20 +00:00
|
|
|
if ( $privacy !== '' ) {
|
|
|
|
if ( $privacy === '0' ) {
|
|
|
|
$queryBuilder->where( $dbr->expr(
|
|
|
|
'af_hidden',
|
|
|
|
'=',
|
|
|
|
0
|
|
|
|
) );
|
|
|
|
} else {
|
|
|
|
$privacy = (int)$privacy;
|
|
|
|
$queryBuilder->where( $dbr->bitAnd(
|
|
|
|
'af_hidden',
|
|
|
|
$privacy
|
|
|
|
) . " = $privacy" );
|
|
|
|
}
|
|
|
|
}
|
2024-08-22 21:01:16 +00:00
|
|
|
$rows = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
|
2020-09-08 13:22:01 +00:00
|
|
|
|
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
$this->output( $row->dbname . "\t" . $row->af_id . "\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$maintClass = SearchFilters::class;
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|