Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SendBoardElectionNotifications
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace MediaWiki\Extension\WikimediaMessages\Maintenance;
4
5use MediaWiki\Maintenance\Maintenance;
6use MediaWiki\Notification\Notification;
7use MediaWiki\Notification\RecipientSet;
8
9class SendBoardElectionNotifications extends Maintenance {
10
11    public function __construct() {
12        parent::__construct();
13        $this->requireExtension( 'WikimediaMessages' );
14        $this->addDescription( 'Send board election notifications to users (read from stdin)' );
15        $this->setBatchSize( 100 );
16    }
17
18    public function execute() {
19        $services = $this->getServiceContainer();
20        $userFactory = $services->getUserFactory();
21        $notifications = $services->getNotificationService();
22
23        $text = $this->getStdin( Maintenance::STDIN_ALL );
24        $success = 0;
25        foreach ( explode( "\n", $text ) as $i => $userName ) {
26            if ( trim( $userName ) === '' ) {
27                continue;
28            }
29            $user = $userFactory->newFromName( $userName );
30            if ( !$user ) {
31                $this->error( ( $i + 1 ) . ": Invalid username: $userName" );
32                continue;
33            }
34            if ( !$user->isNamed() ) {
35                $this->error( ( $i + 1 ) . ": User is not registered: $userName" );
36                continue;
37            }
38            $notifications->notify( new Notification( 'boardelection2025' ), new RecipientSet( $user ) );
39            $this->output( ( $i + 1 ) . "$userName\n" );
40            $success++;
41            if ( $success % $this->getBatchSize() === 0 ) {
42                $this->waitForReplication();
43            }
44        }
45
46        $this->output( "Notified $success users.\n" );
47    }
48}
49
50$maintClass = SendBoardElectionNotifications::class;