r26357 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26356‎ | r26357 | r26358 >
Date:08:46, 3 October 2007
Author:tstarling
Status:old
Tags:
Comment:
WARNING! NEEDS CAREFUL DEPLOYMENT
* Bug 9213: Fixed the plainly broken user_newtalk updating and caching scheme. I tried to keep my changes roughly performance-neutral, but the update on Wikimedia should be watched carefully for performance problems.
* Made UserMailer a class, use the autoloader to load it
* General UserMailer refactoring
* If the user has email-on-newtalk enabled, send them an email for every change, not just the first one before they view the page again.
* Don't add a watchlist entry automatically on change of user talk page
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/JobQueue.php (modified) (history)
  • /trunk/phase3/includes/RecentChange.php (modified) (history)
  • /trunk/phase3/includes/User.php (modified) (history)
  • /trunk/phase3/includes/UserMailer.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/User.php
@@ -206,13 +206,7 @@
207207 return false;
208208 }
209209
210 - # Save to cache
211 - $data = array();
212 - foreach ( self::$mCacheVars as $name ) {
213 - $data[$name] = $this->$name;
214 - }
215 - $data['mVersion'] = MW_USER_VERSION;
216 - $wgMemc->set( $key, $data );
 210+ $this->saveToCache();
217211 } else {
218212 wfDebug( "Got user {$this->mId} from cache\n" );
219213 # Restore from cache
@@ -224,6 +218,25 @@
225219 }
226220
227221 /**
 222+ * Save user data to the shared cache
 223+ */
 224+ function saveToCache() {
 225+ $this->load();
 226+ if ( $this->isAnon() ) {
 227+ // Anonymous users are uncached
 228+ return;
 229+ }
 230+ $data = array();
 231+ foreach ( self::$mCacheVars as $name ) {
 232+ $data[$name] = $this->$name;
 233+ }
 234+ $data['mVersion'] = MW_USER_VERSION;
 235+ $key = wfMemcKey( 'user', 'id', $this->mId );
 236+ global $wgMemc;
 237+ $wgMemc->set( $key, $data );
 238+ }
 239+
 240+ /**
228241 * Static factory method for creation from username.
229242 *
230243 * This is slightly less efficient than newFromId(), so use newFromId() if
@@ -1196,11 +1209,13 @@
11971210 global $wgMemc;
11981211 $key = wfMemcKey( 'newtalk', 'ip', $this->getName() );
11991212 $newtalk = $wgMemc->get( $key );
1200 - if( $newtalk != "" ) {
 1213+ if( strval( $newtalk ) !== '' ) {
12011214 $this->mNewtalk = (bool)$newtalk;
12021215 } else {
1203 - $this->mNewtalk = $this->checkNewtalk( 'user_ip', $this->getName() );
1204 - $wgMemc->set( $key, (int)$this->mNewtalk, time() + 1800 );
 1216+ // Since we are caching this, make sure it is up to date by getting it
 1217+ // from the master
 1218+ $this->mNewtalk = $this->checkNewtalk( 'user_ip', $this->getName(), true );
 1219+ $wgMemc->set( $key, (int)$this->mNewtalk, 1800 );
12051220 }
12061221 } else {
12071222 $this->mNewtalk = $this->checkNewtalk( 'user_id', $this->mId );
@@ -1227,18 +1242,22 @@
12281243
12291244
12301245 /**
1231 - * Perform a user_newtalk check on current slaves; if the memcached data
1232 - * is funky we don't want newtalk state to get stuck on save, as that's
1233 - * damn annoying.
1234 - *
 1246+ * Perform a user_newtalk check, uncached.
 1247+ * Use getNewtalk for a cached check.
 1248+ *
12351249 * @param string $field
12361250 * @param mixed $id
 1251+ * @param bool $fromMaster True to fetch from the master, false for a slave
12371252 * @return bool
12381253 * @private
12391254 */
1240 - function checkNewtalk( $field, $id ) {
1241 - $dbr = wfGetDB( DB_SLAVE );
1242 - $ok = $dbr->selectField( 'user_newtalk', $field,
 1255+ function checkNewtalk( $field, $id, $fromMaster = false ) {
 1256+ if ( $fromMaster ) {
 1257+ $db = wfGetDB( DB_MASTER );
 1258+ } else {
 1259+ $db = wfGetDB( DB_SLAVE );
 1260+ }
 1261+ $ok = $db->selectField( 'user_newtalk', $field,
12431262 array( $field => $id ), __METHOD__ );
12441263 return $ok !== false;
12451264 }
@@ -1250,17 +1269,18 @@
12511270 * @private
12521271 */
12531272 function updateNewtalk( $field, $id ) {
1254 - if( $this->checkNewtalk( $field, $id ) ) {
1255 - wfDebug( __METHOD__." already set ($field, $id), ignoring\n" );
1256 - return false;
1257 - }
12581273 $dbw = wfGetDB( DB_MASTER );
12591274 $dbw->insert( 'user_newtalk',
12601275 array( $field => $id ),
12611276 __METHOD__,
12621277 'IGNORE' );
1263 - wfDebug( __METHOD__.": set on ($field, $id)\n" );
1264 - return true;
 1278+ if ( $dbw->affectedRows() ) {
 1279+ wfDebug( __METHOD__.": set on ($field, $id)\n" );
 1280+ return true;
 1281+ } else {
 1282+ wfDebug( __METHOD__." already set ($field, $id)\n" );
 1283+ return false;
 1284+ }
12651285 }
12661286
12671287 /**
@@ -1270,16 +1290,17 @@
12711291 * @private
12721292 */
12731293 function deleteNewtalk( $field, $id ) {
1274 - if( !$this->checkNewtalk( $field, $id ) ) {
1275 - wfDebug( __METHOD__.": already gone ($field, $id), ignoring\n" );
1276 - return false;
1277 - }
12781294 $dbw = wfGetDB( DB_MASTER );
12791295 $dbw->delete( 'user_newtalk',
12801296 array( $field => $id ),
12811297 __METHOD__ );
1282 - wfDebug( __METHOD__.": killed on ($field, $id)\n" );
1283 - return true;
 1298+ if ( $dbw->affectedRows() ) {
 1299+ wfDebug( __METHOD__.": killed on ($field, $id)\n" );
 1300+ return true;
 1301+ } else {
 1302+ wfDebug( __METHOD__.": already gone ($field, $id)\n" );
 1303+ return false;
 1304+ }
12841305 }
12851306
12861307 /**
@@ -1301,6 +1322,7 @@
13021323 $field = 'user_id';
13031324 $id = $this->getId();
13041325 }
 1326+ global $wgMemc;
13051327
13061328 if( $val ) {
13071329 $changed = $this->updateNewtalk( $field, $id );
@@ -1308,20 +1330,13 @@
13091331 $changed = $this->deleteNewtalk( $field, $id );
13101332 }
13111333
1312 - if( $changed ) {
1313 - if( $this->isAnon() ) {
1314 - // Anons have a separate memcached space, since
1315 - // user records aren't kept for them.
1316 - global $wgMemc;
1317 - $key = wfMemcKey( 'newtalk', 'ip', $val );
1318 - $wgMemc->set( $key, $val ? 1 : 0 );
1319 - } else {
1320 - if( $val ) {
1321 - // Make sure the user page is watched, so a notification
1322 - // will be sent out if enabled.
1323 - $this->addWatch( $this->getTalkPage() );
1324 - }
1325 - }
 1334+ if( $this->isAnon() ) {
 1335+ // Anons have a separate memcached space, since
 1336+ // user records aren't kept for them.
 1337+ $key = wfMemcKey( 'newtalk', 'ip', $id );
 1338+ $wgMemc->set( $key, $val ? 1 : 0, 1800 );
 1339+ }
 1340+ if ( $changed ) {
13261341 $this->invalidateCache();
13271342 }
13281343 }
@@ -1893,7 +1908,7 @@
18941909 'wl_notificationtimestamp' => NULL
18951910 ), array( /* WHERE */
18961911 'wl_user' => $currentUser
1897 - ), 'UserMailer::clearAll'
 1912+ ), __METHOD__
