2007-12-08 21:06:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Hooks for Title Blacklist
|
2010-11-08 21:55:05 +00:00
|
|
|
* @author Victor Vasiliev
|
|
|
|
* @copyright © 2007-2010 Victor Vasiliev et al
|
2007-12-29 04:23:57 +00:00
|
|
|
* @license GNU General Public License 2.0 or later
|
2007-12-08 21:06:21 +00:00
|
|
|
*/
|
2008-08-09 01:54:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hooks for the TitleBlacklist class
|
2010-06-06 15:12:22 +00:00
|
|
|
*
|
|
|
|
* @ingroup Extensions
|
2008-08-09 01:54:39 +00:00
|
|
|
*/
|
2007-12-08 21:06:21 +00:00
|
|
|
class TitleBlacklistHooks {
|
2011-03-14 20:51:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getUserPermissionsErrorsExpensive hook
|
|
|
|
*
|
2017-08-10 11:26:29 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @param User $user
|
|
|
|
* @param string $action
|
|
|
|
* @param array &$result
|
2011-03-14 20:51:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2007-12-29 04:23:57 +00:00
|
|
|
public static function userCan( $title, $user, $action, &$result ) {
|
2011-06-24 21:25:50 +00:00
|
|
|
# Some places check createpage, while others check create.
|
|
|
|
# As it stands, upload does createpage, but normalize both
|
|
|
|
# to the same action, to stop future similar bugs.
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( $action === 'createpage' || $action === 'createtalk' ) {
|
2011-06-24 21:25:50 +00:00
|
|
|
$action = 'create';
|
|
|
|
}
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( $action == 'create' || $action == 'edit' || $action == 'upload' ) {
|
2011-07-30 15:13:28 +00:00
|
|
|
$blacklisted = TitleBlacklist::singleton()->userCannot( $title, $user, $action );
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( $blacklisted instanceof TitleBlacklistEntry ) {
|
2015-03-18 20:58:20 +00:00
|
|
|
$errmsg = $blacklisted->getErrorMessage( 'edit' );
|
2017-06-06 16:18:36 +00:00
|
|
|
$params = [
|
2016-06-28 01:01:50 +00:00
|
|
|
$blacklisted->getRaw(),
|
|
|
|
$title->getFullText()
|
2017-06-06 16:18:36 +00:00
|
|
|
];
|
2016-06-28 01:01:50 +00:00
|
|
|
ApiResult::setIndexedTagName( $params, 'param' );
|
2015-10-19 11:55:46 +00:00
|
|
|
$result = ApiMessage::create(
|
|
|
|
wfMessage(
|
|
|
|
$errmsg,
|
|
|
|
htmlspecialchars( $blacklisted->getRaw() ),
|
|
|
|
$title->getFullText()
|
|
|
|
),
|
|
|
|
'titleblacklist-forbidden',
|
2017-06-06 16:18:36 +00:00
|
|
|
[
|
|
|
|
'message' => [
|
2016-06-28 01:01:50 +00:00
|
|
|
'key' => $errmsg,
|
|
|
|
'params' => $params,
|
2017-06-06 16:18:36 +00:00
|
|
|
],
|
2015-10-19 11:55:46 +00:00
|
|
|
'line' => $blacklisted->getRaw(),
|
2017-06-02 15:43:58 +00:00
|
|
|
// As $errmsg usually represents a non-default message here, and ApiBase
|
|
|
|
// uses ->inLanguage( 'en' )->useDatabase( false ) for all messages, it will
|
|
|
|
// never result in useful 'info' text in the API. Try this, extra data seems
|
|
|
|
// to override the default.
|
2015-10-19 11:55:46 +00:00
|
|
|
'info' => 'TitleBlacklist prevents this title from being created',
|
2017-06-06 16:18:36 +00:00
|
|
|
]
|
2015-03-18 20:58:20 +00:00
|
|
|
);
|
2007-12-29 04:23:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-12-08 21:06:21 +00:00
|
|
|
}
|
2007-12-29 04:23:57 +00:00
|
|
|
return true;
|
2007-12-08 21:06:21 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 17:34:57 +00:00
|
|
|
/**
|
|
|
|
* Display a notice if a user is only able to create or edit a page
|
2015-09-11 00:09:14 +00:00
|
|
|
* because they have tboverride.
|
2014-06-19 17:34:57 +00:00
|
|
|
*
|
|
|
|
* @param Title $title
|
2017-08-10 11:26:29 +00:00
|
|
|
* @param int $oldid
|
2014-06-19 17:34:57 +00:00
|
|
|
* @param array &$notices
|
2017-08-10 11:26:29 +00:00
|
|
|
* @return true
|
2014-06-19 17:34:57 +00:00
|
|
|
*/
|
|
|
|
public static function displayBlacklistOverrideNotice( Title $title, $oldid, array &$notices ) {
|
2015-09-11 00:09:14 +00:00
|
|
|
if ( !RequestContext::getMain()->getUser()->isAllowed( 'tboverride' ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-19 17:34:57 +00:00
|
|
|
$blacklisted = TitleBlacklist::singleton()->isBlacklisted(
|
|
|
|
$title,
|
|
|
|
$title->exists() ? 'edit' : 'create'
|
|
|
|
);
|
2015-09-11 00:09:14 +00:00
|
|
|
if ( !$blacklisted ) {
|
|
|
|
return true;
|
2014-06-19 17:34:57 +00:00
|
|
|
}
|
2015-09-11 00:09:14 +00:00
|
|
|
|
|
|
|
$params = $blacklisted->getParams();
|
2016-06-15 02:42:22 +00:00
|
|
|
if ( isset( $params['autoconfirmed'] ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-11 00:09:14 +00:00
|
|
|
$msg = wfMessage( 'titleblacklist-warning' );
|
|
|
|
$notices['titleblacklist'] = $msg->rawParams(
|
|
|
|
htmlspecialchars( $blacklisted->getRaw() ) )->parseAsBlock();
|
2014-06-19 17:34:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-14 20:51:13 +00:00
|
|
|
/**
|
2014-10-27 18:06:03 +00:00
|
|
|
* MovePageCheckPermissions hook (1.25+)
|
2011-03-14 20:51:13 +00:00
|
|
|
*
|
2014-10-27 18:06:03 +00:00
|
|
|
* @param Title $oldTitle
|
|
|
|
* @param Title $newTitle
|
|
|
|
* @param User $user
|
2017-08-10 11:26:29 +00:00
|
|
|
* @param string $reason
|
2014-10-27 18:06:03 +00:00
|
|
|
* @param Status $status
|
2011-03-14 20:51:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-06-02 15:43:58 +00:00
|
|
|
public static function onMovePageCheckPermissions(
|
|
|
|
Title $oldTitle, Title $newTitle, User $user, $reason, Status $status
|
|
|
|
) {
|
2011-07-30 15:13:28 +00:00
|
|
|
$titleBlacklist = TitleBlacklist::singleton();
|
2014-10-27 18:06:03 +00:00
|
|
|
$blacklisted = $titleBlacklist->userCannot( $newTitle, $user, 'move' );
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( !$blacklisted ) {
|
2014-10-27 18:06:03 +00:00
|
|
|
$blacklisted = $titleBlacklist->userCannot( $oldTitle, $user, 'edit' );
|
2011-03-14 20:51:13 +00:00
|
|
|
}
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( $blacklisted instanceof TitleBlacklistEntry ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$status->fatal( ApiMessage::create( [
|
|
|
|
$blacklisted->getErrorMessage( 'move' ),
|
2012-08-29 13:53:38 +00:00
|
|
|
$blacklisted->getRaw(),
|
2014-10-27 18:06:03 +00:00
|
|
|
$oldTitle->getFullText(),
|
2016-11-03 19:16:57 +00:00
|
|
|
$newTitle->getFullText()
|
|
|
|
] ) );
|
2007-12-08 21:06:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-27 18:06:03 +00:00
|
|
|
|
2007-12-08 21:06:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-08-09 01:54:39 +00:00
|
|
|
|
2016-05-07 11:21:02 +00:00
|
|
|
/**
|
|
|
|
* Check whether a user name is acceptable for account creation or autocreation, and explain
|
|
|
|
* why not if that's the case.
|
|
|
|
*
|
|
|
|
* @param string $userName
|
|
|
|
* @param User $creatingUser
|
|
|
|
* @param bool $override Should the test be skipped, if the user has sufficient privileges?
|
|
|
|
* @param bool $log Log blacklist hits to Special:Log
|
|
|
|
* @return StatusValue
|
|
|
|
*/
|
2017-06-02 15:43:58 +00:00
|
|
|
public static function testUserName(
|
|
|
|
$userName, User $creatingUser, $override = true, $log = false
|
|
|
|
) {
|
2010-08-12 09:28:30 +00:00
|
|
|
$title = Title::makeTitleSafe( NS_USER, $userName );
|
2016-05-07 11:21:02 +00:00
|
|
|
$blacklisted = TitleBlacklist::singleton()->userCannot( $title, $creatingUser,
|
2011-09-22 06:10:45 +00:00
|
|
|
'new-account', $override );
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( $blacklisted instanceof TitleBlacklistEntry ) {
|
2014-05-23 19:02:49 +00:00
|
|
|
if ( $log ) {
|
2016-05-07 11:21:02 +00:00
|
|
|
self::logFilterHitUsername( $creatingUser, $title, $blacklisted->getRaw() );
|
2014-05-23 19:02:49 +00:00
|
|
|
}
|
2016-05-07 11:21:02 +00:00
|
|
|
$message = $blacklisted->getErrorMessage( 'new-account' );
|
2016-06-28 01:01:50 +00:00
|
|
|
$params = [
|
|
|
|
$blacklisted->getRaw(),
|
|
|
|
$userName,
|
|
|
|
];
|
|
|
|
ApiResult::setIndexedTagName( $params, 'param' );
|
2016-05-07 11:21:02 +00:00
|
|
|
return StatusValue::newFatal( ApiMessage::create(
|
|
|
|
[ $message, $blacklisted->getRaw(), $userName ],
|
|
|
|
'titleblacklist-forbidden',
|
|
|
|
[
|
2016-06-28 01:01:50 +00:00
|
|
|
'message' => [
|
|
|
|
'key' => $message,
|
|
|
|
'params' => $params,
|
|
|
|
],
|
2016-05-07 11:21:02 +00:00
|
|
|
'line' => $blacklisted->getRaw(),
|
|
|
|
// The text of the message probably isn't useful API info, so do this instead
|
|
|
|
'info' => 'TitleBlacklist prevents this username from being created',
|
|
|
|
]
|
|
|
|
) );
|
2008-08-09 01:54:39 +00:00
|
|
|
}
|
2016-05-07 11:21:02 +00:00
|
|
|
return StatusValue::newGood();
|
2008-08-09 01:54:39 +00:00
|
|
|
}
|
2010-06-04 13:36:29 +00:00
|
|
|
|
2011-08-22 20:37:26 +00:00
|
|
|
/**
|
|
|
|
* EditFilter hook
|
2011-03-17 22:20:01 +00:00
|
|
|
*
|
2017-08-10 11:26:29 +00:00
|
|
|
* @param EditPage $editor
|
|
|
|
* @param string $text
|
|
|
|
* @param string $section
|
|
|
|
* @param string &$error
|
|
|
|
* @return true
|
2011-03-17 22:20:01 +00:00
|
|
|
*/
|
2012-10-20 14:46:01 +00:00
|
|
|
public static function validateBlacklist( $editor, $text, $section, &$error ) {
|
2017-09-03 07:33:59 +00:00
|
|
|
$title = $editor->getTitle();
|
2011-03-17 22:20:01 +00:00
|
|
|
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( $title->getNamespace() == NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) {
|
2011-07-30 15:13:28 +00:00
|
|
|
$blackList = TitleBlacklist::singleton();
|
2013-03-11 17:53:13 +00:00
|
|
|
$bl = $blackList->parseBlacklist( $text, 'page' );
|
2011-07-30 15:13:28 +00:00
|
|
|
$ok = $blackList->validate( $bl );
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( count( $ok ) == 0 ) {
|
2008-09-14 01:44:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-20 14:46:01 +00:00
|
|
|
$errmsg = wfMessage( 'titleblacklist-invalid' )->numParams( count( $ok ) )->text();
|
2017-06-02 15:43:58 +00:00
|
|
|
$errlines = '* <code>' .
|
|
|
|
implode( "</code>\n* <code>", array_map( 'wfEscapeWikiText', $ok ) ) .
|
|
|
|
'</code>';
|
2017-06-06 16:18:36 +00:00
|
|
|
$error = Html::openElement( 'div', [ 'class' => 'errorbox' ] ) .
|
2008-09-14 01:44:04 +00:00
|
|
|
$errmsg .
|
|
|
|
"\n" .
|
|
|
|
$errlines .
|
2010-08-12 08:38:05 +00:00
|
|
|
Html::closeElement( 'div' ) . "\n" .
|
2017-06-06 16:18:36 +00:00
|
|
|
Html::element( 'br', [ 'clear' => 'all' ] ) . "\n";
|
2010-08-12 08:38:05 +00:00
|
|
|
|
2008-09-14 01:44:04 +00:00
|
|
|
// $error will be displayed by the edit class
|
2007-12-23 09:47:36 +00:00
|
|
|
}
|
2008-09-15 16:33:40 +00:00
|
|
|
return true;
|
2007-12-29 04:23:57 +00:00
|
|
|
}
|
2010-08-12 08:38:05 +00:00
|
|
|
|
2011-08-22 20:37:26 +00:00
|
|
|
/**
|
2016-10-09 17:50:12 +00:00
|
|
|
* PageContentSaveComplete hook
|
2011-03-17 22:20:01 +00:00
|
|
|
*
|
2017-12-08 14:15:33 +00:00
|
|
|
* @param WikiPage &$wikiPage
|
2017-08-10 11:26:29 +00:00
|
|
|
* @param User &$user
|
|
|
|
* @param Content $content
|
|
|
|
* @param string $summary
|
|
|
|
* @param bool $isminor
|
|
|
|
* @param bool $iswatch
|
|
|
|
* @param string $section
|
2017-12-08 14:15:33 +00:00
|
|
|
*
|
2017-08-10 11:26:29 +00:00
|
|
|
* @return true
|
2011-03-17 22:20:01 +00:00
|
|
|
*/
|
2017-12-08 14:15:33 +00:00
|
|
|
public static function clearBlacklist(
|
|
|
|
WikiPage &$wikiPage, &$user,
|
2017-06-06 16:18:36 +00:00
|
|
|
$content, $summary, $isminor, $iswatch, $section
|
|
|
|
) {
|
2017-12-08 14:15:33 +00:00
|
|
|
$title = $wikiPage->getTitle();
|
2012-08-29 13:53:38 +00:00
|
|
|
if ( $title->getNamespace() == NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) {
|
2011-07-30 15:13:28 +00:00
|
|
|
TitleBlacklist::singleton()->invalidate();
|
2007-12-29 04:23:57 +00:00
|
|
|
}
|
2007-12-23 09:47:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-11-08 21:55:05 +00:00
|
|
|
|
2014-05-23 19:02:49 +00:00
|
|
|
/**
|
|
|
|
* Logs the filter username hit to Special:Log if
|
|
|
|
* $wgTitleBlacklistLogHits is enabled.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param Title $title
|
|
|
|
* @param string $entry
|
|
|
|
*/
|
|
|
|
public static function logFilterHitUsername( $user, $title, $entry ) {
|
|
|
|
global $wgTitleBlacklistLogHits;
|
|
|
|
if ( $wgTitleBlacklistLogHits ) {
|
|
|
|
$logEntry = new ManualLogEntry( 'titleblacklist', 'hit-username' );
|
|
|
|
$logEntry->setPerformer( $user );
|
|
|
|
$logEntry->setTarget( $title );
|
2017-06-06 16:18:36 +00:00
|
|
|
$logEntry->setParameters( [
|
2014-05-23 19:02:49 +00:00
|
|
|
'4::entry' => $entry,
|
2017-06-06 16:18:36 +00:00
|
|
|
] );
|
2014-05-23 19:02:49 +00:00
|
|
|
$logid = $logEntry->insert();
|
|
|
|
$logEntry->publish( $logid );
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 16:05:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* External Lua library for Scribunto
|
|
|
|
*
|
|
|
|
* @param string $engine
|
2017-08-10 11:26:29 +00:00
|
|
|
* @param array &$extraLibraries
|
2015-05-19 16:05:32 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function scribuntoExternalLibraries( $engine, array &$extraLibraries ) {
|
2017-06-06 16:18:36 +00:00
|
|
|
if ( $engine == 'lua' ) {
|
2015-05-19 16:05:32 +00:00
|
|
|
$extraLibraries['mw.ext.TitleBlacklist'] = 'Scribunto_LuaTitleBlacklistLibrary';
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-09-15 16:33:40 +00:00
|
|
|
}
|