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