r34631 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34630‎ | r34631 | r34632 >
Date:21:07, 11 May 2008
Author:btongminh
Status:old
Tags:
Comment:
Refactor SpecialEmailUser.php to split outputting from other stuff so that a to be written API module can easily use the existing code.
Modified paths:
  • /trunk/phase3/includes/SpecialEmailuser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialEmailuser.php
@@ -8,52 +8,23 @@
99 * @todo document
1010 */
1111 function wfSpecialEmailuser( $par ) {
12 - global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
 12+ global $wgRequest, $wgUser, $wgOut;
1313
14 - if( !( $wgEnableEmail && $wgEnableUserEmail ) ) {
15 - $wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
 14+ $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
 15+ $error = EmailUserForm::getPermissionsError( $target );
 16+ if ( $error ) {
 17+ $wgOut->showErrorPage( $error[0], $error[1] );
1618 return;
1719 }
1820
19 - if( !$wgUser->canSendEmail() ) {
20 - wfDebug( "User can't send.\n" );
21 - $wgOut->showErrorPage( "mailnologin", "mailnologintext" );
22 - return;
23 - }
24 -
 21+ $form = EmailUserForm::newFromURL( $target,
 22+ $wgRequest->getText( 'wpText' ),
 23+ $wgRequest->getText( 'wpSubject' ),
 24+ $wgRequest->getBool( 'wpCCMe' ) );
 25+
2526 $action = $wgRequest->getVal( 'action' );
26 - $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
27 - if ( "" == $target ) {
28 - wfDebug( "Target is empty.\n" );
29 - $wgOut->showErrorPage( "notargettitle", "notargettext" );
30 - return;
31 - }
32 -
33 - $nt = Title::newFromURL( $target );
34 - if ( is_null( $nt ) ) {
35 - wfDebug( "Target is invalid title.\n" );
36 - $wgOut->showErrorPage( "notargettitle", "notargettext" );
37 - return;
38 - }
39 -
40 - $nu = User::newFromName( $nt->getText() );
41 - if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
42 - wfDebug( "Target is invalid user or can't receive.\n" );
43 - $wgOut->showErrorPage( "noemailtitle", "noemailtext" );
44 - return;
45 - }
46 -
47 - if ( $wgUser->isBlockedFromEmailUser() ) {
48 - // User has been blocked from sending e-mail. Show the std blocked form.
49 - wfDebug( "User is blocked from sending e-mail.\n" );
50 - $wgOut->blockedPage();
51 - return;
52 - }
53 -
54 - $f = new EmailUserForm( $nu );
55 -
5627 if ( "success" == $action ) {
57 - $f->showSuccess( $nu );
 28+ $form->showSuccess();
5829 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
5930 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
6031 {
@@ -63,9 +34,18 @@
6435 return;
6536 }
6637
67 - $f->doSubmit();
 38+ $result = $form->doSubmit();
 39+
 40+ if ( !is_null( $result ) ) {
 41+ $wgOut->addHTML( wfMsg( "usermailererror" ) .
 42+ ' ' . htmlspecialchars( $result->getMessage() ) );
 43+ } else {
 44+ $titleObj = SpecialPage::getTitleFor( "Emailuser" );
 45+ $encTarget = wfUrlencode( $form->getTarget()->getName() );
 46+ $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
 47+ }
6848 } else {
69 - $f->showForm();
 49+ $form->showForm();
7050 }
7151 }
7252
@@ -82,12 +62,11 @@
8363 /**
8464 * @param User $target
8565 */
86 - function EmailUserForm( $target ) {
87 - global $wgRequest;
 66+ function EmailUserForm( $target, $text, $subject, $cc_me ) {
8867 $this->target = $target;
89 - $this->text = $wgRequest->getText( 'wpText' );
90 - $this->subject = $wgRequest->getText( 'wpSubject' );
91 - $this->cc_me = $wgRequest->getBool( 'wpCCMe' );
 68+ $this->text = $text;
 69+ $this->subject = $subject;
 70+ $this->cc_me = $cc_me;
9271 }
9372
9473 function showForm() {
@@ -143,8 +122,13 @@
144123
145124 }
146125
 126+ /*
 127+ * Really send a mail. Permissions should have been checked using
 128+ * EmailUserForm::getPermissionsError. It is probably also a good idea to
 129+ * check the edit token and ping limiter in advance.
 130+ */
147131 function doSubmit() {
148 - global $wgOut, $wgUser, $wgUserEmailUseReplyTo;
 132+ global $wgUser, $wgUserEmailUseReplyTo;
149133
150134 $to = new MailAddress( $this->target );
151135 $from = new MailAddress( $wgUser );
@@ -183,8 +167,8 @@
184168 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
185169
186170 if( WikiError::isError( $mailResult ) ) {
187 - $wgOut->addHTML( wfMsg( "usermailererror" ) .
188 - ' ' . htmlspecialchars( $mailResult->getMessage() ) );
 171+ return $mailResult;
 172+
189173 } else {
190174
191175 // if the user requested a copy of this mail, do this now,
@@ -199,27 +183,68 @@
200184 // We can either show them an error, or we can say everything was fine,
201185 // or we can say we sort of failed AND sort of succeeded. Of these options,
202186 // simply saying there was an error is probably best.
203 - $wgOut->addHTML( wfMsg( "usermailererror" ) .
204 - ' ' . htmlspecialchars( $ccResult->getMessage() ) );
205 - return;
 187+ return $ccResult->getMessage();
206188 }
207189 }
208190 }
209191
210 - $titleObj = SpecialPage::getTitleFor( "Emailuser" );
211 - $encTarget = wfUrlencode( $this->target->getName() );
212 - $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
213192 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
 193+ return;
214194 }
215195 }
216196 }
217197
218 - function showSuccess( &$user ) {
 198+ function showSuccess( &$user = null ) {
219199 global $wgOut;
 200+
 201+ if ( is_null($user) )
 202+ $user = $this->target;
220203
221204 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
222205 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
223206
224207 $wgOut->returnToMain( false, $user->getUserPage() );
225208 }
 209+
 210+ function getTarget() {
 211+ return $this->target;
 212+ }
 213+
 214+ static function getPermissionsError ( $target ) {
 215+ global $wgUser, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
 216+
 217+ if( !( $wgEnableEmail && $wgEnableUserEmail ) )
 218+ return array( "nosuchspecialpage", "nospecialpagetext" );
 219+
 220+ if( !$wgUser->canSendEmail() ) {
 221+ wfDebug( "User can't send.\n" );
 222+ return array( "mailnologin", "mailnologintext" );
 223+ }
 224+
 225+ if ( "" == $target ) {
 226+ wfDebug( "Target is empty.\n" );
 227+ return array( "notargettitle", "notargettext" );
 228+ }
 229+
 230+ $nt = Title::newFromURL( $target );
 231+ if ( is_null( $nt ) ) {
 232+ wfDebug( "Target is invalid title.\n" );
 233+ return array( "notargettitle", "notargettext" );
 234+ }
 235+
 236+ $nu = User::newFromName( $nt->getText() );
 237+ if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
 238+ wfDebug( "Target is invalid user or can't receive.\n" );
 239+ return array( "noemailtitle", "noemailtext" );
 240+ }
 241+
 242+ return;
 243+ }
 244+
 245+ static function newFromURL( $target, $text, $subject, $cc_me )
 246+ {
 247+ $nt = Title::newFromURL( $target );
 248+ $nu = User::newFromName( $nt->getText() );
 249+ return new EmailUserForm( $nu, $text, $subject, $cc_me );
 250+ }
226251 }

Status & tagging log