| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Controller; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use Iterator; |
| 7 |
use MapCacheLRU; |
| 8 |
use MediaWiki\Deferred\DeferredUpdates; |
| 9 |
use MediaWiki\Extension\Notifications\AttributeManager; |
| 10 |
use MediaWiki\Extension\Notifications\CachedList; |
| 11 |
use MediaWiki\Extension\Notifications\ContainmentList; |
| 12 |
use MediaWiki\Extension\Notifications\ContainmentSet; |
| 13 |
use MediaWiki\Extension\Notifications\Hooks\HookRunner; |
| 14 |
use MediaWiki\Extension\Notifications\Iterator\FilteredSequentialIterator; |
| 15 |
use MediaWiki\Extension\Notifications\Jobs\NotificationDeleteJob; |
| 16 |
use MediaWiki\Extension\Notifications\Jobs\NotificationJob; |
| 17 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 18 |
use MediaWiki\Extension\Notifications\NotifUser; |
| 19 |
use MediaWiki\Extension\Notifications\OnWikiList; |
| 20 |
use MediaWiki\Extension\Notifications\Services; |
| 21 |
use MediaWiki\Logger\LoggerFactory; |
| 22 |
use MediaWiki\MediaWikiServices; |
| 23 |
use MediaWiki\Title\Title; |
| 24 |
use MediaWiki\User\User; |
| 25 |
use MediaWiki\User\UserIdentity; |
| 26 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class NotificationController { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
protected static $maxRecipientCacheSize = 200; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
protected static $maxUsersTitleCacheSize = 200; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
protected static $blacklistByUser; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
protected static $mutedPageLinkedTitlesCache; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
protected static $wikiBlacklist; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
protected static $whitelistByUser; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static function getCappedNotificationCount( int $count ): int { |
| 83 |
return min( $count, NotifUser::MAX_BADGE_COUNT + 1 ); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static function formatNotificationCount( $count ) { |
| 97 |
$cappedCount = self::getCappedNotificationCount( $count ); |
| 98 |
|
| 99 |
return wfMessage( 'echo-badge-count' )->numParams( $cappedCount )->text(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static function notify( $event, $defer = true ) { |
| 109 |
|
| 110 |
|
| 111 |
if ( $defer && $event->getUseJobQueue() ) { |
| 112 |
|
| 113 |
|
| 114 |
DeferredUpdates::addCallableUpdate( function () use ( $event ) { |
| 115 |
self::enqueueEvent( $event ); |
| 116 |
} ); |
| 117 |
|
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if ( !$event->isEnabledEvent() ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$type = $event->getType(); |
| 128 |
$notifyTypes = self::getEventNotifyTypes( $type ); |
| 129 |
$userIds = []; |
| 130 |
$userIdsCount = 0; |
| 131 |
$services = MediaWikiServices::getInstance(); |
| 132 |
$hookRunner = new HookRunner( $services->getHookContainer() ); |
| 133 |
$userOptionsLookup = $services->getUserOptionsLookup(); |
| 134 |
|
| 135 |
$hasMinorRevision = null; |
| 136 |
|
| 137 |
foreach ( self::getUsersToNotifyForEvent( $event ) as $user ) { |
| 138 |
$userIds[$user->getId()] = $user->getId(); |
| 139 |
$userNotifyTypes = $notifyTypes; |
| 140 |
|
| 141 |
|
| 142 |
if ( !$userOptionsLookup->getOption( $user, 'enotifminoredits' ) ) { |
| 143 |
if ( $hasMinorRevision === null ) { |
| 144 |
|
| 145 |
$hasMinorRevision = self::hasMinorRevision( $event ); |
| 146 |
} |
| 147 |
if ( $hasMinorRevision ) { |
| 148 |
$userNotifyTypes = array_diff( $userNotifyTypes, [ 'email' ] ); |
| 149 |
} |
| 150 |
} |
| 151 |
$hookRunner->onEchoGetNotificationTypes( $user, $event, $userNotifyTypes ); |
| 152 |
|
| 153 |
|
| 154 |
foreach ( $userNotifyTypes as $type ) { |
| 155 |
self::doNotification( $event, $user, $type ); |
| 156 |
} |
| 157 |
|
| 158 |
$userIdsCount++; |
| 159 |
|
| 160 |
if ( $userIdsCount > 1000 ) { |
| 161 |
self::enqueueDeleteJob( $userIds, $event ); |
| 162 |
$userIds = []; |
| 163 |
$userIdsCount = 0; |
| 164 |
} |
| 165 |
|
| 166 |
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory(); |
| 167 |
$stats->increment( 'echo.notification.all' ); |
| 168 |
$stats->increment( "echo.notification.$type" ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
if ( $userIds ) { |
| 173 |
self::enqueueDeleteJob( $userIds, $event ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private static function hasMinorRevision( Event $event ) { |
| 184 |
$revId = $event->getExtraParam( 'revid' ); |
| 185 |
if ( !$revId ) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
$revisionStore = MediaWikiServices::getInstance()->getRevisionStore(); |
| 190 |
$rev = $revisionStore->getRevisionById( $revId, IDBAccessObject::READ_LATEST ); |
| 191 |
if ( !$rev ) { |
| 192 |
$logger = LoggerFactory::getInstance( 'Echo' ); |
| 193 |
$logger->debug( |
| 194 |
'Notifying for event {eventId}. Revision \'{revId}\' not found.', |
| 195 |
[ |
| 196 |
'eventId' => $event->getId(), |
| 197 |
'revId' => $revId, |
| 198 |
] |
| 199 |
); |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
return $rev->isMinor(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public static function enqueueDeleteJob( array $userIds, Event $event ) { |
| 213 |
|
| 214 |
if ( !$userIds ) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
$jobQueueGroup = MediaWikiServices::getInstance()->getJobQueueGroup(); |
| 219 |
$job = new NotificationDeleteJob( |
| 220 |
$event->getTitle() ?: Title::newMainPage(), |
| 221 |
[ |
| 222 |
'userIds' => $userIds |
| 223 |
], |
| 224 |
$jobQueueGroup |
| 225 |
); |
| 226 |
$jobQueueGroup->push( $job ); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public static function getEventNotifyTypes( $eventType ) { |
| 237 |
$attributeManager = Services::getInstance()->getAttributeManager(); |
| 238 |
|
| 239 |
$category = $attributeManager->getNotificationCategory( $eventType ); |
| 240 |
|
| 241 |
return array_keys( array_filter( |
| 242 |
$attributeManager->getNotifyTypeAvailabilityForCategory( $category ) |
| 243 |
) ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public static function getEventParams( Event $event ) { |
| 252 |
$delay = $event->getExtraParam( 'delay' ); |
| 253 |
$rootJobSignature = $event->getExtraParam( 'rootJobSignature' ); |
| 254 |
$rootJobTimestamp = $event->getExtraParam( 'rootJobTimestamp' ); |
| 255 |
|
| 256 |
return [ 'eventId' => $event->getId() ] |
| 257 |
+ ( $delay ? [ 'jobReleaseTimestamp' => (int)wfTimestamp() + $delay ] : [] ) |
| 258 |
+ ( $rootJobSignature ? [ 'rootJobSignature' => $rootJobSignature ] : [] ) |
| 259 |
+ ( $rootJobTimestamp ? [ 'rootJobTimestamp' => $rootJobTimestamp ] : [] ); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public static function enqueueEvent( Event $event ) { |
| 268 |
$queue = MediaWikiServices::getInstance()->getJobQueueGroup(); |
| 269 |
$params = self::getEventParams( $event ); |
| 270 |
|
| 271 |
$job = new NotificationJob( |
| 272 |
$event->getTitle() ?: Title::newMainPage(), $params |
| 273 |
); |
| 274 |
|
| 275 |
if ( isset( $params[ 'jobReleaseTimestamp' ] ) && !$queue->get( $job->getType() )->delayedJobsEnabled() ) { |
| 276 |
$logger = LoggerFactory::getInstance( 'Echo' ); |
| 277 |
$logger->warning( |
| 278 |
'Delayed jobs are not enabled. Skipping enqueuing event {id} of type {type}.', |
| 279 |
[ |
| 280 |
'id' => $event->getId(), |
| 281 |
'type' => $event->getType() |
| 282 |
] |
| 283 |
); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
$queue->push( $job ); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public static function isBlacklistedByUser( Event $event, User $user ) { |
| 299 |
global $wgEchoAgentBlacklist, $wgEchoPerUserBlacklist; |
| 300 |
|
| 301 |
if ( !$event->getAgent() ) { |
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
if ( self::$blacklistByUser === null ) { |
| 307 |
self::$blacklistByUser = new MapCacheLRU( self::$maxRecipientCacheSize ); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
if ( !self::$blacklistByUser->has( (string)$user->getId() ) ) { |
| 312 |
$blacklist = new ContainmentSet( $user ); |
| 313 |
|
| 314 |
|
| 315 |
$blacklist->addArray( $wgEchoAgentBlacklist ); |
| 316 |
|
| 317 |
|
| 318 |
$wikiBlacklist = self::getWikiBlacklist(); |
| 319 |
if ( $wikiBlacklist !== null ) { |
| 320 |
$blacklist->add( $wikiBlacklist ); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
if ( $wgEchoPerUserBlacklist ) { |
| 325 |
$blacklist->addFromUserOption( 'echo-notifications-blacklist' ); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
self::$blacklistByUser->set( (string)$user->getId(), $blacklist ); |
| 330 |
} else { |
| 331 |
|
| 332 |
$blacklist = self::$blacklistByUser->get( (string)$user->getId() ); |
| 333 |
} |
| 334 |
return $blacklist->contains( $event->getAgent()->getName() ) || |
| 335 |
( $wgEchoPerUserBlacklist && |
| 336 |
$event->getType() === 'page-linked' && |
| 337 |
self::isPageLinkedTitleMutedByUser( $event->getTitle(), $user ) ); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public static function isPageLinkedTitleMutedByUser( Title $title, User $user ) { |
| 348 |
if ( self::$mutedPageLinkedTitlesCache === null ) { |
| 349 |
self::$mutedPageLinkedTitlesCache = new MapCacheLRU( self::$maxUsersTitleCacheSize ); |
| 350 |
} |
| 351 |
if ( !self::$mutedPageLinkedTitlesCache->has( (string)$user->getId() ) ) { |
| 352 |
$pageLinkedTitleMutedList = new ContainmentSet( $user ); |
| 353 |
$pageLinkedTitleMutedList->addTitleIDsFromUserOption( |
| 354 |
'echo-notifications-page-linked-title-muted-list' |
| 355 |
); |
| 356 |
self::$mutedPageLinkedTitlesCache->set( (string)$user->getId(), $pageLinkedTitleMutedList ); |
| 357 |
} else { |
| 358 |
$pageLinkedTitleMutedList = self::$mutedPageLinkedTitlesCache->get( (string)$user->getId() ); |
| 359 |
} |
| 360 |
return $pageLinkedTitleMutedList->contains( (string)$title->getArticleID() ); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
protected static function getWikiBlacklist() { |
| 367 |
global $wgEchoOnWikiBlacklist; |
| 368 |
if ( !$wgEchoOnWikiBlacklist ) { |
| 369 |
return null; |
| 370 |
} |
| 371 |
if ( self::$wikiBlacklist === null ) { |
| 372 |
$clusterCache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 373 |
self::$wikiBlacklist = new CachedList( |
| 374 |
$clusterCache, |
| 375 |
$clusterCache->makeKey( "echo_on_wiki_blacklist" ), |
| 376 |
new OnWikiList( NS_MEDIAWIKI, $wgEchoOnWikiBlacklist ) |
| 377 |
); |
| 378 |
} |
| 379 |
|
| 380 |
return self::$wikiBlacklist; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public static function isWhitelistedByUser( Event $event, User $user ) { |
| 391 |
$clusterCache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 392 |
global $wgEchoPerUserWhitelistFormat; |
| 393 |
|
| 394 |
if ( $wgEchoPerUserWhitelistFormat === null || !$event->getAgent() ) { |
| 395 |
return false; |
| 396 |
} |
| 397 |
|
| 398 |
$userId = $user->getId(); |
| 399 |
if ( $userId === 0 ) { |
| 400 |
|
| 401 |
return false; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
if ( self::$whitelistByUser === null ) { |
| 406 |
self::$whitelistByUser = new MapCacheLRU( self::$maxRecipientCacheSize ); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
if ( !self::$whitelistByUser->has( (string)$userId ) ) { |
| 411 |
$whitelist = new ContainmentSet( $user ); |
| 412 |
self::$whitelistByUser->set( (string)$userId, $whitelist ); |
| 413 |
$whitelist->addOnWiki( |
| 414 |
NS_USER, |
| 415 |
sprintf( $wgEchoPerUserWhitelistFormat, $user->getName() ), |
| 416 |
$clusterCache, |
| 417 |
$clusterCache->makeKey( "echo_on_wiki_whitelist_" . $userId ) |
| 418 |
); |
| 419 |
} else { |
| 420 |
|
| 421 |
$whitelist = self::$whitelistByUser->get( (string)$userId ); |
| 422 |
} |
| 423 |
return $whitelist->contains( $event->getAgent()->getName() ); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
public static function doNotification( $event, $user, $type ) { |
| 434 |
global $wgEchoNotifiers; |
| 435 |
|
| 436 |
if ( !isset( $wgEchoNotifiers[$type] ) ) { |
| 437 |
throw new InvalidArgumentException( "Invalid notification type $type" ); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
if ( !$user->isRegistered() ) { |
| 442 |
throw new InvalidArgumentException( "Cannot notify anonymous user: {$user->getName()}" ); |
| 443 |
} |
| 444 |
|
| 445 |
( $wgEchoNotifiers[$type] )( $user, $event ); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public static function evaluateUserCallable( Event $event, $locator = AttributeManager::ATTR_LOCATORS ) { |
| 457 |
$attributeManager = Services::getInstance()->getAttributeManager(); |
| 458 |
$type = $event->getType(); |
| 459 |
$result = []; |
| 460 |
foreach ( $attributeManager->getUserCallable( $type, $locator ) as $callable ) { |
| 461 |
|
| 462 |
|
| 463 |
if ( is_array( $callable ) ) { |
| 464 |
$options = $callable; |
| 465 |
$spliced = array_splice( $options, 0, 1, [ $event ] ); |
| 466 |
$callable = reset( $spliced ); |
| 467 |
} else { |
| 468 |
$options = [ $event ]; |
| 469 |
} |
| 470 |
if ( is_callable( $callable ) ) { |
| 471 |
$result[] = $callable( ...$options ); |
| 472 |
} else { |
| 473 |
wfDebugLog( __CLASS__, __FUNCTION__ . ": Invalid $locator returned for $type" ); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
return $result; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public static function getUsersToNotifyForEvent( Event $event ) { |
| 487 |
$notify = new FilteredSequentialIterator; |
| 488 |
foreach ( self::evaluateUserCallable( $event, AttributeManager::ATTR_LOCATORS ) as $users ) { |
| 489 |
$notify->add( $users ); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
$users = []; |
| 495 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
| 496 |
->onEchoGetDefaultNotifiedUsers( $event, $users ); |
| 497 |
if ( $users ) { |
| 498 |
$notify->add( $users ); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
foreach ( self::evaluateUserCallable( $event, AttributeManager::ATTR_FILTERS ) as $users ) { |
| 503 |
|
| 504 |
$users = is_array( $users ) ? $users : iterator_to_array( $users ); |
| 505 |
$notify->addFilter( static function ( UserIdentity $user ) use ( $users ) { |
| 506 |
|
| 507 |
|
| 508 |
$userId = $user->getId(); |
| 509 |
$userIds = array_map( static function ( UserIdentity $user ) { |
| 510 |
return $user->getId(); |
| 511 |
}, $users ); |
| 512 |
return !in_array( $userId, $userIds ); |
| 513 |
} ); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
$seen = []; |
| 518 |
$fname = __METHOD__; |
| 519 |
$notify->addFilter( static function ( $user ) use ( &$seen, $fname ) { |
| 520 |
if ( !$user instanceof User ) { |
| 521 |
wfDebugLog( $fname, 'Expected all User instances, received: ' . |
| 522 |
get_debug_type( $user ) |
| 523 |
); |
| 524 |
|
| 525 |
return false; |
| 526 |
} |
| 527 |
if ( !$user->isRegistered() || isset( $seen[$user->getId()] ) ) { |
| 528 |
return false; |
| 529 |
} |
| 530 |
$seen[$user->getId()] = true; |
| 531 |
|
| 532 |
return true; |
| 533 |
} ); |
| 534 |
|
| 535 |
|
| 536 |
if ( !$event->canNotifyAgent() && $event->getAgent() ) { |
| 537 |
$agentId = $event->getAgent()->getId(); |
| 538 |
$notify->addFilter( static function ( $user ) use ( $agentId ) { |
| 539 |
return $user->getId() != $agentId; |
| 540 |
} ); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
$notify->addFilter( function ( $user ) use ( $event ) { |
| 545 |
$title = $event->getTitle(); |
| 546 |
|
| 547 |
if ( self::isBlacklistedByUser( $event, $user ) && |
| 548 |
( |
| 549 |
$title === null || |
| 550 |
!( |
| 551 |
|
| 552 |
|
| 553 |
$title->getText() === $user->getName() && |
| 554 |
$title->getNamespace() === NS_USER_TALK |
| 555 |
) |
| 556 |
) |
| 557 |
) { |
| 558 |
return self::isWhitelistedByUser( $event, $user ); |
| 559 |
} |
| 560 |
|
| 561 |
return true; |
| 562 |
} ); |
| 563 |
|
| 564 |
return $notify->getIterator(); |
| 565 |
} |
| 566 |
} |