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