| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
l |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
namespace MediaWiki\Extension\TitleBlacklist; |
| 10 |
|
| 11 |
use ManualLogEntry; |
| 12 |
use MediaWiki\Api\ApiMessage; |
| 13 |
use MediaWiki\Api\ApiResult; |
| 14 |
use MediaWiki\Context\RequestContext; |
| 15 |
use MediaWiki\EditPage\EditPage; |
| 16 |
use MediaWiki\Hook\EditFilterHook; |
| 17 |
use MediaWiki\Hook\MovePageCheckPermissionsHook; |
| 18 |
use MediaWiki\Hook\TitleGetEditNoticesHook; |
| 19 |
use MediaWiki\Html\Html; |
| 20 |
use MediaWiki\Permissions\GrantsInfo; |
| 21 |
use MediaWiki\Permissions\Hook\GetUserPermissionsErrorsExpensiveHook; |
| 22 |
use MediaWiki\Revision\RevisionRecord; |
| 23 |
use MediaWiki\Status\Status; |
| 24 |
use MediaWiki\Storage\EditResult; |
| 25 |
use MediaWiki\Storage\Hook\PageSaveCompleteHook; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use MediaWiki\User\User; |
| 28 |
use MediaWiki\User\UserIdentity; |
| 29 |
use StatusValue; |
| 30 |
use Wikimedia\Message\MessageSpecifier; |
| 31 |
use WikiPage; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class Hooks implements |
| 39 |
EditFilterHook, |
| 40 |
TitleGetEditNoticesHook, |
| 41 |
MovePageCheckPermissionsHook, |
| 42 |
GetUserPermissionsErrorsExpensiveHook, |
| 43 |
PageSaveCompleteHook |
| 44 |
{ |
| 45 |
|
| 46 |
public static function onRegistration() { |
| 47 |
global $wgGrantRiskGroups; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
if ( $wgGrantRiskGroups['createaccount'] !== GrantsInfo::RISK_INTERNAL ) { |
| 52 |
$wgGrantRiskGroups['createaccount'] = GrantsInfo::RISK_SECURITY; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function onGetUserPermissionsErrorsExpensive( $title, $user, $action, &$result ) { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
if ( $action === 'createpage' || $action === 'createtalk' ) { |
| 71 |
$action = 'create'; |
| 72 |
} |
| 73 |
if ( $action !== 'create' && $action !== 'edit' && $action !== 'upload' ) { |
| 74 |
return true; |
| 75 |
} |
| 76 |
$blacklisted = TitleBlacklist::singleton()->userCannot( $title, $user, $action ); |
| 77 |
if ( !$blacklisted ) { |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
$errmsg = $blacklisted->getErrorMessage( 'edit' ); |
| 82 |
$params = [ |
| 83 |
$blacklisted->getRaw(), |
| 84 |
$title->getFullText() |
| 85 |
]; |
| 86 |
ApiResult::setIndexedTagName( $params, 'param' ); |
| 87 |
$result = ApiMessage::create( |
| 88 |
wfMessage( |
| 89 |
$errmsg, |
| 90 |
wfEscapeWikiText( $blacklisted->getRaw() ), |
| 91 |
$title->getFullText() |
| 92 |
), |
| 93 |
'titleblacklist-forbidden', |
| 94 |
[ |
| 95 |
'message' => [ |
| 96 |
'key' => $errmsg, |
| 97 |
'params' => $params, |
| 98 |
], |
| 99 |
'line' => $blacklisted->getRaw(), |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
'info' => 'TitleBlacklist prevents this title from being created', |
| 105 |
] |
| 106 |
); |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function onTitleGetEditNotices( $title, $oldid, &$notices ) { |
| 119 |
if ( !RequestContext::getMain()->getUser()->isAllowed( 'tboverride' ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
$blacklisted = TitleBlacklist::singleton()->isBlacklisted( |
| 124 |
$title, |
| 125 |
$title->exists() ? 'edit' : 'create' |
| 126 |
); |
| 127 |
if ( !$blacklisted ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
$params = $blacklisted->getParams(); |
| 132 |
if ( isset( $params['autoconfirmed'] ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$msg = wfMessage( 'titleblacklist-warning' ); |
| 137 |
$notices['titleblacklist'] = $msg->plaintextParams( $blacklisted->getRaw() ) |
| 138 |
->parseAsBlock(); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public function onMovePageCheckPermissions( |
| 153 |
$oldTitle, $newTitle, $user, $reason, $status |
| 154 |
) { |
| 155 |
$titleBlacklist = TitleBlacklist::singleton(); |
| 156 |
$blacklisted = $titleBlacklist->userCannot( $newTitle, $user, 'move' ); |
| 157 |
if ( !$blacklisted ) { |
| 158 |
$blacklisted = $titleBlacklist->userCannot( $oldTitle, $user, 'edit' ); |
| 159 |
} |
| 160 |
if ( $blacklisted instanceof TitleBlacklistEntry ) { |
| 161 |
$status->fatal( ApiMessage::create( [ |
| 162 |
$blacklisted->getErrorMessage( 'move' ), |
| 163 |
wfEscapeWikiText( $blacklisted->getRaw() ), |
| 164 |
$oldTitle->getFullText(), |
| 165 |
$newTitle->getFullText() |
| 166 |
] ) ); |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public static function testUserName( |
| 185 |
$userName, User $creatingUser, $override = true, $log = false |
| 186 |
) { |
| 187 |
$title = Title::makeTitleSafe( NS_USER, $userName ); |
| 188 |
$blacklisted = TitleBlacklist::singleton()->userCannot( $title, $creatingUser, |
| 189 |
'new-account', $override ); |
| 190 |
if ( !$blacklisted ) { |
| 191 |
return StatusValue::newGood(); |
| 192 |
} |
| 193 |
|
| 194 |
if ( $log ) { |
| 195 |
self::logFilterHitUsername( $creatingUser, $title, $blacklisted->getRaw() ); |
| 196 |
} |
| 197 |
$message = $blacklisted->getErrorMessage( 'new-account' ); |
| 198 |
$params = [ |
| 199 |
$blacklisted->getRaw(), |
| 200 |
$userName, |
| 201 |
]; |
| 202 |
ApiResult::setIndexedTagName( $params, 'param' ); |
| 203 |
return StatusValue::newFatal( ApiMessage::create( |
| 204 |
[ $message, wfEscapeWikiText( $blacklisted->getRaw() ), $userName ], |
| 205 |
'titleblacklist-forbidden', |
| 206 |
[ |
| 207 |
'message' => [ |
| 208 |
'key' => $message, |
| 209 |
'params' => $params, |
| 210 |
], |
| 211 |
'line' => $blacklisted->getRaw(), |
| 212 |
|
| 213 |
'info' => 'TitleBlacklist prevents this username from being created', |
| 214 |
] |
| 215 |
) ); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function onEditFilter( $editor, $text, $section, &$error, $summary ) { |
| 228 |
$title = $editor->getTitle(); |
| 229 |
|
| 230 |
if ( $title->getNamespace() == NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) { |
| 231 |
$blackList = TitleBlacklist::singleton(); |
| 232 |
$bl = TitleBlacklist::parseBlacklist( $text, 'page' ); |
| 233 |
$ok = $blackList->validate( $bl ); |
| 234 |
if ( $ok === [] ) { |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
$errmsg = wfMessage( 'titleblacklist-invalid' )->numParams( count( $ok ) )->text(); |
| 239 |
$errlines = '* <code>' . |
| 240 |
implode( "</code>\n* <code>", array_map( 'wfEscapeWikiText', $ok ) ) . |
| 241 |
'</code>'; |
| 242 |
$error = Html::errorBox( |
| 243 |
$errmsg . "\n" . $errlines |
| 244 |
) . "\n" . |
| 245 |
Html::element( 'br', [ 'clear' => 'all' ] ) . "\n"; |
| 246 |
|
| 247 |
|
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function onPageSaveComplete( |
| 262 |
$wikiPage, |
| 263 |
$userIdentity, |
| 264 |
$summary, |
| 265 |
$flags, |
| 266 |
$revisionRecord, |
| 267 |
$editResult |
| 268 |
) { |
| 269 |
$title = $wikiPage->getTitle(); |
| 270 |
if ( $title->getNamespace() === NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) { |
| 271 |
TitleBlacklist::singleton()->invalidate(); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public static function logFilterHitUsername( $user, $title, $entry ) { |
| 284 |
global $wgTitleBlacklistLogHits; |
| 285 |
if ( $wgTitleBlacklistLogHits ) { |
| 286 |
$logEntry = new ManualLogEntry( 'titleblacklist', 'hit-username' ); |
| 287 |
$logEntry->setPerformer( $user ); |
| 288 |
$logEntry->setTarget( $title ); |
| 289 |
$logEntry->setParameters( [ |
| 290 |
'4::entry' => $entry, |
| 291 |
] ); |
| 292 |
$logid = $logEntry->insert(); |
| 293 |
$logEntry->publish( $logid ); |
| 294 |
} |
| 295 |
} |
| 296 |
} |