Dmaza has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/379159 )
Change subject: [WIP] Add cclike operator to normalize and compare a string to a list ...................................................................... [WIP] Add cclike operator to normalize and compare a string to a list cclike operator will normalize the left operand and compare against the normalized version of each of the elements in the right operand Bug: T65242 Change-Id: I4034c0054a6849babbf2d96ea13dc97d3660d5b4 --- M i18n/en.json M includes/AbuseFilter.class.php M includes/parser/AFPData.php M includes/parser/AbuseFilterParser.php M includes/parser/AbuseFilterTokenizer.php A tests/parserTests/cclike.r A tests/parserTests/cclike.t 7 files changed, 33 insertions(+), 3 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/AbuseFilter refs/changes/59/379159/1 diff --git a/i18n/en.json b/i18n/en.json index 0d32b9f..b526401 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -220,6 +220,7 @@ "abusefilter-edit-builder-misc-stringlit": "String literal (\"\")", "abusefilter-edit-builder-misc-tern": "Ternary operator (X ? Y : Z)", "abusefilter-edit-builder-misc-cond": "Conditional (if X then Y else Z)", + "abusefilter-edit-builder-misc-cclike": "Normalize string and compare against list", "abusefilter-edit-builder-group-funcs": "Functions", "abusefilter-edit-builder-funcs-length": "String length (length)", "abusefilter-edit-builder-funcs-lcase": "To lower case (lcase)", diff --git a/includes/AbuseFilter.class.php b/includes/AbuseFilter.class.php index 3ce5cdb..6eac7db 100644 --- a/includes/AbuseFilter.class.php +++ b/includes/AbuseFilter.class.php @@ -56,6 +56,7 @@ ], 'misc' => [ 'in' => 'in', + 'cclike' => 'cclike', 'contains' => 'contains', 'like' => 'like', '""' => 'stringlit', diff --git a/includes/parser/AFPData.php b/includes/parser/AFPData.php index d5a0069..66f6a5e 100644 --- a/includes/parser/AFPData.php +++ b/includes/parser/AFPData.php @@ -156,6 +156,31 @@ } /** + * Normalizes operands and compares if they are equal + * + * @param $a AFPData + * @param $b AFPData + * @return AFPData + */ + public static function keywordCclike( $a, $b ) { + $a = $a->toString(); + $b = $b->toList(); + + if ( $a == '' || !$b ) { + return new AFPData( self::DBOOL, false ); + } + + $a = AbuseFilterParser::ccnorm( $a ); + foreach ( $b as $val ) { + if ( $a === AbuseFilterParser::ccnorm( $val->toString() ) ) { + return new AFPData( self::DBOOL, true ); + } + } + + return new AFPData( self::DBOOL, false ); + } + + /** * @param $a AFPData * @param $b AFPData * @return AFPData diff --git a/includes/parser/AbuseFilterParser.php b/includes/parser/AbuseFilterParser.php index f9bd15b..e94732f 100644 --- a/includes/parser/AbuseFilterParser.php +++ b/includes/parser/AbuseFilterParser.php @@ -48,7 +48,8 @@ 'contains' => 'keywordContains', 'rlike' => 'keywordRegex', 'irlike' => 'keywordRegexInsensitive', - 'regex' => 'keywordRegex' + 'regex' => 'keywordRegex', + 'cclike' => 'keywordCclike' ]; public static $funcCache = []; @@ -1105,7 +1106,7 @@ * @param $s * @return mixed */ - protected function ccnorm( $s ) { + public static function ccnorm( $s ) { if ( is_callable( 'AntiSpoof::normalizeString' ) ) { $s = AntiSpoof::normalizeString( $s ); } else { diff --git a/includes/parser/AbuseFilterTokenizer.php b/includes/parser/AbuseFilterTokenizer.php index 025314e..0ce1969 100644 --- a/includes/parser/AbuseFilterTokenizer.php +++ b/includes/parser/AbuseFilterTokenizer.php @@ -54,7 +54,7 @@ public static $keywords = [ 'in', 'like', 'true', 'false', 'null', 'contains', 'matches', - 'rlike', 'irlike', 'regex', 'if', 'then', 'else', 'end', + 'rlike', 'irlike', 'regex', 'if', 'then', 'else', 'end', 'cclike', ]; /** diff --git a/tests/parserTests/cclike.r b/tests/parserTests/cclike.r new file mode 100644 index 0000000..4736e08 --- /dev/null +++ b/tests/parserTests/cclike.r @@ -0,0 +1 @@ +MATCH diff --git a/tests/parserTests/cclike.t b/tests/parserTests/cclike.t new file mode 100644 index 0000000..70646cd --- /dev/null +++ b/tests/parserTests/cclike.t @@ -0,0 +1 @@ +"4any0ne" cclike ["FOO", "AANYONE"] -- To view, visit https://gerrit.wikimedia.org/r/379159 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4034c0054a6849babbf2d96ea13dc97d3660d5b4 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/AbuseFilter Gerrit-Branch: master Gerrit-Owner: Dmaza <dm...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits