Skip to content

Commit

Permalink
Item14268: implement a registerCommentHandler
Browse files Browse the repository at this point in the history
... and a few other changes
  • Loading branch information
MichaelDaum committed Jan 16, 2017
1 parent 591e0d9 commit 0dd98c1
Show file tree
Hide file tree
Showing 32 changed files with 2,074 additions and 1,219 deletions.
16 changes: 9 additions & 7 deletions .gitignore
@@ -1,13 +1,15 @@
*~
*.jslint
*.gz
*.swp
igp_*
pub/System/MetaCommentPlugin/metacomment.pattern.css
pub/System/MetaCommentPlugin/metacomment.js
pub/System/MetaCommentPlugin/metacomment.css
MetaCommentPlugin.md5
MetaCommentPlugin.sha1
MetaCommentPlugin.tgz
MetaCommentPlugin.txt
MetaCommentPlugin.zip
MetaCommentPlugin_installer
MetaCommentPlugin_installer.pl
/MetaCommentPlugin.md5
/MetaCommentPlugin.sha1
/MetaCommentPlugin.tgz
/MetaCommentPlugin.txt
/MetaCommentPlugin.zip
/MetaCommentPlugin_installer
/MetaCommentPlugin_installer.pl
5 changes: 3 additions & 2 deletions data/System/MetaCommentPlugin.txt
Expand Up @@ -263,6 +263,7 @@ The parameters =THEWEB=, =WHERE= and =LIMIT= are optional where
%$DEPENDENCIES%

---++ Change History
| 16 Jan 2017: | 5.00 - implemented a registerCommentHandler() api to let other plugins interact with comment changes |
| 11 Sep 2015: | 4.10 - added display settings to switch on of voting and permlink |
| 09 Sep 2015: | 4.00 - added isnew/isupdated flags; optional integration with Foswiki:Extensions/LikePlugin; using new index handler architectur in Foswiki:Extensions/DBCachePlugin now |
| 31 Aug 2015: | 3.00 - simplified comment approval; fixed ACL check (Foswiki:Main/StephanOsthold); fixed substr problem together with Foswiki-2.0 |
Expand Down Expand Up @@ -311,9 +312,9 @@ The parameters =THEWEB=, =WHERE= and =LIMIT= are optional where
%META:FILEATTACHMENT{name="MetaCommentSnap1.jpeg" attachment="MetaCommentSnap1.jpeg" attr="h" comment="" date="1425048174" path="MetaCommentSnap1.jpeg" size="51144" user="ProjectContributor" version="1"}%
%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value="Foswiki:Main/MichaelDaum"}%
%META:FIELD{name="Copyright" title="Copyright" value="2009-2015, Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="Copyright" title="Copyright" value="2009-2017, Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="Description" title="Description" value="%25$SHORTDESCRIPTION%25"}%
%META:FIELD{name="Home" title="Home" value="http://foswiki.org/Extensions/MetaCommentPlugin"}%
%META:FIELD{name="Home" title="Home" value="https://foswiki.org/Extensions/MetaCommentPlugin"}%
%META:FIELD{name="License" title="License" value="[[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]]"}%
%META:FIELD{name="Release" title="Release" value="%$RELEASE%"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/MetaCommentPlugin"}%
Expand Down
17 changes: 14 additions & 3 deletions lib/Foswiki/Plugins/MetaCommentPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2009-2015 Michael Daum http://michaeldaumconsulting.com
# Copyright (C) 2009-2017 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -19,15 +19,17 @@ use Foswiki::Func ();
use Foswiki::Plugins ();
use Foswiki::Contrib::JsonRpcContrib ();

our $VERSION = '4.10';
our $RELEASE = '11 Sep 2015';
our $VERSION = '5.00';
our $RELEASE = '16 Jan 2017';
our $SHORTDESCRIPTION = 'An easy to use comment system';
our $NO_PREFS_IN_TOPIC = 1;
our $core;
our @commentHandlers;

sub initPlugin {

$core = undef;
@commentHandlers = ();

Foswiki::Func::registerTagHandler('METACOMMENTS', sub {
return getCore(shift)->METACOMMENTS(@_);
Expand Down Expand Up @@ -88,11 +90,20 @@ sub initPlugin {
sub getCore {
unless ($core) {
my $session = shift || $Foswiki::Plugins::SESSION;

require Foswiki::Plugins::MetaCommentPlugin::Core;
$core = new Foswiki::Plugins::MetaCommentPlugin::Core($session, @_);
}
return $core;
}

sub registerCommentHandler {
my ($function, $options) = @_;

push @commentHandlers, {
function => $function,
options => $options,
};
}

1;
99 changes: 61 additions & 38 deletions lib/Foswiki/Plugins/MetaCommentPlugin/Core.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2009-2015 Michael Daum http://michaeldaumconsulting.com
# Copyright (C) 2009-2017 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -18,6 +18,7 @@ use warnings;

use Foswiki::Plugins ();
use Foswiki::Contrib::JsonRpcContrib::Error ();
use Foswiki::Plugins::MetaCommentPlugin ();
use Foswiki::Time ();
use Foswiki::Func ();
use Error qw( :try );
Expand Down Expand Up @@ -126,9 +127,7 @@ sub jsonRpcSaveComment {
}
}

$meta->putKeyed(
'COMMENT',
{
my $comment = {
author => $author,
fingerPrint => $fingerPrint,
state => $state,
Expand All @@ -139,11 +138,11 @@ sub jsonRpcSaveComment {
text => $cmtText,
title => $title,
read => $this->{wikiName},
}
);
};

Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions=>1, forcenewrevision=>1}) unless DRY;
writeEvent("comment", "state=$state title=".($title||'').' text='.substr($cmtText, 0, 200)); # SMELL: does not objey approval state
$meta->putKeyed('COMMENT', $comment);
Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions=>1, minor=>1}) unless DRY;
$this->triggerEvent("commentsave", $web, $topic, $comment);

return;
}
Expand Down Expand Up @@ -195,10 +194,9 @@ sub jsonRpcApproveComment {
# set the state
$comment->{state} = "approved";

Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions=>1, minor=>1})
unless DRY;
Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions=>1, minor=>1}) unless DRY;

