Introduce searchFilters.php

A maintenance script that makes it easier for those with shell
access to search for all filters matching a regular expression
pattern on any of the wikis in a wiki farm.

Bug: T262052
Change-Id: Iea9e87a9055c0b1cedd06e8211fc99e3cef53c3a
This commit is contained in:
Huji Lee 2020-09-08 09:22:01 -04:00
parent 92c3c8dbe1
commit 43e471d056

View file

@ -0,0 +1,67 @@
<?php
if ( getenv( 'MW_INSTALL_PATH' ) ) {
$IP = getenv( 'MW_INSTALL_PATH' );
} else {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
class SearchFilters extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Find all filters matching a regular expression pattern' );
$this->addOption( 'pattern', 'Regular expression pattern', true, true );
$this->requireExtension( 'Abuse Filter' );
}
/**
* @see Maintenance:execute()
*/
public function execute() {
global $wgConf, $wgDBtype;
if ( $wgDBtype !== 'mysql' ) {
$this->fatalError( 'This maintenance script only works with MySQL databases' );
}
$this->output( "wiki\tfilter\n" );
if ( $this->getOption( 'pattern' ) === '' ) {
$this->fatalError( 'Pattern cannot be empty' );
}
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
*/
public function getMatchingFilters( $dbname = false ) {
$dbr = wfGetDB( DB_REPLICA, [], $dbname );
$pattern = $dbr->addQuotes( $this->getOption( 'pattern' ) );
if ( $dbr->tableExists( 'abuse_filter' ) ) {
$rows = $dbr->select(
'abuse_filter',
'DATABASE() AS dbname, af_id',
[
"af_pattern RLIKE $pattern"
]
);
foreach ( $rows as $row ) {
$this->output( $row->dbname . "\t" . $row->af_id . "\n" );
}
}
}
}
$maintClass = SearchFilters::class;
require_once RUN_MAINTENANCE_IF_MAIN;