62 return preg_match( $regex, $text );
75 private static function makeRegex( $filterEntry, $protocol ) {
76 $regex =
'!' . preg_quote( $protocol,
'!' );
77 if ( substr( $filterEntry, 0, 2 ) ==
'*.' ) {
78 $regex .=
'(?:[A-Za-z0-9.-]+\.|)';
79 $filterEntry = substr( $filterEntry, 2 );
81 $regex .= preg_quote( $filterEntry,
'!' ) .
'!Si';
91 return is_callable(
'idn_to_utf8' ) && defined(
'INTL_IDNA_VARIANT_UTS46' );
103 $host = rawurldecode( $host );
104 if ( $host !==
'' && self::supportsIDN() ) {
106 $tmp = idn_to_utf8( $host, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46 );
107 if ( $tmp !==
false ) {
111 $okChars =
'a-zA-Z0-9\\-._~!$&\'()*+,;=';
114 $okChars .=
'\x80-\xf4';
116 $host = preg_replace_callback(
117 '<[^' . $okChars .
']>',
119 return rawurlencode( $m[0] );
125 if ( preg_match(
'/^\[([0-9a-f:*]+)\]$/', rawurldecode( $host ), $m ) ) {
127 if ( IPUtils::isValid( $ip ) ) {
128 return 'V6.' . implode(
'.', explode(
':', IPUtils::sanitizeIP( $ip ) ) ) .
'.';
130 if ( substr( $ip, -2 ) ===
':*' ) {
131 $cutIp = substr( $ip, 0, -2 );
132 if ( IPUtils::isValid(
"{$cutIp}::" ) ) {
134 $ct = count( explode(
':', $ip ) ) - 1;
136 implode(
'.', array_slice( explode(
':', IPUtils::sanitizeIP(
"{$cutIp}::" ) ), 0, $ct ) ) .
139 if ( IPUtils::isValid(
"{$cutIp}:1" ) ) {
142 substr( implode(
'.', explode(
':', IPUtils::sanitizeIP(
"{$cutIp}:1" ) ) ), 0, -1 ) .
150 if ( substr( $host, -1 ) ===
'.' ) {
151 $host = substr( $host, 0, -1 );
155 $b =
'(?:0*25[0-5]|0*2[0-4][0-9]|0*1[0-9][0-9]|0*[0-9]?[0-9])';
156 if ( preg_match(
"/^(?:{$b}\.){3}{$b}$|^(?:{$b}\.){1,3}\*$/", $host ) ) {
157 return 'V4.' . implode(
'.', array_map(
function ( $v ) {
158 return $v ===
'*' ? $v : (int)$v;
159 }, explode(
'.', $host ) ) ) .
'.';
163 return implode(
'.', array_reverse( explode(
'.', $host ) ) ) .
'.';
188 if ( $bits[
'scheme'] ==
'mailto' ) {
189 $mailparts = explode(
'@', $bits[
'host'], 2 );
190 if ( count( $mailparts ) === 2 ) {
196 $bits[
'host'] = $domainpart .
'@' . $mailparts[0];
202 $index = $bits[
'scheme'] . $bits[
'delimiter'] . $bits[
'host'];
204 if ( isset( $bits[
'port'] ) ) {
205 $index .=
':' . $bits[
'port'];
207 if ( isset( $bits[
'path'] ) ) {
208 $index .= $bits[
'path'];
212 if ( isset( $bits[
'query'] ) ) {
213 $index .=
'?' . $bits[
'query'];
215 if ( isset( $bits[
'fragment'] ) ) {
216 $index .=
'#' . $bits[
'fragment'];
219 if ( $bits[
'scheme'] ==
'' ) {
220 return [
"http:$index",
"https:$index" ];
258 'protocol' =>
'http://',
259 'oneWildcard' =>
false,
266 if ( $like ===
false ) {
272 if ( $options[
'oneWildcard'] ) {
273 $like = $trimmedLike;
275 if ( $trimmedLike[count( $trimmedLike ) - 1] instanceof
LikeMatch ) {
276 array_pop( $trimmedLike );
278 $index = implode(
'', $trimmedLike );
280 $p = $options[
'prefix'];
284 $l = strlen( $index );
289 "{$p}_index_60" => substr( $index, 0, 60 ),
290 "{$p}_index" . $db->buildLike( $like ),
297 "{$p}_index_60" . $db->buildLike( $index, $db->anyString() ),
298 "{$p}_index" . $db->buildLike( $like ),
314 public static function makeLikeArray( $filterEntry, $protocol =
'http://' ) {
318 $target = $protocol . $filterEntry;
325 if ( $bits[
'scheme'] ===
'mailto' && strpos( $bits[
'host'],
'@' ) ) {
327 $mailparts = explode(
'@', $bits[
'host'], 2 );
329 if ( $mailparts[0] ===
'*' ) {
331 $bits[
'host'] = $domainpart .
'@';
333 $bits[
'host'] = $domainpart .
'@' . $mailparts[0];
338 if ( substr( $bits[
'host'], -3 ) ===
'.*.' ) {
340 $bits[
'host'] = substr( $bits[
'host'], 0, -2 );
344 $like[] = $bits[
'scheme'] . $bits[
'delimiter'] . $bits[
'host'];
347 $like[] = $db->anyString();
350 if ( isset( $bits[
'port'] ) ) {
351 $like[] =
':' . $bits[
'port'];
353 if ( isset( $bits[
'path'] ) ) {
354 $like[] = $bits[
'path'];
355 } elseif ( !$subdomains ) {
358 if ( isset( $bits[
'query'] ) ) {
359 $like[] =
'?' . $bits[
'query'];
361 if ( isset( $bits[
'fragment'] ) ) {
362 $like[] =
'#' . $bits[
'fragment'];
366 foreach ( $like as $likepart ) {
367 if ( !( $likepart instanceof
LikeMatch ) && strpos( $likepart,
'*' ) !==
false ) {
372 if ( !( $like[count( $like ) - 1] instanceof
LikeMatch ) ) {
374 $like[] = $db->anyString();
389 if ( !is_array( $arr ) ) {
393 foreach ( $arr as $key => $value ) {
395 return array_slice( $arr, 0, $key + 1 );
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Some functions to help implement an external link filter for spam control.
static makeRegex( $filterEntry, $protocol)
Builds a regex pattern for $filterEntry.
static makeIndexes( $url)
Converts a URL into a format for el_index.
static makeLikeArray( $filterEntry, $protocol='http://')
Make an array to be used for calls to Database::buildLike(), which will match the specified string.
static indexifyHost( $host)
Canonicalize a hostname for el_index.
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
static supportsIDN()
Indicate whether LinkFilter IDN support is available.
const VERSION
Increment this when makeIndexes output changes.
static matchEntry(Content $content, $filterEntry, $protocol='http://')
Check whether $content contains a link to $filterEntry.
static keepOneWildcard( $arr)
Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
static isUtf8( $value)
Test whether a string is valid UTF-8.
Content object implementation for representing flat text.
Base interface for content objects.