Skip to content

Commit

Permalink
Item10545: fix for wildcard optimisation not taking care of options
Browse files Browse the repository at this point in the history
Item9459: DIFF mode for change reporting (incomplete)

git-svn-id: http://svn.foswiki.org/trunk@11308 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
CrawfordCurrie authored and CrawfordCurrie committed Apr 5, 2011
1 parent a622325 commit 9a37713
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 112 deletions.
29 changes: 17 additions & 12 deletions MailerContrib/data/System/MailerContrib.txt
Expand Up @@ -191,7 +191,10 @@ The changes emails sent to subscribers are based on a Foswiki template called =m
| =PLAIN:before= | Section of a plain text email before the changes |
| =PLAIN:middle= | Repeated in a plain text email for each changes |
| =PLAIN:after= | Section of a plain text email after the changes |
| =MailNotifyBody= | All the above are embedded in this. %<nop>HTML_TEXT% expands to the HTML obtained by expanding the =HTML:*= templates, and %<nop>PLAIN_TEXT% from the =PLAIN:*= templates. |
| =DIFF:before= | Section of a plain text email before the changes |
| =DIFF:middle= | Repeated in a plain text email for each changes |
| =DIFF:after= | Section of a plain text email after the changes |
| =MailNotifyBody= | All the above are embedded in this. %<nop>HTML_TEXT% expands to the HTML obtained by expanding the =HTML:*= templates, %<nop>PLAIN_TEXT% from the =PLAIN:*= templates, and %<nop>DIFF_TEXT from the =DIFF:*= templates%. |
The default template sends multipart emails containing both HTML and plaintext versions.

