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