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