| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\SpamBlacklist; |
| 4 |
|
| 5 |
use Wikimedia\AtEase\AtEase; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class SpamRegexBatch { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
private static function buildRegexes( array $lines, BaseBlacklist $blacklist, $batchSize = 4096 ) { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
$regexes = []; |
| 27 |
$regexStart = $blacklist->getRegexStart(); |
| 28 |
$regexEnd = $blacklist->getRegexEnd( $batchSize ); |
| 29 |
$build = false; |
| 30 |
foreach ( $lines as $line ) { |
| 31 |
if ( substr( $line, -1, 1 ) == "\\" ) { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
if ( $build === false ) { |
| 40 |
$build = $line; |
| 41 |
} elseif ( strlen( $build ) + strlen( $line ) > $batchSize ) { |
| 42 |
$regexes[] = $regexStart . |
| 43 |
str_replace( '/', '\/', preg_replace( '|\\\*/|u', '/', $build ) ) . |
| 44 |
$regexEnd; |
| 45 |
$build = $line; |
| 46 |
} else { |
| 47 |
$build .= '|'; |
| 48 |
$build .= $line; |
| 49 |
} |
| 50 |
} |
| 51 |
if ( $build !== false ) { |
| 52 |
$regexes[] = $regexStart . |
| 53 |
str_replace( '/', '\/', preg_replace( '|\\\*/|u', '/', $build ) ) . |
| 54 |
$regexEnd; |
| 55 |
} |
| 56 |
return $regexes; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private static function validateRegexes( $regexes ) { |
| 66 |
foreach ( $regexes as $regex ) { |
| 67 |
AtEase::suppressWarnings(); |
| 68 |
|
| 69 |
$ok = preg_match( $regex, '' ); |
| 70 |
AtEase::restoreWarnings(); |
| 71 |
|
| 72 |
if ( $ok === false ) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
} |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
private static function stripLines( array $lines ) { |
| 86 |
return array_filter( |
| 87 |
array_map( 'trim', |
| 88 |
preg_replace( '/#.*$/', '', |
| 89 |
$lines ) |
| 90 |
) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private static function buildSafeRegexes( array $lines, BaseBlacklist $blacklist, $fileName = false ) { |
| 103 |
$lines = self::stripLines( $lines ); |
| 104 |
$regexes = self::buildRegexes( $lines, $blacklist ); |
| 105 |
if ( self::validateRegexes( $regexes ) ) { |
| 106 |
return $regexes; |
| 107 |
} else { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
if ( $fileName ) { |
| 112 |
wfDebugLog( 'SpamBlacklist', "Spam blacklist warning: bogus line in $fileName\n" ); |
| 113 |
} |
| 114 |
return self::buildRegexes( $lines, $blacklist, 0 ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public static function getBadLines( $lines, BaseBlacklist $blacklist ) { |
| 126 |
$lines = self::stripLines( $lines ); |
| 127 |
|
| 128 |
$badLines = []; |
| 129 |
foreach ( $lines as $line ) { |
| 130 |
if ( substr( $line, -1, 1 ) == "\\" ) { |
| 131 |
|
| 132 |
$badLines[] = $line; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$regexes = self::buildRegexes( $lines, $blacklist ); |
| 137 |
if ( self::validateRegexes( $regexes ) ) { |
| 138 |
|
| 139 |
return $badLines; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
foreach ( $lines as $line ) { |
| 144 |
$regexes = self::buildRegexes( [ $line ], $blacklist ); |
| 145 |
if ( !self::validateRegexes( $regexes ) ) { |
| 146 |
$badLines[] = $line; |
| 147 |
} |
| 148 |
} |
| 149 |
return $badLines; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public static function regexesFromText( $source, BaseBlacklist $blacklist, $fileName = false ) { |
| 162 |
$lines = explode( "\n", $source ); |
| 163 |
return self::buildSafeRegexes( $lines, $blacklist, $fileName ); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public static function regexesFromMessage( $message, BaseBlacklist $blacklist ) { |
| 175 |
$source = wfMessage( $message )->inContentLanguage(); |
| 176 |
if ( !$source->isDisabled() ) { |
| 177 |
return self::regexesFromText( $source->plain(), $blacklist ); |
| 178 |
} else { |
| 179 |
return []; |
| 180 |
} |
| 181 |
} |
| 182 |
} |