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