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