MediaWiki
REL1_24
|
00001 <?php 00045 class EmailNotification { 00046 protected $subject, $body, $replyto, $from; 00047 protected $timestamp, $summary, $minorEdit, $oldid, $composed_common, $pageStatus; 00048 protected $mailTargets = array(); 00049 00053 protected $title; 00054 00058 protected $editor; 00059 00067 public static function updateWatchlistTimestamp( User $editor, Title $title, $timestamp ) { 00068 global $wgEnotifWatchlist, $wgShowUpdatedMarker; 00069 00070 if ( !$wgEnotifWatchlist && !$wgShowUpdatedMarker ) { 00071 return array(); 00072 } 00073 00074 $dbw = wfGetDB( DB_MASTER ); 00075 $res = $dbw->select( array( 'watchlist' ), 00076 array( 'wl_user' ), 00077 array( 00078 'wl_user != ' . intval( $editor->getID() ), 00079 'wl_namespace' => $title->getNamespace(), 00080 'wl_title' => $title->getDBkey(), 00081 'wl_notificationtimestamp IS NULL', 00082 ), __METHOD__ 00083 ); 00084 00085 $watchers = array(); 00086 foreach ( $res as $row ) { 00087 $watchers[] = intval( $row->wl_user ); 00088 } 00089 00090 if ( $watchers ) { 00091 // Update wl_notificationtimestamp for all watching users except the editor 00092 $fname = __METHOD__; 00093 $dbw->onTransactionIdle( 00094 function () use ( $dbw, $timestamp, $watchers, $title, $fname ) { 00095 $dbw->update( 'watchlist', 00096 array( /* SET */ 00097 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp ) 00098 ), array( /* WHERE */ 00099 'wl_user' => $watchers, 00100 'wl_namespace' => $title->getNamespace(), 00101 'wl_title' => $title->getDBkey(), 00102 ), $fname 00103 ); 00104 } 00105 ); 00106 } 00107 00108 return $watchers; 00109 } 00110 00125 public function notifyOnPageChange( $editor, $title, $timestamp, $summary, 00126 $minorEdit, $oldid = false, $pageStatus = 'changed' 00127 ) { 00128 global $wgEnotifUseJobQ, $wgEnotifMinorEdits, $wgUsersNotifiedOnAllChanges, $wgEnotifUserTalk; 00129 00130 if ( $title->getNamespace() < 0 ) { 00131 return; 00132 } 00133 00134 // update wl_notificationtimestamp for watchers 00135 $watchers = self::updateWatchlistTimestamp( $editor, $title, $timestamp ); 00136 00137 $sendEmail = true; 00138 // If nobody is watching the page, and there are no users notified on all changes 00139 // don't bother creating a job/trying to send emails 00140 // $watchers deals with $wgEnotifWatchlist 00141 if ( !count( $watchers ) && !count( $wgUsersNotifiedOnAllChanges ) ) { 00142 $sendEmail = false; 00143 // Only send notification for non minor edits, unless $wgEnotifMinorEdits 00144 if ( !$minorEdit || ( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) { 00145 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK ); 00146 if ( $wgEnotifUserTalk 00147 && $isUserTalkPage 00148 && $this->canSendUserTalkEmail( $editor, $title, $minorEdit ) 00149 ) { 00150 $sendEmail = true; 00151 } 00152 } 00153 } 00154 00155 if ( !$sendEmail ) { 00156 return; 00157 } 00158 00159 if ( $wgEnotifUseJobQ ) { 00160 $params = array( 00161 'editor' => $editor->getName(), 00162 'editorID' => $editor->getID(), 00163 'timestamp' => $timestamp, 00164 'summary' => $summary, 00165 'minorEdit' => $minorEdit, 00166 'oldid' => $oldid, 00167 'watchers' => $watchers, 00168 'pageStatus' => $pageStatus 00169 ); 00170 $job = new EnotifNotifyJob( $title, $params ); 00171 JobQueueGroup::singleton()->push( $job ); 00172 } else { 00173 $this->actuallyNotifyOnPageChange( 00174 $editor, 00175 $title, 00176 $timestamp, 00177 $summary, 00178 $minorEdit, 00179 $oldid, 00180 $watchers, 00181 $pageStatus 00182 ); 00183 } 00184 } 00185 00202 public function actuallyNotifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, 00203 $oldid, $watchers, $pageStatus = 'changed' ) { 00204 # we use $wgPasswordSender as sender's address 00205 global $wgEnotifWatchlist; 00206 global $wgEnotifMinorEdits, $wgEnotifUserTalk; 00207 00208 wfProfileIn( __METHOD__ ); 00209 00210 # The following code is only run, if several conditions are met: 00211 # 1. EmailNotification for pages (other than user_talk pages) must be enabled 00212 # 2. minor edits (changes) are only regarded if the global flag indicates so 00213 00214 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK ); 00215 00216 $this->title = $title; 00217 $this->timestamp = $timestamp; 00218 $this->summary = $summary; 00219 $this->minorEdit = $minorEdit; 00220 $this->oldid = $oldid; 00221 $this->editor = $editor; 00222 $this->composed_common = false; 00223 $this->pageStatus = $pageStatus; 00224 00225 $formattedPageStatus = array( 'deleted', 'created', 'moved', 'restored', 'changed' ); 00226 00227 wfRunHooks( 'UpdateUserMailerFormattedPageStatus', array( &$formattedPageStatus ) ); 00228 if ( !in_array( $this->pageStatus, $formattedPageStatus ) ) { 00229 wfProfileOut( __METHOD__ ); 00230 throw new MWException( 'Not a valid page status!' ); 00231 } 00232 00233 $userTalkId = false; 00234 00235 if ( !$minorEdit || ( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) { 00236 if ( $wgEnotifUserTalk 00237 && $isUserTalkPage 00238 && $this->canSendUserTalkEmail( $editor, $title, $minorEdit ) 00239 ) { 00240 $targetUser = User::newFromName( $title->getText() ); 00241 $this->compose( $targetUser ); 00242 $userTalkId = $targetUser->getId(); 00243 } 00244 00245 if ( $wgEnotifWatchlist ) { 00246 // Send updates to watchers other than the current editor 00247 $userArray = UserArray::newFromIDs( $watchers ); 00248 foreach ( $userArray as $watchingUser ) { 00249 if ( $watchingUser->getOption( 'enotifwatchlistpages' ) 00250 && ( !$minorEdit || $watchingUser->getOption( 'enotifminoredits' ) ) 00251 && $watchingUser->isEmailConfirmed() 00252 && $watchingUser->getID() != $userTalkId 00253 ) { 00254 if ( wfRunHooks( 'SendWatchlistEmailNotification', array( $watchingUser, $title, $this ) ) ) { 00255 $this->compose( $watchingUser ); 00256 } 00257 } 00258 } 00259 } 00260 } 00261 00262 global $wgUsersNotifiedOnAllChanges; 00263 foreach ( $wgUsersNotifiedOnAllChanges as $name ) { 00264 if ( $editor->getName() == $name ) { 00265 // No point notifying the user that actually made the change! 00266 continue; 00267 } 00268 $user = User::newFromName( $name ); 00269 $this->compose( $user ); 00270 } 00271 00272 $this->sendMails(); 00273 wfProfileOut( __METHOD__ ); 00274 } 00275 00282 private function canSendUserTalkEmail( $editor, $title, $minorEdit ) { 00283 global $wgEnotifUserTalk; 00284 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK ); 00285 00286 if ( $wgEnotifUserTalk && $isUserTalkPage ) { 00287 $targetUser = User::newFromName( $title->getText() ); 00288 00289 if ( !$targetUser || $targetUser->isAnon() ) { 00290 wfDebug( __METHOD__ . ": user talk page edited, but user does not exist\n" ); 00291 } elseif ( $targetUser->getId() == $editor->getId() ) { 00292 wfDebug( __METHOD__ . ": user edited their own talk page, no notification sent\n" ); 00293 } elseif ( $targetUser->getOption( 'enotifusertalkpages' ) 00294 && ( !$minorEdit || $targetUser->getOption( 'enotifminoredits' ) ) 00295 ) { 00296 if ( !$targetUser->isEmailConfirmed() ) { 00297 wfDebug( __METHOD__ . ": talk page owner doesn't have validated email\n" ); 00298 } elseif ( !wfRunHooks( 'AbortTalkPageEmailNotification', array( $targetUser, $title ) ) ) { 00299 wfDebug( __METHOD__ . ": talk page update notification is aborted for this user\n" ); 00300 } else { 00301 wfDebug( __METHOD__ . ": sending talk page update notification\n" ); 00302 return true; 00303 } 00304 } else { 00305 wfDebug( __METHOD__ . ": talk page owner doesn't want notifications\n" ); 00306 } 00307 } 00308 return false; 00309 } 00310 00314 private function composeCommonMailtext() { 00315 global $wgPasswordSender, $wgNoReplyAddress; 00316 global $wgEnotifFromEditor, $wgEnotifRevealEditorAddress; 00317 global $wgEnotifImpersonal, $wgEnotifUseRealName; 00318 00319 $this->composed_common = true; 00320 00321 # You as the WikiAdmin and Sysops can make use of plenty of 00322 # named variables when composing your notification emails while 00323 # simply editing the Meta pages 00324 00325 $keys = array(); 00326 $postTransformKeys = array(); 00327 $pageTitleUrl = $this->title->getCanonicalURL(); 00328 $pageTitle = $this->title->getPrefixedText(); 00329 00330 if ( $this->oldid ) { 00331 // Always show a link to the diff which triggered the mail. See bug 32210. 00332 $keys['$NEWPAGE'] = "\n\n" . wfMessage( 'enotif_lastdiff', 00333 $this->title->getCanonicalURL( array( 'diff' => 'next', 'oldid' => $this->oldid ) ) ) 00334 ->inContentLanguage()->text(); 00335 00336 if ( !$wgEnotifImpersonal ) { 00337 // For personal mail, also show a link to the diff of all changes 00338 // since last visited. 00339 $keys['$NEWPAGE'] .= "\n\n" . wfMessage( 'enotif_lastvisited', 00340 $this->title->getCanonicalURL( array( 'diff' => '0', 'oldid' => $this->oldid ) ) ) 00341 ->inContentLanguage()->text(); 00342 } 00343 $keys['$OLDID'] = $this->oldid; 00344 // Deprecated since MediaWiki 1.21, not used by default. Kept for backwards-compatibility. 00345 $keys['$CHANGEDORCREATED'] = wfMessage( 'changed' )->inContentLanguage()->text(); 00346 } else { 00347 # clear $OLDID placeholder in the message template 00348 $keys['$OLDID'] = ''; 00349 $keys['$NEWPAGE'] = ''; 00350 // Deprecated since MediaWiki 1.21, not used by default. Kept for backwards-compatibility. 00351 $keys['$CHANGEDORCREATED'] = wfMessage( 'created' )->inContentLanguage()->text(); 00352 } 00353 00354 $keys['$PAGETITLE'] = $this->title->getPrefixedText(); 00355 $keys['$PAGETITLE_URL'] = $this->title->getCanonicalURL(); 00356 $keys['$PAGEMINOREDIT'] = $this->minorEdit ? 00357 wfMessage( 'minoredit' )->inContentLanguage()->text() : ''; 00358 $keys['$UNWATCHURL'] = $this->title->getCanonicalURL( 'action=unwatch' ); 00359 00360 if ( $this->editor->isAnon() ) { 00361 # real anon (user:xxx.xxx.xxx.xxx) 00362 $keys['$PAGEEDITOR'] = wfMessage( 'enotif_anon_editor', $this->editor->getName() ) 00363 ->inContentLanguage()->text(); 00364 $keys['$PAGEEDITOR_EMAIL'] = wfMessage( 'noemailtitle' )->inContentLanguage()->text(); 00365 00366 } else { 00367 $keys['$PAGEEDITOR'] = $wgEnotifUseRealName && $this->editor->getRealName() !== '' 00368 ? $this->editor->getRealName() : $this->editor->getName(); 00369 $emailPage = SpecialPage::getSafeTitleFor( 'Emailuser', $this->editor->getName() ); 00370 $keys['$PAGEEDITOR_EMAIL'] = $emailPage->getCanonicalURL(); 00371 } 00372 00373 $keys['$PAGEEDITOR_WIKI'] = $this->editor->getUserPage()->getCanonicalURL(); 00374 $keys['$HELPPAGE'] = wfExpandUrl( 00375 Skin::makeInternalOrExternalUrl( wfMessage( 'helppage' )->inContentLanguage()->text() ) 00376 ); 00377 00378 # Replace this after transforming the message, bug 35019 00379 $postTransformKeys['$PAGESUMMARY'] = $this->summary == '' ? ' - ' : $this->summary; 00380 00381 // Now build message's subject and body 00382 00383 // Messages: 00384 // enotif_subject_deleted, enotif_subject_created, enotif_subject_moved, 00385 // enotif_subject_restored, enotif_subject_changed 00386 $this->subject = wfMessage( 'enotif_subject_' . $this->pageStatus )->inContentLanguage() 00387 ->params( $pageTitle, $keys['$PAGEEDITOR'] )->text(); 00388 00389 // Messages: 00390 // enotif_body_intro_deleted, enotif_body_intro_created, enotif_body_intro_moved, 00391 // enotif_body_intro_restored, enotif_body_intro_changed 00392 $keys['$PAGEINTRO'] = wfMessage( 'enotif_body_intro_' . $this->pageStatus ) 00393 ->inContentLanguage()->params( $pageTitle, $keys['$PAGEEDITOR'], $pageTitleUrl ) 00394 ->text(); 00395 00396 $body = wfMessage( 'enotif_body' )->inContentLanguage()->plain(); 00397 $body = strtr( $body, $keys ); 00398 $body = MessageCache::singleton()->transform( $body, false, null, $this->title ); 00399 $this->body = wordwrap( strtr( $body, $postTransformKeys ), 72 ); 00400 00401 # Reveal the page editor's address as REPLY-TO address only if 00402 # the user has not opted-out and the option is enabled at the 00403 # global configuration level. 00404 $adminAddress = new MailAddress( $wgPasswordSender, 00405 wfMessage( 'emailsender' )->inContentLanguage()->text() ); 00406 if ( $wgEnotifRevealEditorAddress 00407 && ( $this->editor->getEmail() != '' ) 00408 && $this->editor->getOption( 'enotifrevealaddr' ) 00409 ) { 00410 $editorAddress = MailAddress::newFromUser( $this->editor ); 00411 if ( $wgEnotifFromEditor ) { 00412 $this->from = $editorAddress; 00413 } else { 00414 $this->from = $adminAddress; 00415 $this->replyto = $editorAddress; 00416 } 00417 } else { 00418 $this->from = $adminAddress; 00419 $this->replyto = new MailAddress( $wgNoReplyAddress ); 00420 } 00421 } 00422 00430 function compose( $user ) { 00431 global $wgEnotifImpersonal; 00432 00433 if ( !$this->composed_common ) { 00434 $this->composeCommonMailtext(); 00435 } 00436 00437 if ( $wgEnotifImpersonal ) { 00438 $this->mailTargets[] = MailAddress::newFromUser( $user ); 00439 } else { 00440 $this->sendPersonalised( $user ); 00441 } 00442 } 00443 00447 function sendMails() { 00448 global $wgEnotifImpersonal; 00449 if ( $wgEnotifImpersonal ) { 00450 $this->sendImpersonal( $this->mailTargets ); 00451 } 00452 } 00453 00463 function sendPersonalised( $watchingUser ) { 00464 global $wgContLang, $wgEnotifUseRealName; 00465 // From the PHP manual: 00466 // Note: The to parameter cannot be an address in the form of 00467 // "Something <[email protected]>". The mail command will not parse 00468 // this properly while talking with the MTA. 00469 $to = MailAddress::newFromUser( $watchingUser ); 00470 00471 # $PAGEEDITDATE is the time and date of the page change 00472 # expressed in terms of individual local time of the notification 00473 # recipient, i.e. watching user 00474 $body = str_replace( 00475 array( '$WATCHINGUSERNAME', 00476 '$PAGEEDITDATE', 00477 '$PAGEEDITTIME' ), 00478 array( $wgEnotifUseRealName && $watchingUser->getRealName() !== '' 00479 ? $watchingUser->getRealName() : $watchingUser->getName(), 00480 $wgContLang->userDate( $this->timestamp, $watchingUser ), 00481 $wgContLang->userTime( $this->timestamp, $watchingUser ) ), 00482 $this->body ); 00483 00484 return UserMailer::send( $to, $this->from, $this->subject, $body, $this->replyto ); 00485 } 00486 00493 function sendImpersonal( $addresses ) { 00494 global $wgContLang; 00495 00496 if ( empty( $addresses ) ) { 00497 return null; 00498 } 00499 00500 $body = str_replace( 00501 array( '$WATCHINGUSERNAME', 00502 '$PAGEEDITDATE', 00503 '$PAGEEDITTIME' ), 00504 array( wfMessage( 'enotif_impersonal_salutation' )->inContentLanguage()->text(), 00505 $wgContLang->date( $this->timestamp, false, false ), 00506 $wgContLang->time( $this->timestamp, false, false ) ), 00507 $this->body ); 00508 00509 return UserMailer::send( $addresses, $this->from, $this->subject, $body, $this->replyto ); 00510 } 00511 00512 }