Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.00% covered (success)
92.00%
23 / 25
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
SearchFilters
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 getMatchingFilters
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Maintenance;
4
5use Maintenance;
6
7// @codeCoverageIgnoreStart
8$IP = getenv( 'MW_INSTALL_PATH' );
9if ( $IP === false ) {
10    $IP = __DIR__ . '/../../..';
11}
12require_once "$IP/maintenance/Maintenance.php";
13// @codeCoverageIgnoreEnd
14
15class SearchFilters extends Maintenance {
16    public function __construct() {
17        parent::__construct();
18        $this->addDescription( 'Find all filters matching a regular expression pattern' );
19        $this->addOption( 'pattern', 'Regular expression pattern', true, true );
20
21        $this->requireExtension( 'Abuse Filter' );
22    }
23
24    /**
25     * @see Maintenance:execute()
26     */
27    public function execute() {
28        global $wgConf, $wgDBtype;
29
30        if ( $wgDBtype !== 'mysql' ) {
31            // Code using exit() cannot be tested (T272241)
32            // @codeCoverageIgnoreStart
33            $this->fatalError( 'This maintenance script only works with MySQL databases' );
34            // @codeCoverageIgnoreEnd
35        }
36
37        $this->output( "wiki\tfilter\n" );
38        if ( $this->getOption( 'pattern' ) === '' ) {
39            // Code using exit() cannot be tested (T272241)
40            // @codeCoverageIgnoreStart
41            $this->fatalError( 'Pattern cannot be empty' );
42            // @codeCoverageIgnoreEnd
43        }
44
45        if ( count( $wgConf->wikis ) > 0 ) {
46            foreach ( $wgConf->wikis as $dbname ) {
47                $this->getMatchingFilters( $dbname );
48            }
49        } else {
50            $this->getMatchingFilters();
51        }
52    }
53
54    /**
55     * @param string|false $dbname Name of database, or false if the wiki is not part of a wikifarm
56     */
57    private function getMatchingFilters( $dbname = false ) {
58        $dbr = $this->getDB( DB_REPLICA, [], $dbname );
59        $pattern = $dbr->addQuotes( $this->getOption( 'pattern' ) );
60
61        if ( $dbr->tableExists( 'abuse_filter' ) ) {
62            $rows = $dbr->select(
63                'abuse_filter',
64                [ 'dbname' => 'DATABASE()', 'af_id' ],
65                [
66                    "af_pattern RLIKE $pattern"
67                ]
68            );
69
70            foreach ( $rows as $row ) {
71                $this->output( $row->dbname . "\t" . $row->af_id . "\n" );
72            }
73        }
74    }
75}
76
77$maintClass = SearchFilters::class;
78require_once RUN_MAINTENANCE_IF_MAIN;