$sender = new MailAddress( $wgPasswordSender,
wfMessage( 'emailsender' )->inContentLanguage()->text() );
} else {
- $sender = new MailAddress( $from );
+ $sender = MailAddress::newFromUser( $from );
}
- $to = new MailAddress( $this );
+ $to = MailAddress::newFromUser( $this );
return UserMailer::send( $to, $sender, $subject, $body, $replyto );
}
&& ( $this->editor->getEmail() != '' )
&& $this->editor->getOption( 'enotifrevealaddr' )
) {
- $editorAddress = new MailAddress( $this->editor );
+ $editorAddress = MailAddress::newFromUser( $this->editor );
if ( $wgEnotifFromEditor ) {
$this->from = $editorAddress;
} else {
}
if ( $wgEnotifImpersonal ) {
- $this->mailTargets[] = new MailAddress( $user );
+ $this->mailTargets[] = MailAddress::newFromUser( $user );
} else {
$this->sendPersonalised( $user );
}
// Note: The to parameter cannot be an address in the form of
// "Something <someone@example.com>". The mail command will not parse
// this properly while talking with the MTA.
- $to = new MailAddress( $watchingUser );
+ $to = MailAddress::newFromUser( $watchingUser );
# $PAGEEDITDATE is the time and date of the page change
# expressed in terms of individual local time of the notification
*/
class MailAddress {
/**
- * @param string|User $address String with an email address, or a User object
+ * @param string $address String with an email address, or a User object
* @param string $name Human-readable name if a string address is given
* @param string $realName Human-readable real name if a string address is given
*/
function __construct( $address, $name = null, $realName = null ) {
if ( is_object( $address ) && $address instanceof User ) {
+ // Old calling format, now deprecated
+ wfDeprecated( __METHOD__ . ' with a User object' , '1.24' );
$this->address = $address->getEmail();
$this->name = $address->getName();
$this->realName = $address->getRealName();
}
}
+ /**
+ * Create a new MailAddress object for the given user
+ *
+ * @since 1.24
+ * @param User $user
+ * @return MailAddress
+ */
+ public static function newFromUser( User $user ) {
+ return new MailAddress( $user->getEmail(), $user->getName(), $user->getRealName() );
+ }
+
/**
* Return formatted and quoted address to insert into SMTP headers
* @return string
return $context->msg( $target . 'text' )->parseAsBlock();
}
- $to = new MailAddress( $target );
- $from = new MailAddress( $context->getUser() );
+ $to = MailAddress::newFromUser( $target );
+ $from = MailAddress::newFromUser( $context->getUser() );
$subject = $data['Subject'];
$text = $data['Text'];
class MailAddressTest extends MediaWikiTestCase {
+ /**
+ * @covers MailAddress::__construct
+ */
+ public function testConstructor() {
+ $ma = new MailAddress( 'foo@bar.baz', 'UserName', 'Real name' );
+ $this->assertInstanceOf( 'MailAddress', $ma );
+ }
+
+ /**
+ * @covers MailAddress::newFromUser
+ */
+ public function testNewFromUser() {
+ $user = $this->getMock( 'User' );
+ $user->expects( $this->any() )->method( 'getName' )->will( $this->returnValue( 'UserName' ) );
+ $user->expects( $this->any() )->method( 'getEmail' )->will( $this->returnValue( 'foo@bar.baz' ) );
+ $user->expects( $this->any() )->method( 'getRealName' )->will( $this->returnValue( 'Real name' ) );
+
+ $ma = MailAddress::newFromUser( $user );
+ $this->assertInstanceOf( 'MailAddress', $ma );
+ $this->setMwGlobals( 'wgEnotifUseRealName', true );
+ $this->assertEquals( 'Real name <foo@bar.baz>', $ma->toString() );
+ $this->setMwGlobals( 'wgEnotifUseRealName', false );
+ $this->assertEquals( 'UserName <foo@bar.baz>', $ma->toString() );
+ }
+
/**
* @covers MailAddress::toString
* @dataProvider provideToString