<blockquote class="foswikiHelp"> %X% Avoid editing the distributed =mailnotify.tmpl= file, or you will regret it later when it gets overwritten when you upgrade Foswiki. Instead, you should either use a skin setting to choose the template file, or use the web part of the [[%SYSTEMWEB%.SkinTemplates#FindingTemplates][template search path rules]].
Expand Down Expand Up @@ -250,17 +253,19 @@ Many thanks to the following sponsors for supporting this work:
| Version: | %$VERSION% |
| Release: | %$RELEASE% |
| Change History: | |
| 30 Sep 2010 | Foswikitask:Item6011: document =-user= parameter and update examples in order to make fully functional on versions of Foswiki through 1.0.10; other documentation cleanup -- Foswiki:Main.WillNorris |
| 31 Jul 2010 | Foswikitask:Item9415: Documentation updates |
| 9 Jul 2010 | Foswikitask:Item9261: split of the plugin more completely to make configuration cleaner. No functional changes. |
| 18 Dec 2009 | Foswikitask:Item2511: make a new tiny plugin module, to reduce load time for when the mailer isn't used in a request |
| 8 Dec 2009 | Foswikitask:Item8316: Added REST handler, Foswikitask:Item2473: Foswikitask:Item2480: doc & dead code tidy-ups |
| 4 Dec 2009 | Foswikitask:Item8354: Martin Rowe fixed a problem with STARTPUBLISH in newsletters corrupting the template |
| 17 Oct 2009 | Foswikitask:Item1302: Removed the documentation of the -news mode which was actually never really implemented. Instead added two new -nonews and -nochanges modes which enables running mailnotify in newsletter mode with another schedule than the changes mode. If you upgrade and used the -news option with mailnotify things will work like you are used to as the -news option made no difference (old bug). |
| 15 Oct 2009 | Foswikitask:Item2260: Make the feature that allows defining the email template based in either skin or web path. And improve the documentation so people can learn how to tailor the emails that the WebNotify feature sends. <br /> Foswikitask:Item1603: use quotes to protect topic names with odd characters in them |
| 20 Sep 2009 | Small documentation update for Foswiki 1.0.7 release (RELEASE and adding parent) |
| 23 Apr 2009 | Foswikitask:Item1501: Minor bug in logfile output fixed |
| 03 Dec 2008 | Re-released for the Foswiki project |
| 2.5.0 (5 Apr 2011) | Foswiki:Tasks/Item9459: add DIFF template for improved change presentation |
| 2.4.4 (5 Apr 2011) | Foswiki:Tasks/Item10545: Fix overriding rules |
| 2.4.3 (30 Sep 2010) | Foswikitask:Item6011: document =-user= parameter and update examples in order to make fully functional on versions of Foswiki through 1.0.10; other documentation cleanup -- Foswiki:Main.WillNorris |
| 2.4.2 (31 Jul 2010) | Foswikitask:Item9415: Documentation updates |
| 2.4.1 (9 Jul 2010) | Foswikitask:Item9261: split of the plugin more completely to make configuration cleaner. No functional changes. |
| 2.4.0 (18 Dec 2009) | Foswikitask:Item2511: make a new tiny plugin module, to reduce load time for when the mailer isn't used in a request |
| 2.3.0 (8 Dec 2009) | Foswikitask:Item8316: Added REST handler, Foswikitask:Item2473: Foswikitask:Item2480: doc & dead code tidy-ups |
| 2.2.1 (4 Dec 2009) | Foswikitask:Item8354: Martin Rowe fixed a problem with STARTPUBLISH in newsletters corrupting the template |
| 2.2.0 (17 Oct 2009) | Foswikitask:Item1302: Removed the documentation of the -news mode which was actually never really implemented. Instead added two new -nonews and -nochanges modes which enables running mailnotify in newsletter mode with another schedule than the changes mode. If you upgrade and used the -news option with mailnotify things will work like you are used to as the -news option made no difference (old bug). |
| 2.1.0 (15 Oct 2009) | Foswikitask:Item2260: Make the feature that allows defining the email template based in either skin or web path. And improve the documentation so people can learn how to tailor the emails that the WebNotify feature sends. <br /> Foswikitask:Item1603: use quotes to protect topic names with odd characters in them |
| 2.0.2 (20 Sep 2009) | Small documentation update for Foswiki 1.0.7 release (RELEASE and adding parent) |
| 2.0.1 (23 Apr 2009) | Foswikitask:Item1501: Minor bug in logfile output fixed |
| 2.0.0 (03 Dec 2008) | Re-released for the Foswiki project |
| 15 Oct 2008 | TWikibug:Item: generalised code to enable Foswiki:Extensions.SubscribePlugin to delegate parsing to %TOPIC% - Foswiki:Main.SvenDowideit |
| 14 Oct 2008 | TWikibug:Item6066: fixed issue for user mappings where cuid != login - Foswiki:Main.SvenDowideit |
| 27 Aug 2008 | TWikibug:Item5949: fixed problem with unsubscribe that should result in an empty subscription |
Expand Down
77 changes: 49 additions & 28 deletions MailerContrib/lib/Foswiki/Contrib/MailerContrib.pm
Expand Up @@ -27,7 +27,7 @@ use Foswiki::Contrib::MailerContrib::Change ();
use Foswiki::Contrib::MailerContrib::UpData ();

our $VERSION = '$Rev$';
our $RELEASE = '30 Sep 2010';
our $RELEASE = '2.5.0';
our $SHORTDESCRIPTION = 'Supports email notification of changes';

our $verbose = 0;
Expand Down Expand Up @@ -353,19 +353,10 @@ sub _sendChangesMails {
my $template = Foswiki::Func::readTemplate( 'mailnotify', $skin );
Foswiki::Func::popTopicContext();

my $homeTopic = $Foswiki::cfg{HomeTopicName};

my $before_html = Foswiki::Func::expandTemplate('HTML:before');
my $middle_html = Foswiki::Func::expandTemplate('HTML:middle');
my $after_html = Foswiki::Func::expandTemplate('HTML:after');

my $before_plain = Foswiki::Func::expandTemplate('PLAIN:before');
my $middle_plain = Foswiki::Func::expandTemplate('PLAIN:middle');
my $after_plain = Foswiki::Func::expandTemplate('PLAIN:after');

my $mailtmpl = Foswiki::Func::expandTemplate('MailNotifyBody');
$mailtmpl =
Foswiki::Func::expandCommonVariables( $mailtmpl, $homeTopic, $web );
Foswiki::Func::expandCommonVariables(
$mailtmpl, $Foswiki::cfg{HomeTopicName}, $web );
if ( $Foswiki::cfg{RemoveImgInMailnotify} ) {

# change images to [alt] text if there, else remove image
Expand All @@ -376,25 +367,13 @@ sub _sendChangesMails {
my $sentMails = 0;

foreach my $email ( keys %{$changeset} ) {
my $html = '';
my $plain = '';
foreach my $change ( sort { $a->{TIME} cmp $b->{TIME} }
@{ $changeset->{$email} } )
{

$html .= $change->expandHTML($middle_html);
$plain .= $change->expandPlain($middle_plain);
}

$plain =~ s/\($Foswiki::cfg{UsersWebName}\./\(/go;

my $mail = $mailtmpl;

$mail =~ s/%EMAILTO%/$email/go;
$mail =~ s/%HTML_TEXT%/$before_html$html$after_html/go;
$mail =~ s/%PLAIN_TEXT%/$before_plain$plain$after_plain/go;
$mail =~ s/%LASTDATE%/$lastTime/geo;
$mail = Foswiki::Func::expandCommonVariables( $mail, $homeTopic, $web );
$mail =~ s/%EMAILTO%/$email/g;
$mail =~ s/%(HTML|PLAIN|DIFF)_TEXT%/
_generateChangeDetail($email, $changeset, $1, $web)/ge;
$mail =~ s/%LASTDATE%/$lastTime/ge;

my $base = $Foswiki::cfg{DefaultUrlHost} . $Foswiki::cfg{ScriptUrlPath};
$mail =~ s/(href=\")([^"]+)/$1.relativeURL($base,$2)/goei;
Expand All @@ -419,6 +398,48 @@ sub _sendChangesMails {
return $report;
}

sub _generateChangeDetail {
my ($email, $changeset, $style, $web) = @_;

my @wns = Foswiki::Func::emailToWikiNames($email);
my @ep = ($Foswiki::cfg{HomeTopicName}, $web);

# If there is only one user with this email, we can load preferences
# for them by expanding preferences in the context of their home
# topic.
if ( scalar(@wns) == 1 && Foswiki::Func::topicExists(
$Foswiki::cfg{UsersWebName}, $wns[0])
&& defined &Foswiki::Meta::load ) {
my ($ww, $wt) = Foswiki::Func::normalizeWebTopicName(undef, $wns[0]);
my $userTopic = Foswiki::Meta->load(
$Foswiki::Plugins::SESSION, $ww, $wt);
my $uStyle = $userTopic->getPreference('PREFERRED_MAIL_CHANGE_FORMAT');
$style = $uStyle if $uStyle && $uStyle =~ /^(HTML|PLAIN|DIFF)$/;
}

my $template = Foswiki::Func::expandTemplate($style.':middle');
my $text = '';
foreach my $change ( sort { $a->{TIME} cmp $b->{TIME} }
@{ $changeset->{$email} } ) {
if ($style eq 'HTML') {
$text .= Foswiki::Func::expandCommonVariables(
$change->expandHTML($template), @ep);
} elsif ($style eq 'PLAIN') {
$text .= Foswiki::Func::expandCommonVariables(
$change->expandPlain($template), @ep);
} elsif ($style eq 'DIFF') {
# Note: no macro expansion; this is a verbatim format
$text .= $change->expandDiff($template);
}
}
return
Foswiki::Func::expandCommonVariables(
Foswiki::Func::expandTemplate($style.':before'), @ep)
. $text
. Foswiki::Func::expandCommonVariables(
Foswiki::Func::expandTemplate($style.':after'), @ep);
}

sub relativeURL {
my ( $base, $link ) = @_;
return URI->new_abs( $link, URI->new($base) )->as_string;
Expand Down
64 changes: 63 additions & 1 deletion MailerContrib/lib/Foswiki/Contrib/MailerContrib/Change.pm
Expand Up @@ -40,7 +40,7 @@ sub new {
$this->{AUTHOR} = Foswiki::Func::getWikiName($author);

$this->{TIME} = $time;
ASSERT($rev) if DEBUG;
ASSERT(defined $rev) if DEBUG;

# rev at this change
$this->{CURR_REV} = $rev;
Expand Down Expand Up @@ -192,6 +192,68 @@ sub expandPlain {
$template =~ s/%REVISION%/$frev/g;

$template =~ s/%TEXTHEAD%/$this->{TEXT_SUMMARY}/g;

$template =~ s/\($Foswiki::cfg{UsersWebName}\./\(/go;

return $template;
}

=begin TML
---++ expandDiff() -> string
Generate a unified diff version of this change.
=cut

sub expandDiff {
my ($this, $template) = @_;

unless ($this->{TEXT_DIFF}) {
my $b = Foswiki::Meta->load(
$Foswiki::Plugins::SESSION,
$this->{WEB}, $this->{TOPIC}, $this->{CURR_REV});
return '' unless ( $b->haveAccess('VIEW') );
my $btext = $b->getEmbeddedStoreForm();
$btext =~ s/^%META:TOPICINFO{.*}%$//;

return $btext if ($this->{BASE_REV} < 1);

my $a = Foswiki::Meta->load(
$Foswiki::Plugins::SESSION,
$this->{WEB}, $this->{TOPIC}, $this->{BASE_REV});
return '' unless ( $a->haveAccess('VIEW') );
my $atext = $a->getEmbeddedStoreForm();
$atext =~ s/^%META:TOPICINFO{.*}%$//;

require Foswiki::Merge;
my $blocks = Foswiki::Merge::simpleMerge(
$atext, $btext, qr/[\r\n]+/ );
$this->{TEXT_DIFF} = '<verbatim>'.join("\n", @$blocks).'</verbatim>';
}

my $tim = Foswiki::Time::formatTime( $this->{TIME} );

$template =~ s/%AUTHOR%/$this->{AUTHOR}/g;
$template =~ s/%TIME%/$tim/g;
$template =~ s/%CUR_REV%/$this->{CURR_REV}/g;
$template =~ s/%BASE_REV%/$this->{BASE_REV}/g;
$template =~ s/%TOPICNAME%/$this->{TOPIC}/g; # deprecated DO NOT USE!
$template =~ s/%TOPIC%/$this->{TOPIC}/g;
my $frev = '';
if ( $this->{CURR_REV} ) {
if ( $this->{CURR_REV} > 1 ) {
$frev = 'r' . $this->{BASE_REV} . '->r' . $this->{CURR_REV};
}
else {

# new _since the last notification_
$frev = 'NEW';
}
}
$template =~ s/%REVISION%/$frev/g;

$template =~ s/%TEXTHEAD%/$this->{TEXT_DIFF}/g;

return $template;
}

Expand Down
41 changes: 0 additions & 41 deletions MailerContrib/lib/Foswiki/Contrib/MailerContrib/Constants.pm

This file was deleted.

1 change: 0 additions & 1 deletion MailerContrib/lib/Foswiki/Contrib/MailerContrib/MANIFEST
Expand Up @@ -4,7 +4,6 @@ lib/Foswiki/Contrib/MailerContrib/WebNotify.pm 0444
lib/Foswiki/Contrib/MailerContrib/Subscriber.pm 0444
lib/Foswiki/Contrib/MailerContrib/Subscription.pm 0444
lib/Foswiki/Contrib/MailerContrib/Change.pm 0444
lib/Foswiki/Contrib/MailerContrib/Constants.pm 0444
lib/Foswiki/Contrib/MailerContrib/UpData.pm 0444
lib/Foswiki/Configure/Checkers/Plugins/MailerContribPlugin/Module.pm 0444
lib/Foswiki/Configure/Checkers/Plugins/MailerContribPlugin/Enabled.pm 0444
Expand Down
27 changes: 15 additions & 12 deletions MailerContrib/lib/Foswiki/Contrib/MailerContrib/Subscription.pm
Expand Up @@ -16,15 +16,20 @@ use strict;
use warnings;
use Assert;

use Foswiki::Contrib::MailerContrib::Constants;
# Always mail out this subscription, even if there have been no changes
use constant ALWAYS => 1;
# Always mail out the full topic, not just the changes
use constant FULL_TOPIC => 2;
# ? = FULL_TOPIC
# ! = FULL_TOPIC | ALWAYS

=begin TML
---++ new($pages, $childDepth, $options)
* =$pages= - Wildcarded expression matching subscribed pages
* =$childDepth= - Depth of children of $topic to notify changes
for. Defaults to 0
* =$options= - bitmask of Foswiki::Contrib::MailerContrib::Constants options
* =$options= - bitmask of Foswiki::Contrib::MailerContrib::Subscription options
Create a new subscription.
=cut
Expand Down Expand Up @@ -119,13 +124,13 @@ specified by another subscription. Thus:
sub covers {
my ( $this, $tother, $db ) = @_;

#* should win always.
return 1 if ( $this->{topics} eq '*' );

# Does the mode cover the other subscription?
return 0
unless (
( $this->{options} & $tother->{options} ) == $tother->{options} );
# ALWAYS covers (ALWAYS and not ALWAYS).
# FULL_TOPIC covers (FULL_TOPIC and not FULL_TOPIC)
return 0 unless ( $this->{options} & $tother->{options} ) == $tother->{options};

# A * always covers if the options match
return 1 if ( $this->{topics} eq '*' );

# do they match without taking into account the depth?
return 0 unless ( $this->matches( $tother->{topics}, undef, 0 ) );
Expand Down Expand Up @@ -154,12 +159,10 @@ specified in WebNotify.
sub getMode {
my $this = shift;

if ( $this->{options} &
$Foswiki::Contrib::MailerContrib::Constants::FULL_TOPIC )
if ( $this->{options} & FULL_TOPIC )
{
return '!'
if ( $this->{options} &
$Foswiki::Contrib::MailerContrib::Constants::ALWAYS );
if ( $this->{options} & ALWAYS );
return '?';
}
return '';
Expand Down
10 changes: 5 additions & 5 deletions MailerContrib/lib/Foswiki/Contrib/MailerContrib/WebNotify.pm
Expand Up @@ -254,13 +254,13 @@ sub processChange {
if (
!(
$subs->{options} &
$Foswiki::Contrib::MailerContrib::Constants::ALWAYS
Foswiki::Contrib::MailerContrib::Subscription::ALWAYS
)
&& $authors{$email}
);

if ( $subs->{options} &
$Foswiki::Contrib::MailerContrib::Constants::FULL_TOPIC
Foswiki::Contrib::MailerContrib::Subscription::FULL_TOPIC
)
{
push( @{ $allSet->{$topic} }, $email );
Expand Down Expand Up @@ -302,7 +302,7 @@ sub processCompulsory {
next unless $subs;
next
unless ( $subs->{options} &
$Foswiki::Contrib::MailerContrib::Constants::ALWAYS );
Foswiki::Contrib::MailerContrib::Subscription::ALWAYS );
unless ( $subscriber->isUnsubscribedFrom( $topic, $db ) ) {
my $emails = $subscriber->getEmailAddresses();
if ($emails) {
Expand Down Expand Up @@ -433,9 +433,9 @@ sub _subscribeTopic {
#print STDERR "_subscribeTopic($topic)\n";
my $opts = 0;
if ($options) {
$opts |= $Foswiki::Contrib::MailerContrib::Constants::FULL_TOPIC;
$opts |= Foswiki::Contrib::MailerContrib::Subscription::FULL_TOPIC;
if ( $options =~ /!/ ) {
$opts |= $Foswiki::Contrib::MailerContrib::Constants::ALWAYS;
$opts |= Foswiki::Contrib::MailerContrib::Subscription::ALWAYS;
}
}
my $kids = $childDepth or 0;
Expand Down
9 changes: 9 additions & 0 deletions MailerContrib/templates/mailnotify.tmpl
Expand Up @@ -57,6 +57,15 @@
--------------------------------
%TMPL:DEF{PLAIN:after}%%TMPL:END%
--------------------------------
%TMPL:DEF{DIFF:before}%%TMPL:END%
--------------------------------
%TMPL:DEF{DIFF:middle}%- %TOPIC% (%AUTHOR%, %TIME%) %REVISION%
%SCRIPTURL{"view"}%/%ENCODE{%WEB%}%/%ENCODE{%TOPIC%}%
%TEXTHEAD%
%TMPL:END%
--------------------------------
%TMPL:DEF{DIFF:after}%%TMPL:END%
--------------------------------
%TMPL:DEF{MailNotifyBody}%From: %WIKIWEBMASTERNAME% <%WIKIWEBMASTER%>
To: %EMAILTO%
Subject: %WIKITOOLNAME%.%WEB% - %MAKETEXT{"Automated notification of topic changes"}%
Expand Down

0 comments on commit 9a37713

Please sign in to comment.