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