summaryrefslogtreecommitdiff
path: root/includes/LinkFilter.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/LinkFilter.php')
-rw-r--r--includes/LinkFilter.php44
1 files changed, 26 insertions, 18 deletions
diff --git a/includes/LinkFilter.php b/includes/LinkFilter.php
index 214f4959..d552c696 100644
--- a/includes/LinkFilter.php
+++ b/includes/LinkFilter.php
@@ -20,10 +20,9 @@
* @file
*/
-
/**
* Some functions to help implement an external link filter for spam control.
- *
+ *
* @todo implement the filter. Currently these are just some functions to help
* maintenance/cleanupSpam.php remove links to a single specified domain. The
* next thing is to implement functions for checking a given page against a big
@@ -34,13 +33,22 @@
class LinkFilter {
/**
- * Check whether $text contains a link to $filterEntry
+ * Check whether $content contains a link to $filterEntry
*
- * @param $text String: text to check
- * @param $filterEntry String: domainparts, see makeRegex() for more details
+ * @param $content Content: content to check
+ * @param string $filterEntry domainparts, see makeRegex() for more details
* @return Integer: 0 if no match or 1 if there's at least one match
*/
- static function matchEntry( $text, $filterEntry ) {
+ static function matchEntry( Content $content, $filterEntry ) {
+ if ( !( $content instanceof TextContent ) ) {
+ //TODO: handle other types of content too.
+ // Maybe create ContentHandler::matchFilter( LinkFilter ).
+ // Think about a common base class for LinkFilter and MagicWord.
+ return 0;
+ }
+
+ $text = $content->getNativeData();
+
$regex = LinkFilter::makeRegex( $filterEntry );
return preg_match( $regex, $text );
}
@@ -48,7 +56,7 @@ class LinkFilter {
/**
* Builds a regex pattern for $filterEntry.
*
- * @param $filterEntry String: URL, if it begins with "*.", it'll be
+ * @param string $filterEntry URL, if it begins with "*.", it'll be
* replaced to match any subdomain
* @return String: regex pattern, for preg_match()
*/
@@ -76,11 +84,11 @@ class LinkFilter {
*
* Asterisks in any other location are considered invalid.
*
- * @param $filterEntry String: domainparts
+ * @param string $filterEntry domainparts
* @param $prot String: protocol
* @return Array to be passed to DatabaseBase::buildLike() or false on error
*/
- public static function makeLikeArray( $filterEntry , $prot = 'http://' ) {
+ public static function makeLikeArray( $filterEntry, $prot = 'http://' ) {
$db = wfGetDB( DB_MASTER );
if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
$subdomains = true;
@@ -109,18 +117,18 @@ class LinkFilter {
}
// Reverse the labels in the hostname, convert to lower case
// For emails reverse domainpart only
- if ( $prot == 'mailto:' && strpos($host, '@') ) {
- // complete email adress
+ if ( $prot == 'mailto:' && strpos( $host, '@' ) ) {
+ // complete email address
$mailparts = explode( '@', $host );
$domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
$host = $domainpart . '@' . $mailparts[0];
$like = array( "$prot$host", $db->anyString() );
} elseif ( $prot == 'mailto:' ) {
- // domainpart of email adress only. do not add '.'
- $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
- $like = array( "$prot$host", $db->anyString() );
+ // domainpart of email address only. do not add '.'
+ $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
+ $like = array( "$prot$host", $db->anyString() );
} else {
- $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
+ $host = strtolower( implode( '.', array_reverse( explode( '.', $host ) ) ) );
if ( substr( $host, -1, 1 ) !== '.' ) {
$host .= '.';
}
@@ -140,15 +148,15 @@ class LinkFilter {
/**
* Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
*
- * @param $arr array: array to filter
+ * @param array $arr array to filter
* @return array filtered array
*/
public static function keepOneWildcard( $arr ) {
- if( !is_array( $arr ) ) {
+ if ( !is_array( $arr ) ) {
return $arr;
}
- foreach( $arr as $key => $value ) {
+ foreach ( $arr as $key => $value ) {
if ( $value instanceof LikeMatch ) {
return array_slice( $arr, 0, $key + 1 );
}