18981913 );
18991914
19001915 # we also need to clear here the "you have new message" notification for the own user_talk page
@@ -2378,10 +2393,9 @@
23792394 $from = $wgPasswordSender;
23802395 }
23812396
2382 - require_once( 'UserMailer.php' );
23832397 $to = new MailAddress( $this );
23842398 $sender = new MailAddress( $from );
2385 - $error = userMailer( $to, $sender, $subject, $body );
 2399+ $error = UserMailer::send( $to, $sender, $subject, $body );
23862400
23872401 if( $error == '' ) {
23882402 return true;
@@ -2689,3 +2703,4 @@
26902704 }
26912705
26922706
 2707+
Index: trunk/phase3/includes/RecentChange.php
@@ -221,8 +221,7 @@
222222 if( $wgUseEnotif ) {
223223 # this would be better as an extension hook
224224 global $wgUser;
225 - include_once( "UserMailer.php" );
226 - $enotif = new EmailNotification();
 225+ $enotif = new EmailNotification;
227226 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
228227 $enotif->notifyOnPageChange( $wgUser, $title,
229228 $this->mAttribs['rc_timestamp'],
@@ -626,3 +625,4 @@
627626 }
628627 }
629628
 629+
Index: trunk/phase3/includes/UserMailer.php
@@ -1,9 +1,5 @@
22 <?php
33 /**
4 - * UserMailer.php
5 - * Copyright (C) 2004 Thomas Gries <mail@tgries.de>
6 - * http://www.mediawiki.org/
7 - *
84 * This program is free software; you can redistribute it and/or modify
95 * it under the terms of the GNU General Public License as published by
106 * the Free Software Foundation; either version 2 of the License, or
@@ -21,16 +17,10 @@
2218 *
2319 * @author <brion@pobox.com>
2420 * @author <mail@tgries.de>
 21+ * @author Tim Starling
2522 *
2623 */
2724
28 -/**
29 - * Converts a string into a valid RFC 822 "phrase", such as is used for the sender name
30 - */
31 -function wfRFC822Phrase( $phrase ) {
32 - $phrase = strtr( $phrase, array( "\r" => '', "\n" => '', '"' => '' ) );
33 - return '"' . $phrase . '"';
34 -}
3525
3626 /**
3727 * Stores a single person's name and email address.
@@ -70,155 +60,184 @@
7161 return $this->address;
7262 }
7363 }
74 -}
7564
76 -function send_mail($mailer, $dest, $headers, $body)
77 -{
78 - $mailResult =& $mailer->send($dest, $headers, $body);
79 -
80 - # Based on the result return an error string,
81 - if ($mailResult === true) {
82 - return '';
83 - } elseif (is_object($mailResult)) {
84 - wfDebug( "PEAR::Mail failed: " . $mailResult->getMessage() . "\n" );
85 - return $mailResult->getMessage();
86 - } else {
87 - wfDebug( "PEAR::Mail failed, unknown error result\n" );
88 - return 'Mail object return unknown error.';
 65+ function __toString() {
 66+ return $this->toString();
8967 }
9068 }
9169
 70+
9271 /**
93 - * This function will perform a direct (authenticated) login to
94 - * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an
95 - * array of parameters. It requires PEAR:Mail to do that.
96 - * Otherwise it just uses the standard PHP 'mail' function.
97 - *
98 - * @param $to MailAddress: recipient's email
99 - * @param $from MailAddress: sender's email
100 - * @param $subject String: email's subject.
101 - * @param $body String: email's text.
102 - * @param $replyto String: optional reply-to email (default: null).
 72+ * Collection of static functions for sending mail
10373 */
104 -function userMailer( $to, $from, $subject, $body, $replyto=null ) {
105 - global $wgSMTP, $wgOutputEncoding, $wgErrorString, $wgEnotifImpersonal;
106 - global $wgEnotifMaxRecips;
 74+class UserMailer {
 75+ /**
 76+ * Send mail using a PEAR mailer
 77+ */
 78+ protected static function sendWithPear($mailer, $dest, $headers, $body)
 79+ {
 80+ $mailResult =& $mailer->send($dest, $headers, $body);
10781
108 - if (is_array( $wgSMTP )) {
109 - require_once( 'Mail.php' );
 82+ # Based on the result return an error string,
 83+ if ($mailResult === true) {
 84+ return '';
 85+ } elseif (is_object($mailResult)) {
 86+ wfDebug( "PEAR::Mail failed: " . $mailResult->getMessage() . "\n" );
 87+ return $mailResult->getMessage();
 88+ } else {
 89+ wfDebug( "PEAR::Mail failed, unknown error result\n" );
 90+ return 'Mail object return unknown error.';
 91+ }
 92+ }
11093
111 - $msgid = str_replace(" ", "_", microtime());
112 - if (function_exists('posix_getpid'))
113 - $msgid .= '.' . posix_getpid();
 94+ /**
 95+ * This function will perform a direct (authenticated) login to
 96+ * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an
 97+ * array of parameters. It requires PEAR:Mail to do that.
 98+ * Otherwise it just uses the standard PHP 'mail' function.
 99+ *
 100+ * @param $to MailAddress: recipient's email
 101+ * @param $from MailAddress: sender's email
 102+ * @param $subject String: email's subject.
 103+ * @param $body String: email's text.
 104+ * @param $replyto String: optional reply-to email (default: null).
 105+ */
 106+ static function send( $to, $from, $subject, $body, $replyto=null ) {
 107+ global $wgSMTP, $wgOutputEncoding, $wgErrorString, $wgEnotifImpersonal;
 108+ global $wgEnotifMaxRecips;
114109
115 - if (is_array($to)) {
116 - $dest = array();
117 - foreach ($to as $u)
118 - $dest[] = $u->address;
119 - } else
120 - $dest = $to->address;
 110+ if ( is_array( $to ) ) {
 111+ wfDebug( __METHOD__.': sending mail to ' . implode( ',', $to ) . "\n" );
 112+ } else {
 113+ wfDebug( __METHOD__.': sending mail to ' . implode( ',', array( $to ) ) . "\n" );
 114+ }
121115
122 - $headers['From'] = $from->toString();
 116+ if (is_array( $wgSMTP )) {
 117+ require_once( 'Mail.php' );
123118
124 - if ($wgEnotifImpersonal)
125 - $headers['To'] = 'undisclosed-recipients:;';
126 - else
127 - $headers['To'] = $to->toString();
 119+ $msgid = str_replace(" ", "_", microtime());
 120+ if (function_exists('posix_getpid'))
 121+ $msgid .= '.' . posix_getpid();
128122
129 - if ( $replyto ) {
130 - $headers['Reply-To'] = $replyto->toString();
131 - }
132 - $headers['Subject'] = wfQuotedPrintable( $subject );
133 - $headers['Date'] = date( 'r' );
134 - $headers['MIME-Version'] = '1.0';
135 - $headers['Content-type'] = 'text/plain; charset='.$wgOutputEncoding;
136 - $headers['Content-transfer-encoding'] = '8bit';
137 - $headers['Message-ID'] = "<$msgid@" . $wgSMTP['IDHost'] . '>'; // FIXME
138 - $headers['X-Mailer'] = 'MediaWiki mailer';
 123+ if (is_array($to)) {
 124+ $dest = array();
 125+ foreach ($to as $u)
 126+ $dest[] = $u->address;
 127+ } else
 128+ $dest = $to->address;
139129
140 - // Create the mail object using the Mail::factory method
141 - $mail_object =& Mail::factory('smtp', $wgSMTP);
142 - if( PEAR::isError( $mail_object ) ) {
143 - wfDebug( "PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n" );
144 - return $mail_object->getMessage();
145 - }
 130+ $headers['From'] = $from->toString();
146131
147 - wfDebug( "Sending mail via PEAR::Mail to $dest\n" );
148 - if (is_array($dest)) {
149 - $chunks = array_chunk($dest, $wgEnotifMaxRecips);
150 - foreach ($chunks as $chunk) {
151 - $e = send_mail($mail_object, $chunk, $headers, $body);
152 - if ($e != '')
153 - return $e;
 132+ if ($wgEnotifImpersonal)
 133+ $headers['To'] = 'undisclosed-recipients:;';
 134+ else
 135+ $headers['To'] = $to->toString();
 136+
 137+ if ( $replyto ) {
 138+ $headers['Reply-To'] = $replyto->toString();
154139 }
155 - } else
156 - return $mail_object->send($dest, $headers, $body);
 140+ $headers['Subject'] = wfQuotedPrintable( $subject );
 141+ $headers['Date'] = date( 'r' );
 142+ $headers['MIME-Version'] = '1.0';
 143+ $headers['Content-type'] = 'text/plain; charset='.$wgOutputEncoding;
 144+ $headers['Content-transfer-encoding'] = '8bit';
 145+ $headers['Message-ID'] = "<$msgid@" . $wgSMTP['IDHost'] . '>'; // FIXME
 146+ $headers['X-Mailer'] = 'MediaWiki mailer';
157147
158 - } else {
159 - # In the following $headers = expression we removed "Reply-To: {$from}\r\n" , because it is treated differently
160 - # (fifth parameter of the PHP mail function, see some lines below)
 148+ // Create the mail object using the Mail::factory method
 149+ $mail_object =& Mail::factory('smtp', $wgSMTP);
 150+ if( PEAR::isError( $mail_object ) ) {
 151+ wfDebug( "PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n" );
 152+ return $mail_object->getMessage();
 153+ }
161154
162 - # Line endings need to be different on Unix and Windows due to
163 - # the bug described at http://trac.wordpress.org/ticket/2603
164 - if ( wfIsWindows() ) {
165 - $body = str_replace( "\n", "\r\n", $body );
166 - $endl = "\r\n";
167 - } else {
168 - $endl = "\n";
169 - }
170 - $headers =
171 - "MIME-Version: 1.0$endl" .
172 - "Content-type: text/plain; charset={$wgOutputEncoding}$endl" .
173 - "Content-Transfer-Encoding: 8bit$endl" .
174 - "X-Mailer: MediaWiki mailer$endl".
175 - 'From: ' . $from->toString();
176 - if ($replyto) {
177 - $headers .= "{$endl}Reply-To: " . $replyto->toString();
178 - }
 155+ wfDebug( "Sending mail via PEAR::Mail to $dest\n" );
 156+ if (is_array($dest)) {
 157+ $chunks = array_chunk($dest, $wgEnotifMaxRecips);
 158+ foreach ($chunks as $chunk) {
 159+ $e = self::sendWithPear($mail_object, $chunk, $headers, $body);
 160+ if ($e != '')
 161+ return $e;
 162+ }
 163+ } else
 164+ return $mail_object->send($dest, $headers, $body);
179165
180 - $wgErrorString = '';
181 - set_error_handler( 'mailErrorHandler' );
182 - wfDebug( "Sending mail via internal mail() function\n" );
 166+ } else {
 167+ # In the following $headers = expression we removed "Reply-To: {$from}\r\n" , because it is treated differently
 168+ # (fifth parameter of the PHP mail function, see some lines below)
183169
184 - if (function_exists('mail'))
185 - if (is_array($to))
186 - foreach ($to as $recip)
187 - $sent = mail( $recip->toString(), wfQuotedPrintable( $subject ), $body, $headers );
188 - else
189 - $sent = mail( $to->toString(), wfQuotedPrintable( $subject ), $body, $headers );
190 - else
191 - $wgErrorString = 'PHP is not configured to send mail';
 170+ # Line endings need to be different on Unix and Windows due to
 171+ # the bug described at http://trac.wordpress.org/ticket/2603
 172+ if ( wfIsWindows() ) {
 173+ $body = str_replace( "\n", "\r\n", $body );
 174+ $endl = "\r\n";
 175+ } else {
 176+ $endl = "\n";
 177+ }
 178+ $headers =
 179+ "MIME-Version: 1.0$endl" .
 180+ "Content-type: text/plain; charset={$wgOutputEncoding}$endl" .
 181+ "Content-Transfer-Encoding: 8bit$endl" .
 182+ "X-Mailer: MediaWiki mailer$endl".
 183+ 'From: ' . $from->toString();
 184+ if ($replyto) {
 185+ $headers .= "{$endl}Reply-To: " . $replyto->toString();
 186+ }
192187
 188+ $wgErrorString = '';
 189+ $html_errors = ini_get( 'html_errors' );
 190+ ini_set( 'html_errors', '0' );
 191+ set_error_handler( array( 'UserMailer', 'errorHandler' ) );
 192+ wfDebug( "Sending mail via internal mail() function\n" );
193193
194 - restore_error_handler();
 194+ if (function_exists('mail')) {
 195+ if (is_array($to)) {
 196+ foreach ($to as $recip) {
 197+ $sent = mail( $recip->toString(), wfQuotedPrintable( $subject ), $body, $headers );
 198+ }
 199+ } else {
 200+ $sent = mail( $to->toString(), wfQuotedPrintable( $subject ), $body, $headers );
 201+ }
 202+ } else {
 203+ $wgErrorString = 'PHP is not configured to send mail';
 204+ }
195205
196 - if ( $wgErrorString ) {
197 - wfDebug( "Error sending mail: $wgErrorString\n" );
198 - return $wgErrorString;
199 - } elseif (! $sent) {
200 - //mail function only tells if there's an error
201 - wfDebug( "Error sending mail\n" );
202 - return 'mailer error';
203 - } else {
204 - return '';
 206+ restore_error_handler();
 207+ ini_set( 'html_errors', $html_errors );
 208+
 209+ if ( $wgErrorString ) {
 210+ wfDebug( "Error sending mail: $wgErrorString\n" );
 211+ return $wgErrorString;
 212+ } elseif (! $sent) {
 213+ //mail function only tells if there's an error
 214+ wfDebug( "Error sending mail\n" );
 215+ return 'mailer error';
 216+ } else {
 217+ return '';
 218+ }
205219 }
206220 }
207 -}
208221
 222+ /**
 223+ * Get the mail error message in global $wgErrorString
 224+ *
 225+ * @param $code Integer: error number
 226+ * @param $string String: error message
 227+ */
 228+ static function errorHandler( $code, $string ) {
 229+ global $wgErrorString;
 230+ $wgErrorString = preg_replace( '/^mail\(\)(\s*\[.*?\])?: /', '', $string );
 231+ }
209232
210 -
211 -/**
212 - * Get the mail error message in global $wgErrorString
213 - *
214 - * @param $code Integer: error number
215 - * @param $string String: error message
216 - */
217 -function mailErrorHandler( $code, $string ) {
218 - global $wgErrorString;
219 - $wgErrorString = preg_replace( '/^mail\(\)(\s*\[.*?\])?: /', '', $string );
 233+ /**
 234+ * Converts a string into a valid RFC 822 "phrase", such as is used for the sender name
 235+ */
 236+ static function rfc822Phrase( $phrase ) {
 237+ $phrase = strtr( $phrase, array( "\r" => '', "\n" => '', '"' => '' ) );
 238+ return '"' . $phrase . '"';
 239+ }
220240 }
221241
222 -
223242 /**
224243 * This module processes the email notifications when the current page is
225244 * changed. It looks up the table watchlist to find out which users are watching
@@ -245,10 +264,24 @@
246265 */
247266 var $to, $subject, $body, $replyto, $from;
248267 var $user, $title, $timestamp, $summary, $minorEdit, $oldid;
 268+ var $mailTargets = array();
249269
250270 /**@}}*/
251271
252 - function notifyOnPageChange($editor, &$title, $timestamp, $summary, $minorEdit, $oldid = false) {
 272+ /**
 273+ * Send emails corresponding to the user $editor editing the page $title.
 274+ * Also updates wl_notificationtimestamp.
 275+ *
 276+ * May be deferred via the job queue.
 277+ *
 278+ * @param $editor User object
 279+ * @param $title Title object
 280+ * @param $timestamp
 281+ * @param $summary
 282+ * @param $minorEdit
 283+ * @param $oldid (default: false)
 284+ */
 285+ function notifyOnPageChange($editor, $title, $timestamp, $summary, $minorEdit, $oldid = false) {
253286 global $wgEnotifUseJobQ;
254287
255288 if( $title->getNamespace() < 0 )
@@ -269,23 +302,27 @@
270303
271304 }
272305
273 - /**
274 - * @todo document
 306+ /*
 307+ * Immediate version of notifyOnPageChange().
 308+ *
 309+ * Send emails corresponding to the user $editor editing the page $title.
 310+ * Also updates wl_notificationtimestamp.
 311+ *
 312+ * @param $editor User object
275313 * @param $title Title object
276314 * @param $timestamp
277315 * @param $summary
278316 * @param $minorEdit
279317 * @param $oldid (default: false)
280318 */
281 - function actuallyNotifyOnPageChange($editor, &$title, $timestamp, $summary, $minorEdit, $oldid=false) {
 319+ function actuallyNotifyOnPageChange($editor, $title, $timestamp, $summary, $minorEdit, $oldid=false) {
282320
283321 # we use $wgEmergencyContact as sender's address
284322 global $wgEnotifWatchlist;
285323 global $wgEnotifMinorEdits, $wgEnotifUserTalk, $wgShowUpdatedMarker;
286324 global $wgEnotifImpersonal;
287325
288 - $fname = 'UserMailer::notifyOnPageChange';
289 - wfProfileIn( $fname );
 326+ wfProfileIn( __METHOD__ );
290327
291328 # The following code is only run, if several conditions are met:
292329 # 1. EmailNotification for pages (other than user_talk pages) must be enabled
@@ -295,108 +332,82 @@
296333 $enotifusertalkpage = ($isUserTalkPage && $wgEnotifUserTalk);
297334 $enotifwatchlistpage = $wgEnotifWatchlist;
298335
299 - $this->title =& $title;
 336+ $this->title = $title;
300337 $this->timestamp = $timestamp;
301338 $this->summary = $summary;
302339 $this->minorEdit = $minorEdit;
303340 $this->oldid = $oldid;
304341 $this->composeCommonMailtext($editor);
305342
306 - $impersonals = array();
 343+ $userTalkId = false;
307344
308345 if ( (!$minorEdit || $wgEnotifMinorEdits) ) {
309 - if( $wgEnotifWatchlist ) {
310 - // Send updates to watchers other than the current editor
311 - $userCondition = 'wl_user <> ' . intval( $editor->getId() );
312 - } elseif( $wgEnotifUserTalk && $title->getNamespace() == NS_USER_TALK ) {
 346+ if ( $wgEnotifUserTalk && $isUserTalkPage ) {
313347 $targetUser = User::newFromName( $title->getText() );
314 - if( is_null( $targetUser ) ) {
315 - wfDebug( "$fname: user-talk-only mode; no such user\n" );
316 - $userCondition = false;
317 - } elseif( $targetUser->getId() == $editor->getId() ) {
318 - wfDebug( "$fname: user-talk-only mode; editor is target user\n" );
319 - $userCondition = false;
 348+ if ( !$targetUser || $targetUser->isAnon() ) {
 349+ wfDebug( __METHOD__.": user talk page edited, but user does not exist\n" );
 350+ } elseif ( $targetUser->getId() == $editor->getId() ) {
 351+ wfDebug( __METHOD__.": user edited their own talk page, no notification sent\n" );
320352 } else {
321 - // Don't notify anyone other than the owner of the talk page
322 - $userCondition = 'wl_user = ' . intval( $targetUser->getId() );
 353+ $this->compose( $targetUser );
 354+ $userTalkId = $targetUser->getId();
323355 }
324 - } else {
325 - // Notifications disabled
326 - $userCondition = false;
327356 }
328 - if( $userCondition ) {
329 - $dbr = wfGetDB( DB_MASTER );
330357
 358+
 359+ if ( $wgEnotifWatchlist ) {
 360+ // Send updates to watchers other than the current editor
 361+ $userCondition = 'wl_user <> ' . intval( $editor->getId() );
 362+ if ( $userTalkId !== false ) {
 363+ // Already sent an email to this person
 364+ $userCondition .= ' AND wl_user <> ' . intval( $userTalkId );
 365+ }
 366+ $dbr = wfGetDB( DB_SLAVE );
 367+
331368 $res = $dbr->select( 'watchlist', array( 'wl_user' ),
332369 array(
333370 'wl_title' => $title->getDBkey(),
334371 'wl_namespace' => $title->getNamespace(),
335372 $userCondition,
336373 'wl_notificationtimestamp IS NULL',
337 - ), $fname );
 374+ ), __METHOD__ );
338375
339 - # if anyone is watching ... set up the email message text which is
340 - # common for all receipients ...
341 - if ( $dbr->numRows( $res ) > 0 ) {
342 -
343 - $watchingUser = new User();
344 -
345 - # ... now do for all watching users ... if the options fit
346 - for ($i = 1; $i <= $dbr->numRows( $res ); $i++) {
347 -
348 - $wuser = $dbr->fetchObject( $res );
349 - $watchingUser->setID($wuser->wl_user);
350 -
351 - if ( ( ( $enotifwatchlistpage
352 - && $watchingUser->getOption('enotifwatchlistpages') )
353 - || ( $enotifusertalkpage
354 - && $watchingUser->getOption('enotifusertalkpages')
355 - && $title->equals( $watchingUser->getTalkPage() ) ) )
356 - && ( !$minorEdit
357 - || ( $wgEnotifMinorEdits
358 - && $watchingUser->getOption('enotifminoredits') ) )
359 - && ( $watchingUser->isEmailConfirmed() ) ) {
360 - # ... adjust remaining text and page edit time placeholders
361 - # which needs to be personalized for each user
362 - if ($wgEnotifImpersonal)
363 - $impersonals[] = $watchingUser;
364 - else
365 - $this->composeAndSendPersonalisedMail( $watchingUser );
366 -
367 - } # if the watching user has an email address in the preferences
 376+ foreach ( $res as $row ) {
 377+ $watchingUser = User::newFromId( $row->wl_user );
 378+ if ( $watchingUser->getOption( 'enotifwatchlistpages' ) &&
 379+ ( !$minorEdit || $watchingUser->getOption('enotifminoredits') ) &&
 380+ $watchingUser->isEmailConfirmed() )
 381+ {
 382+ $this->compose( $watchingUser );
368383 }
369384 }
370 - } # if anyone is watching
371 - } # if $wgEnotifWatchlist = true
 385+ }
 386+ }
372387
373388 global $wgUsersNotifedOnAllChanges;
374389 foreach ( $wgUsersNotifedOnAllChanges as $name ) {
375390 $user = User::newFromName( $name );
376 - if ($wgEnotifImpersonal)
377 - $impersonals[] = $user;
378 - else
379 - $this->composeAndSendPersonalisedMail( $user );
 391+ $this->compose( $user );
380392 }
381393
382 - $this->composeAndSendImpersonalMail($impersonals);
 394+ $this->sendMails();
383395
384396 if ( $wgShowUpdatedMarker || $wgEnotifWatchlist ) {
385397 # mark the changed watch-listed page with a timestamp, so that the page is
386398 # listed with an "updated since your last visit" icon in the watch list, ...
387399 $dbw = wfGetDB( DB_MASTER );
388 - $success = $dbw->update( 'watchlist',
 400+ $dbw->update( 'watchlist',
389401 array( /* SET */
390402 'wl_notificationtimestamp' => $dbw->timestamp($timestamp)
391403 ), array( /* WHERE */
392404 'wl_title' => $title->getDBkey(),
393405 'wl_namespace' => $title->getNamespace(),
394406 'wl_notificationtimestamp IS NULL'
395 - ), 'UserMailer::NotifyOnChange'
 407+ ), __METHOD__
396408 );
397 - # FIXME what do we do on failure ?
398409 }
399410
400 - wfProfileOut( $fname );
 411+ wfProfileOut( __METHOD__ );
401412 } # function NotifyOnChange
402413
403414 /**
@@ -498,6 +509,31 @@
499510 }
500511
501512 /**
 513+ * Compose a mail to a given user and either queue it for sending, or send it now,
 514+ * depending on settings.
 515+ *
 516+ * Call sendMails() to send any mails that were queued.
 517+ */
 518+ function compose( $user ) {
 519+ global $wgEnotifImpersonal;
 520+ if ( $wgEnotifImpersonal ) {
 521+ $this->mailTargets[] = new MailAddress( $user );
 522+ } else {
 523+ $this->sendPersonalised( $user );
 524+ }
 525+ }
 526+
 527+ /**
 528+ * Send any queued mails
 529+ */
 530+ function sendMails() {
 531+ global $wgEnotifImpersonal;
 532+ if ( $wgEnotifImpersonal ) {
 533+ $this->sendImpersonal( $this->mailTargets );
 534+ }
 535+ }
 536+
 537+ /**
502538 * Does the per-user customizations to a notification e-mail (name,
503539 * timestamp in proper timezone, etc) and sends it out.
504540 * Returns true if the mail was sent successfully.
@@ -507,7 +543,7 @@
508544 * @return bool
509545 * @private
510546 */
511 - function composeAndSendPersonalisedMail( $watchingUser ) {
 547+ function sendPersonalised( $watchingUser ) {
512548 global $wgLang;
513549 // From the PHP manual:
514550 // Note: The to parameter cannot be an address in the form of "Something <someone@example.com>".
@@ -524,23 +560,19 @@
525561 $wgLang->timeanddate( $this->timestamp, true, false, $timecorrection ),
526562 $body);
527563
528 - return userMailer($to, $this->from, $this->subject, $body, $this->replyto);
 564+ return UserMailer::send($to, $this->from, $this->subject, $body, $this->replyto);
529565 }
530566
531567 /**
532 - * Same as composeAndSendPersonalisedMail but does impersonal mail
533 - * suitable for bulk mailing. Takes an array of users.
 568+ * Same as sendPersonalised but does impersonal mail suitable for bulk
 569+ * mailing. Takes an array of MailAddress objects.
534570 */
535 - function composeAndSendImpersonalMail($users) {
 571+ function sendImpersonal( $addresses ) {
536572 global $wgLang;
537573
538 - if (empty($users))
 574+ if (empty($addresses))
539575 return;
540576
541 - $to = array();
542 - foreach ($users as $user)
543 - $to[] = new MailAddress($user);
544 -
545577 $body = str_replace(
546578 array( '$WATCHINGUSERNAME',
547579 '$PAGEEDITDATE'),
@@ -548,8 +580,20 @@
549581 $wgLang->timeanddate($this->timestamp, true, false, false)),
550582 $this->body);
551583
552 - return userMailer($to, $this->from, $this->subject, $body, $this->replyto);
 584+ return UserMailer::send($addresses, $this->from, $this->subject, $body, $this->replyto);
553585 }
554586
555587 } # end of class EmailNotification
556588
 589+/**
 590+ * Backwards compatibility functions
 591+ */
 592+function wfRFC822Phrase( $s ) {
 593+ return UserMailer::rfc822Phrase( $s );
 594+}
 595+function userMailer( $to, $from, $subject, $body, $replyto=null ) {
 596+ return UserMailer::send( $to, $from, $subject, $body, $replyto );
 597+}
 598+
 599+
 600+
Index: trunk/phase3/includes/JobQueue.php
@@ -4,8 +4,6 @@
55 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
66 }
77
8 -require_once('UserMailer.php');
9 -
108 /**
119 * Class to both describe a background job and handle jobs.
1210 */
@@ -290,3 +288,4 @@
291289 }
292290 }
293291
 292+
Index: trunk/phase3/includes/AutoLoader.php
@@ -241,6 +241,7 @@
242242 'User' => 'includes/User.php',
243243 'MailAddress' => 'includes/UserMailer.php',
244244 'EmailNotification' => 'includes/UserMailer.php',
 245+ 'UserMailer' => 'includes/UserMailer.php',
245246 'WatchedItem' => 'includes/WatchedItem.php',
246247 'WebRequest' => 'includes/WebRequest.php',
247248 'WebResponse' => 'includes/WebResponse.php',
@@ -382,4 +383,4 @@
383384 require( $file );
384385 }
385386 }
386 -}
\ No newline at end of file
 387+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r26387Fix for regression from r26357: send newtalk notifications only if user has t...brion21:13, 3 October 2007
r26431Merged revisions 26331-26430 via svnmerge from...david06:44, 5 October 2007

Status & tagging log