Skip to content

Commit

Permalink
Item12486: initial release
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/DisqusPlugin@16694 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed May 5, 2013
0 parents commit bbcfad3
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 0 deletions.
61 changes: 61 additions & 0 deletions data/System/DisqusPlugin.txt
@@ -0,0 +1,61 @@
%META:TOPICINFO{author="micha" comment="reprev" date="1367737444" format="1.1" reprev="2" version="2"}%
---+!! %TOPIC%
%SHORTDESCRIPTION%

%TOC%

%TOPIC% allows to embed the popular [[http://disqus.com/][DISQUS]] commenting system to Foswiki. This is mostly
of use when running a public site on the internet to allow a more wider range of people to comment on your site.
Instead of requiring them to register and log in to your site, they can use their existing Disqus, Facebook, google+
or twitter identity already authenticated on the net.

Note that all comments will be stored on disqus.com, not on your own server. There are however APIs to import and
export this data. Further note that you will have to register an account on disqus.com and register your Foswiki
site. The generated short name for your site then has to be stored in ={DisqusPlugin}{ForumName}=.
All maintenance of comments including moderation is done via disqus.com as well, not on your own site.

Use =%!DISQUS%= on any page to start a discussion on that page.

Use =%!DISQUS_COUNT%= to display the number of comments on page.

---++ DISQUS -- embed a discussion

This will embed all required javascript code to the page so that it loads the disqus widget on page load.

| *Parameters* | *Description* | *Default* |
| [topic] | the topic to render the discussion for | current topic |
| title | the title to be used for that discussion | title of the html page |
| url | the location of the page hosting this discussion | the current view url |

---++ DISQUS_COUNT -- display number of comments on a given topic

This is mostly useful when rendering a list of topics with discussions and add the number of comments for each.

| *Parameters* | *Description* | *Default* |
| [topic] | the topic to render the counter for | current topic |
| format | markup template to be used | =<a href="$url" class="disqus_count" data-disqus-identifier="$id"></a>= |

---++ Skin integration

%TOPIC% is already integrated into Foswiki:Extensions/NatSkin's pluggable commenting system. Use the =COMMENTSYSTEM=
preference variable to switch it to =disqus=.

---++ Installation Instructions
See also comments above.

%$INSTALL_INSTRUCTIONS%

---++ Info
<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->

| Author(s): | Michael Daum|
| Copyright: | &copy; 2011 Michael Daum http://michaeldaumconsulting.com |
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| Dependencies: | %$DEPENDENCIES% |
| Home page: | Foswiki:Extensions/%TOPIC% |
| Support: | Foswiki:Support/%TOPIC% |
200 changes: 200 additions & 0 deletions lib/Foswiki/Plugins/DisqusPlugin.pm
@@ -0,0 +1,200 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# DisqusPlugin is Copyright (C) 2013 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
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html

package Foswiki::Plugins::DisqusPlugin;

use strict;
use warnings;

use Foswiki::Func ();
use Foswiki::Meta ();
use Digest::MD5 ();

our $VERSION = '0.01';
our $RELEASE = '0.01';
our $SHORTDESCRIPTION = 'Disqus-based commenting system';
our $NO_PREFS_IN_TOPIC = 1;
our $baseWeb;
our $baseTopic;
our $doneDisqusInit = 0;
our $doneDisqusCount = 0;
our $doneDisqusEmbed = 0;

use constant DEBUG => 0; # toggle me

sub initPlugin {
($baseTopic, $baseWeb) = @_;

Foswiki::Func::registerTagHandler('DISQUS', \&DISQUS);
Foswiki::Func::registerTagHandler('DISQUS_COUNT', \&DISQUS_COUNT);
Foswiki::Meta::registerMETA("DISQUS", # or do we standardize on PAGE_ID
require => ['name'],
allow => ['description', 'date']
);

$doneDisqusInit = 0;
$doneDisqusCount = 0;
$doneDisqusEmbed = 0;

return 1;
}

sub writeDebug {
print STDERR "DisqusPlugin - $_[0]\n" if DEBUG;
}

sub beforeSaveHandler {
my ($text, $topic, $web, $meta) = @_;

writeDebug("called beforeSaveHandler($web, $topic)");

if ($text =~ /%DISQUS({.*?})?%/ || Foswiki::Func::getPreferencesFlag("DISPLAYCOMMENTS")) {
my $disqusData = $meta->get("DISQUS");
unless (defined $disqusData) {
writeDebug("adding META::DISQUS");

# normalize web name
$web =~ s/\//./g;

# store a page id stable between topic renames
$meta->putKeyed("DISQUS", {
name => Digest::MD5::md5_hex("$web.$topic"),
date => time(),
});
}
}
}

sub requireDisqusInit {
return if $doneDisqusInit;
$doneDisqusInit = 1;

my $code = <<"HERE";
<script type="text/javascript">var disqus_shortname = "$Foswiki::cfg{DisqusPlugin}{ForumName}";</script>
HERE

Foswiki::Func::addToZone("script", "DISQUS::INIT", $code, "JQUERYPLUGIN");
}

sub requireDisqusCount {
return if $doneDisqusCount;
$doneDisqusCount = 1;

requireDisqusInit();

my $code = <<'HERE';
<script type="text/javascript">
jQuery(function($) {
$("<script />").attr({
type: "text/javascript",
async: true,
src: '//' + disqus_shortname + '.disqus.com/count.js'
}).appendTo("body");
});
</script>
HERE

Foswiki::Func::addToZone("script", "DISQUS::COUNT", $code, "DISQUS::INIT");
}

sub requireDisqusEmbed {
return if $doneDisqusEmbed;
$doneDisqusEmbed = 1;

requireDisqusInit();

my $code = <<'HERE';
<script type="text/javascript">
jQuery(function($) {
$("<script />").attr({
type: "text/javascript",
async: true,
src: '//' + disqus_shortname + '.disqus.com/embed.js'
}).appendTo("body");
});
</script>
HERE

Foswiki::Func::addToZone("script", "DISQUS::EMBED", $code, "DISQUS::INIT");
}

sub DISQUS_COUNT {
my ($session, $params, $topic, $web) = @_;

writeDebug("called DISQUS_COUNT{$web, $topic}");

my $webTopic = $params->{_DEFAULT} || $params->{topic} || $topic;
($web, $topic) = Foswiki::Func::normalizeWebTopicName($web, $webTopic);

my ($meta) = Foswiki::Func::readTopic($web, $topic);
my $disqusData = $meta->get("DISQUS");
return "" unless defined $disqusData;

my $id = $disqusData->{name};
return "" unless defined $id;

requireDisqusCount();

my $format = $params->{format} || '<a href="$url" class="disqus_count" data-disqus-identifier="$id"></a>';
my $viewUrl = Foswiki::Func::getScriptUrl($web, $topic, "view", '#' => 'disqus_thread');
$format =~ s/\$url/$viewUrl/g;
$format =~ s/\$id/$id/g;

return $format;
}

sub DISQUS {
my ($session, $params, $topic, $web) = @_;

writeDebug("called DISQUS($web, $topic)");

my $webTopic = $params->{_DEFAUT} || $params->{topic} || $topic;
($web, $topic) = Foswiki::Func::normalizeWebTopicName($web, $webTopic);

my ($meta) = Foswiki::Func::readTopic($web, $topic);
my $disqusData = $meta->get("DISQUS");
return "" unless defined $disqusData;

my $id = $disqusData->{name};
return "" unless defined $id;

my @vars = ();
push @vars, "disqus_identifier = '$id'";

my $title = $params->{title};
push @vars, "disqus_title = '$title'" if defined $title;

my $url = $params->{url};
push @vars, "disqus_url = '$url'" if defined $url;

my $category = $params->{category};
push @vars, "disqus_category_id = '$category'" if defined $category;

my $vars = join(",\n ", @vars);

requireDisqusEmbed();

my $code = <<"HERE";
<script type="text/javascript">
var $vars;
</script>
HERE

Foswiki::Func::addToZone("script", "DISQUS::VARS", $code, "DISQUS::INIT");

return '<div id="disqus_thread"></div>';
}

1;
11 changes: 11 additions & 0 deletions lib/Foswiki/Plugins/DisqusPlugin/Config.spec
@@ -0,0 +1,11 @@
# ---+ Extensions
# ---++ DisqusPlugin
# This is the configuration used by the <b>DisqusPlugin</b>.

# **STRING**
# The "short-name" describing this site's forum.
# Use the setup instructor on discus.com to register a forum. Add the value
# of the <code>disqus_shortname</code> JavaScript variable here.
$Foswiki::cfg{DisqusPlugin}{ForumName} = '';

1;
5 changes: 5 additions & 0 deletions lib/Foswiki/Plugins/DisqusPlugin/DEPENDENCIES
@@ -0,0 +1,5 @@
# Dependencies for DisqusPlugin
# Example:
# Time::ParseDate,>=2003.0211,cpan,Required.
# Foswiki::Plugins,>=1.2,perl,Requires version 1.2 of handler API.

3 changes: 3 additions & 0 deletions lib/Foswiki/Plugins/DisqusPlugin/MANIFEST
@@ -0,0 +1,3 @@
data/System/DisqusPlugin.txt 0644
lib/Foswiki/Plugins/DisqusPlugin/Config.spec 0644
lib/Foswiki/Plugins/DisqusPlugin.pm 0644
24 changes: 24 additions & 0 deletions lib/Foswiki/Plugins/DisqusPlugin/build.pl
@@ -0,0 +1,24 @@
#!/usr/bin/perl -w
BEGIN { unshift @INC, split( /:/, $ENV{FOSWIKI_LIBS} ); }
use Foswiki::Contrib::Build;

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

# (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 bbcfad3

Please sign in to comment.