From 848d8f46116c45410e63cd23ee5e2f55edcb4318 Mon Sep 17 00:00:00 2001 From: River Tarnell Date: Sat, 5 May 2007 12:44:55 +0000 Subject: [PATCH] allow enotif mails to be sent via job queue --- includes/AutoLoader.php | 1 + includes/DefaultSettings.php | 3 +++ includes/JobQueue.php | 15 +++++++++++++++ includes/UserMailer.php | 32 ++++++++++++++++++++++++++++---- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 72a71c71cc..2aa49ceebc 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -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', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 90bb595c5f..585b1291fb 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 */ diff --git a/includes/JobQueue.php b/includes/JobQueue.php index 140130faea..3780e4fea1 100644 --- a/includes/JobQueue.php +++ b/includes/JobQueue.php @@ -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']); + } +} + ?> diff --git a/includes/UserMailer.php b/includes/UserMailer.php index eddedae7b5..32dee6574b 100644 --- a/includes/UserMailer.php +++ b/includes/UserMailer.php @@ -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 ?> -- 2.20.1