7use Psr\Log\LoggerInterface;
10use Wikimedia\ObjectFactory\ObjectFactory;
24 'class' => RecentChangeNotificationHandler::class,
29 'types' => [
'mediawiki.recent_change' ]
33 private array $handlersByType = [];
35 private LoggerInterface $logger;
36 private ObjectFactory $objectFactory;
41 LoggerInterface $logger,
42 ObjectFactory $objectFactory,
46 $this->logger = $logger;
47 $this->objectFactory = $objectFactory;
48 $this->middlewareChain = $middlewareChain;
49 $this->specs = $specs;
52 private function getHandlers(): array {
53 if ( $this->handlersByType === [] ) {
54 foreach ( $this->specs as $spec ) {
55 $obj = $this->objectFactory->createObject( $spec, [
'assertClass' => NotificationHandler::class ] );
56 foreach ( $spec[
'types'] as $type ) {
57 if ( str_contains( $type,
'*' ) && $type !==
'*' ) {
59 throw new RuntimeException(
"Partial wildcards are not supported, tried to use \"$type\"" );
61 if ( isset( $this->handlersByType[$type] ) ) {
63 throw new RuntimeException(
"Handler for notification type \"$type\" already present" );
65 $this->handlersByType[$type] = $obj;
69 return $this->handlersByType;
77 $handlers = $this->getHandlers();
82 $batch = $this->middlewareChain->process(
89 foreach ( $batch as $envelope ) {
90 $scheduledNotification = $envelope->getNotification();
91 $scheduledRecipients = $envelope->getRecipientSet();
92 $handler = $handlers[$scheduledNotification->getType()] ?? $handlers[
'*'] ??
null;
93 if ( $handler ===
null ) {
94 $this->logger->warning(
"No handler defined for notification type {type}", [
95 'type' => $scheduledNotification->getType(),
99 $handler->notify( $scheduledNotification, $scheduledRecipients );
110 $notification = new Types\SimpleNotification( $message );
111 $this->notify( $notification,
new RecipientSet( [ $recipient ] ) );
if(!defined('MW_SETUP_CALLBACK'))