summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 8f8472f075f20c69e57578b75041c4ce145224f1 (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. section => "web",
  37. },
  38. comments_pagespec => {
  39. type => 'pagespec',
  40. example => 'blog/* and !*/Discussion',
  41. description => 'PageSpec of pages where comments are allowed',
  42. link => 'ikiwiki/PageSpec',
  43. safe => 1,
  44. rebuild => 1,
  45. },
  46. comments_closed_pagespec => {
  47. type => 'pagespec',
  48. example => 'blog/controversial or blog/flamewar',
  49. description => 'PageSpec of pages where posting new comments is not allowed',
  50. link => 'ikiwiki/PageSpec',
  51. safe => 1,
  52. rebuild => 1,
  53. },
  54. comments_pagename => {
  55. type => 'string',
  56. default => 'comment_',
  57. description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
  58. safe => 0, # manual page moving required
  59. rebuild => undef,
  60. },
  61. comments_allowdirectives => {
  62. type => 'boolean',
  63. example => 0,
  64. description => 'Interpret directives in comments?',
  65. safe => 1,
  66. rebuild => 0,
  67. },
  68. comments_allowauthor => {
  69. type => 'boolean',
  70. example => 0,
  71. description => 'Allow anonymous commenters to set an author name?',
  72. safe => 1,
  73. rebuild => 0,
  74. },
  75. comments_commit => {
  76. type => 'boolean',
  77. example => 1,
  78. description => 'commit comments to the VCS',
  79. # old uncommitted comments are likely to cause
  80. # confusion if this is changed
  81. safe => 0,
  82. rebuild => 0,
  83. },
  84. }
  85. sub checkconfig () {
  86. $config{comments_commit} = 1
  87. unless defined $config{comments_commit};
  88. $config{comments_pagespec} = ''
  89. unless defined $config{comments_pagespec};
  90. $config{comments_closed_pagespec} = ''
  91. unless defined $config{comments_closed_pagespec};
  92. $config{comments_pagename} = 'comment_'
  93. unless defined $config{comments_pagename};
  94. }
  95. sub htmlize {
  96. my %params = @_;
  97. return $params{content};
  98. }
  99. # FIXME: copied verbatim from meta
  100. sub safeurl ($) {
  101. my $url=shift;
  102. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  103. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  104. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  105. }
  106. else {
  107. return 1;
  108. }
  109. }
  110. sub preprocess {
  111. my %params = @_;
  112. my $page = $params{page};
  113. my $format = $params{format};
  114. if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
  115. error(sprintf(gettext("unsupported page format %s"), $format));
  116. }
  117. my $content = $params{content};
  118. if (! defined $content) {
  119. error(gettext("comment must have content"));
  120. }
  121. $content =~ s/\\"/"/g;
  122. $content = IkiWiki::filter($page, $params{destpage}, $content);
  123. if ($config{comments_allowdirectives}) {
  124. $content = IkiWiki::preprocess($page, $params{destpage},
  125. $content);
  126. }
  127. # no need to bother with htmlize if it's just HTML
  128. $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
  129. if defined $format;
  130. IkiWiki::run_hooks(sanitize => sub {
  131. $content = shift->(
  132. page => $page,
  133. destpage => $params{destpage},
  134. content => $content,
  135. );
  136. });
  137. # set metadata, possibly overriding [[!meta]] directives from the
  138. # comment itself
  139. my $commentuser;
  140. my $commentip;
  141. my $commentauthor;
  142. my $commentauthorurl;
  143. my $commentopenid;
  144. if (defined $params{username}) {
  145. $commentuser = $params{username};
  146. my $oiduser = eval { IkiWiki::openiduser($commentuser) };
  147. if (defined $oiduser) {
  148. # looks like an OpenID
  149. $commentauthorurl = $commentuser;
  150. $commentauthor = $oiduser;
  151. $commentopenid = $commentuser;
  152. }
  153. else {
  154. $commentauthorurl = IkiWiki::cgiurl(
  155. do => 'goto',
  156. page => IkiWiki::userpage($commentuser)
  157. );
  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. $template->param(addcommenturl => addcommenturl($page));
  633. }
  634. }
  635. if ($shown) {
  636. if ($template->query(name => 'commentsurl')) {
  637. $template->param(commentsurl =>
  638. urlto($page, undef, 1).'#comments');
  639. }
  640. if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
  641. # This will 404 until there are some comments, but I
  642. # think that's probably OK...
  643. $template->param(atomcommentsurl =>
  644. urlto($page, undef, 1).'comments.atom');
  645. }
  646. if ($template->query(name => 'commentslink')) {
  647. my $num=num_comments($page, $config{srcdir});
  648. my $link;
  649. if ($num > 0) {
  650. $link = htmllink($page, $params{destpage}, $page,
  651. linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
  652. anchor => "comments",
  653. noimageinline => 1
  654. );
  655. }
  656. elsif (commentsopen($page)) {
  657. $link = "<a href=\"".addcommenturl($page)."\">".
  658. #translators: Here "Comment" is a verb;
  659. #translators: the user clicks on it to
  660. #translators: post a comment.
  661. gettext("Comment").
  662. "</a>";
  663. }
  664. $template->param(commentslink => $link)
  665. if defined $link;
  666. }
  667. }
  668. # everything below this point is only relevant to the comments
  669. # themselves
  670. if (!exists $commentstate{$page}) {
  671. return;
  672. }
  673. if ($template->query(name => 'commentid')) {
  674. $template->param(commentid => page_to_id($page));
  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 addcommenturl ($) {
  705. my $page=shift;
  706. return IkiWiki::cgiurl(do => 'comment', page => $page);
  707. }
  708. sub num_comments ($$) {
  709. my $page=shift;
  710. my $dir=shift;
  711. my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
  712. return @comments;
  713. }
  714. sub unique_comment_location ($$$) {
  715. my $page=shift;
  716. eval q{use Digest::MD5 'md5_hex'};
  717. error($@) if $@;
  718. my $content_md5=md5_hex(shift);
  719. my $dir=shift;
  720. my $location;
  721. my $i = num_comments($page, $dir);
  722. do {
  723. $i++;
  724. $location = "$page/$config{comments_pagename}${i}_${content_md5}";
  725. } while (-e "$dir/$location._comment");
  726. return $location;
  727. }
  728. sub page_to_id ($) {
  729. # Converts a comment page name into a unique, legal html id
  730. # addtibute value, that can be used as an anchor to link to the
  731. # comment.
  732. my $page=shift;
  733. eval q{use Digest::MD5 'md5_hex'};
  734. error($@) if $@;
  735. return "comment-".md5_hex($page);
  736. }
  737. package IkiWiki::PageSpec;
  738. sub match_postcomment ($$;@) {
  739. my $page = shift;
  740. my $glob = shift;
  741. if (! $postcomment) {
  742. return IkiWiki::FailReason->new("not posting a comment");
  743. }
  744. return match_glob($page, $glob);
  745. }
  746. 1