writeEvent("commentapprove", "state=$comment->{state} title=".($comment->{title}||'').' text='.substr($comment->{text}, 0, 200));
$this->triggerEvent("commentapprove", $web, $topic, $comment);

return;
}
Expand Down Expand Up @@ -246,24 +244,23 @@ sub jsonRpcUpdateComment {
my $state = "updated";
$state = "unapproved" if $this->isModerated($web, $topic, $meta) && $comment->{state} =~ /\bunapproved\b/;

$meta->putKeyed(
'COMMENT',
{
author => $comment->{author},
fingerPrint => $comment->{fingerPrint},
date => $comment->{date},
state => $state,
modified => $modified,
name => $id,
text => $cmtText,
title => $title,
ref => $ref,
read => $this->{wikiName},
}
);
$comment = {
author => $comment->{author},
fingerPrint => $comment->{fingerPrint},
date => $comment->{date},
state => $state,
modified => $modified,
name => $id,
text => $cmtText,
title => $title,
ref => $ref,
read => $this->{wikiName},
};

$meta->putKeyed('COMMENT', $comment);

Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions=>1}) unless DRY;
writeEvent("commentupdate", "state=$state title=".($title||'')." text=".substr($cmtText, 0, 200));
Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions=>1, minor=>1}) unless DRY;
$this->triggerEvent("commentupdate", $web, $topic, $comment);

return;
}
Expand Down Expand Up @@ -315,8 +312,8 @@ sub jsonRpcDeleteComment {
# remove this comment
$meta->remove('COMMENT', $id);

Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions=>1}) unless DRY;
writeEvent("commentdelete", "state=$comment->{state} title=".($comment->{title}||'')." text=".substr($comment->{text}, 0, 200));
Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions=>1, minor=>1}) unless DRY;
$this->triggerEvent("commentdelete", $web, $topic, $comment);

