Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / api / ApiEmailUser.php
blob8aff6f8afd463d5a4fc170855a07d27dbe02a7b3
1 <?php
2 /**
5 * Created on June 1, 2008
7 * Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * API Module to facilitate sending of emails to users
29 * @ingroup API
31 class ApiEmailUser extends ApiBase {
33 public function execute() {
34 $params = $this->extractRequestParams();
36 // Validate target
37 $targetUser = SpecialEmailUser::getTarget( $params['target'] );
38 if ( !( $targetUser instanceof User ) ) {
39 switch ( $targetUser ) {
40 case 'notarget':
41 $this->dieWithError( 'apierror-notarget' );
42 case 'noemail':
43 $this->dieWithError( [ 'noemail', $params['target'] ] );
44 case 'nowikiemail':
45 $this->dieWithError( 'nowikiemailtext', 'nowikiemail' );
46 default:
47 $this->dieWithError( [ 'apierror-unknownerror', $targetUser ] );
51 // Check permissions and errors
52 $error = SpecialEmailUser::getPermissionsError(
53 $this->getUser(),
54 $params['token'],
55 $this->getConfig()
57 if ( $error ) {
58 $this->dieWithError( $error );
61 $data = [
62 'Target' => $targetUser->getName(),
63 'Text' => $params['text'],
64 'Subject' => $params['subject'],
65 'CCMe' => $params['ccme'],
67 $retval = SpecialEmailUser::submit( $data, $this->getContext() );
68 if ( !$retval instanceof Status ) {
69 // This is probably the reason
70 $retval = Status::newFatal( 'hookaborted' );
73 $result = array_filter( [
74 'result' => $retval->isGood() ? 'Success' : $retval->isOk() ? 'Warnings' : 'Failure',
75 'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
76 'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
77 ] );
79 $this->getResult()->addValue( null, $this->getModuleName(), $result );
82 public function mustBePosted() {
83 return true;
86 public function isWriteMode() {
87 return true;
90 public function getAllowedParams() {
91 return [
92 'target' => [
93 ApiBase::PARAM_TYPE => 'string',
94 ApiBase::PARAM_REQUIRED => true
96 'subject' => null,
97 'text' => [
98 ApiBase::PARAM_TYPE => 'text',
99 ApiBase::PARAM_REQUIRED => true
101 'ccme' => false,
105 public function needsToken() {
106 return 'csrf';
109 protected function getExamplesMessages() {
110 return [
111 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
112 => 'apihelp-emailuser-example-email',
116 public function getHelpUrls() {
117 return 'https://www.mediawiki.org/wiki/API:Email';