Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.33% covered (warning)
73.33%
33 / 45
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiMassMessage
73.33% covered (warning)
73.33%
33 / 45
71.43% covered (warning)
71.43%
5 / 7
9.21
0.00% covered (danger)
0.00%
0 / 1
 execute
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 getAllowedParams
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 mustBePosted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 needsToken
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isWriteMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\MassMessage\Api;
4
5use ApiBase;
6use MediaWiki\MassMessage\MassMessage;
7use MediaWiki\MassMessage\RequestProcessing\MassMessageRequestParser;
8use Wikimedia\ParamValidator\ParamValidator;
9
10/**
11 * API module to send MassMessages.
12 *
13 * @file
14 * @ingroup API
15 * @author Kunal Mehta
16 * @license GPL-2.0-or-later
17 */
18
19class ApiMassMessage extends ApiBase {
20    public function execute() {
21        $this->checkUserRightsAny( 'massmessage' );
22
23        $data = $this->extractRequestParams();
24
25        // Must provide message or page-message
26        $this->requireAtLeastOneParameter( $data, 'message', 'page-message' );
27
28        $requestParser = new MassMessageRequestParser();
29        $status = $requestParser->parseRequest( $data, $this->getUser() );
30
31        if ( !$status->isOK() ) {
32            $this->dieStatus( $status );
33        }
34
35        $count = MassMessage::submit( $this->getUser(), $status->getValue() );
36
37        $this->getResult()->addValue(
38            null,
39            $this->getModuleName(),
40            [ 'result' => 'success', 'count' => $count ]
41        );
42    }
43
44    /** @inheritDoc */
45    public function getAllowedParams() {
46        return [
47            'spamlist' => [
48                ParamValidator::PARAM_TYPE => 'string',
49                ParamValidator::PARAM_REQUIRED => true
50            ],
51            'subject' => [
52                ParamValidator::PARAM_TYPE => 'string',
53                ParamValidator::PARAM_REQUIRED => true
54            ],
55            'message' => [
56                ParamValidator::PARAM_TYPE => 'string'
57            ],
58            'page-message' => [
59                ParamValidator::PARAM_TYPE => 'string'
60            ],
61            'token' => null,
62        ];
63    }
64
65    /** @inheritDoc */
66    public function mustBePosted() {
67        return true;
68    }
69
70    /** @inheritDoc */
71    public function needsToken() {
72        return 'csrf';
73    }
74
75    /** @inheritDoc */
76    public function isWriteMode() {
77        return true;
78    }
79
80    /** @inheritDoc */
81    protected function getExamplesMessages() {
82        return [
83            'action=massmessage&spamlist=Signpost%20Spamlist&subject=New%20Signpost' .
84            '&message=Please%20read%20it&token=TOKEN'
85                => 'apihelp-massmessage-example-1',
86            'action=massmessage&spamlist=Signpost%20Spamlist&subject=New%20Signpost' .
87            '&page-message=Help_Page&token=TOKEN'
88                => 'apihelp-massmessage-example-2',
89            'action=massmessage&spamlist=Signpost%20Spamlist&subject=New%20Signpost' .
90            '&message=Please%20read%20it&page-message=Help_Page&token=TOKEN'
91                => 'apihelp-massmessage-example-3',
92        ];
93    }
94
95    /**
96     * @return array
97     */
98    public function getHelpUrls() {
99        return [ 'https://www.mediawiki.org/wiki/Extension:MassMessage/API' ];
100    }
101
102}