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