MediaWiki REL1_31
LinkFilter.php
Go to the documentation of this file.
1<?php
23
35
43 static function matchEntry( Content $content, $filterEntry ) {
44 if ( !( $content instanceof TextContent ) ) {
45 // TODO: handle other types of content too.
46 // Maybe create ContentHandler::matchFilter( LinkFilter ).
47 // Think about a common base class for LinkFilter and MagicWord.
48 return 0;
49 }
50
51 $text = $content->getNativeData();
52
53 $regex = self::makeRegex( $filterEntry );
54 return preg_match( $regex, $text );
55 }
56
64 private static function makeRegex( $filterEntry ) {
65 $regex = '!http://';
66 if ( substr( $filterEntry, 0, 2 ) == '*.' ) {
67 $regex .= '(?:[A-Za-z0-9.-]+\.|)';
68 $filterEntry = substr( $filterEntry, 2 );
69 }
70 $regex .= preg_quote( $filterEntry, '!' ) . '!Si';
71 return $regex;
72 }
73
95 public static function makeLikeArray( $filterEntry, $protocol = 'http://' ) {
96 $db = wfGetDB( DB_REPLICA );
97
98 $target = $protocol . $filterEntry;
99 $bits = wfParseUrl( $target );
100
101 if ( $bits == false ) {
102 // Unknown protocol?
103 return false;
104 }
105
106 if ( substr( $bits['host'], 0, 2 ) == '*.' ) {
107 $subdomains = true;
108 $bits['host'] = substr( $bits['host'], 2 );
109 if ( $bits['host'] == '' ) {
110 // We don't want to make a clause that will match everything,
111 // that could be dangerous
112 return false;
113 }
114 } else {
115 $subdomains = false;
116 }
117
118 // Reverse the labels in the hostname, convert to lower case
119 // For emails reverse domainpart only
120 if ( $bits['scheme'] === 'mailto' && strpos( $bits['host'], '@' ) ) {
121 // complete email address
122 $mailparts = explode( '@', $bits['host'] );
123 $domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
124 $bits['host'] = $domainpart . '@' . $mailparts[0];
125 } elseif ( $bits['scheme'] === 'mailto' ) {
126 // domainpart of email address only, do not add '.'
127 $bits['host'] = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
128 } else {
129 $bits['host'] = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
130 if ( substr( $bits['host'], -1, 1 ) !== '.' ) {
131 $bits['host'] .= '.';
132 }
133 }
134
135 $like[] = $bits['scheme'] . $bits['delimiter'] . $bits['host'];
136
137 if ( $subdomains ) {
138 $like[] = $db->anyString();
139 }
140
141 if ( isset( $bits['port'] ) ) {
142 $like[] = ':' . $bits['port'];
143 }
144 if ( isset( $bits['path'] ) ) {
145 $like[] = $bits['path'];
146 } elseif ( !$subdomains ) {
147 $like[] = '/';
148 }
149 if ( isset( $bits['query'] ) ) {
150 $like[] = '?' . $bits['query'];
151 }
152 if ( isset( $bits['fragment'] ) ) {
153 $like[] = '#' . $bits['fragment'];
154 }
155
156 // Check for stray asterisks: asterisk only allowed at the start of the domain
157 foreach ( $like as $likepart ) {
158 if ( !( $likepart instanceof LikeMatch ) && strpos( $likepart, '*' ) !== false ) {
159 return false;
160 }
161 }
162
163 if ( !( $like[count( $like ) - 1] instanceof LikeMatch ) ) {
164 // Add wildcard at the end if there isn't one already
165 $like[] = $db->anyString();
166 }
167
168 return $like;
169 }
170
178 public static function keepOneWildcard( $arr ) {
179 if ( !is_array( $arr ) ) {
180 return $arr;
181 }
182
183 foreach ( $arr as $key => $value ) {
184 if ( $value instanceof LikeMatch ) {
185 return array_slice( $arr, 0, $key + 1 );
186 }
187 }
188
189 return $arr;
190 }
191}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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 makeLikeArray( $filterEntry, $protocol='http://')
Make an array to be used for calls to Database::buildLike(), which will match the specified string.
static makeRegex( $filterEntry)
Builds a regex pattern for $filterEntry.
static matchEntry(Content $content, $filterEntry)
Check whether $content contains a link to $filterEntry.
static keepOneWildcard( $arr)
Filters an array returned by makeLikeArray(), removing everything past first pattern placeholder.
Content object implementation for representing flat text.
Used by Database::buildLike() to represent characters that have special meaning in SQL LIKE clauses a...
Definition LikeMatch.php:10
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
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:37
Base interface for content objects.
Definition Content.php:34
getNativeData()
Returns native representation of the data.
const DB_REPLICA
Definition defines.php:25