4 * Some functions to help implement an external link filter for spam control.
6 * TODO: implement the filter. Currently these are just some functions to help
7 * maintenance/cleanupSpam.php remove links to a single specified domain. The
8 * next thing is to implement functions for checking a given page against a big
11 * Another cool thing to do would be a web interface for fast spam removal.
17 static function matchEntry( $text, $filterEntry ) {
18 $regex = LinkFilter
::makeRegex( $filterEntry );
19 return preg_match( $regex, $text );
25 private static function makeRegex( $filterEntry ) {
27 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
28 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
29 $filterEntry = substr( $filterEntry, 2 );
31 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
36 * Make a string to go after an SQL LIKE, which will match the specified
37 * string. There are several kinds of filter entry:
38 * *.domain.com - Produces http://com.domain.%, matches domain.com
40 * domain.com - Produces http://com.domain./%, matches domain.com
41 * or domain.com/ but not www.domain.com
42 * *.domain.com/x - Produces http://com.domain.%/x%, matches
44 * domain.com/x - Produces http://com.domain./x%, matches
45 * domain.com/xy but not www.domain.com/xy
47 * Asterisks in any other location are considered invalid.
50 * @param $filterEntry String: domainparts
51 * @param $prot String: protocol
52 * @deprecated Use makeLikeArray() and pass result to Database::buildLike() instead
54 public static function makeLike( $filterEntry , $prot = 'http://' ) {
55 wfDeprecated( __METHOD__
);
57 $like = self
::makeLikeArray( $filterEntry , $prot );
61 $dbw = wfGetDB( DB_MASTER
);
63 return $dbw->buildLike( $like );
67 * Make an array to be used for calls to Database::buildLike(), which will match the specified
68 * string. There are several kinds of filter entry:
69 * *.domain.com - Produces http://com.domain.%, matches domain.com
71 * domain.com - Produces http://com.domain./%, matches domain.com
72 * or domain.com/ but not www.domain.com
73 * *.domain.com/x - Produces http://com.domain.%/x%, matches
75 * domain.com/x - Produces http://com.domain./x%, matches
76 * domain.com/xy but not www.domain.com/xy
78 * Asterisks in any other location are considered invalid.
81 * @param $filterEntry String: domainparts
82 * @param $prot String: protocol
84 public static function makeLikeArray( $filterEntry , $prot = 'http://' ) {
85 $db = wfGetDB( DB_MASTER
);
86 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
88 $filterEntry = substr( $filterEntry, 2 );
89 if ( $filterEntry == '' ) {
90 // We don't want to make a clause that will match everything,
91 // that could be dangerous
97 // No stray asterisks, that could cause confusion
98 // It's not simple or efficient to handle it properly so we don't
100 if ( strpos( $filterEntry, '*' ) !== false ) {
103 $slash = strpos( $filterEntry, '/' );
104 if ( $slash !== false ) {
105 $path = substr( $filterEntry, $slash );
106 $host = substr( $filterEntry, 0, $slash );
109 $host = $filterEntry;
111 // Reverse the labels in the hostname, convert to lower case
112 // For emails reverse domainpart only
113 if ( $prot == 'mailto:' && strpos($host, '@') ) {
114 // complete email adress
115 $mailparts = explode( '@', $host );
116 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
117 $host = $domainpart . '@' . $mailparts[0];
118 $like = array( "$prot$host", $db->anyString() );
119 } elseif ( $prot == 'mailto:' ) {
120 // domainpart of email adress only. do not add '.'
121 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
122 $like = array( "$prot$host", $db->anyString() );
124 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
125 if ( substr( $host, -1, 1 ) !== '.' ) {
128 $like = array( "$prot$host" );
131 $like[] = $db->anyString();
133 if ( !$subdomains ||
$path !== '/' ) {
135 $like[] = $db->anyString();
142 * Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
144 * @param $arr array: array to filter
145 * @return filtered array
147 public static function keepOneWildcard( $arr ) {
148 if( !is_array( $arr ) ) {
152 foreach( $arr as $key => $value ) {
153 if ( $value instanceof LikeMatch
) {
154 return array_slice( $arr, 0, $key +
1 );