summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: ee53dbc91e4e0b6768e36d23705da64a02cf0e1b (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. "#".$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)."?updated#$location");
  427. }
  428. else {
  429. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  430. forcebaseurl => $baseurl);
  431. }
  432. exit;
  433. }
  434. sub commentmoderation ($$) {
  435. my $cgi=shift;
  436. my $session=shift;
  437. IkiWiki::needsignin($cgi, $session);
  438. if (! IkiWiki::is_admin($session->param("name"))) {
  439. error(gettext("you are not logged in as an admin"));
  440. }
  441. IkiWiki::decode_cgi_utf8($cgi);
  442. if (defined $cgi->param('sid')) {
  443. IkiWiki::checksessionexpiry($cgi, $session);
  444. my $rejectalldefer=$cgi->param('rejectalldefer');
  445. my %vars=$cgi->Vars;
  446. my $added=0;
  447. foreach my $id (keys %vars) {
  448. if ($id =~ /(.*)\Q._comment\E$/) {
  449. my $action=$cgi->param($id);
  450. next if $action eq 'Defer' && ! $rejectalldefer;
  451. # Make sure that the id is of a legal
  452. # pending comment before untainting.
  453. my ($f)= $id =~ /$config{wiki_file_regexp}/;
  454. if (! defined $f || ! length $f ||
  455. IkiWiki::file_pruned($f, $config{srcdir})) {
  456. error("illegal file");
  457. }
  458. my $page=IkiWiki::possibly_foolish_untaint(IkiWiki::dirname($1));
  459. my $file="$config{wikistatedir}/comments_pending/".
  460. IkiWiki::possibly_foolish_untaint($id);
  461. if ($action eq 'Accept') {
  462. my $content=eval { readfile($file) };
  463. next if $@; # file vanished since form was displayed
  464. my $dest=unique_comment_location($page, $config{srcdir})."._comment";
  465. writefile($dest, $config{srcdir}, $content);
  466. if ($config{rcs} and $config{comments_commit}) {
  467. IkiWiki::rcs_add($dest);
  468. }
  469. $added++;
  470. }
  471. # This removes empty subdirs, so the
  472. # .ikiwiki/comments_pending dir will
  473. # go away when all are moderated.
  474. require IkiWiki::Render;
  475. IkiWiki::prune($file);
  476. }
  477. }
  478. if ($added) {
  479. my $conflict;
  480. if ($config{rcs} and $config{comments_commit}) {
  481. my $message = gettext("Comment moderation");
  482. IkiWiki::disable_commit_hook();
  483. $conflict=IkiWiki::rcs_commit_staged($message,
  484. $session->param('name'), $ENV{REMOTE_ADDR});
  485. IkiWiki::enable_commit_hook();
  486. IkiWiki::rcs_update();
  487. }
  488. # Now we need a refresh
  489. require IkiWiki::Render;
  490. IkiWiki::refresh();
  491. IkiWiki::saveindex();
  492. error($conflict) if defined $conflict;
  493. }
  494. }
  495. my @comments=map {
  496. my ($id, $ctime)=@{$_};
  497. my $file="$config{wikistatedir}/comments_pending/$id";
  498. my $content=readfile($file);
  499. my $preview=previewcomment($content, $id,
  500. IkiWiki::dirname($_), $ctime);
  501. {
  502. id => $id,
  503. view => $preview,
  504. }
  505. } sort { $b->[1] <=> $a->[1] } comments_pending();
  506. my $template=template("commentmoderation.tmpl");
  507. $template->param(
  508. sid => $session->id,
  509. comments => \@comments,
  510. );
  511. IkiWiki::printheader($session);
  512. my $out=$template->output;
  513. IkiWiki::run_hooks(format => sub {
  514. $out = shift->(page => "", content => $out);
  515. });
  516. print IkiWiki::misctemplate(gettext("comment moderation"), $out);
  517. exit;
  518. }
  519. sub formbuilder_setup (@) {
  520. my %params=@_;
  521. my $form=$params{form};
  522. if ($form->title eq "preferences" &&
  523. IkiWiki::is_admin($params{session}->param("name"))) {
  524. push @{$params{buttons}}, "Comment Moderation";
  525. if ($form->submitted && $form->submitted eq "Comment Moderation") {
  526. commentmoderation($params{cgi}, $params{session});
  527. }
  528. }
  529. }
  530. sub comments_pending () {
  531. my $dir="$config{wikistatedir}/comments_pending/";
  532. return unless -d $dir;
  533. my @ret;
  534. eval q{use File::Find};
  535. error($@) if $@;
  536. find({
  537. no_chdir => 1,
  538. wanted => sub {
  539. $_=decode_utf8($_);
  540. if (IkiWiki::file_pruned($_, $dir)) {
  541. $File::Find::prune=1;
  542. }
  543. elsif (! -l $_ && ! -d _) {
  544. $File::Find::prune=0;
  545. my ($f)=/$config{wiki_file_regexp}/; # untaint
  546. if (defined $f && $f =~ /\Q._comment\E$/) {
  547. my $ctime=(stat($f))[10];
  548. $f=~s/^\Q$dir\E\/?//;
  549. push @ret, [$f, $ctime];
  550. }
  551. }
  552. }
  553. }, $dir);
  554. return @ret;
  555. }
  556. sub previewcomment ($$$) {
  557. my $content=shift;
  558. my $location=shift;
  559. my $page=shift;
  560. my $time=shift;
  561. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  562. IkiWiki::linkify($location, $page,
  563. IkiWiki::preprocess($location, $page,
  564. IkiWiki::filter($location, $page, $content), 0, 1)));
  565. my $template = template("comment.tmpl");
  566. $template->param(content => $preview);
  567. $template->param(ctime => displaytime($time));
  568. IkiWiki::run_hooks(pagetemplate => sub {
  569. shift->(page => $location,
  570. destpage => $page,
  571. template => $template);
  572. });
  573. $template->param(have_actions => 0);
  574. return $template->output;
  575. }
  576. sub commentsshown ($) {
  577. my $page=shift;
  578. return ! pagespec_match($page, "*/$config{comments_pagename}*",
  579. location => $page) &&
  580. pagespec_match($page, $config{comments_pagespec},
  581. location => $page);
  582. }
  583. sub commentsopen ($) {
  584. my $page = shift;
  585. return length $config{cgiurl} > 0 &&
  586. (! length $config{comments_closed_pagespec} ||
  587. ! pagespec_match($page, $config{comments_closed_pagespec},
  588. location => $page));
  589. }
  590. sub pagetemplate (@) {
  591. my %params = @_;
  592. my $page = $params{page};
  593. my $template = $params{template};
  594. my $shown = ($template->query(name => 'commentslink') ||
  595. $template->query(name => 'commentsurl') ||
  596. $template->query(name => 'atomcommentsurl') ||
  597. $template->query(name => 'comments')) &&
  598. commentsshown($page);
  599. if ($template->query(name => 'comments')) {
  600. my $comments = undef;
  601. if ($shown) {
  602. $comments = IkiWiki::preprocess_inline(
  603. pages => "internal($page/$config{comments_pagename}*)",
  604. template => 'comment',
  605. show => 0,
  606. reverse => 'yes',
  607. page => $page,
  608. destpage => $params{destpage},
  609. feedfile => 'comments',
  610. emptyfeeds => 'no',
  611. );
  612. }
  613. if (defined $comments && length $comments) {
  614. $template->param(comments => $comments);
  615. }
  616. if ($shown && commentsopen($page)) {
  617. my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
  618. page => $page);
  619. $template->param(addcommenturl => $addcommenturl);
  620. }
  621. }
  622. if ($template->query(name => 'commentsurl')) {
  623. if ($shown) {
  624. $template->param(commentsurl =>
  625. urlto($page, undef, 1).'#comments');
  626. }
  627. }
  628. if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
  629. if ($shown) {
  630. # This will 404 until there are some comments, but I
  631. # think that's probably OK...
  632. $template->param(atomcommentsurl =>
  633. urlto($page, undef, 1).'comments.atom');
  634. }
  635. }
  636. if ($template->query(name => 'commentslink')) {
  637. # XXX Would be nice to say how many comments there are in
  638. # the link. But, to update the number, blog pages
  639. # would have to update whenever comments of any inlines
  640. # page are added, which is not currently done.
  641. if ($shown) {
  642. $template->param(commentslink =>
  643. htmllink($page, $params{destpage}, $page,
  644. linktext => gettext("Comments"),
  645. anchor => "comments",
  646. noimageinline => 1));
  647. }
  648. }
  649. # everything below this point is only relevant to the comments
  650. # themselves
  651. if (!exists $commentstate{$page}) {
  652. return;
  653. }
  654. if ($template->query(name => 'commentuser')) {
  655. $template->param(commentuser =>
  656. $commentstate{$page}{commentuser});
  657. }
  658. if ($template->query(name => 'commentopenid')) {
  659. $template->param(commentopenid =>
  660. $commentstate{$page}{commentopenid});
  661. }
  662. if ($template->query(name => 'commentip')) {
  663. $template->param(commentip =>
  664. $commentstate{$page}{commentip});
  665. }
  666. if ($template->query(name => 'commentauthor')) {
  667. $template->param(commentauthor =>
  668. $commentstate{$page}{commentauthor});
  669. }
  670. if ($template->query(name => 'commentauthorurl')) {
  671. $template->param(commentauthorurl =>
  672. $commentstate{$page}{commentauthorurl});
  673. }
  674. if ($template->query(name => 'removeurl') &&
  675. IkiWiki::Plugin::remove->can("check_canremove") &&
  676. length $config{cgiurl}) {
  677. $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
  678. page => $page));
  679. $template->param(have_actions => 1);
  680. }
  681. }
  682. sub unique_comment_location ($) {
  683. my $page=shift;
  684. my $dir=shift;
  685. my $location;
  686. my $i = 0;
  687. do {
  688. $i++;
  689. $location = "$page/$config{comments_pagename}$i";
  690. } while (-e "$dir/$location._comment");
  691. return $location;
  692. }
  693. package IkiWiki::PageSpec;
  694. sub match_postcomment ($$;@) {
  695. my $page = shift;
  696. my $glob = shift;
  697. if (! $postcomment) {
  698. return IkiWiki::FailReason->new("not posting a comment");
  699. }
  700. return match_glob($page, $glob);
  701. }
  702. 1