Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| AbuseFilter | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| findInSet | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\AbuseFilter; |
| 4 | |
| 5 | use Wikimedia\Rdbms\IExpression; |
| 6 | use Wikimedia\Rdbms\IReadableDatabase; |
| 7 | use Wikimedia\Rdbms\LikeValue; |
| 8 | |
| 9 | class AbuseFilter { |
| 10 | |
| 11 | /** |
| 12 | * @deprecated |
| 13 | * @todo Phase out |
| 14 | */ |
| 15 | public const HISTORY_MAPPINGS = [ |
| 16 | 'af_pattern' => 'afh_pattern', |
| 17 | 'af_user' => 'afh_user', |
| 18 | 'af_user_text' => 'afh_user_text', |
| 19 | 'af_actor' => 'afh_actor', |
| 20 | 'af_timestamp' => 'afh_timestamp', |
| 21 | 'af_comments' => 'afh_comments', |
| 22 | 'af_public_comments' => 'afh_public_comments', |
| 23 | 'af_deleted' => 'afh_deleted', |
| 24 | 'af_id' => 'afh_filter', |
| 25 | 'af_group' => 'afh_group', |
| 26 | ]; |
| 27 | |
| 28 | /** |
| 29 | * Convenience wrapper around IReadableDatabase::expr simulating MySQL's FIND_IN_SET. |
| 30 | * |
| 31 | * This method returns an IExpression corresponding to a FIND_IN_SET(needle, field) |
| 32 | * SQL condition, which checks if the string `$needle` is present |
| 33 | * in the comma-separated list stored in the column `$field`. |
| 34 | */ |
| 35 | public static function findInSet( IReadableDatabase $db, string $field, string $needle ): IExpression { |
| 36 | return $db->expr( $field, '=', $needle ) |
| 37 | ->or( $field, IExpression::LIKE, new LikeValue( |
| 38 | $needle, ',', $db->anyString() |
| 39 | ) ) |
| 40 | ->or( $field, IExpression::LIKE, new LikeValue( |
| 41 | $db->anyString(), ',', $needle |
| 42 | ) ) |
| 43 | ->or( $field, IExpression::LIKE, new LikeValue( |
| 44 | $db->anyString(), |
| 45 | ',', $needle, ',', |
| 46 | $db->anyString() |
| 47 | ) ); |
| 48 | } |
| 49 | |
| 50 | } |