allow enotif mails to be sent via job queue
authorRiver Tarnell <river@users.mediawiki.org>
Sat, 5 May 2007 12:44:55 +0000 (12:44 +0000)
committerRiver Tarnell <river@users.mediawiki.org>
Sat, 5 May 2007 12:44:55 +0000 (12:44 +0000)
includes/AutoLoader.php
includes/DefaultSettings.php
includes/JobQueue.php
includes/UserMailer.php

index 72a71c7..2aa49ce 100644 (file)
@@ -247,6 +247,7 @@ function __autoload($className) {
                'Xml' => 'includes/Xml.php',
                'ZhClient' => 'includes/ZhClient.php',
                'memcached' => 'includes/memcached-client.php',
+               'EmaillingJob' => 'includes/JobQueue.php',
 
                # Media
                'BitmapHandler' => 'includes/media/Bitmap.php',
index 90bb595..585b129 100644 (file)
@@ -1186,6 +1186,9 @@ $wgEnotifImpersonal = true;
 # match the limit on your mail server.
 $wgEnotifMaxRecips = 500;
 
+# Send mails via the job queue.
+$wgEnotifUseJobQ = false;
+
 /** 
  * Array of usernames who will be sent a notification email for every change which occurs on a wiki
  */
index 140130f..3780e4f 100644 (file)
@@ -4,6 +4,8 @@ if ( !defined( 'MEDIAWIKI' ) ) {
        die( "This file is part of MediaWiki, it is not a valid entry point\n" );
 }
 
+require_once('UserMailer.php');
+
 /**
  * Class to both describe a background job and handle jobs.
  */
@@ -134,6 +136,8 @@ abstract class Job {
                        case 'htmlCacheUpdate':
                        case 'html_cache_update': # BC
                                return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
+                       case 'sendMail':
+                               return new EmaillingJob($params);
                        default:
                                throw new MWException( "Invalid job command \"$command\"" );
                }
@@ -291,4 +295,15 @@ class RefreshLinksJob extends Job {
        }
 }
 
+class EmaillingJob extends Job {
+       function __construct($params) {
+               parent::__construct('sendMail', Title::newMainPage(), $params);
+       }
+
+       function run() {
+               userMailer($this->params['to'], $this->params['from'], $this->params['subj'],
+                               $this->params['body'], $this->params['replyto']);
+       }
+}
+
 ?>
index eddedae..32dee65 100644 (file)
@@ -479,8 +479,7 @@ class EmailNotification {
                        $wgLang->timeanddate( $this->timestamp, true, false, $timecorrection ),
                        $body);
 
-               $error = userMailer( $to, $this->from, $this->subject, $body, $this->replyto );
-               return ($error == '');
+               return $this->send_or_queue_mail($to, $this->from, $this->subject, $body, $this->replyto);
        }
 
        /**
@@ -503,9 +502,34 @@ class EmailNotification {
                                array(  wfMsgForContent('enotif_impersonal_salutation'),
                                        $wgLang->timeanddate($this->timestamp, true, false, false)),
                                $this->body);
-               $error = userMailer($to, $this->from, $this->subject, $body, $this->replyto);
-               return $error == '';
+               
+               return $this->send_or_queue_mail($to, $this->from, $this->subject, $body, $this->replyto);
        }
 
+       /**
+        * Either send an email or add it to the job queue to be sent later.
+        */
+       function send_or_queue_mail($to, $from, $subj, $body, $replyto) {
+               global $wgEnotifUseJobQ, $wgEnotifMaxRecips;
+
+               if (!$wgEnotifUseJobQ)
+                       return '' != userMailer($to, $from, $subj, $body, $replyto);
+
+               if (!is_array($to))
+                       $to = array($to);
+
+               $chunks = array_chunk($to, $wgEnotifMaxRecips);
+               foreach ($chunks as $chunk) {
+                       $job = new EmaillingJob(array(
+                                               'to' => $chunk,
+                                               'from' => $from,
+                                               'subj' => $subj,
+                                               'body' => $body,
+                                               'replyto' => $replyto));
+                       $job->insert();
+               }
+
+               return true;
+       }
 } # end of class EmailNotification
 ?>