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