2020-09-08 13:22:01 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-02 13:49:41 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Maintenance;
|
|
|
|
|
|
|
|
use Maintenance;
|
2024-08-22 21:01:16 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
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(
|
|
|
|
'Find all filters matching a regular expression pattern and/or that have given consequence'
|
|
|
|
);
|
|
|
|
$this->addOption( 'pattern', 'Regular expression pattern', false, true );
|
|
|
|
$this->addOption( 'consequence', 'The consequence that the filter should have', 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-08-22 21:01:16 +00:00
|
|
|
if ( !$this->getOption( 'pattern' ) && !$this->getOption( 'consequence' ) ) {
|
|
|
|
$this->fatalError( 'One of --consequence or --pattern 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' );
|
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() )
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
$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;
|