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