r75319 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75318‎ | r75319 | r75320 >
Date:17:05, 24 October 2010
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
* (bug 25628) Send email on revision state change. Some refactoring, inverting of logic, functionising. Added seperate extra messages after discussion with Siebrand
Modified paths:
  • /trunk/extensions/CodeReview/CodeReview.i18n.php (modified) (history)
  • /trunk/extensions/CodeReview/backend/CodeRevision.php (modified) (history)
  • /trunk/extensions/CodeReview/ui/CodeRevisionCommitter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CodeReview/CodeReview.i18n.php
@@ -143,6 +143,26 @@
144144 Commit summary:
145145
146146 $4',
 147+
 148+ 'codereview-email-subj3' => '[$1] [$2]: Revision status changed',
 149+ 'codereview-email-body3' => 'User "$1" changed the status of $2.
 150+
 151+Old Status: $3
 152+New Status: $4',
 153+
 154+ 'codereview-email-subj4' => '[$1] [$2]: New comment added, and revision status changed',
 155+ 'codereview-email-body4' => 'User "$1" changed the status of $2.
 156+
 157+Old Status: $3
 158+New Status: $4
 159+
 160+User "$1" also posted a comment on $2.
 161+
 162+Full URL: $5
 163+
 164+Comment:
 165+
 166+$6',
147167
148168 'code-stats' => 'statistics',
149169 'code-stats-header' => 'Statistics for repository $1',
Index: trunk/extensions/CodeReview/backend/CodeRevision.php
@@ -12,6 +12,7 @@
1313 $rev->mMessage = rtrim( $data['msg'] );
1414 $rev->mPaths = $data['paths'];
1515 $rev->mStatus = 'new';
 16+ $rev->mOldStatus = '';
1617
1718 $common = null;
1819 if ( $rev->mPaths ) {
@@ -71,6 +72,7 @@
7273 $rev->mTimestamp = wfTimestamp( TS_MW, $row->cr_timestamp );
7374 $rev->mMessage = $row->cr_message;
7475 $rev->mStatus = $row->cr_status;
 76+ $rev->mOldStatus = '';
7577 $rev->mCommonPath = $row->cr_path;
7678 return $rev;
7779 }
@@ -147,12 +149,12 @@
148150 }
149151 // Get the old status from the master
150152 $dbw = wfGetDB( DB_MASTER );
151 - $oldStatus = $dbw->selectField( 'code_rev',
 153+ $this->moldStatus = $dbw->selectField( 'code_rev',
152154 'cr_status',
153155 array( 'cr_repo_id' => $this->mRepoId, 'cr_id' => $this->mId ),
154156 __METHOD__
155157 );
156 - if ( $oldStatus === $status ) {
 158+ if ( $this->moldStatus === $status ) {
157159 return false; // nothing to do here
158160 }
159161 // Update status
@@ -171,7 +173,7 @@
172174 'cpc_repo_id' => $this->getRepoId(),
173175 'cpc_rev_id' => $this->getId(),
174176 'cpc_attrib' => 'status',
175 - 'cpc_removed' => $oldStatus,
 177+ 'cpc_removed' => $this->moldStatus,
176178 'cpc_added' => $status,
177179 'cpc_timestamp' => $dbw->timestamp(),
178180 'cpc_user' => $user->getId(),
@@ -181,7 +183,7 @@
182184 );
183185 }
184186
185 - $this->sendStatusToUDP( $status, $oldStatus );
 187+ $this->sendStatusToUDP( $status, $this->moldStatus );
