MediaWiki REL1_34
SpamBlacklistHooks.php
Go to the documentation of this file.
1<?php
2
19 public static function filterMergedContent(
22 Status $status,
23 $summary,
24 User $user,
25 $minoredit
26 ) {
27 $title = $context->getTitle();
28
29 // get the link from the not-yet-saved page content.
30 $editInfo = $context->getWikiPage()->prepareContentForEdit( $content );
31 $pout = $editInfo->output;
32 $links = array_keys( $pout->getExternalLinks() );
33
34 // HACK: treat the edit summary as a link if it contains anything
35 // that looks like it could be a URL or e-mail address.
36 if ( preg_match( '/\S(\.[^\s\d]{2,}|[\/@]\S)/', $summary ) ) {
37 $links[] = $summary;
38 }
39
41 $matches = $spamObj->filter( $links, $title );
42
43 if ( $matches !== false ) {
44 $error = new ApiMessage(
45 wfMessage( 'spam-blacklisted-link', Message::listParam( $matches ) ),
46 'spamblacklist',
47 [
48 'spamblacklist' => [ 'matches' => $matches ],
49 ]
50 );
51 $status->fatal( $error );
52 }
53
54 // Always return true, EditPage will look at $status->isOk().
55 return true;
56 }
57
58 public static function onParserOutputStashForEdit(
59 WikiPage $page,
61 ParserOutput $output
62 ) {
63 $links = array_keys( $output->getExternalLinks() );
65 $spamObj->warmCachesForFilter( $page->getTitle(), $links );
66 }
67
75 public static function userCanSendEmail( &$user, &$hookErr ) {
77 if ( $blacklist->checkUser( $user ) ) {
78 return true;
79 }
80
81 $hookErr = [ 'spam-blacklisted-email', 'spam-blacklisted-email-text', null ];
82
83 return false;
84 }
85
97 public static function validate( EditPage $editPage, $text, $section, &$hookError ) {
98 $title = $editPage->getTitle();
99 $thisPageName = $title->getPrefixedDBkey();
100
102 wfDebugLog( 'SpamBlacklist',
103 "Spam blacklist validator: [[$thisPageName]] not a local blacklist\n"
104 );
105 return true;
106 }
107
109 if ( $type === false ) {
110 return true;
111 }
112
113 $lines = explode( "\n", $text );
114
116 if ( $badLines ) {
117 wfDebugLog( 'SpamBlacklist',
118 "Spam blacklist validator: [[$thisPageName]] given invalid input lines: " .
119 implode( ', ', $badLines ) . "\n"
120 );
121
122 $badList = "*<code>" .
123 implode( "</code>\n*<code>",
124 array_map( 'wfEscapeWikiText', $badLines ) ) .
125 "</code>\n";
126 $hookError =
127 "<div class='errorbox'>" .
128 wfMessage( 'spam-invalid-lines' )->numParams( $badLines )->text() . "<br />" .
129 $badList .
130 "</div>\n" .
131 "<br clear='all' />\n";
132 } else {
133 wfDebugLog( 'SpamBlacklist',
134 "Spam blacklist validator: [[$thisPageName]] ok or empty blacklist\n"
135 );
136 }
137
138 return true;
139 }
140
159 public static function pageSaveContent(
160 WikiPage $wikiPage,
161 User $user,
163 $summary,
164 $isMinor,
165 $isWatch,
166 $section,
167 $flags,
168 $revision,
169 Status $status,
170 $baseRevId
171 ) {
172 if ( !BaseBlacklist::isLocalSource( $wikiPage->getTitle() ) ) {
173 return true;
174 }
175
176 // This sucks because every Blacklist needs to be cleared
177 foreach ( BaseBlacklist::getBlacklistTypes() as $type => $class ) {
178 $blacklist = BaseBlacklist::getInstance( $type );
179 $blacklist->clearCache();
180 }
181
182 return true;
183 }
184
194 public static function onUploadVerifyUpload(
195 UploadBase $upload,
196 User $user,
197 $props,
198 $comment,
199 $pageText,
200 &$error
201 ) {
202 $title = $upload->getTitle();
203
204 // get the link from the not-yet-saved page content.
205 $content = ContentHandler::makeContent( $pageText, $title );
206 $parserOptions = ParserOptions::newCanonical( 'canonical' );
207 $output = $content->getParserOutput( $title, null, $parserOptions );
208 $links = array_keys( $output->getExternalLinks() );
209
210 // HACK: treat comment as a link if it contains anything
211 // that looks like it could be a URL or e-mail address.
212 if ( preg_match( '/\S(\.[^\s\d]{2,}|[\/@]\S)/', $comment ) ) {
213 $links[] = $comment;
214 }
215 if ( !$links ) {
216 return true;
217 }
218
220 $matches = $spamObj->filter( $links, $title );
221
222 if ( $matches !== false ) {
223 $error = new ApiMessage(
224 wfMessage( 'spam-blacklisted-link', Message::listParam( $matches ) ),
225 'spamblacklist',
226 [
227 'spamblacklist' => [ 'matches' => $matches ],
228 ]
229 );
230 }
231
232 return true;
233 }
234}
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Extension of Message implementing IApiMessage.
static getBlacklistTypes()
Return the array of blacklist types currently defined.
static getEmailBlacklist()
static getSpamBlacklist()
static getInstance( $type)
Returns an instance of the given blacklist.
static getTypeFromTitle(Title $title)
Returns the type of blacklist from the given title.
static isLocalSource(Title $title)
Check if the given local page title is a spam regex source.
The edit page/HTML interface (split from Article) The actual database and text munging is still in Ar...
Definition EditPage.php:46
static listParam(array $list, $type='text')
Definition Message.php:1115
Hooks for the spam blacklist extension.
static filterMergedContent(IContextSource $context, Content $content, Status $status, $summary, User $user, $minoredit)
Hook function for EditFilterMergedContent.
static pageSaveContent(WikiPage $wikiPage, User $user, Content $content, $summary, $isMinor, $isWatch, $section, $flags, $revision, Status $status, $baseRevId)
Hook function for PageContentSaveComplete Clear local spam blacklist caches on page save.
static onUploadVerifyUpload(UploadBase $upload, User $user, $props, $comment, $pageText, &$error)
static onParserOutputStashForEdit(WikiPage $page, Content $content, ParserOutput $output)
static userCanSendEmail(&$user, &$hookErr)
Verify that the user can send emails.
static validate(EditPage $editPage, $text, $section, &$hookError)
Hook function for EditFilter Confirm that a local blacklist page being saved is valid,...
static getBadLines( $lines, BaseBlacklist $blacklist)
Returns an array of invalid lines.
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:40
UploadBase and subclasses are the backend of MediaWiki's file uploads.
getTitle()
Returns the title of the file to be uploaded.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
Class representing a MediaWiki article and history.
Definition WikiPage.php:47
getTitle()
Get the title object of the article.
Definition WikiPage.php:298
Base interface for content objects.
Definition Content.php:34
Interface for objects which can provide a MediaWiki context on request.
$context
Definition load.php:45
$lines
Definition router.php:61
$content
Definition router.php:78