MediaWiki  1.34.0
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(
43  wfMessage(
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' );
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 }
TitleBlacklistHooks\onValidateBlacklist
static onValidateBlacklist( $editor, $text, $section, &$error)
EditFilter hook.
Definition: TitleBlacklistHooks.php:183
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
TitleBlacklist\singleton
static singleton()
Get an instance of this class.
Definition: TitleBlacklist.php:36
TitleBlacklistHooks\onClearBlacklist
static onClearBlacklist(WikiPage $wikiPage, &$user, $content, $summary, $isminor, $iswatch, $section)
PageContentSaveComplete hook.
Definition: TitleBlacklistHooks.php:220
getUser
getUser()
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:47
TitleBlacklistHooks\logFilterHitUsername
static logFilterHitUsername( $user, $title, $entry)
Logs the filter username hit to Special:Log if $wgTitleBlacklistLogHits is enabled.
Definition: TitleBlacklistHooks.php:237
TitleBlacklistHooks\onScribuntoExternalLibraries
static onScribuntoExternalLibraries( $engine, array &$extraLibraries)
External Lua library for Scribunto.
Definition: TitleBlacklistHooks.php:257
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
TitleBlacklistHooks\onDisplayBlacklistOverrideNotice
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.
Definition: TitleBlacklistHooks.php:76
TitleBlacklistHooks
Hooks for Title Blacklist.
Definition: TitleBlacklistHooks.php:14
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
TitleBlacklistHooks\onMovePageCheckPermissions
static onMovePageCheckPermissions(Title $oldTitle, Title $newTitle, User $user, $reason, Status $status)
MovePageCheckPermissions hook (1.25+)
Definition: TitleBlacklistHooks.php:110
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:298
TitleBlacklistEntry
Represents a title blacklist entry.
Definition: TitleBlacklistEntry.php:19
Title\getFullText
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition: Title.php:1842
$title
$title
Definition: testCompression.php:34
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:40
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:616
$content
$content
Definition: router.php:78
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:431
TitleBlacklist\parseBlacklist
static parseBlacklist( $list, $sourceName)
Parse blacklist from a string.
Definition: TitleBlacklist.php:162
Title
Represents a title within MediaWiki.
Definition: Title.php:42
TitleBlacklistHooks\onUserCan
static onUserCan( $title, $user, $action, &$result)
getUserPermissionsErrorsExpensive hook
Definition: TitleBlacklistHooks.php:26
$status
return $status
Definition: SyntaxHighlight.php:347
NS_USER
const NS_USER
Definition: Defines.php:62
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:37
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:68
TitleBlacklistHooks\testUserName
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 ...
Definition: TitleBlacklistHooks.php:142
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51