mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-24 06:03:49 +00:00
Use preg_match rather than fnmatch for 'like' operator
fnmatch() will not recognize 'é' as a single character when the LC_CTYPE locale is set to C / POSIX. So transform the shell-style pattern to PCRE, and use preg_match() instead. fnmatch() was not available on Windows prior to PHP 5.3, so code snippets for preg_match()-powered polyfills abound. I used the pattern translation map from <http://www.php.net/manual/en/function.fnmatch.php#100207> after testing different implementations and finding it to be the most complete. Bug: 66930 Change-Id: Ice12c7b9dbe6472fe4131679a48a0ad54fac6394
This commit is contained in:
parent
f0325d3a92
commit
ea46bfdd16
|
@ -69,6 +69,18 @@ class AFPData {
|
|||
const DFloat = 'float';
|
||||
const DList = 'list';
|
||||
|
||||
// Translation table mapping shell-style wildcards to PCRE equivalents.
|
||||
// Derived from <http://www.php.net/manual/en/function.fnmatch.php#100207>
|
||||
private static $wildcardMap = array(
|
||||
'\*' => '.*',
|
||||
'\.' => '\.',
|
||||
'\?' => '.',
|
||||
'\[' => '[',
|
||||
'\[\!' => '[^',
|
||||
'\\' => '\\\\',
|
||||
'\]' => ']',
|
||||
);
|
||||
|
||||
public $type;
|
||||
public $data;
|
||||
|
||||
|
@ -247,9 +259,9 @@ class AFPData {
|
|||
*/
|
||||
public static function keywordLike( $str, $pattern ) {
|
||||
$str = $str->toString();
|
||||
$pattern = $pattern->toString();
|
||||
$pattern = '#^' . strtr( preg_quote( $pattern->toString(), '#' ), self::$wildcardMap ) . '$#u';
|
||||
wfSuppressWarnings();
|
||||
$result = fnmatch( $pattern, $str );
|
||||
$result = preg_match( $pattern, $str );
|
||||
wfRestoreWarnings();
|
||||
return new AFPData( self::DBool, (bool)$result );
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue