summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: d9183970d8b1525c34a69a68dcec57fa88eaf58e (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 and wiki_file_regexp anyway.
  321. my $page = $form->field('page')=~/$config{wiki_file_regexp}/;
  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. $form->title(sprintf(gettext("commenting on %s"),
  328. IkiWiki::pagetitle(IkiWiki::basename($page))));
  329. $form->tmpl_param('helponformattinglink',
  330. htmllink($page, $page, 'ikiwiki/formatting',
  331. noimageinline => 1,
  332. linktext => 'FormattingHelp'),
  333. allowdirectives => $config{allow_directives});
  334. if ($form->submitted eq CANCEL) {
  335. # bounce back to the page they wanted to comment on, and exit.
  336. IkiWiki::redirect($cgi, urlto($page));
  337. exit;
  338. }
  339. if (not exists $pagesources{$page}) {
  340. error(sprintf(gettext(
  341. "page '%s' doesn't exist, so you can't comment"),
  342. $page));
  343. }
  344. if (pagespec_match($page, $config{comments_closed_pagespec},
  345. location => $page)) {
  346. error(sprintf(gettext(
  347. "comments on page '%s' are closed"),
  348. $page));
  349. }
  350. # Set a flag to indicate that we're posting a comment,
  351. # so that postcomment() can tell it should match.
  352. $postcomment=1;
  353. IkiWiki::check_canedit($page, $cgi, $session);
  354. $postcomment=0;
  355. my $content = "[[!comment format=$type\n";
  356. if (defined $session->param('name')) {
  357. my $username = $session->param('name');
  358. $username =~ s/"/&quot;/g;
  359. $content .= " username=\"$username\"\n";
  360. }
  361. if (defined $session->param('nickname')) {
  362. my $nickname = $session->param('nickname');
  363. $nickname =~ s/"/&quot;/g;
  364. $content .= " nickname=\"$nickname\"\n";
  365. }
  366. elsif (defined $session->remote_addr()) {
  367. my $ip = $session->remote_addr();
  368. if ($ip =~ m/^([.0-9]+)$/) {
  369. $content .= " ip=\"$1\"\n";
  370. }
  371. }
  372. if ($config{comments_allowauthor}) {
  373. my $author = $form->field('author');
  374. if (defined $author && length $author) {
  375. $author =~ s/"/&quot;/g;
  376. $content .= " claimedauthor=\"$author\"\n";
  377. }
  378. my $url = $form->field('url');
  379. if (defined $url && length $url) {
  380. $url =~ s/"/&quot;/g;
  381. $content .= " url=\"$url\"\n";
  382. }
  383. }
  384. my $subject = $form->field('subject');
  385. if (defined $subject && length $subject) {
  386. $subject =~ s/"/&quot;/g;
  387. }
  388. else {
  389. $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
  390. }
  391. $content .= " subject=\"$subject\"\n";
  392. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  393. my $editcontent = $form->field('editcontent');
  394. $editcontent="" if ! defined $editcontent;
  395. $editcontent =~ s/\r\n/\n/g;
  396. $editcontent =~ s/\r/\n/g;
  397. $editcontent =~ s/"/\\"/g;
  398. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  399. my $location=unique_comment_location($page, $content, $config{srcdir});
  400. # This is essentially a simplified version of editpage:
  401. # - the user does not control the page that's created, only the parent
  402. # - it's always a create operation, never an edit
  403. # - this means that conflicts should never happen
  404. # - this means that if they do, rocks fall and everyone dies
  405. if ($form->submitted eq PREVIEW) {
  406. my $preview=previewcomment($content, $location, $page, time);
  407. IkiWiki::run_hooks(format => sub {
  408. $preview = shift->(page => $page,
  409. content => $preview);
  410. });
  411. $form->tmpl_param(page_preview => $preview);
  412. }
  413. else {
  414. $form->tmpl_param(page_preview => "");
  415. }
  416. if ($form->submitted eq POST_COMMENT && $form->validate) {
  417. IkiWiki::checksessionexpiry($cgi, $session);
  418. $postcomment=1;
  419. my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
  420. subject => $form->field('subject'),
  421. $config{comments_allowauthor} ? (
  422. author => $form->field('author'),
  423. url => $form->field('url'),
  424. ) : (),
  425. page => $location,
  426. cgi => $cgi,
  427. session => $session,
  428. nonfatal => 1,
  429. );
  430. $postcomment=0;
  431. if (! $ok) {
  432. $location=unique_comment_location($page, $content, $config{srcdir}, "._comment_pending");
  433. writefile("$location._comment_pending", $config{srcdir}, $content);
  434. # Refresh so anything that deals with pending
  435. # comments can be updated.
  436. require IkiWiki::Render;
  437. IkiWiki::refresh();
  438. IkiWiki::saveindex();
  439. IkiWiki::printheader($session);
  440. print IkiWiki::cgitemplate($cgi, gettext(gettext("comment stored for moderation")),
  441. "<p>".
  442. gettext("Your comment will be posted after moderator review").
  443. "</p>");
  444. exit;
  445. }
  446. # FIXME: could probably do some sort of graceful retry
  447. # on error? Would require significant unwinding though
  448. my $file = "$location._comment";
  449. writefile($file, $config{srcdir}, $content);
  450. my $conflict;
  451. if ($config{rcs} and $config{comments_commit}) {
  452. my $message = gettext("Added a comment");
  453. if (defined $form->field('subject') &&
  454. length $form->field('subject')) {
  455. $message = sprintf(
  456. gettext("Added a comment: %s"),
  457. $form->field('subject'));
  458. }
  459. IkiWiki::rcs_add($file);
  460. IkiWiki::disable_commit_hook();
  461. $conflict = IkiWiki::rcs_commit_staged(
  462. message => $message,
  463. session => $session,
  464. );
  465. IkiWiki::enable_commit_hook();
  466. IkiWiki::rcs_update();
  467. }
  468. # Now we need a refresh
  469. require IkiWiki::Render;
  470. IkiWiki::refresh();
  471. IkiWiki::saveindex();
  472. # this should never happen, unless a committer deliberately
  473. # breaks it or something
  474. error($conflict) if defined $conflict;
  475. # Jump to the new comment on the page.
  476. # The trailing question mark tries to avoid broken
  477. # caches and get the most recent version of the page.
  478. IkiWiki::redirect($cgi, urlto($page).
  479. "?updated#".page_to_id($location));
  480. }
  481. else {
  482. IkiWiki::showform($form, \@buttons, $session, $cgi,
  483. page => $page);
  484. }
  485. exit;
  486. }
  487. sub commentmoderation ($$) {
  488. my $cgi=shift;
  489. my $session=shift;
  490. IkiWiki::needsignin($cgi, $session);
  491. if (! IkiWiki::is_admin($session->param("name"))) {
  492. error(gettext("you are not logged in as an admin"));
  493. }
  494. IkiWiki::decode_cgi_utf8($cgi);
  495. if (defined $cgi->param('sid')) {
  496. IkiWiki::checksessionexpiry($cgi, $session);
  497. my $rejectalldefer=$cgi->param('rejectalldefer');
  498. my %vars=$cgi->Vars;
  499. my $added=0;
  500. foreach my $id (keys %vars) {
  501. if ($id =~ /(.*)\._comment(?:_pending)?$/) {
  502. $id=decode_utf8($id);
  503. my $action=$cgi->param($id);
  504. next if $action eq 'Defer' && ! $rejectalldefer;
  505. # Make sure that the id is of a legal
  506. # pending comment.
  507. my ($f) = $id =~ /$config{wiki_file_regexp}/;
  508. if (! defined $f || ! length $f ||
  509. IkiWiki::file_pruned($f)) {
  510. error("illegal file");
  511. }
  512. my $page=IkiWiki::dirname($f);
  513. my $file="$config{srcdir}/$f";
  514. if (! -e $file) {
  515. # old location
  516. $file="$config{wikistatedir}/comments_pending/".$f;
  517. }
  518. if ($action eq 'Accept') {
  519. my $content=eval { readfile($file) };
  520. next if $@; # file vanished since form was displayed
  521. my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
  522. writefile($dest, $config{srcdir}, $content);
  523. if ($config{rcs} and $config{comments_commit}) {
  524. IkiWiki::rcs_add($dest);
  525. }
  526. $added++;
  527. }
  528. require IkiWiki::Render;
  529. IkiWiki::prune($file);
  530. }
  531. }
  532. if ($added) {
  533. my $conflict;
  534. if ($config{rcs} and $config{comments_commit}) {
  535. my $message = gettext("Comment moderation");
  536. IkiWiki::disable_commit_hook();
  537. $conflict=IkiWiki::rcs_commit_staged(
  538. message => $message,
  539. session => $session,
  540. );
  541. IkiWiki::enable_commit_hook();
  542. IkiWiki::rcs_update();
  543. }
  544. # Now we need a refresh
  545. require IkiWiki::Render;
  546. IkiWiki::refresh();
  547. IkiWiki::saveindex();
  548. error($conflict) if defined $conflict;
  549. }
  550. }
  551. my @comments=map {
  552. my ($id, $dir, $ctime)=@{$_};
  553. my $content=readfile("$dir/$id");
  554. my $preview=previewcomment($content, $id,
  555. $id, $ctime);
  556. {
  557. id => $id,
  558. view => $preview,
  559. }
  560. } sort { $b->[2] <=> $a->[2] } comments_pending();
  561. my $template=template("commentmoderation.tmpl");
  562. $template->param(
  563. sid => $session->id,
  564. comments => \@comments,
  565. cgiurl => IkiWiki::cgiurl(),
  566. );
  567. IkiWiki::printheader($session);
  568. my $out=$template->output;
  569. IkiWiki::run_hooks(format => sub {
  570. $out = shift->(page => "", content => $out);
  571. });
  572. print IkiWiki::cgitemplate($cgi, gettext("comment moderation"), $out);
  573. exit;
  574. }
  575. sub formbuilder_setup (@) {
  576. my %params=@_;
  577. my $form=$params{form};
  578. if ($form->title eq "preferences" &&
  579. IkiWiki::is_admin($params{session}->param("name"))) {
  580. push @{$params{buttons}}, "Comment Moderation";
  581. if ($form->submitted && $form->submitted eq "Comment Moderation") {
  582. commentmoderation($params{cgi}, $params{session});
  583. }
  584. }
  585. }
  586. sub comments_pending () {
  587. my @ret;
  588. eval q{use File::Find};
  589. error($@) if $@;
  590. eval q{use Cwd};
  591. error($@) if $@;
  592. my $origdir=getcwd();
  593. my $find_comments=sub {
  594. my $dir=shift;
  595. my $extension=shift;
  596. return unless -d $dir;
  597. chdir($dir) || die "chdir $dir: $!";
  598. find({
  599. no_chdir => 1,
  600. wanted => sub {
  601. my $file=decode_utf8($_);
  602. $file=~s/^\.\///;
  603. return if ! length $file || IkiWiki::file_pruned($file)
  604. || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
  605. my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
  606. if (defined $f) {
  607. my $ctime=(stat($_))[10];
  608. push @ret, [$f, $dir, $ctime];
  609. }
  610. }
  611. }, ".");
  612. chdir($origdir) || die "chdir $origdir: $!";
  613. };
  614. $find_comments->($config{srcdir}, "._comment_pending");
  615. # old location
  616. $find_comments->("$config{wikistatedir}/comments_pending/",
  617. "._comment");
  618. return @ret;
  619. }
  620. sub previewcomment ($$$) {
  621. my $content=shift;
  622. my $location=shift;
  623. my $page=shift;
  624. my $time=shift;
  625. # Previewing a comment should implicitly enable comment posting mode.
  626. my $oldpostcomment=$postcomment;
  627. $postcomment=1;
  628. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  629. IkiWiki::linkify($location, $page,
  630. IkiWiki::preprocess($location, $page,
  631. IkiWiki::filter($location, $page, $content), 0, 1)));
  632. my $template = template("comment.tmpl");
  633. $template->param(content => $preview);
  634. $template->param(ctime => displaytime($time, undef, 1));
  635. $template->param(html5 => $config{html5});
  636. IkiWiki::run_hooks(pagetemplate => sub {
  637. shift->(page => $location,
  638. destpage => $page,
  639. template => $template);
  640. });
  641. $template->param(have_actions => 0);
  642. $postcomment=$oldpostcomment;
  643. return $template->output;
  644. }
  645. sub commentsshown ($) {
  646. my $page=shift;
  647. return ! pagespec_match($page, "comment(*)",
  648. location => $page) &&
  649. pagespec_match($page, $config{comments_pagespec},
  650. location => $page);
  651. }
  652. sub commentsopen ($) {
  653. my $page = shift;
  654. return length $config{cgiurl} > 0 &&
  655. (! length $config{comments_closed_pagespec} ||
  656. ! pagespec_match($page, $config{comments_closed_pagespec},
  657. location => $page));
  658. }
  659. sub pagetemplate (@) {
  660. my %params = @_;
  661. my $page = $params{page};
  662. my $template = $params{template};
  663. my $shown = ($template->query(name => 'commentslink') ||
  664. $template->query(name => 'commentsurl') ||
  665. $template->query(name => 'atomcommentsurl') ||
  666. $template->query(name => 'comments')) &&
  667. commentsshown($page);
  668. if ($template->query(name => 'comments')) {
  669. my $comments = undef;
  670. if ($shown) {
  671. $comments = IkiWiki::preprocess_inline(
  672. pages => "comment($page)",
  673. template => 'comment',
  674. show => 0,
  675. reverse => 'yes',
  676. page => $page,
  677. destpage => $params{destpage},
  678. feedfile => 'comments',
  679. emptyfeeds => 'no',
  680. );
  681. }
  682. if (defined $comments && length $comments) {
  683. $template->param(comments => $comments);
  684. }
  685. if ($shown && commentsopen($page)) {
  686. $template->param(addcommenturl => addcommenturl($page));
  687. }
  688. }
  689. if ($shown) {
  690. if ($template->query(name => 'commentsurl')) {
  691. $template->param(commentsurl =>
  692. urlto($page).'#comments');
  693. }
  694. if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
  695. # This will 404 until there are some comments, but I
  696. # think that's probably OK...
  697. $template->param(atomcommentsurl =>
  698. urlto($page).'comments.atom');
  699. }
  700. if ($template->query(name => 'commentslink')) {
  701. my $num=num_comments($page, $config{srcdir});
  702. my $link;
  703. if ($num > 0) {
  704. $link = htmllink($page, $params{destpage}, $page,
  705. linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
  706. anchor => "comments",
  707. noimageinline => 1
  708. );
  709. }
  710. elsif (commentsopen($page)) {
  711. $link = "<a href=\"".addcommenturl($page)."\">".
  712. #translators: Here "Comment" is a verb;
  713. #translators: the user clicks on it to
  714. #translators: post a comment.
  715. gettext("Comment").
  716. "</a>";
  717. }
  718. $template->param(commentslink => $link)
  719. if defined $link;
  720. }
  721. }
  722. # everything below this point is only relevant to the comments
  723. # themselves
  724. if (!exists $commentstate{$page}) {
  725. return;
  726. }
  727. if ($template->query(name => 'commentid')) {
  728. $template->param(commentid => page_to_id($page));
  729. }
  730. if ($template->query(name => 'commentuser')) {
  731. $template->param(commentuser =>
  732. $commentstate{$page}{commentuser});
  733. }
  734. if ($template->query(name => 'commentopenid')) {
  735. $template->param(commentopenid =>
  736. $commentstate{$page}{commentopenid});
  737. }
  738. if ($template->query(name => 'commentip')) {
  739. $template->param(commentip =>
  740. $commentstate{$page}{commentip});
  741. }
  742. if ($template->query(name => 'commentauthor')) {
  743. $template->param(commentauthor =>
  744. $commentstate{$page}{commentauthor});
  745. }
  746. if ($template->query(name => 'commentauthorurl')) {
  747. $template->param(commentauthorurl =>
  748. $commentstate{$page}{commentauthorurl});
  749. }
  750. if ($template->query(name => 'removeurl') &&
  751. IkiWiki::Plugin::remove->can("check_canremove") &&
  752. length $config{cgiurl}) {
  753. $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
  754. page => $page));
  755. $template->param(have_actions => 1);
  756. }
  757. }
  758. sub addcommenturl ($) {
  759. my $page=shift;
  760. return IkiWiki::cgiurl(do => 'comment', page => $page);
  761. }
  762. sub num_comments ($$) {
  763. my $page=shift;
  764. my $dir=shift;
  765. my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
  766. return int @comments;
  767. }
  768. sub unique_comment_location ($$$$) {
  769. my $page=shift;
  770. eval q{use Digest::MD5 'md5_hex'};
  771. error($@) if $@;
  772. my $content_md5=md5_hex(Encode::encode_utf8(shift));
  773. my $dir=shift;
  774. my $ext=shift || "._comment";
  775. my $location;
  776. my $i = num_comments($page, $dir);
  777. do {
  778. $i++;
  779. $location = "$page/$config{comments_pagename}${i}_${content_md5}";
  780. } while (-e "$dir/$location$ext");
  781. return $location;
  782. }
  783. sub page_to_id ($) {
  784. # Converts a comment page name into a unique, legal html id
  785. # attribute value, that can be used as an anchor to link to the
  786. # comment.
  787. my $page=shift;
  788. eval q{use Digest::MD5 'md5_hex'};
  789. error($@) if $@;
  790. return "comment-".md5_hex(Encode::encode_utf8(($page)));
  791. }
  792. package IkiWiki::PageSpec;
  793. sub match_postcomment ($$;@) {
  794. my $page = shift;
  795. my $glob = shift;
  796. if (! $postcomment) {
  797. return IkiWiki::FailReason->new("not posting a comment");
  798. }
  799. return match_glob($page, $glob, @_);
  800. }
  801. sub match_comment ($$;@) {
  802. my $page = shift;
  803. my $glob = shift;
  804. if (! $postcomment) {
  805. # To see if it's a comment, check the source file type.
  806. # Deal with comments that were just deleted.
  807. my $source=exists $IkiWiki::pagesources{$page} ?
  808. $IkiWiki::pagesources{$page} :
  809. $IkiWiki::delpagesources{$page};
  810. my $type=defined $source ? IkiWiki::pagetype($source) : undef;
  811. if (! defined $type || $type ne "_comment") {
  812. return IkiWiki::FailReason->new("$page is not a comment");
  813. }
  814. }
  815. return match_glob($page, "$glob/*", internal => 1, @_);
  816. }
  817. sub match_comment_pending ($$;@) {
  818. my $page = shift;
  819. my $glob = shift;
  820. my $source=exists $IkiWiki::pagesources{$page} ?
  821. $IkiWiki::pagesources{$page} :
  822. $IkiWiki::delpagesources{$page};
  823. my $type=defined $source ? IkiWiki::pagetype($source) : undef;
  824. if (! defined $type || $type ne "_comment_pending") {
  825. return IkiWiki::FailReason->new("$page is not a pending comment");
  826. }
  827. return match_glob($page, "$glob/*", internal => 1, @_);
  828. }
  829. 1