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