aboutsummaryrefslogtreecommitdiffstats
path: root/includes/api
diff options
context:
space:
mode:
Diffstat (limited to 'includes/api')
-rw-r--r--includes/api/ApiEmailUser.php75
-rw-r--r--includes/api/ApiMain.php4
-rw-r--r--includes/api/i18n/en.json1
-rw-r--r--includes/api/i18n/qqq.json1
4 files changed, 43 insertions, 38 deletions
diff --git a/includes/api/ApiEmailUser.php b/includes/api/ApiEmailUser.php
index ec807c8fd7c2..5a05f1a58422 100644
--- a/includes/api/ApiEmailUser.php
+++ b/includes/api/ApiEmailUser.php
@@ -20,9 +20,9 @@
* @file
*/
-use MediaWiki\Specials\SpecialEmailUser;
+use MediaWiki\Mail\EmailUserFactory;
use MediaWiki\Status\Status;
-use MediaWiki\User\User;
+use MediaWiki\User\UserFactory;
use Wikimedia\ParamValidator\ParamValidator;
/**
@@ -31,48 +31,51 @@ use Wikimedia\ParamValidator\ParamValidator;
*/
class ApiEmailUser extends ApiBase {
+ private EmailUserFactory $emailUserFactory;
+ private UserFactory $userFactory;
+
+ public function __construct( ApiMain $mainModule, $moduleName,
+ EmailUserFactory $emailUserFactory, UserFactory $userFactory ) {
+ parent::__construct( $mainModule, $moduleName );
+
+ $this->emailUserFactory = $emailUserFactory;
+ $this->userFactory = $userFactory;
+ }
+
public function execute() {
$params = $this->extractRequestParams();
- // Validate target
- $targetUser = SpecialEmailUser::getTarget( $params['target'], $this->getUser() );
- if ( !( $targetUser instanceof User ) ) {
- switch ( $targetUser ) {
- case 'notarget':
- $this->dieWithError( 'apierror-notarget' );
- // dieWithError prevents continuation
-
- case 'noemail':
- $this->dieWithError( [ 'noemail', $params['target'] ] );
- // dieWithError prevents continuation
-
- case 'nowikiemail':
- $this->dieWithError( 'nowikiemailtext', 'nowikiemail' );
- // dieWithError prevents continuation
-
- default:
- $this->dieWithError( [ 'apierror-unknownerror', $targetUser ] );
- }
+ $emailUser = $this->emailUserFactory->newEmailUser( RequestContext::getMain()->getAuthority() );
+ $targetUser = $this->userFactory->newFromName( $params['target'] );
+
+ if ( $targetUser === null ) {
+ $this->dieWithError(
+ [ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ],
+ "baduser_target"
+ );
+ }
+
+ $status = $emailUser->validateTarget( $targetUser );
+
+ if ( !$status->isOK() ) {
+ $this->dieStatus( $status );
}
// Check permissions and errors
- $error = SpecialEmailUser::getPermissionsError(
- $this->getUser(),
- $params['token'],
- $this->getConfig(),
- true // authorize!
- );
- if ( $error ) {
- $this->dieWithError( $error );
+ $error = $emailUser->canSend();
+
+ if ( !$error->isGood() ) {
+ $this->dieStatus( $error );
}
- $data = [
- 'Target' => $targetUser->getName(),
- 'Text' => $params['text'],
- 'Subject' => $params['subject'],
- 'CCMe' => $params['ccme'],
- ];
- $retval = SpecialEmailUser::submit( $data, $this->getContext() );
+ $retval = $emailUser->sendEmailUnsafe(
+ $targetUser,
+ $params['subject'],
+ $params['text'],
+ $params['ccme'],
+ $this->getLanguage()->getCode()
+ );
+
if ( !$retval instanceof Status ) {
// This is probably the reason
$retval = Status::newFatal( 'hookaborted' );
diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php
index cbdc186aa258..273af81c6fd0 100644
--- a/includes/api/ApiMain.php
+++ b/includes/api/ApiMain.php
@@ -370,6 +370,10 @@ class ApiMain extends ApiBase {
],
'emailuser' => [
'class' => ApiEmailUser::class,
+ 'services' => [
+ 'EmailUserFactory',
+ 'UserFactory',
+ ]
],
'watch' => [
'class' => ApiWatch::class,
diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json
index f4632bf20f40..89a8927659ab 100644
--- a/includes/api/i18n/en.json
+++ b/includes/api/i18n/en.json
@@ -1923,7 +1923,6 @@
"apierror-nosuchsection": "There is no section $1.",
"apierror-nosuchsection-what": "There is no section $1 in $2.",
"apierror-nosuchuserid": "There is no user with ID $1.",
- "apierror-notarget": "You have not specified a valid target for this action.",
"apierror-notpatrollable": "The revision r$1 can't be patrolled as it's too old.",
"apierror-nouploadmodule": "No upload module set.",
"apierror-opensearch-json-warnings": "Warnings cannot be represented in OpenSearch JSON format.",
diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json
index 73129343442e..80c7e52f1c40 100644
--- a/includes/api/i18n/qqq.json
+++ b/includes/api/i18n/qqq.json
@@ -1826,7 +1826,6 @@
"apierror-nosuchsection": "{{doc-apierror}}\n\nParameters:\n* $1 - Section identifier. Probably a number or \"T-\" followed by a number.",
"apierror-nosuchsection-what": "{{doc-apierror}}\n\nParameters:\n* $1 - Section identifier. Probably a number or \"T-\" followed by a number.\n* $2 - Page title, revision ID formatted with {{msg-mw|revid}}, or page ID formatted with {{msg-mw|pageid}}.",
"apierror-nosuchuserid": "{{doc-apierror}}",
- "apierror-notarget": "{{doc-apierror}}",
"apierror-notpatrollable": "{{doc-apierror}}\n\nParameters:\n* $1 - Revision ID number.",
"apierror-nouploadmodule": "{{doc-apierror}}",
"apierror-opensearch-json-warnings": "{{doc-apierror}}",