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