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