summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 0aa4759d807c3060eed580d58f862c6d22ab9d70 (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. foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
  256. push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
  257. }
  258. }
  259. @page_types=sort @page_types;
  260. $form->field(name => 'do', type => 'hidden');
  261. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  262. force => 1);
  263. $form->field(name => 'page', type => 'hidden');
  264. $form->field(name => 'subject', type => 'text', size => 72);
  265. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  266. $form->field(name => "type", value => $type, force => 1,
  267. type => 'select', options => \@page_types);
  268. $form->tmpl_param(username => $session->param('name'));
  269. if ($config{comments_allowauthor} and
  270. ! defined $session->param('name')) {
  271. $form->tmpl_param(allowauthor => 1);
  272. $form->field(name => 'author', type => 'text', size => '40');
  273. $form->field(name => 'url', type => 'text', size => '40');
  274. }
  275. else {
  276. $form->tmpl_param(allowauthor => 0);
  277. $form->field(name => 'author', type => 'hidden', value => '',
  278. force => 1);
  279. $form->field(name => 'url', type => 'hidden', value => '',
  280. force => 1);
  281. }
  282. if (! defined $session->param('name')) {
  283. # Make signinurl work and return here.
  284. $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'signin'));
  285. $session->param(postsignin => $ENV{QUERY_STRING});
  286. IkiWiki::cgi_savesession($session);
  287. }
  288. # The untaint is OK (as in editpage) because we're about to pass
  289. # it to file_pruned anyway
  290. my $page = $form->field('page');
  291. $page = IkiWiki::possibly_foolish_untaint($page);
  292. if (! defined $page || ! length $page ||
  293. IkiWiki::file_pruned($page, $config{srcdir})) {
  294. error(gettext("bad page name"));
  295. }
  296. my $baseurl = urlto($page, undef, 1);
  297. $form->title(sprintf(gettext("commenting on %s"),
  298. IkiWiki::pagetitle($page)));
  299. $form->tmpl_param('helponformattinglink',
  300. htmllink($page, $page, 'ikiwiki/formatting',
  301. noimageinline => 1,
  302. linktext => 'FormattingHelp'),
  303. allowdirectives => $config{allow_directives});
  304. if ($form->submitted eq CANCEL) {
  305. # bounce back to the page they wanted to comment on, and exit.
  306. # CANCEL need not be considered in future
  307. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  308. exit;
  309. }
  310. if (not exists $pagesources{$page}) {
  311. error(sprintf(gettext(
  312. "page '%s' doesn't exist, so you can't comment"),
  313. $page));
  314. }
  315. if (pagespec_match($page, $config{comments_closed_pagespec},
  316. location => $page)) {
  317. error(sprintf(gettext(
  318. "comments on page '%s' are closed"),
  319. $page));
  320. }
  321. # Set a flag to indicate that we're posting a comment,
  322. # so that postcomment() can tell it should match.
  323. $postcomment=1;
  324. IkiWiki::check_canedit($page, $cgi, $session);
  325. $postcomment=0;
  326. my $location=unique_comment_location($page, $config{srcdir});
  327. my $content = "[[!_comment format=$type\n";
  328. # FIXME: handling of double quotes probably wrong?
  329. if (defined $session->param('name')) {
  330. my $username = $session->param('name');
  331. $username =~ s/"/&quot;/g;
  332. $content .= " username=\"$username\"\n";
  333. }
  334. elsif (defined $ENV{REMOTE_ADDR}) {
  335. my $ip = $ENV{REMOTE_ADDR};
  336. if ($ip =~ m/^([.0-9]+)$/) {
  337. $content .= " ip=\"$1\"\n";
  338. }
  339. }
  340. if ($config{comments_allowauthor}) {
  341. my $author = $form->field('author');
  342. if (defined $author && length $author) {
  343. $author =~ s/"/&quot;/g;
  344. $content .= " claimedauthor=\"$author\"\n";
  345. }
  346. my $url = $form->field('url');
  347. if (defined $url && length $url) {
  348. $url =~ s/"/&quot;/g;
  349. $content .= " url=\"$url\"\n";
  350. }
  351. }
  352. my $subject = $form->field('subject');
  353. if (defined $subject && length $subject) {
  354. $subject =~ s/"/&quot;/g;
  355. $content .= " subject=\"$subject\"\n";
  356. }
  357. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  358. my $editcontent = $form->field('editcontent') || '';
  359. $editcontent =~ s/\r\n/\n/g;
  360. $editcontent =~ s/\r/\n/g;
  361. $editcontent =~ s/"/\\"/g;
  362. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  363. # This is essentially a simplified version of editpage:
  364. # - the user does not control the page that's created, only the parent
  365. # - it's always a create operation, never an edit
  366. # - this means that conflicts should never happen
  367. # - this means that if they do, rocks fall and everyone dies
  368. if ($form->submitted eq PREVIEW) {
  369. my $preview=previewcomment($content, $location, $page, time);
  370. IkiWiki::run_hooks(format => sub {
  371. $preview = shift->(page => $page,
  372. content => $preview);
  373. });
  374. $form->tmpl_param(page_preview => $preview);
  375. }
  376. else {
  377. $form->tmpl_param(page_preview => "");
  378. }
  379. if ($form->submitted eq POST_COMMENT && $form->validate) {
  380. IkiWiki::checksessionexpiry($cgi, $session);
  381. $postcomment=1;
  382. my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
  383. subject => $form->field('subject'),
  384. $config{comments_allowauthor} ? (
  385. author => $form->field('author'),
  386. url => $form->field('url'),
  387. ) : (),
  388. page => $location,
  389. cgi => $cgi,
  390. session => $session,
  391. nonfatal => 1,
  392. );
  393. $postcomment=0;
  394. if (! $ok) {
  395. my $penddir=$config{wikistatedir}."/comments_pending";
  396. $location=unique_comment_location($page, $penddir);
  397. writefile("$location._comment", $penddir, $content);
  398. IkiWiki::printheader($session);
  399. print IkiWiki::misctemplate(gettext(gettext("comment stored for moderation")),
  400. "<p>".
  401. gettext("Your comment will be posted after moderator review").
  402. "</p>");
  403. exit;
  404. }
  405. # FIXME: could probably do some sort of graceful retry
  406. # on error? Would require significant unwinding though
  407. my $file = "$location._comment";
  408. writefile($file, $config{srcdir}, $content);
  409. my $conflict;
  410. if ($config{rcs} and $config{comments_commit}) {
  411. my $message = gettext("Added a comment");
  412. if (defined $form->field('subject') &&
  413. length $form->field('subject')) {
  414. $message = sprintf(
  415. gettext("Added a comment: %s"),
  416. $form->field('subject'));
  417. }
  418. IkiWiki::rcs_add($file);
  419. IkiWiki::disable_commit_hook();
  420. $conflict = IkiWiki::rcs_commit_staged($message,
  421. $session->param('name'), $ENV{REMOTE_ADDR});
  422. IkiWiki::enable_commit_hook();
  423. IkiWiki::rcs_update();
  424. }
  425. # Now we need a refresh
  426. require IkiWiki::Render;
  427. IkiWiki::refresh();
  428. IkiWiki::saveindex();
  429. # this should never happen, unless a committer deliberately
  430. # breaks it or something
  431. error($conflict) if defined $conflict;
  432. # Jump to the new comment on the page.
  433. # The trailing question mark tries to avoid broken
  434. # caches and get the most recent version of the page.
  435. IkiWiki::redirect($cgi, urlto($page, undef, 1).
  436. "?updated#".page_to_id($location));
  437. }
  438. else {
  439. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  440. forcebaseurl => $baseurl);
  441. }
  442. exit;
  443. }
  444. sub commentmoderation ($$) {
  445. my $cgi=shift;
  446. my $session=shift;
  447. IkiWiki::needsignin($cgi, $session);
  448. if (! IkiWiki::is_admin($session->param("name"))) {
  449. error(gettext("you are not logged in as an admin"));
  450. }
  451. IkiWiki::decode_cgi_utf8($cgi);
  452. if (defined $cgi->param('sid')) {
  453. IkiWiki::checksessionexpiry($cgi, $session);
  454. my $rejectalldefer=$cgi->param('rejectalldefer');
  455. my %vars=$cgi->Vars;
  456. my $added=0;
  457. foreach my $id (keys %vars) {
  458. if ($id =~ /(.*)\Q._comment\E$/) {
  459. my $action=$cgi->param($id);
  460. next if $action eq 'Defer' && ! $rejectalldefer;
  461. # Make sure that the id is of a legal
  462. # pending comment before untainting.
  463. my ($f)= $id =~ /$config{wiki_file_regexp}/;
  464. if (! defined $f || ! length $f ||
  465. IkiWiki::file_pruned($f, $config{srcdir})) {
  466. error("illegal file");
  467. }
  468. my $page=IkiWiki::possibly_foolish_untaint(IkiWiki::dirname($1));
  469. my $file="$config{wikistatedir}/comments_pending/".
  470. IkiWiki::possibly_foolish_untaint($id);
  471. if ($action eq 'Accept') {
  472. my $content=eval { readfile($file) };
  473. next if $@; # file vanished since form was displayed
  474. my $dest=unique_comment_location($page, $config{srcdir})."._comment";
  475. writefile($dest, $config{srcdir}, $content);
  476. if ($config{rcs} and $config{comments_commit}) {
  477. IkiWiki::rcs_add($dest);
  478. }
  479. $added++;
  480. }
  481. # This removes empty subdirs, so the
  482. # .ikiwiki/comments_pending dir will
  483. # go away when all are moderated.
  484. require IkiWiki::Render;
  485. IkiWiki::prune($file);
  486. }
  487. }
  488. if ($added) {
  489. my $conflict;
  490. if ($config{rcs} and $config{comments_commit}) {
  491. my $message = gettext("Comment moderation");
  492. IkiWiki::disable_commit_hook();
  493. $conflict=IkiWiki::rcs_commit_staged($message,
  494. $session->param('name'), $ENV{REMOTE_ADDR});
  495. IkiWiki::enable_commit_hook();
  496. IkiWiki::rcs_update();
  497. }
  498. # Now we need a refresh
  499. require IkiWiki::Render;
  500. IkiWiki::refresh();
  501. IkiWiki::saveindex();
  502. error($conflict) if defined $conflict;
  503. }
  504. }
  505. my @comments=map {
  506. my ($id, $ctime)=@{$_};
  507. my $file="$config{wikistatedir}/comments_pending/$id";
  508. my $content=readfile($file);
  509. my $preview=previewcomment($content, $id,
  510. IkiWiki::dirname($_), $ctime);
  511. {
  512. id => $id,
  513. view => $preview,
  514. }
  515. } sort { $b->[1] <=> $a->[1] } comments_pending();
  516. my $template=template("commentmoderation.tmpl");
  517. $template->param(
  518. sid => $session->id,
  519. comments => \@comments,
  520. );
  521. IkiWiki::printheader($session);
  522. my $out=$template->output;
  523. IkiWiki::run_hooks(format => sub {
  524. $out = shift->(page => "", content => $out);
  525. });
  526. print IkiWiki::misctemplate(gettext("comment moderation"), $out);
  527. exit;
  528. }
  529. sub formbuilder_setup (@) {
  530. my %params=@_;
  531. my $form=$params{form};
  532. if ($form->title eq "preferences" &&
  533. IkiWiki::is_admin($params{session}->param("name"))) {
  534. push @{$params{buttons}}, "Comment Moderation";
  535. if ($form->submitted && $form->submitted eq "Comment Moderation") {
  536. commentmoderation($params{cgi}, $params{session});
  537. }
  538. }
  539. }
  540. sub comments_pending () {
  541. my $dir="$config{wikistatedir}/comments_pending/";
  542. return unless -d $dir;
  543. my @ret;
  544. eval q{use File::Find};
  545. error($@) if $@;
  546. find({
  547. no_chdir => 1,
  548. wanted => sub {
  549. $_=decode_utf8($_);
  550. if (IkiWiki::file_pruned($_, $dir)) {
  551. $File::Find::prune=1;
  552. }
  553. elsif (! -l $_ && ! -d _) {
  554. $File::Find::prune=0;
  555. my ($f)=/$config{wiki_file_regexp}/; # untaint
  556. if (defined $f && $f =~ /\Q._comment\E$/) {
  557. my $ctime=(stat($f))[10];
  558. $f=~s/^\Q$dir\E\/?//;
  559. push @ret, [$f, $ctime];
  560. }
  561. }
  562. }
  563. }, $dir);
  564. return @ret;
  565. }
  566. sub previewcomment ($$$) {
  567. my $content=shift;
  568. my $location=shift;
  569. my $page=shift;
  570. my $time=shift;
  571. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  572. IkiWiki::linkify($location, $page,
  573. IkiWiki::preprocess($location, $page,
  574. IkiWiki::filter($location, $page, $content), 0, 1)));
  575. my $template = template("comment.tmpl");
  576. $template->param(content => $preview);
  577. $template->param(ctime => displaytime($time));
  578. IkiWiki::run_hooks(pagetemplate => sub {
  579. shift->(page => $location,
  580. destpage => $page,
  581. template => $template);
  582. });
  583. $template->param(have_actions => 0);
  584. return $template->output;
  585. }
  586. sub commentsshown ($) {
  587. my $page=shift;
  588. return ! pagespec_match($page, "internal(*/$config{comments_pagename}*)",
  589. location => $page) &&
  590. pagespec_match($page, $config{comments_pagespec},
  591. location => $page);
  592. }
  593. sub commentsopen ($) {
  594. my $page = shift;
  595. return length $config{cgiurl} > 0 &&
  596. (! length $config{comments_closed_pagespec} ||
  597. ! pagespec_match($page, $config{comments_closed_pagespec},
  598. location => $page));
  599. }
  600. sub pagetemplate (@) {
  601. my %params = @_;
  602. my $page = $params{page};
  603. my $template = $params{template};
  604. my $shown = ($template->query(name => 'commentslink') ||
  605. $template->query(name => 'commentsurl') ||
  606. $template->query(name => 'atomcommentsurl') ||
  607. $template->query(name => 'comments')) &&
  608. commentsshown($page);
  609. if ($template->query(name => 'comments')) {
  610. my $comments = undef;
  611. if ($shown) {
  612. $comments = IkiWiki::preprocess_inline(
  613. pages => "internal($page/$config{comments_pagename}*)",
  614. template => 'comment',
  615. show => 0,
  616. reverse => 'yes',
  617. page => $page,
  618. destpage => $params{destpage},
  619. feedfile => 'comments',
  620. emptyfeeds => 'no',
  621. );
  622. }
  623. if (defined $comments && length $comments) {
  624. $template->param(comments => $comments);
  625. }
  626. if ($shown && commentsopen($page)) {
  627. my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
  628. page => $page);
  629. $template->param(addcommenturl => $addcommenturl);
  630. }
  631. }
  632. if ($template->query(name => 'commentsurl')) {
  633. if ($shown) {
  634. $template->param(commentsurl =>
  635. urlto($page, undef, 1).'#comments');
  636. }
  637. }
  638. if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
  639. if ($shown) {
  640. # This will 404 until there are some comments, but I
  641. # think that's probably OK...
  642. $template->param(atomcommentsurl =>
  643. urlto($page, undef, 1).'comments.atom');
  644. }
  645. }
  646. if ($template->query(name => 'commentslink')) {
  647. # XXX Would be nice to say how many comments there are in
  648. # the link. But, to update the number, blog pages
  649. # would have to update whenever comments of any inlines
  650. # page are added, which is not currently done.
  651. if ($shown) {
  652. $template->param(commentslink =>
  653. htmllink($page, $params{destpage}, $page,
  654. linktext => gettext("Comments"),
  655. anchor => "comments",
  656. noimageinline => 1));
  657. }
  658. }
  659. # everything below this point is only relevant to the comments
  660. # themselves
  661. if (!exists $commentstate{$page}) {
  662. return;
  663. }
  664. if ($template->query(name => 'commentid')) {
  665. $template->param(commentid => page_to_id($page));
  666. }
  667. if ($template->query(name => 'commentuser')) {
  668. $template->param(commentuser =>
  669. $commentstate{$page}{commentuser});
  670. }
  671. if ($template->query(name => 'commentopenid')) {
  672. $template->param(commentopenid =>
  673. $commentstate{$page}{commentopenid});
  674. }
  675. if ($template->query(name => 'commentip')) {
  676. $template->param(commentip =>
  677. $commentstate{$page}{commentip});
  678. }
  679. if ($template->query(name => 'commentauthor')) {
  680. $template->param(commentauthor =>
  681. $commentstate{$page}{commentauthor});
  682. }
  683. if ($template->query(name => 'commentauthorurl')) {
  684. $template->param(commentauthorurl =>
  685. $commentstate{$page}{commentauthorurl});
  686. }
  687. if ($template->query(name => 'removeurl') &&
  688. IkiWiki::Plugin::remove->can("check_canremove") &&
  689. length $config{cgiurl}) {
  690. $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
  691. page => $page));
  692. $template->param(have_actions => 1);
  693. }
  694. }
  695. sub unique_comment_location ($) {
  696. my $page=shift;
  697. my $dir=shift;
  698. my $location;
  699. my $i = 0;
  700. do {
  701. $i++;
  702. $location = "$page/$config{comments_pagename}$i";
  703. } while (-e "$dir/$location._comment");
  704. return $location;
  705. }
  706. sub page_to_id ($) {
  707. # Converts a comment page name into a unique, legal html id
  708. # addtibute value, that can be used as an anchor to link to the
  709. # comment.
  710. my $page=shift;
  711. eval q{use Digest::MD5 'md5_hex'};
  712. error($@) if $@;
  713. return "comment-".md5_hex($page);
  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