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