MediaWiki  1.32.0
LinkFilter.php
Go to the documentation of this file.
1 <?php
23 
34 class LinkFilter {
35 
44  public static function matchEntry( Content $content, $filterEntry, $protocol = 'http://' ) {
45  if ( !( $content instanceof TextContent ) ) {
46  // TODO: handle other types of content too.
47  // Maybe create ContentHandler::matchFilter( LinkFilter ).
48  // Think about a common base class for LinkFilter and MagicWord.
49  return 0;
50  }
51 
52  $text = $content->getNativeData();
53 
54  $regex = self::makeRegex( $filterEntry, $protocol );
55  return preg_match( $regex, $text );
56  }
57 
67  private static function makeRegex( $filterEntry, $protocol ) {
68  $regex = '!' . preg_quote( $protocol, '!' );
69  if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
70  $regex .= '(?:[A-Za-z0-9.-]+\.|)';
71  $filterEntry = substr( $filterEntry, 2 );
72  }
73  $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
74  return $regex;
75  }
76 
98  public static function makeLikeArray( $filterEntry, $protocol = 'http://' ) {
99  $db = wfGetDB( DB_REPLICA );
100 
101  $target = $protocol . $filterEntry;
102  $bits = wfParseUrl( $target );
103 
104  if ( $bits == false ) {
105  // Unknown protocol?
106  return false;
107  }
108 
109  if ( substr( $bits['host'], 0, 2 ) == '*.' ) {
110  $subdomains = true;
111  $bits['host'] = substr( $bits['host'], 2 );
112  if ( $bits['host'] == '' ) {
113  // We don't want to make a clause that will match everything,
114  // that could be dangerous
115  return false;
116  }
117  } else {
118  $subdomains = false;
119  }
120 
121  // Reverse the labels in the hostname, convert to lower case
122  // For emails reverse domainpart only
123  if ( $bits['scheme'] === 'mailto' && strpos( $bits['host'], '@' ) ) {
124  // complete email address
125  $mailparts = explode( '@', $bits['host'] );
126  $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
127  $bits['host'] = $domainpart . '@' . $mailparts[0];
128  } elseif ( $bits['scheme'] === 'mailto' ) {
129  // domainpart of email address only, do not add '.'
130  $bits['host'] = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
131  } else {
132  $bits['host'] = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
133  if ( substr( $bits['host'], -1, 1 ) !== '.' ) {
134  $bits['host'] .= '.';
135  }
136  }
137 
138  $like[] = $bits['scheme'] . $bits['delimiter'] . $bits['host'];
139 
140  if ( $subdomains ) {
141  $like[] = $db->anyString();
142  }
143 
144  if ( isset( $bits['port'] ) ) {
145  $like[] = ':' . $bits['port'];
146  }
147  if ( isset( $bits['path'] ) ) {
148  $like[] = $bits['path'];
149  } elseif ( !$subdomains ) {
150  $like[] = '/';
151  }
152  if ( isset( $bits['query'] ) ) {
153  $like[] = '?' . $bits['query'];
154  }
155  if ( isset( $bits['fragment'] ) ) {
156  $like[] = '#' . $bits['fragment'];
157  }
158 
159  // Check for stray asterisks: asterisk only allowed at the start of the domain
160  foreach ( $like as $likepart ) {
161  if ( !( $likepart instanceof LikeMatch ) && strpos( $likepart, '*' ) !== false ) {
162  return false;
163  }
164  }
165 
166  if ( !( $like[count( $like ) - 1] instanceof LikeMatch ) ) {
167  // Add wildcard at the end if there isn't one already
168  $like[] = $db->anyString();
169  }
170 
171  return $like;
172  }
173 
181  public static function keepOneWildcard( $arr ) {
182  if ( !is_array( $arr ) ) {
183  return $arr;
184  }
185 
186  foreach ( $arr as $key => $value ) {
187  if ( $value instanceof LikeMatch ) {
188  return array_slice( $arr, 0, $key + 1 );
189  }
190  }
191 
192  return $arr;
193  }
194 }
LinkFilter\matchEntry
static matchEntry(Content $content, $filterEntry, $protocol='http://')
Check whether $content contains a link to $filterEntry.
Definition: LinkFilter.php:44
captcha-old.count
count
Definition: captcha-old.py:249
LinkFilter\keepOneWildcard
static keepOneWildcard( $arr)
Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
Definition: LinkFilter.php:181
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:814
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2693
LinkFilter\makeLikeArray
static makeLikeArray( $filterEntry, $protocol='http://')
Make an array to be used for calls to Database::buildLike(), which will match the specified string.
Definition: LinkFilter.php:98
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
LinkFilter\makeRegex
static makeRegex( $filterEntry, $protocol)
Builds a regex pattern for $filterEntry.
Definition: LinkFilter.php:67
$value
$value
Definition: styleTest.css.php:49
LinkFilter
Some functions to help implement an external link filter for spam control.
Definition: LinkFilter.php:34
Wikimedia\Rdbms\LikeMatch
Used by Database::buildLike() to represent characters that have special meaning in SQL LIKE clauses a...
Definition: LikeMatch.php:10
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
Content
Base interface for content objects.
Definition: Content.php:34
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$content
$content
Definition: pageupdater.txt:72