follow-up to r85876: fix some coding style issues (some of which were not introduced...
[lhc/web/wiklou.git] / includes / specials / SpecialEmailuser.php
index c098e88..b3ba9c5 100644 (file)
  */
 class SpecialEmailUser extends UnlistedSpecialPage {
        protected $mTarget;
-       
-       public function __construct(){
+
+       public function __construct() {
                parent::__construct( 'Emailuser' );
        }
-       
-       protected function getFormFields(){
+
+       protected function getFormFields() {
                global $wgUser;
                return array(
                        'From' => array(
                                'type' => 'info',
                                'raw' => 1,
-                               'default' => $wgUser->getSkin()->link( 
-                                       $wgUser->getUserPage(), 
-                                       htmlspecialchars( $wgUser->getName() ) 
+                               'default' => $wgUser->getSkin()->link(
+                                       $wgUser->getUserPage(),
+                                       htmlspecialchars( $wgUser->getName() )
                                ),
                                'label-message' => 'emailfrom',
                                'id' => 'mw-emailuser-sender',
@@ -49,15 +49,14 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        'To' => array(
                                'type' => 'info',
                                'raw' => 1,
-                               'default' => $wgUser->getSkin()->link( 
-                                       $this->mTargetObj->getUserPage(), 
+                               'default' => $wgUser->getSkin()->link(
+                                       $this->mTargetObj->getUserPage(),
                                        htmlspecialchars( $this->mTargetObj->getName() )
                                ),
                                'label-message' => 'emailto',
                                'id' => 'mw-emailuser-recipient',
                        ),
                        'Target' => array(
-                               'name' => 'wpTarget',
                                'type' => 'hidden',
                                'default' => $this->mTargetObj->getName(),
                        ),
@@ -83,25 +82,17 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        ),
                );
        }
-       
+
        public function execute( $par ) {
                global $wgRequest, $wgOut, $wgUser;
 
                $this->setHeaders();
                $this->outputHeader();
-
+               $wgOut->addModuleStyles( 'mediawiki.special' );
                $this->mTarget = is_null( $par )
-                       ? $wgRequest->getVal( 'wpTarget', '' )
+                       ? $wgRequest->getVal( 'wpTarget', $wgRequest->getVal( 'target', '' ) )
                        : $par;
-                       
-               $ret = self::getTarget( $this->mTarget );
-               if( $ret instanceof User ){
-                       $this->mTargetObj = $ret;
-               } else {
-                       $wgOut->showErrorPage( "{$ret}title", "{$ret}text" );
-                       return false;
-               }
-       
+               // error out if sending user cannot do this
                $error = self::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
                switch ( $error ) {
                        case null:
@@ -126,7 +117,19 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                                $wgOut->showErrorPage( $title, $msg, $params );
                                return;
                }
-               
+               // Got a valid target user name? Else ask for one.
+               $ret = self::getTarget( $this->mTarget );
+               if( !$ret instanceof User ) {
+                       if( $this->mTarget != '' ) {
+                               $ret = ( $ret == 'notarget' ) ? 'emailnotarget' : ( $ret . 'text' );
+                               $wgOut->addHTML( '<p class="error">' . wfMessage( $ret )->parse() . '</p>' );
+                       }
+                       $wgOut->addHTML( self::userForm( $this->mTarget ) );
+                       return false;
+               }
+
+               $this->mTargetObj = $ret;
+
                $form = new HTMLForm( $this->getFormFields() );
                $form->addPreText( wfMsgExt( 'emailpagetext', 'parseinline' ) );
                $form->setSubmitText( wfMsg( 'emailsend' ) );
@@ -134,16 +137,16 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                $form->setSubmitCallback( array( __CLASS__, 'submit' ) );
                $form->setWrapperLegend( wfMsgExt( 'email-legend', 'parsemag' ) );
                $form->loadData();
-               
-               if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ){
+
+               if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
                        return false;
                }
-               
-               $wgOut->setPagetitle( wfMsg( 'emailpage' ) );
+
+               $wgOut->setPageTitle( wfMsg( 'emailpage' ) );
                $result = $form->show();
-               
-               if( $result === true ){
-                       $wgOut->setPagetitle( wfMsg( 'emailsent' ) );
+
+               if( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
+                       $wgOut->setPageTitle( wfMsg( 'emailsent' ) );
                        $wgOut->addWikiMsg( 'emailsenttext' );
                        $wgOut->returnToMain( false, $this->mTargetObj->getUserPage() );
                }
@@ -160,15 +163,15 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        wfDebug( "Target is empty.\n" );
                        return 'notarget';
                }
-               
+
                $nu = User::newFromName( $target );
                if( !$nu instanceof User || !$nu->getId() ) {
                        wfDebug( "Target is invalid user.\n" );
                        return 'notarget';
-               } else if ( !$nu->isEmailConfirmed() ) {
+               } elseif ( !$nu->isEmailConfirmed() ) {
                        wfDebug( "User has no valid email.\n" );
                        return 'noemail';
-               } else if ( !$nu->canReceiveEmail() ) {
+               } elseif ( !$nu->canReceiveEmail() ) {
                        wfDebug( "User does not allow user emails.\n" );
                        return 'nowikiemail';
                }
@@ -185,15 +188,15 @@ class SpecialEmailUser extends UnlistedSpecialPage {
         */
        public static function getPermissionsError( $user, $editToken ) {
                global $wgEnableEmail, $wgEnableUserEmail;
-               if( !$wgEnableEmail || !$wgEnableUserEmail ){
+               if( !$wgEnableEmail || !$wgEnableUserEmail ) {
                        return 'usermaildisabled';
                }
-               
+
                if( !$user->isAllowed( 'sendemail' ) ) {
                        return 'badaccess';
                }
-               
-               if( !$user->isEmailConfirmed() ){
+
+               if( !$user->isEmailConfirmed() ) {
                        return 'mailnologin';
                }
 
@@ -217,18 +220,36 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                return null;
        }
 
+       /**
+        * Form to ask for target user name.
+        *
+        * @param $name String: user name submitted.
+        * @return String: form asking for user name.
+        */
+       static function userForm( $name ) {
+               $string = Xml::openElement( 'form', array( 'method' => 'get', 'action' => '', 'id' => 'askusername' ) ) .
+                               Xml::openElement( 'fieldset' ) .
+                               Html::rawElement( 'legend', null, wfMessage( 'emailtarget' )->parse() ) .
+                               Xml::inputLabel( wfMessage( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
+                               Xml::submitButton( wfMessage( 'emailusernamesubmit' )->text() ) .
+                               Xml::closeElement( 'fieldset' ) .
+                               Xml::closeElement( 'form' ) . "\n";
+               return $string;
+       }
+
        /**
         * Really send a mail. Permissions should have been checked using
-        * getPermissionsError(). It is probably also a good 
+        * getPermissionsError(). It is probably also a good
         * idea to check the edit token and ping limiter in advance.
         *
-        * @return Mixed: True on success, String on error
+        * @return Mixed: Status object, or potentially a String on error
+        * or maybe even true on success if anything uses the EmailUser hook.
         */
        public static function submit( $data ) {
                global $wgUser, $wgUserEmailUseReplyTo;
 
                $target = self::getTarget( $data['Target'] );
-               if( !$target instanceof User ){
+               if( !$target instanceof User ) {
                        return wfMsgExt( $target . 'text', 'parse' );
                }
                $to = new MailAddress( $target );
@@ -238,17 +259,17 @@ class SpecialEmailUser extends UnlistedSpecialPage {
 
                // Add a standard footer and trim up trailing newlines
                $text = rtrim( $text ) . "\n\n-- \n";
-               $text .= wfMsgExt( 
+               $text .= wfMsgExt(
                        'emailuserfooter',
-                       array( 'content', 'parsemag' ), 
-                       array( $from->name, $to->name ) 
+                       array( 'content', 'parsemag' ),
+                       array( $from->name, $to->name )
                );
 
                $error = '';
                if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
                        return $error;
                }
-               
+
                if( $wgUserEmailUseReplyTo ) {
                        // Put the generic wiki autogenerated address in the From:
                        // header and reserve the user for Reply-To.
@@ -277,35 +298,27 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        $replyTo = null;
                }
 
-               $mailResult = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
+               $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
 
-               if( WikiError::isError( $mailResult ) && false ) {
-                       return $mailResult->getMessage();
+               if( !$status->isGood() ) {
+                       return $status;
                } else {
                        // if the user requested a copy of this mail, do this now,
-                       // unless they are emailing themselves, in which case one 
+                       // unless they are emailing themselves, in which case one
                        // copy of the message is sufficient.
                        if ( $data['CCMe'] && $to != $from ) {
                                $cc_subject = wfMsg(
-                                       'emailccsubject', 
-                                       $target->getName(), 
+                                       'emailccsubject',
+                                       $target->getName(),
                                        $subject
                                );
                                wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
-                               $ccResult = UserMailer::send( $from, $from, $cc_subject, $text );
-                               if( WikiError::isError( $ccResult ) ) {
-                                       // At this stage, the user's CC mail has failed, but their
-                                       // original mail has succeeded. It's unlikely, but still, 
-                                       // what to do? We can either show them an error, or we can 
-                                       // say everything was fine, or we can say we sort of failed 
-                                       // AND sort of succeeded. Of these options, simply saying 
-                                       // there was an error is probably best.
-                                       return $ccResult->getMessage();
-                               }
+                               $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
+                               $status->merge( $ccStatus );
                        }
 
                        wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
-                       return true;
+                       return $status;
                }
        }
 }