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