summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 89560c88b11e803787df08bff66d50836756275c (plain)
  1. #!/usr/bin/perl
  2. # Copyright © 2006-2008 Joey Hess <joey@ikiwiki.info>
  3. # Copyright © 2008 Simon McVittie <http://smcv.pseudorandom.co.uk/>
  4. # Licensed under the GNU GPL, version 2, or any later version published by the
  5. # Free Software Foundation
  6. package IkiWiki::Plugin::comments;
  7. use warnings;
  8. use strict;
  9. use IkiWiki 3.00;
  10. use Encode;
  11. use POSIX qw(strftime);
  12. use constant PREVIEW => "Preview";
  13. use constant POST_COMMENT => "Post comment";
  14. use constant CANCEL => "Cancel";
  15. my $postcomment;
  16. my %commentstate;
  17. sub import {
  18. hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
  19. hook(type => "getsetup", id => 'comments', call => \&getsetup);
  20. hook(type => "preprocess", id => 'comment', call => \&preprocess);
  21. # here for backwards compatability with old comments
  22. hook(type => "preprocess", id => '_comment', call => \&preprocess);
  23. hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
  24. hook(type => "htmlize", id => "_comment", call => \&htmlize);
  25. hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
  26. hook(type => "formbuilder_setup", id => "comments", call => \&formbuilder_setup);
  27. # Load goto to fix up user page links for logged-in commenters
  28. IkiWiki::loadplugin("goto");
  29. IkiWiki::loadplugin("inline");
  30. }
  31. sub getsetup () {
  32. return
  33. plugin => {
  34. safe => 1,
  35. rebuild => 1,
  36. section => "web",
  37. },
  38. comments_pagespec => {
  39. type => 'pagespec',
  40. example => 'blog/* and !*/Discussion',
  41. description => 'PageSpec of pages where comments are allowed',
  42. link => 'ikiwiki/PageSpec',
  43. safe => 1,
  44. rebuild => 1,
  45. },
  46. comments_closed_pagespec => {
  47. type => 'pagespec',
  48. example => 'blog/controversial or blog/flamewar',
  49. description => 'PageSpec of pages where posting new comments is not allowed',
  50. link => 'ikiwiki/PageSpec',
  51. safe => 1,
  52. rebuild => 1,
  53. },
  54. comments_pagename => {
  55. type => 'string',
  56. default => 'comment_',
  57. description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
  58. safe => 0, # manual page moving required
  59. rebuild => undef,
  60. },
  61. comments_allowdirectives => {
  62. type => 'boolean',
  63. example => 0,
  64. description => 'Interpret directives in comments?',
  65. safe => 1,
  66. rebuild => 0,
  67. },
  68. comments_allowauthor => {
  69. type => 'boolean',
  70. example => 0,
  71. description => 'Allow anonymous commenters to set an author name?',
  72. safe => 1,
  73. rebuild => 0,
  74. },
  75. comments_commit => {
  76. type => 'boolean',
  77. example => 1,
  78. description => 'commit comments to the VCS',
  79. # old uncommitted comments are likely to cause
  80. # confusion if this is changed
  81. safe => 0,
  82. rebuild => 0,
  83. },
  84. }
  85. sub checkconfig () {
  86. $config{comments_commit} = 1
  87. unless defined $config{comments_commit};
  88. $config{comments_pagespec} = ''
  89. unless defined $config{comments_pagespec};
  90. $config{comments_closed_pagespec} = ''
  91. unless defined $config{comments_closed_pagespec};
  92. $config{comments_pagename} = 'comment_'
  93. unless defined $config{comments_pagename};
  94. }
  95. sub htmlize {
  96. my %params = @_;
  97. return $params{content};
  98. }
  99. # FIXME: copied verbatim from meta
  100. sub safeurl ($) {
  101. my $url=shift;
  102. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  103. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  104. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  105. }
  106. else {
  107. return 1;
  108. }
  109. }
  110. sub preprocess {
  111. my %params = @_;
  112. my $page = $params{page};
  113. my $format = $params{format};
  114. if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
  115. error(sprintf(gettext("unsupported page format %s"), $format));
  116. }
  117. my $content = $params{content};
  118. if (! defined $content) {
  119. error(gettext("comment must have content"));
  120. }
  121. $content =~ s/\\"/"/g;
  122. $content = IkiWiki::filter($page, $params{destpage}, $content);
  123. if ($config{comments_allowdirectives}) {
  124. $content = IkiWiki::preprocess($page, $params{destpage},
  125. $content);
  126. }
  127. # no need to bother with htmlize if it's just HTML
  128. $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
  129. if defined $format;
  130. IkiWiki::run_hooks(sanitize => sub {
  131. $content = shift->(
  132. page => $page,
  133. destpage => $params{destpage},
  134. content => $content,
  135. );
  136. });
  137. # set metadata, possibly overriding [[!meta]] directives from the
  138. # comment itself
  139. my $commentuser;
  140. my $commentip;
  141. my $commentauthor;
  142. my $commentauthorurl;
  143. my $commentopenid;
  144. if (defined $params{username}) {
  145. $commentuser = $params{username};
  146. my $oiduser = eval { IkiWiki::openiduser($commentuser) };
  147. if (defined $oiduser) {
  148. # looks like an OpenID
  149. $commentauthorurl = $commentuser;
  150. $commentauthor = $oiduser;
  151. $commentopenid = $commentuser;
  152. }
  153. else {
  154. $commentauthorurl = IkiWiki::cgiurl(
  155. do => 'goto',
  156. page => IkiWiki::userpage($commentuser)
  157. );
  158. $commentauthor = $commentuser;
  159. }
  160. }
  161. else {
  162. if (defined $params{ip}) {
  163. $commentip = $params{ip};
  164. }
  165. $commentauthor = gettext("Anonymous");
  166. }
  167. $commentstate{$page}{commentuser} = $commentuser;
  168. $commentstate{$page}{commentopenid} = $commentopenid;
  169. $commentstate{$page}{commentip} = $commentip;
  170. $commentstate{$page}{commentauthor} = $commentauthor;
  171. $commentstate{$page}{commentauthorurl} = $commentauthorurl;
  172. if (! defined $pagestate{$page}{meta}{author}) {
  173. $pagestate{$page}{meta}{author} = $commentauthor;
  174. }
  175. if (! defined $pagestate{$page}{meta}{authorurl}) {
  176. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  177. }
  178. if ($config{comments_allowauthor}) {
  179. if (defined $params{claimedauthor}) {
  180. $pagestate{$page}{meta}{author} = $params{claimedauthor};
  181. }
  182. if (defined $params{url}) {
  183. my $url=$params{url};
  184. eval q{use URI::Heuristic};
  185. if (! $@) {
  186. $url=URI::Heuristic::uf_uristr($url);
  187. }
  188. if (safeurl($url)) {
  189. $pagestate{$page}{meta}{authorurl} = $url;
  190. }
  191. }
  192. }
  193. else {
  194. $pagestate{$page}{meta}{author} = $commentauthor;
  195. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  196. }
  197. if (defined $params{subject}) {
  198. # decode title the same way meta does
  199. eval q{use HTML::Entities};
  200. $pagestate{$page}{meta}{title} = decode_entities($params{subject});
  201. }
  202. if ($params{page} =~ m/\/\Q$config{comments_pagename}\E\d+_/) {
  203. $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
  204. "#".page_to_id($params{page});
  205. }
  206. eval q{use Date::Parse};
  207. if (! $@) {
  208. my $time = str2time($params{date});
  209. $IkiWiki::pagectime{$page} = $time if defined $time;
  210. }
  211. return $content;
  212. }
  213. sub sessioncgi ($$) {
  214. my $cgi=shift;
  215. my $session=shift;
  216. my $do = $cgi->param('do');
  217. if ($do eq 'comment') {
  218. editcomment($cgi, $session);
  219. }
  220. elsif ($do eq 'commentmoderation') {
  221. commentmoderation($cgi, $session);
  222. }
  223. }
  224. # Mostly cargo-culted from IkiWiki::plugin::editpage
  225. sub editcomment ($$) {
  226. my $cgi=shift;
  227. my $session=shift;
  228. IkiWiki::decode_cgi_utf8($cgi);
  229. eval q{use CGI::FormBuilder};
  230. error($@) if $@;
  231. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  232. my $form = CGI::FormBuilder->new(
  233. fields => [qw{do sid page subject editcontent type author url}],
  234. charset => 'utf-8',
  235. method => 'POST',
  236. required => [qw{editcontent}],
  237. javascript => 0,
  238. params => $cgi,
  239. action => $config{cgiurl},
  240. header => 0,
  241. table => 0,
  242. template => { template('editcomment.tmpl') },
  243. );
  244. IkiWiki::decode_form_utf8($form);
  245. IkiWiki::run_hooks(formbuilder_setup => sub {
  246. shift->(title => "comment", form => $form, cgi => $cgi,
  247. session => $session, buttons => \@buttons);
  248. });
  249. IkiWiki::decode_form_utf8($form);
  250. my $type = $form->param('type');
  251. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  252. $type = IkiWiki::possibly_foolish_untaint($type);
  253. }
  254. else {
  255. $type = $config{default_pageext};
  256. }
  257. my @page_types;
  258. if (exists $IkiWiki::hooks{htmlize}) {
  259. foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
  260. push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
  261. }
  262. }
  263. @page_types=sort @page_types;
  264. $form->field(name => 'do', type => 'hidden');
  265. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  266. force => 1);
  267. $form->field(name => 'page', type => 'hidden');
  268. $form->field(name => 'subject', type => 'text', size => 72);
  269. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  270. $form->field(name => "type", value => $type, force => 1,
  271. type => 'select', options => \@page_types);
  272. $form->tmpl_param(username => $session->param('name'));
  273. if ($config{comments_allowauthor} and
  274. ! defined $session->param('name')) {
  275. $form->tmpl_param(allowauthor => 1);
  276. $form->field(name => 'author', type => 'text', size => '40');
  277. $form->field(name => 'url', type => 'text', size => '40');
  278. }
  279. else {
  280. $form->tmpl_param(allowauthor => 0);
  281. $form->field(name => 'author', type => 'hidden', value => '',
  282. force => 1);
  283. $form->field(name => 'url', type => 'hidden', value => '',
  284. force => 1);
  285. }
  286. if (! defined $session->param('name')) {
  287. # Make signinurl work and return here.
  288. $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'signin'));
  289. $session->param(postsignin => $ENV{QUERY_STRING});
  290. IkiWiki::cgi_savesession($session);
  291. }
  292. # The untaint is OK (as in editpage) because we're about to pass
  293. # it to file_pruned anyway
  294. my $page = $form->field('page');
  295. $page = IkiWiki::possibly_foolish_untaint($page);
  296. if (! defined $page || ! length $page ||
  297. IkiWiki::file_pruned($page)) {
  298. error(gettext("bad page name"));
  299. }
  300. my $baseurl = urlto($page, undef, 1);
  301. $form->title(sprintf(gettext("commenting on %s"),
  302. IkiWiki::pagetitle($page)));
  303. $form->tmpl_param('helponformattinglink',
  304. htmllink($page, $page, 'ikiwiki/formatting',
  305. noimageinline => 1,
  306. linktext => 'FormattingHelp'),
  307. allowdirectives => $config{allow_directives});
  308. if ($form->submitted eq CANCEL) {
  309. # bounce back to the page they wanted to comment on, and exit.
  310. # CANCEL need not be considered in future
  311. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  312. exit;
  313. }
  314. if (not exists $pagesources{$page}) {
  315. error(sprintf(gettext(
  316. "page '%s' doesn't exist, so you can't comment"),
  317. $page));
  318. }
  319. if (pagespec_match($page, $config{comments_closed_pagespec},
  320. location => $page)) {
  321. error(sprintf(gettext(
  322. "comments on page '%s' are closed"),
  323. $page));
  324. }
  325. # Set a flag to indicate that we're posting a comment,
  326. # so that postcomment() can tell it should match.
  327. $postcomment=1;
  328. IkiWiki::check_canedit($page, $cgi, $session);
  329. $postcomment=0;
  330. my $content = "[[!comment format=$type\n";
  331. # FIXME: handling of double quotes probably wrong?
  332. if (defined $session->param('name')) {
  333. my $username = $session->param('name');
  334. $username =~ s/"/&quot;/g;
  335. $content .= " username=\"$username\"\n";
  336. }
  337. elsif (defined $ENV{REMOTE_ADDR}) {
  338. my $ip = $ENV{REMOTE_ADDR};
  339. if ($ip =~ m/^([.0-9]+)$/) {
  340. $content .= " ip=\"$1\"\n";
  341. }
  342. }
  343. if ($config{comments_allowauthor}) {
  344. my $author = $form->field('author');
  345. if (defined $author && length $author) {
  346. $author =~ s/"/&quot;/g;
  347. $content .= " claimedauthor=\"$author\"\n";
  348. }
  349. my $url = $form->field('url');
  350. if (defined $url && length $url) {
  351. $url =~ s/"/&quot;/g;
  352. $content .= " url=\"$url\"\n";
  353. }
  354. }
  355. my $subject = $form->field('subject');
  356. if (defined $subject && length $subject) {
  357. $subject =~ s/"/&quot;/g;
  358. }
  359. else {
  360. $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
  361. }
  362. $content .= " subject=\"$subject\"\n";
  363. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  364. my $editcontent = $form->field('editcontent') || '';
  365. $editcontent =~ s/\r\n/\n/g;
  366. $editcontent =~ s/\r/\n/g;
  367. $editcontent =~ s/"/\\"/g;
  368. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  369. my $location=unique_comment_location($page, $content, $config{srcdir});
  370. # This is essentially a simplified version of editpage:
  371. # - the user does not control the page that's created, only the parent
  372. # - it's always a create operation, never an edit
  373. # - this means that conflicts should never happen
  374. # - this means that if they do, rocks fall and everyone dies
  375. if ($form->submitted eq PREVIEW) {
  376. my $preview=previewcomment($content, $location, $page, time);
  377. IkiWiki::run_hooks(format => sub {
  378. $preview = shift->(page => $page,
  379. content => $preview);
  380. });
  381. $form->tmpl_param(page_preview => $preview);
  382. }
  383. else {
  384. $form->tmpl_param(page_preview => "");
  385. }
  386. if ($form->submitted eq POST_COMMENT && $form->validate) {
  387. IkiWiki::checksessionexpiry($cgi, $session);
  388. $postcomment=1;
  389. my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
  390. subject => $form->field('subject'),
  391. $config{comments_allowauthor} ? (
  392. author => $form->field('author'),
  393. url => $form->field('url'),
  394. ) : (),
  395. page => $location,
  396. cgi => $cgi,
  397. session => $session,
  398. nonfatal => 1,
  399. );
  400. $postcomment=0;
  401. if (! $ok) {
  402. my $penddir=$config{wikistatedir}."/comments_pending";
  403. $location=unique_comment_location($page, $content, $penddir);
  404. writefile("$location._comment", $penddir, $content);
  405. IkiWiki::printheader($session);
  406. print IkiWiki::misctemplate(gettext(gettext("comment stored for moderation")),
  407. "<p>".
  408. gettext("Your comment will be posted after moderator review").
  409. "</p>");
  410. exit;
  411. }
  412. # FIXME: could probably do some sort of graceful retry
  413. # on error? Would require significant unwinding though
  414. my $file = "$location._comment";
  415. writefile($file, $config{srcdir}, $content);
  416. my $conflict;
  417. if ($config{rcs} and $config{comments_commit}) {
  418. my $message = gettext("Added a comment");
  419. if (defined $form->field('subject') &&
  420. length $form->field('subject')) {
  421. $message = sprintf(
  422. gettext("Added a comment: %s"),
  423. $form->field('subject'));
  424. }
  425. IkiWiki::rcs_add($file);
  426. IkiWiki::disable_commit_hook();
  427. $conflict = IkiWiki::rcs_commit_staged($message,
  428. $session->param('name'), $ENV{REMOTE_ADDR});
  429. IkiWiki::enable_commit_hook();
  430. IkiWiki::rcs_update();
  431. }
  432. # Now we need a refresh
  433. require IkiWiki::Render;
  434. IkiWiki::refresh();
  435. IkiWiki::saveindex();
  436. # this should never happen, unless a committer deliberately
  437. # breaks it or something
  438. error($conflict) if defined $conflict;
  439. # Jump to the new comment on the page.
  440. # The trailing question mark tries to avoid broken
  441. # caches and get the most recent version of the page.
  442. IkiWiki::redirect($cgi, urlto($page, undef, 1).
  443. "?updated#".page_to_id($location));
  444. }
  445. else {
  446. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  447. forcebaseurl => $baseurl);
  448. }
  449. exit;
  450. }
  451. sub commentmoderation ($$) {
  452. my $cgi=shift;
  453. my $session=shift;
  454. IkiWiki::needsignin($cgi, $session);
  455. if (! IkiWiki::is_admin($session->param("name"))) {
  456. error(gettext("you are not logged in as an admin"));
  457. }
  458. IkiWiki::decode_cgi_utf8($cgi);
  459. if (defined $cgi->param('sid')) {
  460. IkiWiki::checksessionexpiry($cgi, $session);
  461. my $rejectalldefer=$cgi->param('rejectalldefer');
  462. my %vars=$cgi->Vars;
  463. my $added=0;
  464. foreach my $id (keys %vars) {
  465. if ($id =~ /(.*)\Q._comment\E$/) {
  466. my $action=$cgi->param($id);
  467. next if $action eq 'Defer' && ! $rejectalldefer;
  468. # Make sure that the id is of a legal
  469. # pending comment before untainting.
  470. my ($f)= $id =~ /$config{wiki_file_regexp}/;
  471. if (! defined $f || ! length $f ||
  472. IkiWiki::file_pruned($f)) {
  473. error("illegal file");
  474. }
  475. my $page=IkiWiki::possibly_foolish_untaint(IkiWiki::dirname($1));
  476. my $file="$config{wikistatedir}/comments_pending/".
  477. IkiWiki::possibly_foolish_untaint($id);
  478. if ($action eq 'Accept') {
  479. my $content=eval { readfile($file) };
  480. next if $@; # file vanished since form was displayed
  481. my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
  482. writefile($dest, $config{srcdir}, $content);
  483. if ($config{rcs} and $config{comments_commit}) {
  484. IkiWiki::rcs_add($dest);
  485. }
  486. $added++;
  487. }
  488. # This removes empty subdirs, so the
  489. # .ikiwiki/comments_pending dir will
  490. # go away when all are moderated.
  491. require IkiWiki::Render;
  492. IkiWiki::prune($file);
  493. }
  494. }
  495. if ($added) {
  496. my $conflict;
  497. if ($config{rcs} and $config{comments_commit}) {
  498. my $message = gettext("Comment moderation");
  499. IkiWiki::disable_commit_hook();
  500. $conflict=IkiWiki::rcs_commit_staged($message,
  501. $session->param('name'), $ENV{REMOTE_ADDR});
  502. IkiWiki::enable_commit_hook();
  503. IkiWiki::rcs_update();
  504. }
  505. # Now we need a refresh
  506. require IkiWiki::Render;
  507. IkiWiki::refresh();
  508. IkiWiki::saveindex();
  509. error($conflict) if defined $conflict;
  510. }
  511. }
  512. my @comments=map {
  513. my ($id, $ctime)=@{$_};
  514. my $file="$config{wikistatedir}/comments_pending/$id";
  515. my $content=readfile($file);
  516. my $preview=previewcomment($content, $id,
  517. IkiWiki::dirname($_), $ctime);
  518. {
  519. id => $id,
  520. view => $preview,
  521. }
  522. } sort { $b->[1] <=> $a->[1] } comments_pending();
  523. my $template=template("commentmoderation.tmpl");
  524. $template->param(
  525. sid => $session->id,
  526. comments => \@comments,
  527. );
  528. IkiWiki::printheader($session);
  529. my $out=$template->output;
  530. IkiWiki::run_hooks(format => sub {
  531. $out = shift->(page => "", content => $out);
  532. });
  533. print IkiWiki::misctemplate(gettext("comment moderation"), $out);
  534. exit;
  535. }
  536. sub formbuilder_setup (@) {
  537. my %params=@_;
  538. my $form=$params{form};
  539. if ($form->title eq "preferences" &&
  540. IkiWiki::is_admin($params{session}->param("name"))) {
  541. push @{$params{buttons}}, "Comment Moderation";
  542. if ($form->submitted && $form->submitted eq "Comment Moderation") {
  543. commentmoderation($params{cgi}, $params{session});
  544. }
  545. }
  546. }
  547. sub comments_pending () {
  548. my $dir="$config{wikistatedir}/comments_pending/";
  549. return unless -d $dir;
  550. my @ret;
  551. eval q{use File::Find};
  552. error($@) if $@;
  553. find({
  554. no_chdir => 1,
  555. wanted => sub {
  556. my $file=decode_utf8($_);
  557. $file=~s/^\Q$dir\E\/?//;
  558. return if ! length $file || IkiWiki::file_pruned($file)
  559. || -l $_ || -d _ || $file !~ /\Q._comment\E$/;
  560. my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
  561. if (defined $f) {
  562. my $ctime=(stat($_))[10];
  563. push @ret, [$f, $ctime];
  564. }
  565. }
  566. }, $dir);
  567. return @ret;
  568. }
  569. sub previewcomment ($$$) {
  570. my $content=shift;
  571. my $location=shift;
  572. my $page=shift;
  573. my $time=shift;
  574. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  575. IkiWiki::linkify($location, $page,
  576. IkiWiki::preprocess($location, $page,
  577. IkiWiki::filter($location, $page, $content), 0, 1)));
  578. my $template = template("comment.tmpl");
  579. $template->param(content => $preview);
  580. $template->param(ctime => displaytime($time, undef, 1));
  581. $template->param(html5 => $config{html5});
  582. IkiWiki::run_hooks(pagetemplate => sub {
  583. shift->(page => $location,
  584. destpage => $page,
  585. template => $template);
  586. });
  587. $template->param(have_actions => 0);
  588. return $template->output;
  589. }
  590. sub commentsshown ($) {
  591. my $page=shift;
  592. return ! pagespec_match($page, "internal(*/$config{comments_pagename}*)",
  593. location => $page) &&
  594. pagespec_match($page, $config{comments_pagespec},
  595. location => $page);
  596. }
  597. sub commentsopen ($) {
  598. my $page = shift;
  599. return length $config{cgiurl} > 0 &&
  600. (! length $config{comments_closed_pagespec} ||
  601. ! pagespec_match($page, $config{comments_closed_pagespec},
  602. location => $page));
  603. }
  604. sub pagetemplate (@) {
  605. my %params = @_;
  606. my $page = $params{page};
  607. my $template = $params{template};
  608. my $shown = ($template->query(name => 'commentslink') ||
  609. $template->query(name => 'commentsurl') ||
  610. $template->query(name => 'atomcommentsurl') ||
  611. $template->query(name => 'comments')) &&
  612. commentsshown($page);
  613. if ($template->query(name => 'comments')) {
  614. my $comments = undef;
  615. if ($shown) {
  616. $comments = IkiWiki::preprocess_inline(
  617. pages => "internal($page/$config{comments_pagename}*)",
  618. template => 'comment',
  619. show => 0,
  620. reverse => 'yes',
  621. page => $page,
  622. destpage => $params{destpage},
  623. feedfile => 'comments',
  624. emptyfeeds => 'no',
  625. );
  626. }
  627. if (defined $comments && length $comments) {
  628. $template->param(comments => $comments);
  629. }
  630. if ($shown && commentsopen($page)) {
  631. $template->param(addcommenturl => addcommenturl($page));
  632. }
  633. }
  634. if ($shown) {
  635. if ($template->query(name => 'commentsurl')) {
  636. $template->param(commentsurl =>
  637. urlto($page, undef, 1).'#comments');
  638. }
  639. if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
  640. # This will 404 until there are some comments, but I
  641. # think that's probably OK...
  642. $template->param(atomcommentsurl =>
  643. urlto($page, undef, 1).'comments.atom');
  644. }
  645. if ($template->query(name => 'commentslink')) {
  646. my $num=num_comments($page, $config{srcdir});
  647. my $link;
  648. if ($num > 0) {
  649. $link = htmllink($page, $params{destpage}, $page,
  650. linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
  651. anchor => "comments",
  652. noimageinline => 1
  653. );
  654. }
  655. elsif (commentsopen($page)) {
  656. $link = "<a href=\"".addcommenturl($page)."\">".
  657. #translators: Here "Comment" is a verb;
  658. #translators: the user clicks on it to
  659. #translators: post a comment.
  660. gettext("Comment").
  661. "</a>";
  662. }
  663. $template->param(commentslink => $link)
  664. if defined $link;
  665. }
  666. }
  667. # everything below this point is only relevant to the comments
  668. # themselves
  669. if (!exists $commentstate{$page}) {
  670. return;
  671. }
  672. if ($template->query(name => 'commentid')) {
  673. $template->param(commentid => page_to_id($page));
  674. }
  675. if ($template->query(name => 'commentuser')) {
  676. $template->param(commentuser =>
  677. $commentstate{$page}{commentuser});
  678. }
  679. if ($template->query(name => 'commentopenid')) {
  680. $template->param(commentopenid =>
  681. $commentstate{$page}{commentopenid});
  682. }
  683. if ($template->query(name => 'commentip')) {
  684. $template->param(commentip =>
  685. $commentstate{$page}{commentip});
  686. }
  687. if ($template->query(name => 'commentauthor')) {
  688. $template->param(commentauthor =>
  689. $commentstate{$page}{commentauthor});
  690. }
  691. if ($template->query(name => 'commentauthorurl')) {
  692. $template->param(commentauthorurl =>
  693. $commentstate{$page}{commentauthorurl});
  694. }
  695. if ($template->query(name => 'removeurl') &&
  696. IkiWiki::Plugin::remove->can("check_canremove") &&
  697. length $config{cgiurl}) {
  698. $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
  699. page => $page));
  700. $template->param(have_actions => 1);
  701. }
  702. }
  703. sub addcommenturl ($) {
  704. my $page=shift;
  705. return IkiWiki::cgiurl(do => 'comment', page => $page);
  706. }
  707. sub num_comments ($$) {
  708. my $page=shift;
  709. my $dir=shift;
  710. my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
  711. return @comments;
  712. }
  713. sub unique_comment_location ($$$) {
  714. my $page=shift;
  715. eval q{use Digest::MD5 'md5_hex'};
  716. error($@) if $@;
  717. my $content_md5=md5_hex(Encode::encode_utf8(shift));
  718. my $dir=shift;
  719. my $location;
  720. my $i = num_comments($page, $dir);
  721. do {
  722. $i++;
  723. $location = "$page/$config{comments_pagename}${i}_${content_md5}";
  724. } while (-e "$dir/$location._comment");
  725. return $location;
  726. }
  727. sub page_to_id ($) {
  728. # Converts a comment page name into a unique, legal html id
  729. # attribute value, that can be used as an anchor to link to the
  730. # comment.
  731. my $page=shift;
  732. eval q{use Digest::MD5 'md5_hex'};
  733. error($@) if $@;
  734. return "comment-".md5_hex(Encode::encode_utf8(($page)));
  735. }
  736. package IkiWiki::PageSpec;
  737. sub match_postcomment ($$;@) {
  738. my $page = shift;
  739. my $glob = shift;
  740. if (! $postcomment) {
  741. return IkiWiki::FailReason->new("not posting a comment");
  742. }
  743. return match_glob($page, $glob);
  744. }
  745. 1