Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiEmailUser
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 7
272
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
110
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
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/**
3 * Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23use MediaWiki\Specials\SpecialEmailUser;
24use MediaWiki\Status\Status;
25use MediaWiki\User\User;
26use Wikimedia\ParamValidator\ParamValidator;
27
28/**
29 * API Module to facilitate sending of emails to users
30 * @ingroup API
31 */
32class ApiEmailUser extends ApiBase {
33
34    public function execute() {
35        $params = $this->extractRequestParams();
36
37        // Validate target
38        $targetUser = SpecialEmailUser::getTarget( $params['target'], $this->getUser() );
39        if ( !( $targetUser instanceof User ) ) {
40            switch ( $targetUser ) {
41                case 'notarget':
42                    $this->dieWithError( 'apierror-notarget' );
43                    // dieWithError prevents continuation
44
45                case 'noemail':
46                    $this->dieWithError( [ 'noemail', $params['target'] ] );
47                    // dieWithError prevents continuation
48
49                case 'nowikiemail':
50                    $this->dieWithError( 'nowikiemailtext', 'nowikiemail' );
51                    // dieWithError prevents continuation
52
53                default:
54                    $this->dieWithError( [ 'apierror-unknownerror', $targetUser ] );
55            }
56        }
57
58        // Check permissions and errors
59        $error = SpecialEmailUser::getPermissionsError(
60            $this->getUser(),
61            $params['token'],
62            $this->getConfig(),
63            true // authorize!
64        );
65        if ( $error ) {
66            $this->dieWithError( $error );
67        }
68
69        $data = [
70            'Target' => $targetUser->getName(),
71            'Text' => $params['text'],
72            'Subject' => $params['subject'],
73            'CCMe' => $params['ccme'],
74        ];
75        $retval = SpecialEmailUser::submit( $data, $this->getContext() );
76        if ( !$retval instanceof Status ) {
77            // This is probably the reason
78            $retval = Status::newFatal( 'hookaborted' );
79        }
80
81        $result = array_filter( [
82            'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
83            'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
84            'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
85        ] );
86
87        $this->getResult()->addValue( null, $this->getModuleName(), $result );
88    }
89
90    public function mustBePosted() {
91        return true;
92    }
93
94    public function isWriteMode() {
95        return true;
96    }
97
98    public function getAllowedParams() {
99        return [
100            'target' => [
101                ParamValidator::PARAM_TYPE => 'string',
102                ParamValidator::PARAM_REQUIRED => true
103            ],
104            'subject' => [
105                ParamValidator::PARAM_TYPE => 'string',
106                ParamValidator::PARAM_REQUIRED => true
107            ],
108            'text' => [
109                ParamValidator::PARAM_TYPE => 'text',
110                ParamValidator::PARAM_REQUIRED => true
111            ],
112            'ccme' => false,
113        ];
114    }
115
116    public function needsToken() {
117        return 'csrf';
118    }
119
120    protected function getExamplesMessages() {
121        return [
122            'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
123                => 'apihelp-emailuser-example-email',
124        ];
125    }
126
127    public function getHelpUrls() {
128        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
129    }
130}