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.
16 * Check whether $text contains a link to $filterEntry
18 * @param $text String: text to check
19 * @param $filterEntry String: domainparts, see makeRegex() for more details
20 * @return Integer: 0 if no match or 1 if there's at least one match
22 static function matchEntry( $text, $filterEntry ) {
23 $regex = LinkFilter
::makeRegex( $filterEntry );
24 return preg_match( $regex, $text );
28 * Builds a regex pattern for $filterEntry.
30 * @param $filterEntry String: URL, if it begins with "*.", it'll be
31 * replaced to match any subdomain
32 * @return String: regex pattern, for preg_match()
34 private static function makeRegex( $filterEntry ) {
36 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
37 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
38 $filterEntry = substr( $filterEntry, 2 );
40 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
45 * Make a string to go after an SQL LIKE, which will match the specified
46 * string. There are several kinds of filter entry:
47 * *.domain.com - Produces http://com.domain.%, matches domain.com
49 * domain.com - Produces http://com.domain./%, matches domain.com
50 * or domain.com/ but not www.domain.com
51 * *.domain.com/x - Produces http://com.domain.%/x%, matches
53 * domain.com/x - Produces http://com.domain./x%, matches
54 * domain.com/xy but not www.domain.com/xy
56 * Asterisks in any other location are considered invalid.
58 * @param $filterEntry String: domainparts
59 * @param $prot String: protocol
61 * @deprecated Use makeLikeArray() and pass result to Database::buildLike() instead
63 public static function makeLike( $filterEntry , $prot = 'http://' ) {
64 wfDeprecated( __METHOD__
);
66 $like = self
::makeLikeArray( $filterEntry , $prot );
70 $dbw = wfGetDB( DB_MASTER
);
71 $s = $dbw->buildLike( $like );
73 if ( preg_match( "/^ *LIKE '(.*)' *$/", $s, $m ) ) {
76 throw new MWException( __METHOD__
.': this DBMS is not supported by this function.' );
81 * Make an array to be used for calls to DatabaseBase::buildLike(), which
82 * will match the specified string. There are several kinds of filter entry:
83 * *.domain.com - Produces http://com.domain.%, matches domain.com
85 * domain.com - Produces http://com.domain./%, matches domain.com
86 * or domain.com/ but not www.domain.com
87 * *.domain.com/x - Produces http://com.domain.%/x%, matches
89 * domain.com/x - Produces http://com.domain./x%, matches
90 * domain.com/xy but not www.domain.com/xy
92 * Asterisks in any other location are considered invalid.
94 * @param $filterEntry String: domainparts
95 * @param $prot String: protocol
96 * @return Array to be passed to DatabaseBase::buildLike() or false on error
98 public static function makeLikeArray( $filterEntry , $prot = 'http://' ) {
99 $db = wfGetDB( DB_MASTER
);
100 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
102 $filterEntry = substr( $filterEntry, 2 );
103 if ( $filterEntry == '' ) {
104 // We don't want to make a clause that will match everything,
105 // that could be dangerous
111 // No stray asterisks, that could cause confusion
112 // It's not simple or efficient to handle it properly so we don't
114 if ( strpos( $filterEntry, '*' ) !== false ) {
117 $slash = strpos( $filterEntry, '/' );
118 if ( $slash !== false ) {
119 $path = substr( $filterEntry, $slash );
120 $host = substr( $filterEntry, 0, $slash );
123 $host = $filterEntry;
125 // Reverse the labels in the hostname, convert to lower case
126 // For emails reverse domainpart only
127 if ( $prot == 'mailto:' && strpos($host, '@') ) {
128 // complete email adress
129 $mailparts = explode( '@', $host );
130 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
131 $host = $domainpart . '@' . $mailparts[0];
132 $like = array( "$prot$host", $db->anyString() );
133 } elseif ( $prot == 'mailto:' ) {
134 // domainpart of email adress only. do not add '.'
135 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
136 $like = array( "$prot$host", $db->anyString() );
138 $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
139 if ( substr( $host, -1, 1 ) !== '.' ) {
142 $like = array( "$prot$host" );
145 $like[] = $db->anyString();
147 if ( !$subdomains ||
$path !== '/' ) {
149 $like[] = $db->anyString();
156 * Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
158 * @param $arr array: array to filter
159 * @return filtered array
161 public static function keepOneWildcard( $arr ) {
162 if( !is_array( $arr ) ) {
166 foreach( $arr as $key => $value ) {
167 if ( $value instanceof LikeMatch
) {
168 return array_slice( $arr, 0, $key +
1 );