186188
187189 return true;
188190 }
@@ -399,7 +401,6 @@
400402 }
401403
402404 public function saveComment( $text, $review, $parent = null ) {
403 - global $wgUser;
404405 $text = trim( $text );
405406 if ( !strlen( $text ) ) {
406407 return 0;
@@ -413,47 +414,61 @@
414415 $commentId = $dbw->insertId();
415416 $dbw->commit();
416417
 418+ $url = $this->getFullUrl( $commentId );
 419+
 420+ $this->sendCommentToUDP( $commentId, $text, $url );
 421+
 422+ return $commentId;
 423+ }
 424+
 425+ /**
 426+ * @param $subject
 427+ * @param $body
 428+ * @return void
 429+ */
 430+ public function emailNotifyUsersOfChanges( $subject, $body ) {
417431 // Give email notices to committer and commenters
418 - global $wgCodeReviewENotif, $wgEnableEmail, $wgCodeReviewCommentWatcher;
419 - if ( $wgCodeReviewENotif && $wgEnableEmail ) {
420 - // Make list of users to send emails to
421 - $users = $this->getCommentingUsers();
422 - if ( $user = $this->getWikiUser() ) {
423 - $users[$user->getId()] = $user;
424 - }
425 - // If we've got a spam list, send e-mails to it too
426 - if ( $wgCodeReviewCommentWatcher ) {
427 - $watcher = new User();
428 - $watcher->setEmail( $wgCodeReviewCommentWatcher );
429 - $users[0] = $watcher; // We don't have any anons, so using 0 is safe
430 - }
431 - // Get repo and build comment title (for url)
432 - $url = $this->getFullUrl( $commentId );
 432+ global $wgCodeReviewENotif, $wgEnableEmail, $wgCodeReviewCommentWatcher,
 433+ $wgUser;
 434+ if ( !$wgCodeReviewENotif && !$wgEnableEmail ) {
 435+ return;
 436+ }
433437
434 - foreach ( $users as $userId => $user ) {
435 - // Notify user with its own message if he already want
436 - // to be CCed of all emails it sends.
437 - if ( $wgUser->getId() == $user->getId() ) {
438 - if(! $user->getBoolOption( 'ccmeonemails' ) ) {
439 - continue;
440 - }
441 - }
 438+ $args = func_get_args();
 439+ array_shift( $args ); //Drop $subject
 440+ array_shift( $args ); //Drop $body
442441
443 - if ( $user->canReceiveEmail() ) {
444 - // Send message in receiver's language
445 - $lang = array( 'language' => $user->getOption( 'language' ) );
446 -
447 - $user->sendMail(
448 - wfMsgExt( 'codereview-email-subj', $lang, $this->mRepo->getName(), $this->getIdString() ),
449 - wfMsgExt( 'codereview-email-body', $lang, $wgUser->getName(), $url, $this->getIdStringUnique(), $text )
450 - );
 442+ // Make list of users to send emails to
 443+ $users = $this->getCommentingUsers();
 444+ if ( $user = $this->getWikiUser() ) {
 445+ $users[$user->getId()] = $user;
 446+ }
 447+ // If we've got a spam list, send e-mails to it too
 448+ if ( $wgCodeReviewCommentWatcher ) {
 449+ $watcher = new User();
 450+ $watcher->setEmail( $wgCodeReviewCommentWatcher );
 451+ $users[0] = $watcher; // We don't have any anons, so using 0 is safe
 452+ }
 453+
 454+ foreach ( $users as $user ) {
 455+ // Notify user with its own message if he already want
 456+ // to be CCed of all emails it sends.
 457+ if ( $wgUser->getId() == $user->getId() ) {
 458+ if(! $user->getBoolOption( 'ccmeonemails' ) ) {
 459+ continue;
451460 }
452461 }
453 - }
454462
455 - $this->sendCommentToUDP( $commentId, $text, $url );
 463+ if ( $user->canReceiveEmail() ) {
 464+ // Send message in receiver's language
 465+ $lang = array( 'language' => $user->getOption( 'language' ) );
456466
457 - return $commentId;
 467+ $localSubject = wfMsgExt( $subject, $lang, $this->mRepo->getName(), $this->getIdString() );
 468+ $localBody = call_user_func_array( 'wfMsgExt', array_merge( array( $body, $lang, $wgUser->getName() ), $args ) );
 469+
 470+ $user->sendMail( $localSubject, $localBody );
 471+ }
 472+ }
458473 }
459474
460475 protected function commentData( $text, $review, $parent = null ) {
Index: trunk/extensions/CodeReview/ui/CodeRevisionCommitter.php
@@ -25,8 +25,9 @@
2626
2727 $dbw->begin();
2828 // Change the status if allowed
 29+ $statusChanged = false;
2930 if ( $this->validPost( 'codereview-set-status' ) && $this->mRev->isValidStatus( $this->mStatus ) ) {
30 - $this->mRev->setStatus( $this->mStatus, $wgUser );
 31+ $statusChanged = $this->mRev->setStatus( $this->mStatus, $wgUser );
3132 }
3233 $addTags = $removeTags = array();
3334 if ( $this->validPost( 'codereview-add-tag' ) && count( $this->mAddTags ) ) {
@@ -40,18 +41,40 @@
4142 $this->mRev->changeTags( $addTags, $removeTags, $wgUser );
4243 }
4344 // Add any comments
 45+ $commentAdded = false;
4446 if ( $this->validPost( 'codereview-post-comment' ) && strlen( $this->text ) ) {
4547 $parent = $wgRequest->getIntOrNull( 'wpParent' );
4648 $review = $wgRequest->getInt( 'wpReview' );
4749 // $isPreview = $wgRequest->getCheck( 'wpPreview' );
48 - $id = $this->mRev->saveComment( $this->text, $review, $parent );
 50+ $commentId = $this->mRev->saveComment( $this->text, $review, $parent );
 51+
 52+ $commentAdded = ($commentId !== 0);
 53+
4954 // For comments, take us back to the rev page focused on the new comment
5055 if ( !$this->jumpToNext ) {
51 - $redirTarget = $this->commentLink( $id );
 56+ $redirTarget = $this->commentLink( $commentId );
5257 }
5358 }
5459 $dbw->commit();
5560
 61+ if ( $statusChanged || $commentAdded ) {
 62+ if ( $statusChanged && $commentAdded ) {
 63+ $url = $this->mRev->getFullUrl( $commentId );
 64+ $this->mRev->emailNotifyUsersOfChanges( 'codereview-email-subj4', 'codereview-email-body4',
 65+ $wgUser->getName(), $this->mRev->getIdStringUnique(), $this->mRev->mOldStatus, $this->mRev->mStatus,
 66+ $url, $this->text
 67+ );
 68+ } else if ( $statusChanged ) {
 69+ $this->mRev->emailNotifyUsersOfChanges( 'codereview-email-subj3', 'codereview-email-body3',
 70+ $wgUser->getName(), $this->mRev->getIdStringUnique(), $this->mRev->mOldStatus, $this->mRev->mStatus
 71+ );
 72+ } else if ( $commentAdded ) {
 73+ $url = $this->mRev->getFullUrl( $commentId );
 74+ $this->mRev->emailNotifyUsersOfChanges( 'codereview-email-subj', 'codereview-email-body',
 75+ $wgUser->getName(), $url, $this->mRev->getIdStringUnique(), $this->text );
 76+ }
 77+ }
 78+
5679 // Return to rev page
5780 if ( !$redirTarget ) {
5881 // Was "next & unresolved" clicked?

Follow-up revisions

RevisionCommit summaryAuthorDate
r75323Minor followup r75319, fix some moldStatus to mOldStatusreedy17:33, 24 October 2010
r80961Fixup whitespace issues from r75319...reedy08:28, 25 January 2011
r80985Followup r75319, swap && for ||, only care about one being false to short cir...reedy20:10, 25 January 2011

Comments

#Comment by Catrope (talk | contribs)   01:57, 25 January 2011
-		if ( $wgCodeReviewENotif && $wgEnableEmail ) {
+		if ( !$wgCodeReviewENotif && !$wgEnableEmail ) {

Those conditions aren't each other's inverses; you should probably use || here instead of && (DeMorgan's law).

+		$commentAdded = false;
 		if ( $this->validPost( 'codereview-post-comment' ) && strlen( $this->text ) ) {
 			$parent = $wgRequest->getIntOrNull( 'wpParent' );
 			$review = $wgRequest->getInt( 'wpReview' );
 			// $isPreview = $wgRequest->getCheck( 'wpPreview' );
-			$id = $this->mRev->saveComment( $this->text, $review, $parent );
+			$commentId = $this->mRev->saveComment( $this->text, $review, $parent );
+
+		    $commentAdded = ($commentId !== 0);

Indentation (2x).

+	    if ( $statusChanged || $commentAdded ) {
+		    if ( $statusChanged && $commentAdded ) {
+			    $url = $this->mRev->getFullUrl( $commentId );
+		        $this->mRev->emailNotifyUsersOfChanges( 'codereview-email-subj4', 'codereview-email-body4',
+			        $wgUser->getName(), $this->mRev->getIdStringUnique(), $this->mRev->mOldStatus, $this->mRev->mStatus,
+					$url, $this->text
+		            );
+		    } else if ( $statusChanged ) {
+				$this->mRev->emailNotifyUsersOfChanges( 'codereview-email-subj3', 'codereview-email-body3',
+					$wgUser->getName(), $this->mRev->getIdStringUnique(), $this->mRev->mOldStatus, $this->mRev->mStatus
+					);
+		    } else if ( $commentAdded ) {
+			    $url = $this->mRev->getFullUrl( $commentId );
+				$this->mRev->emailNotifyUsersOfChanges( 'codereview-email-subj', 'codereview-email-body',
+					$wgUser->getName(), $url, $this->mRev->getIdStringUnique(), $this->text );
+		    }
+	    }
+

Indentation issues (spaces instead of tabs) all over. Also, you should use getters (and add them if they don't exist) rather than using ->mStatus and ->mOldstatus

#Comment by Reedy (talk | contribs)   20:16, 25 January 2011

Other issues fixed. As for the using direct member variable access, rather than getters, fair point. Though, I've got a feeling that this sort of thing is done widespread through the CR extension..

And hence as such we get:

/** * @var CodeRepository */ public $mRepo;

public $mRepoId, $mId, $mAuthor, $mTimestamp, $mMessage, $mPaths, $mStatus, $mOldStatus, $mCommonPath;

Which should probably be cleaned up. (I've got a feeling I explicitally defined them private, and then random places were using them and then complained about access).

Whether you want to still leave it fixme for that reason, I'm not fussed, but I'll look at tidying it up over the whole extension later on.

#Comment by Catrope (talk | contribs)   22:40, 26 January 2011

It's fine.

Status & tagging log