Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SendMessages
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2
3namespace MediaWiki\MassMessage;
4
5use Maintenance;
6use MediaWiki\MassMessage\Job\MassMessageServerSideJob;
7use MediaWiki\MassMessage\Job\MassMessageSubmitJob;
8use MediaWiki\Title\Title;
9use MediaWiki\WikiMap\WikiMap;
10
11$IP = getenv( 'MW_INSTALL_PATH' );
12if ( $IP === false ) {
13    $IP = __DIR__ . '/../../..';
14}
15require_once "$IP/maintenance/Maintenance.php";
16
17/**
18 * Script to send MassMessages server-side
19 *
20 * Expects a page list formatted as a .tsv file, with "PageName<tab>WikiId" on each line.
21 * Subject line and message body are also stored as files.
22 */
23class SendMessages extends Maintenance {
24    public function __construct() {
25        parent::__construct();
26        $this->addOption( 'pagelist', 'Name of file with a list of pages to send to in it', true, true );
27        $this->addOption( 'subject', 'Name of file with the subject in it', true, true );
28        $this->addOption( 'message', 'Name of file with the message body in it', true, true );
29        $this->requireExtension( 'MassMessage' );
30    }
31
32    public function execute() {
33        $info = [];
34
35        foreach ( [ 'pagelist', 'subject', 'message' ] as $arg ) {
36            $option = $this->getOption( $arg );
37            if ( !is_file( $this->getOption( $arg ) ) ) {
38                $this->fatalError( "Required argument $arg was passed an invalid filename.\n" );
39            }
40
41            // Also include check if the file size before even reading the file
42            if ( $arg !== 'pagelist' && filesize( $this->getOption( $arg ) ) !== 0 ) {
43                $contents = file_get_contents( $option );
44                if ( $contents !== false ) {
45                    $info[$arg] = trim( $contents );
46                } else {
47                    $this->fatalError( "Unable to read $option.\n" );
48                }
49            } else {
50                $this->fatalError( "$option is empty, must have some content.\n" );
51            }
52        }
53
54        $list = $this->getOption( 'pagelist' );
55        if ( filesize( $this->getOption( $arg ) ) !== 0 ) {
56            $file = fopen( $list, 'r' );
57            if ( $file === false ) {
58                $this->fatalError( "Could not open pagelist file: \"$list\".\n" );
59            }
60        } else {
61            $this->fatalError( "Error: $list is empty.\n" );
62        }
63
64        $pages = [];
65        $this->output( "Reading from \"$list\".\n" );
66
67        $lineNum = 0;
68        // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
69        while ( $line = trim( fgets( $file ) ) ) {
70            $lineNum++;
71            $exp = explode( "\t", $line );
72            if ( count( $exp ) !== 2 ) {
73                $this->fatalError( "Line $lineNum should have two components: $line" );
74            }
75            if ( !WikiMap::getWiki( $exp[1] ) ) {
76                $this->fatalError( "Invalid wiki name on line $lineNum" . $exp[1] );
77            }
78            $pages[] = [
79                'title' => $exp[0],
80                'wiki' => $exp[1],
81            ];
82        }
83
84        fclose( $file );
85
86        // Submit the jobs
87        $params = [
88            'data' => $info,
89            'pages' => $pages,
90            'class' => MassMessageServerSideJob::class,
91        ];
92
93        $submitJob = new MassMessageSubmitJob(
94            Title::newFromText( 'SendMassMessages' ),
95            $params
96        );
97        // Just insert the individual jobs into the queue now.
98        $submitJob->run();
99        $count = count( $pages );
100        $this->output( "Queued $count jobs. Done!\n" );
101    }
102}
103
104$maintClass = SendMessages::class;
105require_once RUN_MAINTENANCE_IF_MAIN;