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