2008-06-01 17:58:27 +00:00
|
|
|
<?php
|
2010-02-22 12:20:54 +00:00
|
|
|
/**
|
|
|
|
|
* Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
|
2008-06-01 17:58:27 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-06-01 17:58:27 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2008-06-01 17:58:27 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-09-25 16:17:29 +00:00
|
|
|
namespace MediaWiki\Api;
|
|
|
|
|
|
2024-06-14 21:24:11 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2024-05-04 14:09:29 +00:00
|
|
|
use MediaWiki\Mail\EmailUserFactory;
|
2023-08-25 12:29:41 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2024-05-04 14:09:29 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2022-06-05 23:18:50 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
|
2008-06-01 17:58:27 +00:00
|
|
|
/**
|
2010-05-11 22:30:18 +00:00
|
|
|
* API Module to facilitate sending of emails to users
|
2008-06-01 17:58:27 +00:00
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiEmailUser extends ApiBase {
|
|
|
|
|
|
2024-05-04 14:09:29 +00:00
|
|
|
private EmailUserFactory $emailUserFactory;
|
|
|
|
|
private UserFactory $userFactory;
|
|
|
|
|
|
2024-10-14 20:12:27 +00:00
|
|
|
public function __construct( ApiMain $mainModule, string $moduleName,
|
2024-05-04 14:09:29 +00:00
|
|
|
EmailUserFactory $emailUserFactory, UserFactory $userFactory ) {
|
|
|
|
|
parent::__construct( $mainModule, $moduleName );
|
|
|
|
|
|
|
|
|
|
$this->emailUserFactory = $emailUserFactory;
|
|
|
|
|
$this->userFactory = $userFactory;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-01 17:58:27 +00:00
|
|
|
public function execute() {
|
|
|
|
|
$params = $this->extractRequestParams();
|
2010-02-22 12:20:54 +00:00
|
|
|
|
2024-05-04 14:09:29 +00:00
|
|
|
$emailUser = $this->emailUserFactory->newEmailUser( RequestContext::getMain()->getAuthority() );
|
|
|
|
|
$targetUser = $this->userFactory->newFromName( $params['target'] );
|
2021-04-12 21:22:21 +00:00
|
|
|
|
2024-05-04 14:09:29 +00:00
|
|
|
if ( $targetUser === null ) {
|
|
|
|
|
$this->dieWithError(
|
|
|
|
|
[ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ],
|
|
|
|
|
"baduser_target"
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-04-12 21:22:21 +00:00
|
|
|
|
2024-05-04 14:09:29 +00:00
|
|
|
$status = $emailUser->validateTarget( $targetUser );
|
2021-04-12 21:22:21 +00:00
|
|
|
|
2024-05-04 14:09:29 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
$this->dieStatus( $status );
|
2010-02-22 12:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
2010-04-10 21:14:35 +00:00
|
|
|
// Check permissions and errors
|
2024-05-04 14:09:29 +00:00
|
|
|
$error = $emailUser->canSend();
|
|
|
|
|
|
|
|
|
|
if ( !$error->isGood() ) {
|
|
|
|
|
$this->dieStatus( $error );
|
2010-02-22 12:20:54 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2024-05-04 14:09:29 +00:00
|
|
|
$retval = $emailUser->sendEmailUnsafe(
|
|
|
|
|
$targetUser,
|
|
|
|
|
$params['subject'],
|
|
|
|
|
$params['text'],
|
|
|
|
|
$params['ccme'],
|
|
|
|
|
$this->getLanguage()->getCode()
|
|
|
|
|
);
|
|
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
if ( !$retval instanceof Status ) {
|
|
|
|
|
// This is probably the reason
|
|
|
|
|
$retval = Status::newFatal( 'hookaborted' );
|
2011-03-05 03:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
$result = array_filter( [
|
2017-12-27 11:33:38 +00:00
|
|
|
'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
|
2016-10-19 16:54:25 +00:00
|
|
|
'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
|
|
|
|
|
'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
|
|
|
|
|
] );
|
2010-02-22 12:20:54 +00:00
|
|
|
|
2008-06-01 17:58:27 +00:00
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
|
}
|
2010-02-22 12:20:54 +00:00
|
|
|
|
2010-02-14 21:12:11 +00:00
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-06-01 17:58:27 +00:00
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-01 17:58:27 +00:00
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'target' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2022-03-16 05:41:57 +00:00
|
|
|
'subject' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => true
|
2022-03-16 05:41:57 +00:00
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
'text' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'text',
|
|
|
|
|
ParamValidator::PARAM_REQUIRED => true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2008-06-01 17:58:27 +00:00
|
|
|
'ccme' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2008-06-01 17:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-01 20:12:50 +00:00
|
|
|
public function needsToken() {
|
2014-08-08 16:56:07 +00:00
|
|
|
return 'csrf';
|
2010-02-14 22:20:27 +00:00
|
|
|
}
|
2008-06-01 17:58:27 +00:00
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
|
|
|
|
|
=> 'apihelp-emailuser-example-email',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2008-06-01 17:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2017-04-04 22:52:57 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
2010-01-11 15:55:52 +00:00
|
|
|
}
|
2024-09-25 16:17:29 +00:00
|
|
|
|
|
|
|
|
/** @deprecated class alias since 1.43 */
|
|
|
|
|
class_alias( ApiEmailUser::class, 'ApiEmailUser' );
|