MediaWiki  1.34.0
SpamBlacklistHooks.php
Go to the documentation of this file.
1 <?php
2 
19  public static function filterMergedContent(
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,
62  ) {
63  $links = array_keys( $output->getExternalLinks() );
65  $spamObj->warmCachesForFilter( $page->getTitle(), $links );
66  }
67 
75  public static function userCanSendEmail( &$user, &$hookErr ) {
76  $blacklist = BaseBlacklist::getEmailBlacklist();
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.
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 
219  $spamObj = BaseBlacklist::getSpamBlacklist();
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 }
BaseBlacklist\getSpamBlacklist
static getSpamBlacklist()
Definition: BaseBlacklist.php:99
BaseBlacklist\getBlacklistTypes
static getBlacklistTypes()
Return the array of blacklist types currently defined.
Definition: BaseBlacklist.php:92
BaseBlacklist\getInstance
static getInstance( $type)
Returns an instance of the given blacklist.
Definition: BaseBlacklist.php:118
ParserOutput
Definition: ParserOutput.php:25
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:47
BaseBlacklist\isLocalSource
static isLocalSource(Title $title)
Check if the given local page title is a spam regex source.
Definition: BaseBlacklist.php:151
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
BaseBlacklist\getTypeFromTitle
static getTypeFromTitle(Title $title)
Returns the type of blacklist from the given title.
Definition: BaseBlacklist.php:205
SpamRegexBatch\getBadLines
static getBadLines( $lines, BaseBlacklist $blacklist)
Returns an array of invalid lines.
Definition: SpamRegexBatch.php:119
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1007
SpamBlacklistHooks\filterMergedContent
static filterMergedContent(IContextSource $context, Content $content, Status $status, $summary, User $user, $minoredit)
Hook function for EditFilterMergedContent.
Definition: SpamBlacklistHooks.php:19
ApiMessage
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:26
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
SpamBlacklistHooks\validate
static validate(EditPage $editPage, $text, $section, &$hookError)
Hook function for EditFilter Confirm that a local blacklist page being saved is valid,...
Definition: SpamBlacklistHooks.php:97
$matches
$matches
Definition: NoLocalSettings.php:24
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:298
SpamBlacklistHooks\onParserOutputStashForEdit
static onParserOutputStashForEdit(WikiPage $page, Content $content, ParserOutput $output)
Definition: SpamBlacklistHooks.php:58
$lines
$lines
Definition: router.php:61
$title
$title
Definition: testCompression.php:34
$output
$output
Definition: SyntaxHighlight.php:335
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:135
$content
$content
Definition: router.php:78
ParserOptions\newCanonical
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1073
EditPage
The edit page/HTML interface (split from Article) The actual database and text munging is still in Ar...
Definition: EditPage.php:46
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
$context
$context
Definition: load.php:45
Content
Base interface for content objects.
Definition: Content.php:34
SpamBlacklistHooks
Hooks for the spam blacklist extension.
Definition: SpamBlacklistHooks.php:6
$status
return $status
Definition: SyntaxHighlight.php:347
BaseBlacklist\getEmailBlacklist
static getEmailBlacklist()
Definition: BaseBlacklist.php:106
EditPage\getTitle
getTitle()
Definition: EditPage.php:517
SpamBlacklistHooks\userCanSendEmail
static userCanSendEmail(&$user, &$hookErr)
Verify that the user can send emails.
Definition: SpamBlacklistHooks.php:75
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
SpamBlacklistHooks\onUploadVerifyUpload
static onUploadVerifyUpload(UploadBase $upload, User $user, $props, $comment, $pageText, &$error)
Definition: SpamBlacklistHooks.php:194
SpamBlacklistHooks\pageSaveContent
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.
Definition: SpamBlacklistHooks.php:159
$type
$type
Definition: testCompression.php:48