MediaWiki REL1_34
TitleBlacklistHooks.php
Go to the documentation of this file.
1<?php
15
26 public static function onUserCan( $title, $user, $action, &$result ) {
27 # Some places check createpage, while others check create.
28 # As it stands, upload does createpage, but normalize both
29 # to the same action, to stop future similar bugs.
30 if ( $action === 'createpage' || $action === 'createtalk' ) {
31 $action = 'create';
32 }
33 if ( $action == 'create' || $action == 'edit' || $action == 'upload' ) {
34 $blacklisted = TitleBlacklist::singleton()->userCannot( $title, $user, $action );
35 if ( $blacklisted instanceof TitleBlacklistEntry ) {
36 $errmsg = $blacklisted->getErrorMessage( 'edit' );
37 $params = [
38 $blacklisted->getRaw(),
39 $title->getFullText()
40 ];
41 ApiResult::setIndexedTagName( $params, 'param' );
42 $result = ApiMessage::create(
44 $errmsg,
45 htmlspecialchars( $blacklisted->getRaw() ),
46 $title->getFullText()
47 ),
48 'titleblacklist-forbidden',
49 [
50 'message' => [
51 'key' => $errmsg,
52 'params' => $params,
53 ],
54 'line' => $blacklisted->getRaw(),
55 // As $errmsg usually represents a non-default message here, and ApiBase
56 // uses ->inLanguage( 'en' )->useDatabase( false ) for all messages, it will
57 // never result in useful 'info' text in the API. Try this, extra data seems
58 // to override the default.
59 'info' => 'TitleBlacklist prevents this title from being created',
60 ]
61 );
62 return false;
63 }
64 }
65 return true;
66 }
67
76 public static function onDisplayBlacklistOverrideNotice( Title $title, $oldid, array &$notices ) {
77 if ( !RequestContext::getMain()->getUser()->isAllowed( 'tboverride' ) ) {
78 return;
79 }
80
81 $blacklisted = TitleBlacklist::singleton()->isBlacklisted(
82 $title,
83 $title->exists() ? 'edit' : 'create'
84 );
85 if ( !$blacklisted ) {
86 return;
87 }
88
89 $params = $blacklisted->getParams();
90 if ( isset( $params['autoconfirmed'] ) ) {
91 return;
92 }
93
94 $msg = wfMessage( 'titleblacklist-warning' );
95 $notices['titleblacklist'] = $msg->plaintextParams( $blacklisted->getRaw() )
96 ->parseAsBlock();
97 }
98
110 public static function onMovePageCheckPermissions(
111 Title $oldTitle, Title $newTitle, User $user, $reason, Status $status
112 ) {
113 $titleBlacklist = TitleBlacklist::singleton();
114 $blacklisted = $titleBlacklist->userCannot( $newTitle, $user, 'move' );
115 if ( !$blacklisted ) {
116 $blacklisted = $titleBlacklist->userCannot( $oldTitle, $user, 'edit' );
117 }
118 if ( $blacklisted instanceof TitleBlacklistEntry ) {
119 $status->fatal( ApiMessage::create( [
120 $blacklisted->getErrorMessage( 'move' ),
121 $blacklisted->getRaw(),
122 $oldTitle->getFullText(),
123 $newTitle->getFullText()
124 ] ) );
125 return false;
126 }
127
128 return true;
129 }
130
142 public static function testUserName(
143 $userName, User $creatingUser, $override = true, $log = false
144 ) {
145 $title = Title::makeTitleSafe( NS_USER, $userName );
146 $blacklisted = TitleBlacklist::singleton()->userCannot( $title, $creatingUser,
147 'new-account', $override );
148 if ( $blacklisted instanceof TitleBlacklistEntry ) {
149 if ( $log ) {
150 self::logFilterHitUsername( $creatingUser, $title, $blacklisted->getRaw() );
151 }
152 $message = $blacklisted->getErrorMessage( 'new-account' );
153 $params = [
154 $blacklisted->getRaw(),
155 $userName,
156 ];
157 ApiResult::setIndexedTagName( $params, 'param' );
158 return StatusValue::newFatal( ApiMessage::create(
159 [ $message, $blacklisted->getRaw(), $userName ],
160 'titleblacklist-forbidden',
161 [
162 'message' => [
163 'key' => $message,
164 'params' => $params,
165 ],
166 'line' => $blacklisted->getRaw(),
167 // The text of the message probably isn't useful API info, so do this instead
168 'info' => 'TitleBlacklist prevents this username from being created',
169 ]
170 ) );
171 }
172 return StatusValue::newGood();
173 }
174
183 public static function onValidateBlacklist( $editor, $text, $section, &$error ) {
184 $title = $editor->getTitle();
185
186 if ( $title->getNamespace() == NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) {
187 $blackList = TitleBlacklist::singleton();
188 $bl = TitleBlacklist::parseBlacklist( $text, 'page' );
189 $ok = $blackList->validate( $bl );
190 if ( $ok === [] ) {
191 return;
192 }
193
194 $errmsg = wfMessage( 'titleblacklist-invalid' )->numParams( count( $ok ) )->text();
195 $errlines = '* <code>' .
196 implode( "</code>\n* <code>", array_map( 'wfEscapeWikiText', $ok ) ) .
197 '</code>';
198 $error = Html::openElement( 'div', [ 'class' => 'errorbox' ] ) .
199 $errmsg .
200 "\n" .
201 $errlines .
202 Html::closeElement( 'div' ) . "\n" .
203 Html::element( 'br', [ 'clear' => 'all' ] ) . "\n";
204
205 // $error will be displayed by the edit class
206 }
207 }
208
220 public static function onClearBlacklist(
221 WikiPage $wikiPage, &$user, $content, $summary, $isminor, $iswatch, $section
222 ) {
223 $title = $wikiPage->getTitle();
224 if ( $title->getNamespace() === NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) {
225 TitleBlacklist::singleton()->invalidate();
226 }
227 }
228
237 public static function logFilterHitUsername( $user, $title, $entry ) {
238 global $wgTitleBlacklistLogHits;
239 if ( $wgTitleBlacklistLogHits ) {
240 $logEntry = new ManualLogEntry( 'titleblacklist', 'hit-username' );
241 $logEntry->setPerformer( $user );
242 $logEntry->setTarget( $title );
243 $logEntry->setParameters( [
244 '4::entry' => $entry,
245 ] );
246 $logid = $logEntry->insert();
247 $logEntry->publish( $logid );
248 }
249 }
250
257 public static function onScribuntoExternalLibraries( $engine, array &$extraLibraries ) {
258 if ( $engine == 'lua' ) {
259 $extraLibraries['mw.ext.TitleBlacklist'] = 'Scribunto_LuaTitleBlacklistLibrary';
260 }
261 }
262}
getUser()
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Class for creating new log entries and inserting them into the database.
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
Represents a title blacklist entry.
Hooks for Title Blacklist.
static onClearBlacklist(WikiPage $wikiPage, &$user, $content, $summary, $isminor, $iswatch, $section)
PageContentSaveComplete hook.
static onDisplayBlacklistOverrideNotice(Title $title, $oldid, array &$notices)
Display a notice if a user is only able to create or edit a page because they have tboverride.
static onScribuntoExternalLibraries( $engine, array &$extraLibraries)
External Lua library for Scribunto.
static testUserName( $userName, User $creatingUser, $override=true, $log=false)
Check whether a user name is acceptable for account creation or autocreation, and explain why not if ...
static logFilterHitUsername( $user, $title, $entry)
Logs the filter username hit to Special:Log if $wgTitleBlacklistLogHits is enabled.
static onMovePageCheckPermissions(Title $oldTitle, Title $newTitle, User $user, $reason, Status $status)
MovePageCheckPermissions hook (1.25+)
static onUserCan( $title, $user, $action, &$result)
getUserPermissionsErrorsExpensive hook
static onValidateBlacklist( $editor, $text, $section, &$error)
EditFilter hook.
static singleton()
Get an instance of this class.
static parseBlacklist( $list, $sourceName)
Parse blacklist from a string.
Represents a title within MediaWiki.
Definition Title.php:42
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition Title.php:1842
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
const NS_USER
Definition Defines.php:71
const NS_MEDIAWIKI
Definition Defines.php:77
$content
Definition router.php:78