| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Context\RequestContext; |
| 26 |
use MediaWiki\Mail\EmailUserFactory; |
| 27 |
use MediaWiki\Status\Status; |
| 28 |
use MediaWiki\User\UserFactory; |
| 29 |
use Wikimedia\ParamValidator\ParamValidator; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class ApiEmailUser extends ApiBase { |
| 36 |
|
| 37 |
private EmailUserFactory $emailUserFactory; |
| 38 |
private UserFactory $userFactory; |
| 39 |
|
| 40 |
public function __construct( ApiMain $mainModule, string $moduleName, |
| 41 |
EmailUserFactory $emailUserFactory, UserFactory $userFactory ) { |
| 42 |
parent::__construct( $mainModule, $moduleName ); |
| 43 |
|
| 44 |
$this->emailUserFactory = $emailUserFactory; |
| 45 |
$this->userFactory = $userFactory; |
| 46 |
} |
| 47 |
|
| 48 |
public function execute() { |
| 49 |
$params = $this->extractRequestParams(); |
| 50 |
|
| 51 |
$emailUser = $this->emailUserFactory->newEmailUser( RequestContext::getMain()->getAuthority() ); |
| 52 |
$targetUser = $this->userFactory->newFromName( $params['target'] ); |
| 53 |
|
| 54 |
if ( $targetUser === null ) { |
| 55 |
$this->dieWithError( |
| 56 |
[ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ], |
| 57 |
"baduser_target" |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
$status = $emailUser->validateTarget( $targetUser ); |
| 62 |
|
| 63 |
if ( !$status->isOK() ) { |
| 64 |
$this->dieStatus( $status ); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
$error = $emailUser->canSend(); |
| 69 |
|
| 70 |
if ( !$error->isGood() ) { |
| 71 |
$this->dieStatus( $error ); |
| 72 |
} |
| 73 |
|
| 74 |
$retval = $emailUser->sendEmailUnsafe( |
| 75 |
$targetUser, |
| 76 |
$params['subject'], |
| 77 |
$params['text'], |
| 78 |
$params['ccme'], |
| 79 |
$this->getLanguage()->getCode() |
| 80 |
); |
| 81 |
|
| 82 |
if ( !$retval instanceof Status ) { |
| 83 |
|
| 84 |
$retval = Status::newFatal( 'hookaborted' ); |
| 85 |
} |
| 86 |
|
| 87 |
$result = array_filter( [ |
| 88 |
'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ), |
| 89 |
'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ), |
| 90 |
'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ), |
| 91 |
] ); |
| 92 |
|
| 93 |
$this->getResult()->addValue( null, $this->getModuleName(), $result ); |
| 94 |
} |
| 95 |
|
| 96 |
public function mustBePosted() { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
public function isWriteMode() { |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
public function getAllowedParams() { |
| 105 |
return [ |
| 106 |
'target' => [ |
| 107 |
ParamValidator::PARAM_TYPE => 'string', |
| 108 |
ParamValidator::PARAM_REQUIRED => true |
| 109 |
], |
| 110 |
'subject' => [ |
| 111 |
ParamValidator::PARAM_TYPE => 'string', |
| 112 |
ParamValidator::PARAM_REQUIRED => true |
| 113 |
], |
| 114 |
'text' => [ |
| 115 |
ParamValidator::PARAM_TYPE => 'text', |
| 116 |
ParamValidator::PARAM_REQUIRED => true |
| 117 |
], |
| 118 |
'ccme' => false, |
| 119 |
]; |
| 120 |
} |
| 121 |
|
| 122 |
public function needsToken() { |
| 123 |
return 'csrf'; |
| 124 |
} |
| 125 |
|
| 126 |
protected function getExamplesMessages() { |
| 127 |
return [ |
| 128 |
'action=emailuser&target=WikiSysop&text=Content&token=123ABC' |
| 129 |
=> 'apihelp-emailuser-example-email', |
| 130 |
]; |
| 131 |
} |
| 132 |
|
| 133 |
public function getHelpUrls() { |
| 134 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email'; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
class_alias( ApiEmailUser::class, 'ApiEmailUser' ); |