Debian Codegraph @

extensions/AbuseFilter/maintenance/SearchFilters.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\AbuseFilter\Maintenance;
4
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Maintenance\Maintenance;
7 use Wikimedia\Rdbms\IExpression;
8 use Wikimedia\Rdbms\LikeValue;
9
10 // @codeCoverageIgnoreStart
11 $IP = getenv( 'MW_INSTALL_PATH' );
12 if ( $IP === false ) {
13 $IP = __DIR__ . '/../../..';
14 }
15 require_once "$IP/maintenance/Maintenance.php";
16 // @codeCoverageIgnoreEnd
17
18 class SearchFilters extends Maintenance {
19 public function __construct() {
20 parent::__construct();
21 $this->addDescription(
22 'Find all filters matching a regular expression pattern and/or that have given consequence'
23 );
24 $this->addOption( 'pattern', 'Regular expression pattern', false, true );
25 $this->addOption( 'consequence', 'The consequence that the filter should have', false, true );
26
27 $this->requireExtension( 'Abuse Filter' );
28 }
29
30 /**
31 * @see Maintenance:execute()
32 */
33 public function execute() {
34 global $wgConf;
35
36 if ( $this->getConfig()->get( MainConfigNames::DBtype ) !== 'mysql' ) {
37 $this->fatalError( 'This maintenance script only works with MySQL databases' );
38 }
39
40 if ( !$this->getOption( 'pattern' ) && !$this->getOption( 'consequence' ) ) {
41 $this->fatalError( 'One of --consequence or --pattern should be specified.' );
42 }
43
44 $this->output( "wiki\tfilter\n" );
45
46 if ( count( $wgConf->wikis ) > 0 ) {
47 foreach ( $wgConf->wikis as $dbname ) {
48 $this->getMatchingFilters( $dbname );
49 }
50 } else {
51 $this->getMatchingFilters();
52 }
53 }
54
55 /**
56 * @param string|false $dbname Name of database, or false if the wiki is not part of a wikifarm
57 */
58 private function getMatchingFilters( $dbname = false ) {
59 $dbr = $this->getDB( DB_REPLICA, [], $dbname );
60 $pattern = $dbr->addQuotes( $this->getOption( 'pattern' ) );
61 $consequence = $this->getOption( 'consequence' );
62
63 if ( $dbr->tableExists( 'abuse_filter', __METHOD__ ) ) {
64 $queryBuilder = $dbr->newSelectQueryBuilder()
65 ->select( [ 'dbname' => 'DATABASE()', 'af_id' ] )
66 ->from( 'abuse_filter' );
67 if ( $this->getOption( 'pattern' ) ) {
68 $queryBuilder->where( "af_pattern RLIKE $pattern" );
69 }
70 if ( $consequence ) {
71 $queryBuilder->where( $dbr->expr(
72 'af_actions',
73 IExpression::LIKE,
74 new LikeValue( $dbr->anyString(), $consequence, $dbr->anyString() )
75 ) );
76 }
77 $rows = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
78
79 foreach ( $rows as $row ) {
80 $this->output( $row->dbname . "\t" . $row->af_id . "\n" );
81 }
82 }
83 }
84 }
85
86 $maintClass = SearchFilters::class;
87 require_once RUN_MAINTENANCE_IF_MAIN;