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