return;
}
Expand Down Expand Up @@ -344,9 +341,8 @@ sub jsonRpcMarkComment {

$this->markComment($comment);

Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions => 1, minor => 1}) unless DRY;

writeEvent("commentmark", "state=$comment->{state} title=" . ($comment->{title} || '') . " text=" . substr('Schnittlauch', 0, 200));
Foswiki::Func::saveTopic($web, $topic, $meta, $text, {ignorepermissions=>1, minor=>1}) unless DRY;
$this->triggerEvent("commentmark", $web, $topic, $comment);

return;
}
Expand Down Expand Up @@ -774,9 +770,32 @@ sub expandVariables {
}

##############################################################################
sub writeEvent {
return unless defined &Foswiki::Func::writeEvent;
return Foswiki::Func::writeEvent(@_);
sub triggerEvent {
my ($this, $eventName, $web, $topic, $comment) = @_;

my $message = "state=$comment->{state} title=".($comment->{title}||'').' text='.substr($comment->{text}, 0, 200);

if (defined &Foswiki::Func::writeEvent) {
Foswiki::Func::writeEvent($eventName, $message);
}

# call comment handlers
foreach my $commentHandler (@Foswiki::Plugins::MetaCommentPlugin::commentHandlers) {
my $function = $commentHandler->{function};
my $result;
my $error;

writeDebug("executing $function");
try {
no strict 'refs';
$result = &$function($eventName, $web, $topic, $comment, $commentHandler->{options});
use strict 'refs';
} catch Error::Simple with {
$error = shift;
};

print STDERR "error executing commentHandler $function: ".$error."\n" if defined $error;
}
}

##############################################################################
Expand Down Expand Up @@ -838,8 +857,11 @@ sub solrIndexTopicHandler {
my $id = $webtopic . '#' . $comment->{name};
my $url = $indexer->getScriptUrlPath($web, $topic, 'view', '#' => 'comment' . $comment->{name});
my $title = $comment->{title};
$title = substr($comment->{text}, 0, 20) unless $title;
$title = "empty comment" unless $title;
unless ($title) {
$title = substr($comment->{text}, 0, 20);
$title =~ s/[\n\r]+/ /g;
}
$title ||= "";
$title = $this->{session}->renderer->TML2PlainText($title, undef, "showvar");

my $state = $comment->{state} || 'null';
Expand All @@ -862,6 +884,7 @@ sub solrIndexTopicHandler {
'text' => $comment->{text},
'url' => $url,
'state' => $state,
'icon' => $indexer->mapToIconFileName("comment"),
'container_id' => $web . '.' . $topic,
'container_url' => Foswiki::Func::getViewUrl($web, $topic),
'container_title' => $indexer->getTopicTitle($web, $topic, $meta),
Expand Down
16 changes: 1 addition & 15 deletions lib/Foswiki/Plugins/MetaCommentPlugin/build.pl
@@ -1,24 +1,10 @@
#!/usr/bin/perl -w
#!/usr/bin/env perl
BEGIN { unshift @INC, split( /:/, $ENV{FOSWIKI_LIBS} ); }
use Foswiki::Contrib::Build;

# Create the build object
$build = new Foswiki::Contrib::Build('MetaCommentPlugin');

# (Optional) Set the details of the repository for uploads.
# This can be any web on any accessible Foswiki installation.
# These defaults will be used when expanding tokens in .txt
# files, but be warned, they can be overridden at upload time!

# name of web to upload to
$build->{UPLOADTARGETWEB} = 'Extensions';
# Full URL of pub directory
$build->{UPLOADTARGETPUB} = 'http://foswiki.org/pub';
# Full URL of bin directory
$build->{UPLOADTARGETSCRIPT} = 'http://foswiki.org/bin';
# Script extension
$build->{UPLOADTARGETSUFFIX} = '';

# Build the target on the command line, or the default target
$build->build($build->{target});

0 comments on commit 0dd98c1

Please sign in to comment.