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