2008-09-21 20:33:45 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Copyright © 2006-2008 Joey Hess <joey@ikiwiki.info>
|
|
|
|
# Copyright © 2008 Simon McVittie <http://smcv.pseudorandom.co.uk/>
|
|
|
|
# Licensed under the GNU GPL, version 2, or any later version published by the
|
|
|
|
# Free Software Foundation
|
2008-11-18 11:17:18 +01:00
|
|
|
package IkiWiki::Plugin::comments;
|
2008-09-21 20:33:45 +02:00
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2008-12-11 00:53:04 +01:00
|
|
|
use Encode;
|
2008-12-12 12:02:41 +01:00
|
|
|
use POSIX qw(strftime);
|
2008-09-21 20:33:45 +02:00
|
|
|
|
|
|
|
use constant PREVIEW => "Preview";
|
|
|
|
use constant POST_COMMENT => "Post comment";
|
|
|
|
use constant CANCEL => "Cancel";
|
|
|
|
|
2008-12-12 21:05:26 +01:00
|
|
|
my $postcomment;
|
2008-12-21 02:55:38 +01:00
|
|
|
my %commentstate;
|
2008-12-12 21:05:26 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-11-23 18:42:50 +01:00
|
|
|
hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
|
2008-11-18 11:17:18 +01:00
|
|
|
hook(type => "getsetup", id => 'comments', call => \&getsetup);
|
2008-12-11 22:23:55 +01:00
|
|
|
hook(type => "preprocess", id => '_comment', call => \&preprocess);
|
2008-11-18 11:17:18 +01:00
|
|
|
hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
|
2008-11-18 11:43:11 +01:00
|
|
|
hook(type => "htmlize", id => "_comment", call => \&htmlize);
|
2008-11-23 13:04:00 +01:00
|
|
|
hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
|
2008-11-23 19:31:11 +01:00
|
|
|
hook(type => "cgi", id => "comments", call => \&linkcgi);
|
2008-12-11 00:53:04 +01:00
|
|
|
IkiWiki::loadplugin("inline");
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-11-17 11:41:50 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-12-12 20:59:23 +01:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
2008-12-18 00:50:04 +01:00
|
|
|
comments_pagespec => {
|
2008-12-12 20:59:23 +01:00
|
|
|
type => 'pagespec',
|
2008-12-18 02:22:42 +01:00
|
|
|
example => 'blog/* and !*/Discussion',
|
2008-12-18 00:50:04 +01:00
|
|
|
description => 'PageSpec of pages where comments are allowed',
|
2008-12-12 20:59:23 +01:00
|
|
|
link => 'ikiwiki/PageSpec',
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
2008-12-18 00:50:04 +01:00
|
|
|
comments_closed_pagespec => {
|
2008-12-12 20:59:23 +01:00
|
|
|
type => 'pagespec',
|
2008-12-18 00:50:04 +01:00
|
|
|
example => 'blog/controversial or blog/flamewar',
|
|
|
|
description => 'PageSpec of pages where posting new comments is not allowed',
|
2008-12-12 20:59:23 +01:00
|
|
|
link => 'ikiwiki/PageSpec',
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
|
|
|
},
|
|
|
|
comments_pagename => {
|
|
|
|
type => 'string',
|
|
|
|
default => 'comment_',
|
|
|
|
description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
|
|
|
|
safe => 0, # manual page moving required
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
|
|
|
comments_allowdirectives => {
|
|
|
|
type => 'boolean',
|
|
|
|
example => 0,
|
|
|
|
description => 'Interpret directives in comments?',
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
|
|
|
comments_allowauthor => {
|
|
|
|
type => 'boolean',
|
|
|
|
example => 0,
|
|
|
|
description => 'Allow anonymous commenters to set an author name?',
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
|
|
|
comments_commit => {
|
|
|
|
type => 'boolean',
|
|
|
|
example => 1,
|
|
|
|
description => 'commit comments to the VCS',
|
|
|
|
# old uncommitted comments are likely to cause
|
|
|
|
# confusion if this is changed
|
|
|
|
safe => 0,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-12-12 20:59:23 +01:00
|
|
|
|
2008-12-17 23:05:49 +01:00
|
|
|
sub checkconfig () {
|
|
|
|
$config{comments_commit} = 1
|
|
|
|
unless defined $config{comments_commit};
|
2008-12-18 00:50:04 +01:00
|
|
|
$config{comments_pagespec} = ''
|
|
|
|
unless defined $config{comments_pagespec};
|
|
|
|
$config{comments_closed_pagespec} = ''
|
|
|
|
unless defined $config{comments_closed_pagespec};
|
2008-12-17 23:05:49 +01:00
|
|
|
$config{comments_pagename} = 'comment_'
|
|
|
|
unless defined $config{comments_pagename};
|
|
|
|
}
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub htmlize {
|
2008-12-11 00:53:04 +01:00
|
|
|
my %params = @_;
|
|
|
|
return $params{content};
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-12-11 00:53:04 +01:00
|
|
|
|
2008-12-11 02:33:43 +01:00
|
|
|
# FIXME: copied verbatim from meta
|
2008-12-17 21:22:16 +01:00
|
|
|
sub safeurl ($) {
|
2008-12-11 02:33:43 +01:00
|
|
|
my $url=shift;
|
|
|
|
if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
|
|
|
|
defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
|
|
|
|
return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1;
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-12-11 02:33:43 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess {
|
2008-12-11 00:53:04 +01:00
|
|
|
my %params = @_;
|
|
|
|
my $page = $params{page};
|
|
|
|
|
|
|
|
my $format = $params{format};
|
2008-12-12 21:13:07 +01:00
|
|
|
if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
|
2008-12-11 00:53:04 +01:00
|
|
|
error(sprintf(gettext("unsupported page format %s"), $format));
|
|
|
|
}
|
|
|
|
|
|
|
|
my $content = $params{content};
|
2008-12-12 21:13:07 +01:00
|
|
|
if (! defined $content) {
|
2008-12-11 00:53:04 +01:00
|
|
|
error(gettext("comment must have content"));
|
|
|
|
}
|
|
|
|
$content =~ s/\\"/"/g;
|
|
|
|
|
|
|
|
$content = IkiWiki::filter($page, $params{destpage}, $content);
|
|
|
|
|
|
|
|
if ($config{comments_allowdirectives}) {
|
|
|
|
$content = IkiWiki::preprocess($page, $params{destpage},
|
|
|
|
$content);
|
|
|
|
}
|
|
|
|
|
|
|
|
# no need to bother with htmlize if it's just HTML
|
|
|
|
$content = IkiWiki::htmlize($page, $params{destpage}, $format,
|
|
|
|
$content) if defined $format;
|
|
|
|
|
|
|
|
IkiWiki::run_hooks(sanitize => sub {
|
|
|
|
$content = shift->(
|
|
|
|
page => $page,
|
|
|
|
destpage => $params{destpage},
|
|
|
|
content => $content,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2008-12-11 02:33:43 +01:00
|
|
|
# set metadata, possibly overriding [[!meta]] directives from the
|
|
|
|
# comment itself
|
|
|
|
|
|
|
|
my $commentuser;
|
|
|
|
my $commentip;
|
|
|
|
my $commentauthor;
|
|
|
|
my $commentauthorurl;
|
2008-12-18 20:56:36 +01:00
|
|
|
my $commentopenid;
|
2008-12-11 00:53:04 +01:00
|
|
|
if (defined $params{username}) {
|
2008-12-11 02:33:43 +01:00
|
|
|
$commentuser = $params{username};
|
2008-12-18 20:56:36 +01:00
|
|
|
|
|
|
|
my $oiduser = eval { IkiWiki::openiduser($commentuser) };
|
|
|
|
|
|
|
|
if (defined $oiduser) {
|
|
|
|
# looks like an OpenID
|
|
|
|
$commentauthorurl = $commentuser;
|
|
|
|
$commentauthor = $oiduser;
|
|
|
|
$commentopenid = $commentuser;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$commentauthorurl = IkiWiki::cgiurl(
|
|
|
|
do => 'commenter',
|
|
|
|
page => (length $config{userdir}
|
|
|
|
? "$config{userdir}/$commentuser"
|
|
|
|
: "$commentuser"));
|
|
|
|
|
|
|
|
$commentauthor = $commentuser;
|
|
|
|
}
|
2008-12-11 00:53:04 +01:00
|
|
|
}
|
|
|
|
else {
|
2008-12-11 03:29:11 +01:00
|
|
|
if (defined $params{ip}) {
|
|
|
|
$commentip = $params{ip};
|
|
|
|
}
|
2008-12-11 02:33:43 +01:00
|
|
|
$commentauthor = gettext("Anonymous");
|
2008-12-11 00:53:04 +01:00
|
|
|
}
|
|
|
|
|
2008-12-21 02:55:38 +01:00
|
|
|
$commentstate{$page}{commentuser} = $commentuser;
|
|
|
|
$commentstate{$page}{commentopenid} = $commentopenid;
|
|
|
|
$commentstate{$page}{commentip} = $commentip;
|
|
|
|
$commentstate{$page}{commentauthor} = $commentauthor;
|
|
|
|
$commentstate{$page}{commentauthorurl} = $commentauthorurl;
|
2008-12-12 21:13:07 +01:00
|
|
|
if (! defined $pagestate{$page}{meta}{author}) {
|
2008-12-11 02:33:43 +01:00
|
|
|
$pagestate{$page}{meta}{author} = $commentauthor;
|
|
|
|
}
|
2008-12-12 21:13:07 +01:00
|
|
|
if (! defined $pagestate{$page}{meta}{authorurl}) {
|
2008-12-11 02:33:43 +01:00
|
|
|
$pagestate{$page}{meta}{authorurl} = $commentauthorurl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($config{comments_allowauthor}) {
|
|
|
|
if (defined $params{claimedauthor}) {
|
|
|
|
$pagestate{$page}{meta}{author} = $params{claimedauthor};
|
|
|
|
}
|
|
|
|
|
2008-12-26 20:07:19 +01:00
|
|
|
if (defined $params{url}) {
|
|
|
|
my $url=$params{url};
|
|
|
|
|
|
|
|
eval q{use URI::Heuristic};
|
|
|
|
if (! $@) {
|
|
|
|
$url=URI::Heuristic::uf_uristr($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (safeurl($url)) {
|
|
|
|
$pagestate{$page}{meta}{authorurl} = $url;
|
|
|
|
}
|
2008-12-11 02:33:43 +01:00
|
|
|
}
|
2008-12-11 00:53:04 +01:00
|
|
|
}
|
|
|
|
else {
|
2008-12-11 02:33:43 +01:00
|
|
|
$pagestate{$page}{meta}{author} = $commentauthor;
|
|
|
|
$pagestate{$page}{meta}{authorurl} = $commentauthorurl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $params{subject}) {
|
|
|
|
$pagestate{$page}{meta}{title} = $params{subject};
|
2008-12-11 00:53:04 +01:00
|
|
|
}
|
|
|
|
|
2008-12-12 21:38:23 +01:00
|
|
|
if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
|
2008-12-19 23:33:40 +01:00
|
|
|
$pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
|
2008-12-19 02:58:16 +01:00
|
|
|
"#".$params{page};
|
2008-12-11 00:53:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
eval q{use Date::Parse};
|
|
|
|
if (! $@) {
|
|
|
|
my $time = str2time($params{date});
|
|
|
|
$IkiWiki::pagectime{$page} = $time if defined $time;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-09-21 20:33:45 +02:00
|
|
|
|
2008-11-23 19:31:11 +01:00
|
|
|
# This is exactly the same as recentchanges_link :-(
|
2008-12-17 21:22:16 +01:00
|
|
|
sub linkcgi ($) {
|
2008-11-23 19:31:11 +01:00
|
|
|
my $cgi=shift;
|
|
|
|
if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
|
|
|
|
|
|
|
|
my $page=decode_utf8($cgi->param("page"));
|
2008-12-12 21:13:07 +01:00
|
|
|
if (! defined $page) {
|
2008-11-23 19:31:11 +01:00
|
|
|
error("missing page parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
IkiWiki::loadindex();
|
|
|
|
|
|
|
|
my $link=bestlink("", $page);
|
|
|
|
if (! length $link) {
|
|
|
|
print "Content-type: text/html\n\n";
|
|
|
|
print IkiWiki::misctemplate(gettext(gettext("missing page")),
|
|
|
|
"<p>".
|
|
|
|
sprintf(gettext("The page %s does not exist."),
|
|
|
|
htmllink("", "", $page)).
|
|
|
|
"</p>");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
IkiWiki::redirect($cgi, urlto($link, undef, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-21 20:33:45 +02:00
|
|
|
# Mostly cargo-culted from IkiWiki::plugin::editpage
|
2008-12-17 21:22:16 +01:00
|
|
|
sub sessioncgi ($$) {
|
2008-09-21 20:33:45 +02:00
|
|
|
my $cgi=shift;
|
|
|
|
my $session=shift;
|
|
|
|
|
|
|
|
my $do = $cgi->param('do');
|
2008-11-18 11:17:18 +01:00
|
|
|
return unless $do eq 'comment';
|
2008-09-21 20:33:45 +02:00
|
|
|
|
|
|
|
IkiWiki::decode_cgi_utf8($cgi);
|
|
|
|
|
|
|
|
eval q{use CGI::FormBuilder};
|
|
|
|
error($@) if $@;
|
|
|
|
|
|
|
|
my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
|
|
|
|
my $form = CGI::FormBuilder->new(
|
2008-12-11 02:33:43 +01:00
|
|
|
fields => [qw{do sid page subject editcontent type author url}],
|
2008-09-21 20:33:45 +02:00
|
|
|
charset => 'utf-8',
|
|
|
|
method => 'POST',
|
2008-12-11 01:22:34 +01:00
|
|
|
required => [qw{editcontent}],
|
2008-09-21 20:33:45 +02:00
|
|
|
javascript => 0,
|
|
|
|
params => $cgi,
|
|
|
|
action => $config{cgiurl},
|
|
|
|
header => 0,
|
|
|
|
table => 0,
|
2008-12-19 20:07:22 +01:00
|
|
|
template => scalar IkiWiki::template_params('editcomment.tmpl'),
|
2008-09-21 20:33:45 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
IkiWiki::decode_form_utf8($form);
|
|
|
|
IkiWiki::run_hooks(formbuilder_setup => sub {
|
2008-11-18 11:17:18 +01:00
|
|
|
shift->(title => "comment", form => $form, cgi => $cgi,
|
2008-09-21 20:33:45 +02:00
|
|
|
session => $session, buttons => \@buttons);
|
|
|
|
});
|
|
|
|
IkiWiki::decode_form_utf8($form);
|
|
|
|
|
2008-12-11 01:19:39 +01:00
|
|
|
my $type = $form->param('type');
|
|
|
|
if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
|
2008-12-11 03:22:20 +01:00
|
|
|
$type = IkiWiki::possibly_foolish_untaint($type);
|
2008-12-11 01:19:39 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$type = $config{default_pageext};
|
|
|
|
}
|
|
|
|
my @page_types;
|
|
|
|
if (exists $IkiWiki::hooks{htmlize}) {
|
2008-12-12 21:13:07 +01:00
|
|
|
@page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
|
2008-12-11 01:19:39 +01:00
|
|
|
}
|
|
|
|
|
2008-09-21 20:33:45 +02:00
|
|
|
$form->field(name => 'do', type => 'hidden');
|
|
|
|
$form->field(name => 'sid', type => 'hidden', value => $session->id,
|
|
|
|
force => 1);
|
|
|
|
$form->field(name => 'page', type => 'hidden');
|
2008-11-16 19:11:55 +01:00
|
|
|
$form->field(name => 'subject', type => 'text', size => 72);
|
2008-12-11 01:22:34 +01:00
|
|
|
$form->field(name => 'editcontent', type => 'textarea', rows => 10);
|
2008-12-11 01:19:39 +01:00
|
|
|
$form->field(name => "type", value => $type, force => 1,
|
|
|
|
type => 'select', options => \@page_types);
|
2008-09-21 20:33:45 +02:00
|
|
|
|
2008-12-11 02:33:43 +01:00
|
|
|
$form->tmpl_param(username => $session->param('name'));
|
|
|
|
|
2008-12-12 21:38:23 +01:00
|
|
|
if ($config{comments_allowauthor} and
|
|
|
|
! defined $session->param('name')) {
|
2008-12-11 02:33:43 +01:00
|
|
|
$form->tmpl_param(allowauthor => 1);
|
|
|
|
$form->field(name => 'author', type => 'text', size => '40');
|
|
|
|
$form->field(name => 'url', type => 'text', size => '40');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$form->tmpl_param(allowauthor => 0);
|
|
|
|
$form->field(name => 'author', type => 'hidden', value => '',
|
|
|
|
force => 1);
|
|
|
|
$form->field(name => 'url', type => 'hidden', value => '',
|
|
|
|
force => 1);
|
|
|
|
}
|
|
|
|
|
2008-09-21 20:33:45 +02:00
|
|
|
# The untaint is OK (as in editpage) because we're about to pass
|
|
|
|
# it to file_pruned anyway
|
|
|
|
my $page = $form->field('page');
|
|
|
|
$page = IkiWiki::possibly_foolish_untaint($page);
|
2008-12-12 21:13:07 +01:00
|
|
|
if (! defined $page || ! length $page ||
|
2008-09-21 20:33:45 +02:00
|
|
|
IkiWiki::file_pruned($page, $config{srcdir})) {
|
2008-11-15 15:11:24 +01:00
|
|
|
error(gettext("bad page name"));
|
2008-09-21 20:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# FIXME: is this right? Or should we be using the candidate subpage
|
|
|
|
# (whatever that might mean) as the base URL?
|
|
|
|
my $baseurl = urlto($page, undef, 1);
|
|
|
|
|
|
|
|
$form->title(sprintf(gettext("commenting on %s"),
|
|
|
|
IkiWiki::pagetitle($page)));
|
|
|
|
|
|
|
|
$form->tmpl_param('helponformattinglink',
|
|
|
|
htmllink($page, $page, 'ikiwiki/formatting',
|
|
|
|
noimageinline => 1,
|
2008-11-17 10:08:54 +01:00
|
|
|
linktext => 'FormattingHelp'),
|
2008-12-12 21:38:23 +01:00
|
|
|
allowdirectives => $config{allow_directives});
|
2008-09-21 20:33:45 +02:00
|
|
|
|
2008-11-23 18:10:44 +01:00
|
|
|
if ($form->submitted eq CANCEL) {
|
|
|
|
# bounce back to the page they wanted to comment on, and exit.
|
|
|
|
# CANCEL need not be considered in future
|
|
|
|
IkiWiki::redirect($cgi, urlto($page, undef, 1));
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2008-09-21 20:33:45 +02:00
|
|
|
if (not exists $pagesources{$page}) {
|
2008-11-15 15:11:24 +01:00
|
|
|
error(sprintf(gettext(
|
|
|
|
"page '%s' doesn't exist, so you can't comment"),
|
|
|
|
$page));
|
2008-09-21 20:33:45 +02:00
|
|
|
}
|
2008-11-23 18:10:44 +01:00
|
|
|
|
2008-12-18 00:50:04 +01:00
|
|
|
if (pagespec_match($page, $config{comments_closed_pagespec},
|
2008-11-23 18:10:44 +01:00
|
|
|
location => $page)) {
|
2008-11-17 10:09:52 +01:00
|
|
|
error(sprintf(gettext(
|
2008-11-23 18:10:44 +01:00
|
|
|
"comments on page '%s' are closed"),
|
2008-11-17 10:09:52 +01:00
|
|
|
$page));
|
|
|
|
}
|
2008-09-21 20:33:45 +02:00
|
|
|
|
2008-12-12 21:05:26 +01:00
|
|
|
# Set a flag to indicate that we're posting a comment,
|
|
|
|
# so that postcomment() can tell it should match.
|
|
|
|
$postcomment=1;
|
|
|
|
IkiWiki::check_canedit($page, $cgi, $session);
|
|
|
|
$postcomment=0;
|
2008-11-16 19:23:23 +01:00
|
|
|
|
2008-11-23 18:10:44 +01:00
|
|
|
# FIXME: rather a simplistic way to make the comments...
|
|
|
|
my $i = 0;
|
|
|
|
my $file;
|
|
|
|
my $location;
|
|
|
|
do {
|
|
|
|
$i++;
|
2008-12-12 21:38:23 +01:00
|
|
|
$location = "$page/$config{comments_pagename}$i";
|
2008-11-23 18:10:44 +01:00
|
|
|
} while (-e "$config{srcdir}/$location._comment");
|
|
|
|
|
2008-12-11 22:23:55 +01:00
|
|
|
my $content = "[[!_comment format=$type\n";
|
2008-12-11 00:53:04 +01:00
|
|
|
|
|
|
|
# FIXME: handling of double quotes probably wrong?
|
|
|
|
if (defined $session->param('name')) {
|
|
|
|
my $username = $session->param('name');
|
|
|
|
$username =~ s/"/"/g;
|
|
|
|
$content .= " username=\"$username\"\n";
|
|
|
|
}
|
|
|
|
elsif (defined $ENV{REMOTE_ADDR}) {
|
|
|
|
my $ip = $ENV{REMOTE_ADDR};
|
|
|
|
if ($ip =~ m/^([.0-9]+)$/) {
|
|
|
|
$content .= " ip=\"$1\"\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-12 21:38:23 +01:00
|
|
|
if ($config{comments_allowauthor}) {
|
2008-12-11 02:33:43 +01:00
|
|
|
my $author = $form->field('author');
|
|
|
|
if (length $author) {
|
|
|
|
$author =~ s/"/"/g;
|
|
|
|
$content .= " claimedauthor=\"$author\"\n";
|
|
|
|
}
|
|
|
|
my $url = $form->field('url');
|
|
|
|
if (length $url) {
|
|
|
|
$url =~ s/"/"/g;
|
|
|
|
$content .= " url=\"$url\"\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-11 00:53:04 +01:00
|
|
|
my $subject = $form->field('subject');
|
2008-12-11 03:03:07 +01:00
|
|
|
if (length $subject) {
|
|
|
|
$subject =~ s/"/"/g;
|
|
|
|
$content .= " subject=\"$subject\"\n";
|
|
|
|
}
|
2008-12-11 00:53:04 +01:00
|
|
|
|
2008-12-12 12:02:41 +01:00
|
|
|
$content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
|
2008-11-18 11:29:16 +01:00
|
|
|
|
2008-12-12 21:19:01 +01:00
|
|
|
my $editcontent = $form->field('editcontent') || '';
|
|
|
|
$editcontent =~ s/\r\n/\n/g;
|
|
|
|
$editcontent =~ s/\r/\n/g;
|
|
|
|
$editcontent =~ s/"/\\"/g;
|
2008-12-11 01:22:34 +01:00
|
|
|
$content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
|
2008-09-21 20:33:45 +02:00
|
|
|
|
|
|
|
# This is essentially a simplified version of editpage:
|
|
|
|
# - the user does not control the page that's created, only the parent
|
|
|
|
# - it's always a create operation, never an edit
|
|
|
|
# - this means that conflicts should never happen
|
|
|
|
# - this means that if they do, rocks fall and everyone dies
|
|
|
|
|
|
|
|
if ($form->submitted eq PREVIEW) {
|
2008-12-11 01:19:39 +01:00
|
|
|
my $preview = IkiWiki::htmlize($location, $page, '_comment',
|
2008-12-21 16:07:36 +01:00
|
|
|
IkiWiki::linkify($location, $page,
|
|
|
|
IkiWiki::preprocess($location, $page,
|
2008-11-23 18:10:44 +01:00
|
|
|
IkiWiki::filter($location,
|
|
|
|
$page, $content),
|
2008-09-21 20:33:45 +02:00
|
|
|
0, 1)));
|
|
|
|
IkiWiki::run_hooks(format => sub {
|
|
|
|
$preview = shift->(page => $page,
|
|
|
|
content => $preview);
|
|
|
|
});
|
|
|
|
|
2008-12-19 20:03:26 +01:00
|
|
|
my $template = template("comment.tmpl");
|
2008-09-21 20:33:45 +02:00
|
|
|
$template->param(content => $preview);
|
|
|
|
$template->param(title => $form->field('subject'));
|
|
|
|
$template->param(ctime => displaytime(time));
|
|
|
|
|
2008-12-21 16:08:14 +01:00
|
|
|
IkiWiki::run_hooks(pagetemplate => sub {
|
|
|
|
shift->(page => $location,
|
|
|
|
destpage => $page,
|
|
|
|
template => $template);
|
|
|
|
});
|
|
|
|
|
2008-09-21 20:33:45 +02:00
|
|
|
$form->tmpl_param(page_preview => $template->output);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$form->tmpl_param(page_preview => "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form->submitted eq POST_COMMENT && $form->validate) {
|
2008-11-23 18:10:44 +01:00
|
|
|
my $file = "$location._comment";
|
2008-09-21 20:33:45 +02:00
|
|
|
|
2008-12-17 20:26:08 +01:00
|
|
|
IkiWiki::checksessionexpiry($cgi, $session);
|
2008-11-23 18:52:30 +01:00
|
|
|
|
2008-09-21 20:33:45 +02:00
|
|
|
# FIXME: could probably do some sort of graceful retry
|
2008-11-23 18:10:44 +01:00
|
|
|
# on error? Would require significant unwinding though
|
2008-09-21 20:33:45 +02:00
|
|
|
writefile($file, $config{srcdir}, $content);
|
|
|
|
|
|
|
|
my $conflict;
|
|
|
|
|
2008-12-12 21:38:23 +01:00
|
|
|
if ($config{rcs} and $config{comments_commit}) {
|
2008-11-15 15:11:24 +01:00
|
|
|
my $message = gettext("Added a comment");
|
2008-09-21 20:33:45 +02:00
|
|
|
if (defined $form->field('subject') &&
|
|
|
|
length $form->field('subject')) {
|
2008-11-23 18:10:44 +01:00
|
|
|
$message = sprintf(
|
|
|
|
gettext("Added a comment: %s"),
|
|
|
|
$form->field('subject'));
|
2008-09-21 20:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IkiWiki::rcs_add($file);
|
|
|
|
IkiWiki::disable_commit_hook();
|
|
|
|
$conflict = IkiWiki::rcs_commit_staged($message,
|
|
|
|
$session->param('name'), $ENV{REMOTE_ADDR});
|
|
|
|
IkiWiki::enable_commit_hook();
|
|
|
|
IkiWiki::rcs_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
# Now we need a refresh
|
|
|
|
require IkiWiki::Render;
|
|
|
|
IkiWiki::refresh();
|
|
|
|
IkiWiki::saveindex();
|
|
|
|
|
|
|
|
# this should never happen, unless a committer deliberately
|
|
|
|
# breaks it or something
|
|
|
|
error($conflict) if defined $conflict;
|
|
|
|
|
2008-12-19 02:58:16 +01:00
|
|
|
# Jump to the new comment on the page.
|
2008-12-29 04:20:22 +01:00
|
|
|
# The trailing question mark tries to avoid broken
|
|
|
|
# caches and get the most recent version of the page.
|
2009-01-07 17:12:32 +01:00
|
|
|
IkiWiki::redirect($cgi, urlto($page, undef, 1)."?updated#$location");
|
2008-12-29 04:20:22 +01:00
|
|
|
|
2008-09-21 20:33:45 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
IkiWiki::showform ($form, \@buttons, $session, $cgi,
|
|
|
|
forcebaseurl => $baseurl);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-09-21 20:33:45 +02:00
|
|
|
|
2008-12-18 01:38:02 +01:00
|
|
|
sub commentsshown ($) {
|
|
|
|
my $page=shift;
|
|
|
|
|
|
|
|
return ! pagespec_match($page, "*/$config{comments_pagename}*",
|
|
|
|
location => $page) &&
|
|
|
|
pagespec_match($page, $config{comments_pagespec},
|
|
|
|
location => $page);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub commentsopen ($) {
|
|
|
|
my $page = shift;
|
|
|
|
|
|
|
|
return length $config{cgiurl} > 0 &&
|
|
|
|
(! length $config{comments_closed_pagespec} ||
|
|
|
|
! pagespec_match($page, $config{comments_closed_pagespec},
|
|
|
|
location => $page));
|
|
|
|
}
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub pagetemplate (@) {
|
2008-11-23 13:04:00 +01:00
|
|
|
my %params = @_;
|
|
|
|
|
|
|
|
my $page = $params{page};
|
|
|
|
my $template = $params{template};
|
2008-12-19 19:55:41 +01:00
|
|
|
my $shown = ($template->query(name => 'commentslink') ||
|
2008-12-21 18:15:49 +01:00
|
|
|
$template->query(name => 'commentsurl') ||
|
|
|
|
$template->query(name => 'atomcommentsurl') ||
|
2008-12-19 19:55:41 +01:00
|
|
|
$template->query(name => 'comments')) &&
|
|
|
|
commentsshown($page);
|
2008-11-23 13:04:00 +01:00
|
|
|
|
|
|
|
if ($template->query(name => 'comments')) {
|
2008-12-18 01:38:02 +01:00
|
|
|
my $comments = undef;
|
2008-11-23 18:11:14 +01:00
|
|
|
if ($shown) {
|
2008-12-12 21:08:06 +01:00
|
|
|
$comments = IkiWiki::preprocess_inline(
|
2008-12-12 21:38:23 +01:00
|
|
|
pages => "internal($page/$config{comments_pagename}*)",
|
2008-12-19 20:03:26 +01:00
|
|
|
template => 'comment',
|
2008-11-23 18:11:14 +01:00
|
|
|
show => 0,
|
|
|
|
reverse => 'yes',
|
|
|
|
page => $page,
|
|
|
|
destpage => $params{destpage},
|
2008-12-11 22:19:50 +01:00
|
|
|
feedfile => 'comments',
|
|
|
|
emptyfeeds => 'no',
|
2008-11-23 18:11:14 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-11-23 13:04:00 +01:00
|
|
|
if (defined $comments && length $comments) {
|
2008-11-23 18:11:14 +01:00
|
|
|
$template->param(comments => $comments);
|
|
|
|
}
|
|
|
|
|
2008-12-18 01:38:02 +01:00
|
|
|
if ($shown && commentsopen($page)) {
|
2008-12-18 21:01:08 +01:00
|
|
|
my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
|
2008-11-23 18:11:14 +01:00
|
|
|
page => $page);
|
2008-12-18 21:01:08 +01:00
|
|
|
$template->param(addcommenturl => $addcommenturl);
|
2008-11-23 13:04:00 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-11 02:33:43 +01:00
|
|
|
|
2008-12-21 18:15:49 +01:00
|
|
|
if ($template->query(name => 'commentsurl')) {
|
|
|
|
if ($shown) {
|
|
|
|
$template->param(commentsurl =>
|
|
|
|
urlto($page, undef, 1).'#comments');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
|
|
|
|
if ($shown) {
|
|
|
|
# This will 404 until there are some comments, but I
|
|
|
|
# think that's probably OK...
|
|
|
|
$template->param(atomcommentsurl =>
|
|
|
|
urlto($page, undef, 1).'comments.atom');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-18 01:38:02 +01:00
|
|
|
if ($template->query(name => 'commentslink')) {
|
|
|
|
# XXX Would be nice to say how many comments there are in
|
|
|
|
# the link. But, to update the number, blog pages
|
|
|
|
# would have to update whenever comments of any inlines
|
|
|
|
# page are added, which is not currently done.
|
2008-12-19 19:55:41 +01:00
|
|
|
if ($shown) {
|
2008-12-18 01:38:02 +01:00
|
|
|
$template->param(commentslink =>
|
|
|
|
htmllink($page, $params{destpage}, $page,
|
|
|
|
linktext => gettext("Comments"),
|
|
|
|
anchor => "comments",
|
|
|
|
noimageinline => 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-11 02:33:43 +01:00
|
|
|
if ($template->query(name => 'commentuser')) {
|
|
|
|
$template->param(commentuser =>
|
2008-12-21 02:55:38 +01:00
|
|
|
$commentstate{$page}{commentuser});
|
2008-12-11 02:33:43 +01:00
|
|
|
}
|
|
|
|
|
2008-12-18 20:57:25 +01:00
|
|
|
if ($template->query(name => 'commentopenid')) {
|
|
|
|
$template->param(commentopenid =>
|
2008-12-21 02:55:38 +01:00
|
|
|
$commentstate{$page}{commentopenid});
|
2008-12-18 20:57:25 +01:00
|
|
|
}
|
|
|
|
|
2008-12-11 02:33:43 +01:00
|
|
|
if ($template->query(name => 'commentip')) {
|
|
|
|
$template->param(commentip =>
|
2008-12-21 02:55:38 +01:00
|
|
|
$commentstate{$page}{commentip});
|
2008-12-11 02:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($template->query(name => 'commentauthor')) {
|
|
|
|
$template->param(commentauthor =>
|
2008-12-21 02:55:38 +01:00
|
|
|
$commentstate{$page}{commentauthor});
|
2008-12-11 02:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($template->query(name => 'commentauthorurl')) {
|
|
|
|
$template->param(commentauthorurl =>
|
2008-12-21 02:55:38 +01:00
|
|
|
$commentstate{$page}{commentauthorurl});
|
2008-12-11 02:33:43 +01:00
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-11-23 13:04:00 +01:00
|
|
|
|
2008-11-16 19:23:23 +01:00
|
|
|
package IkiWiki::PageSpec;
|
|
|
|
|
2008-11-17 12:16:31 +01:00
|
|
|
sub match_postcomment ($$;@) {
|
2008-11-16 19:23:23 +01:00
|
|
|
my $page = shift;
|
|
|
|
my $glob = shift;
|
|
|
|
|
2008-12-12 21:05:26 +01:00
|
|
|
if (! $postcomment) {
|
2008-11-16 19:23:23 +01:00
|
|
|
return IkiWiki::FailReason->new("not posting a comment");
|
|
|
|
}
|
|
|
|
return match_glob($page, $glob);
|
|
|
|
}
|
|
|
|
|
2008-09-21 20:33:45 +02:00
|
|
|
1
|