47 parent::__construct(
'Emailuser' );
48 $this->userNameUtils = $userNameUtils;
49 $this->userNamePrefixSearch = $userNamePrefixSearch;
50 $this->userOptionsLookup = $userOptionsLookup;
51 $this->emailUserFactory = $emailUserFactory;
52 $this->userFactory = $userFactory;
62 return $this->
msg(
'emailuser-title-notarget' );
72 'default' => $linkRenderer->makeLink(
76 'label-message' =>
'emailfrom',
77 'id' =>
'mw-emailuser-sender',
82 'default' => $linkRenderer->makeLink(
86 'label-message' =>
'emailto',
87 'id' =>
'mw-emailuser-recipient',
91 'default' => $target->
getName(),
95 'default' => $this->
msg(
'defemailsubject', $user->getName() )->inContentLanguage()->text(),
96 'label-message' =>
'emailsubject',
102 'type' =>
'textarea',
104 'label-message' =>
'emailmessage',
109 'label-message' =>
'emailccme',
110 'default' => $this->userOptionsLookup->getBoolOption( $user,
'ccmeonemails' ),
118 private function handleCanSendEmailStatus(
StatusValue $status ): void {
119 if ( !$status->isGood() ) {
121 $status->throwErrorPageError();
122 } elseif ( $status->
hasMessage(
'mailnologin' ) ) {
123 throw new ErrorPageError(
'mailnologin',
'mailnologintext' );
124 } elseif ( $status->
hasMessage(
'usermaildisabled' ) ) {
125 throw new ErrorPageError(
'usermaildisabled',
'usermaildisabledtext' );
126 } elseif ( $status->
getValue() !==
null ) {
130 throw new ErrorPageError( $status->
getValue(), $msg );
133 throw new ErrorPageError( $this->getDescription(), Status::wrap( $status )->getMessage() );
141 $this->outputHeader();
143 $out = $this->getOutput();
144 $request = $this->getRequest();
145 $out->addModuleStyles(
'mediawiki.special' );
148 $emailUser = $this->emailUserFactory->newEmailUserBC(
152 $emailUser->setEditToken( (
string)$request->getVal(
'wpEditToken' ) );
153 $status = $emailUser->canSend();
159 $this->handleCanSendEmailStatus( $status );
164 $this->requireNamedUser(
'mailnologintext',
'mailnologin' );
166 $this->handleCanSendEmailStatus( $status );
170 $target = $par ?? $request->getVal(
'wpTarget', $request->getVal(
'target',
'' ) );
172 $this->userForm( $target );
184 $targetObject = MediaWikiServices::getInstance()->getUserFactory()->newFromName( $target );
185 if ( !$targetObject instanceof
User ) {
189 $status = MediaWikiServices::getInstance()->getEmailUserFactory()
190 ->newEmailUser( $sender )
191 ->validateTarget( $targetObject );
192 if ( !$status->
isGood() ) {
194 $ret = $msg ===
'emailnotarget' ?
'notarget' : preg_replace(
'/text$/',
'', $msg );
196 $ret = $targetObject;
207 $htmlForm = HTMLForm::factory(
'ooui', [
212 'label-message' =>
'emailusername',
213 'id' =>
'emailusertarget',
216 'excludetemp' =>
true,
220 'filter-callback' =>
static function ( $value ) use ( $name ) {
221 return str_replace(
'_',
' ',
222 ( $value !==
'' && $value !==
false && $value !==
null ) ? $value : $name );
224 'validation-callback' =>
function ( $value ) {
226 $target = $this->userFactory->
newFromName( $value );
227 $statusValue = $this->emailUserFactory
229 ->newEmailUser( $this->getUser() )->validateTarget( $target );
230 if ( !$statusValue->isGood() ) {
232 return Status::wrap( $statusValue )->getMessage();
237 ], $this->getContext() );
241 ->setTitle( $this->getPageTitle() )
242 ->setSubmitCallback( $this->sendEmailForm( ... ) )
243 ->setId(
'askusername' )
244 ->setWrapperLegendMsg(
'emailtarget' )
245 ->setSubmitTextMsg(
'emailusernamesubmit' )
253 private function sendEmailForm( array $data ) {
254 $out = $this->getOutput();
257 $target = $this->userFactory->
newFromName( $data[
'Target'] );
259 $htmlForm = HTMLForm::factory(
'ooui', $this->getFormFields( $target ), $this->getContext() );
261 ->setTitle( $this->getPageTitle() )
262 ->addPreHtml( $this->msg(
'emailpagetext', $target->
getName() )->parse() )
263 ->setSubmitTextMsg(
'emailsend' )
264 ->setSubmitCallback( $this->onFormSubmit( ... ) )
265 ->setWrapperLegendMsg(
'email-legend' )
268 if ( !$this->getHookRunner()->onEmailUserForm( $htmlForm ) ) {
272 $result = $htmlForm->show();
274 if ( $result ===
true || ( $result instanceof Status && $result->isGood() ) ) {
275 $out->setPageTitleMsg( $this->msg(
'emailsent' ) );
276 $out->addWikiMsg(
'emailsenttext', $target->
getName() );
277 $out->returnToMain(
false, $target->
getUserPage() );
279 $out->setPageTitleMsg( $this->msg(
'emailuser-title-target', $target->
getName() ) );
288 private function onFormSubmit( array $data ) {
290 $target = $this->userFactory->
newFromName( $data[
'Target'] );
292 $emailUser = $this->emailUserFactory->newEmailUser( $this->
getAuthority() );
293 $emailUser->setEditToken( $this->
getRequest()->getVal(
'wpEditToken' ) );
296 $status = $emailUser->authorizeSend();
298 if ( !$status->
isOK() ) {
303 $res = $emailUser->sendEmailUnsafe(
308 $this->getLanguage()->getCode()
310 if ( $res->hasMessage(
'hookaborted' ) ) {
315 $res = Status::wrap( $res );
329 $search = $this->userNameUtils->getCanonical( $search );
335 return $this->userNamePrefixSearch
336 ->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset );
343 return $this->getConfig()->get( MainConfigNames::EnableUserEmail );
353class_alias( SpecialEmailUser::class,
'SpecialEmailUser' );
if(!defined('MW_SETUP_CALLBACK'))
An error page which can definitely be safely rendered using the OutputPage.
A class containing constants representing the names of configuration variables.
Parent class for all special pages.
getUser()
Shortcut to get the User executing this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
getMessages(?string $type=null)
Returns a list of error messages, optionally only those of the given type.
hasMessage(string $message)
Returns true if the specified message is present as a warning or error.
isOK()
Returns whether the operation completed.
isGood()
Returns whether the operation completed and didn't have any error or warnings.