Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ApiEmailUser | |
0.00% |
0 / 53 |
|
0.00% |
0 / 8 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
56 | |||
| mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllowedParams | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
| needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com> |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | use MediaWiki\Mail\EmailUserFactory; |
| 12 | use MediaWiki\Status\Status; |
| 13 | use MediaWiki\User\UserFactory; |
| 14 | use Wikimedia\ParamValidator\ParamValidator; |
| 15 | |
| 16 | /** |
| 17 | * API Module to facilitate sending of emails to users |
| 18 | * @ingroup API |
| 19 | */ |
| 20 | class ApiEmailUser extends ApiBase { |
| 21 | |
| 22 | public function __construct( |
| 23 | ApiMain $mainModule, |
| 24 | string $moduleName, |
| 25 | private readonly EmailUserFactory $emailUserFactory, |
| 26 | private readonly UserFactory $userFactory |
| 27 | ) { |
| 28 | parent::__construct( $mainModule, $moduleName ); |
| 29 | } |
| 30 | |
| 31 | public function execute() { |
| 32 | $params = $this->extractRequestParams(); |
| 33 | |
| 34 | $emailUser = $this->emailUserFactory->newEmailUser( $this->getAuthority() ); |
| 35 | $targetUser = $this->userFactory->newFromName( $params['target'] ); |
| 36 | |
| 37 | if ( $targetUser === null ) { |
| 38 | $this->dieWithError( |
| 39 | [ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ], |
| 40 | "baduser_target" |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | $status = $emailUser->validateTarget( $targetUser ); |
| 45 | |
| 46 | if ( !$status->isOK() ) { |
| 47 | $this->dieStatus( $status ); |
| 48 | } |
| 49 | |
| 50 | // Check permissions and errors |
| 51 | $error = $emailUser->authorizeSend(); |
| 52 | |
| 53 | if ( !$error->isGood() ) { |
| 54 | $this->dieStatus( $error ); |
| 55 | } |
| 56 | |
| 57 | $retval = $emailUser->sendEmailUnsafe( |
| 58 | $targetUser, |
| 59 | $params['subject'], |
| 60 | $params['text'], |
| 61 | $params['ccme'], |
| 62 | $this->getLanguage()->getCode() |
| 63 | ); |
| 64 | |
| 65 | if ( !$retval instanceof Status ) { |
| 66 | // This is probably the reason |
| 67 | $retval = Status::newFatal( 'hookaborted' ); |
| 68 | } |
| 69 | |
| 70 | $result = array_filter( [ |
| 71 | 'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ), |
| 72 | 'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ), |
| 73 | 'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ), |
| 74 | ] ); |
| 75 | |
| 76 | $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
| 77 | } |
| 78 | |
| 79 | /** @inheritDoc */ |
| 80 | public function mustBePosted() { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | /** @inheritDoc */ |
| 85 | public function isWriteMode() { |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | /** @inheritDoc */ |
| 90 | public function getAllowedParams() { |
| 91 | return [ |
| 92 | 'target' => [ |
| 93 | ParamValidator::PARAM_TYPE => 'string', |
| 94 | ParamValidator::PARAM_REQUIRED => true |
| 95 | ], |
| 96 | 'subject' => [ |
| 97 | ParamValidator::PARAM_TYPE => 'string', |
| 98 | ParamValidator::PARAM_REQUIRED => true |
| 99 | ], |
| 100 | 'text' => [ |
| 101 | ParamValidator::PARAM_TYPE => 'text', |
| 102 | ParamValidator::PARAM_REQUIRED => true |
| 103 | ], |
| 104 | 'ccme' => false, |
| 105 | ]; |
| 106 | } |
| 107 | |
| 108 | /** @inheritDoc */ |
| 109 | public function needsToken() { |
| 110 | return 'csrf'; |
| 111 | } |
| 112 | |
| 113 | /** @inheritDoc */ |
| 114 | protected function getExamplesMessages() { |
| 115 | return [ |
| 116 | 'action=emailuser&target=WikiSysop&text=Content&token=123ABC' |
| 117 | => 'apihelp-emailuser-example-email', |
| 118 | ]; |
| 119 | } |
| 120 | |
| 121 | /** @inheritDoc */ |
| 122 | public function getHelpUrls() { |
| 123 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email'; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** @deprecated class alias since 1.43 */ |
| 128 | class_alias( ApiEmailUser::class, 'ApiEmailUser' ); |