summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 995d1f4eb6a1252db56914bc6083af07edd390b8 (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. 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 => 'goto',
  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. sub sessioncgi ($$) {
  208. my $cgi=shift;
  209. my $session=shift;
  210. my $do = $cgi->param('do');
  211. if ($do eq 'comment') {
  212. editcomment($cgi, $session);
  213. }
  214. elsif ($do eq 'commentmoderation') {
  215. commentmoderation($cgi, $session);
  216. }
  217. }
  218. # Mostly cargo-culted from IkiWiki::plugin::editpage
  219. sub editcomment ($$) {
  220. my $cgi=shift;
  221. my $session=shift;
  222. IkiWiki::decode_cgi_utf8($cgi);
  223. eval q{use CGI::FormBuilder};
  224. error($@) if $@;
  225. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  226. my $form = CGI::FormBuilder->new(
  227. fields => [qw{do sid page subject editcontent type author url}],
  228. charset => 'utf-8',
  229. method => 'POST',
  230. required => [qw{editcontent}],
  231. javascript => 0,
  232. params => $cgi,
  233. action => $config{cgiurl},
  234. header => 0,
  235. table => 0,
  236. template => scalar IkiWiki::template_params('editcomment.tmpl'),
  237. );
  238. IkiWiki::decode_form_utf8($form);
  239. IkiWiki::run_hooks(formbuilder_setup => sub {
  240. shift->(title => "comment", form => $form, cgi => $cgi,
  241. session => $session, buttons => \@buttons);
  242. });
  243. IkiWiki::decode_form_utf8($form);
  244. my $type = $form->param('type');
  245. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  246. $type = IkiWiki::possibly_foolish_untaint($type);
  247. }
  248. else {
  249. $type = $config{default_pageext};
  250. }
  251. my @page_types;
  252. if (exists $IkiWiki::hooks{htmlize}) {
  253. @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
  254. }
  255. $form->field(name => 'do', type => 'hidden');
  256. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  257. force => 1);
  258. $form->field(name => 'page', type => 'hidden');
  259. $form->field(name => 'subject', type => 'text', size => 72);
  260. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  261. $form->field(name => "type", value => $type, force => 1,
  262. type => 'select', options => \@page_types);
  263. $form->tmpl_param(username => $session->param('name'));
  264. if ($config{comments_allowauthor} and
  265. ! defined $session->param('name')) {
  266. $form->tmpl_param(allowauthor => 1);
  267. $form->field(name => 'author', type => 'text', size => '40');
  268. $form->field(name => 'url', type => 'text', size => '40');
  269. }
  270. else {
  271. $form->tmpl_param(allowauthor => 0);
  272. $form->field(name => 'author', type => 'hidden', value => '',
  273. force => 1);
  274. $form->field(name => 'url', type => 'hidden', value => '',
  275. force => 1);
  276. }
  277. # The untaint is OK (as in editpage) because we're about to pass
  278. # it to file_pruned anyway
  279. my $page = $form->field('page');
  280. $page = IkiWiki::possibly_foolish_untaint($page);
  281. if (! defined $page || ! length $page ||
  282. IkiWiki::file_pruned($page, $config{srcdir})) {
  283. error(gettext("bad page name"));
  284. }
  285. my $baseurl = urlto($page, undef, 1);
  286. $form->title(sprintf(gettext("commenting on %s"),
  287. IkiWiki::pagetitle($page)));
  288. $form->tmpl_param('helponformattinglink',
  289. htmllink($page, $page, 'ikiwiki/formatting',
  290. noimageinline => 1,
  291. linktext => 'FormattingHelp'),
  292. allowdirectives => $config{allow_directives});
  293. if ($form->submitted eq CANCEL) {
  294. # bounce back to the page they wanted to comment on, and exit.
  295. # CANCEL need not be considered in future
  296. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  297. exit;
  298. }
  299. if (not exists $pagesources{$page}) {
  300. error(sprintf(gettext(
  301. "page '%s' doesn't exist, so you can't comment"),
  302. $page));
  303. }
  304. if (pagespec_match($page, $config{comments_closed_pagespec},
  305. location => $page)) {
  306. error(sprintf(gettext(
  307. "comments on page '%s' are closed"),
  308. $page));
  309. }
  310. # Set a flag to indicate that we're posting a comment,
  311. # so that postcomment() can tell it should match.
  312. $postcomment=1;
  313. IkiWiki::check_canedit($page, $cgi, $session);
  314. $postcomment=0;
  315. my $location=unique_comment_location($page, $config{srcdir});
  316. my $content = "[[!_comment format=$type\n";
  317. # FIXME: handling of double quotes probably wrong?
  318. if (defined $session->param('name')) {
  319. my $username = $session->param('name');
  320. $username =~ s/"/&quot;/g;
  321. $content .= " username=\"$username\"\n";
  322. }
  323. elsif (defined $ENV{REMOTE_ADDR}) {
  324. my $ip = $ENV{REMOTE_ADDR};
  325. if ($ip =~ m/^([.0-9]+)$/) {
  326. $content .= " ip=\"$1\"\n";
  327. }
  328. }
  329. if ($config{comments_allowauthor}) {
  330. my $author = $form->field('author');
  331. if (defined $author && length $author) {
  332. $author =~ s/"/&quot;/g;
  333. $content .= " claimedauthor=\"$author\"\n";
  334. }
  335. my $url = $form->field('url');
  336. if (defined $url && length $url) {
  337. $url =~ s/"/&quot;/g;
  338. $content .= " url=\"$url\"\n";
  339. }
  340. }
  341. my $subject = $form->field('subject');
  342. if (defined $subject && length $subject) {
  343. $subject =~ s/"/&quot;/g;
  344. $content .= " subject=\"$subject\"\n";
  345. }
  346. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  347. my $editcontent = $form->field('editcontent') || '';
  348. $editcontent =~ s/\r\n/\n/g;
  349. $editcontent =~ s/\r/\n/g;
  350. $editcontent =~ s/"/\\"/g;
  351. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  352. # This is essentially a simplified version of editpage:
  353. # - the user does not control the page that's created, only the parent
  354. # - it's always a create operation, never an edit
  355. # - this means that conflicts should never happen
  356. # - this means that if they do, rocks fall and everyone dies
  357. if ($form->submitted eq PREVIEW) {
  358. my $preview=previewcomment($content, $location, $page, time);
  359. IkiWiki::run_hooks(format => sub {
  360. $preview = shift->(page => $page,
  361. content => $preview);
  362. });
  363. $form->tmpl_param(page_preview => $preview);
  364. }
  365. else {
  366. $form->tmpl_param(page_preview => "");
  367. }
  368. if ($form->submitted eq POST_COMMENT && $form->validate) {
  369. IkiWiki::checksessionexpiry($cgi, $session);
  370. $postcomment=1;
  371. my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
  372. subject => $form->field('subject'),
  373. $config{comments_allowauthor} ? (
  374. author => $form->field('author'),
  375. url => $form->field('url'),
  376. ) : (),
  377. page => $location,
  378. cgi => $cgi,
  379. session => $session,
  380. nonfatal => 1,
  381. );
  382. $postcomment=0;
  383. if (! $ok) {
  384. my $penddir=$config{wikistatedir}."/comments_pending";
  385. $location=unique_comment_location($page, $penddir);
  386. writefile("$location._comment", $penddir, $content);
  387. IkiWiki::printheader($session);
  388. print IkiWiki::misctemplate(gettext(gettext("comment stored for moderation")),
  389. "<p>".
  390. gettext("Your comment will be posted after moderator review").
  391. "</p>");
  392. exit;
  393. }
  394. # FIXME: could probably do some sort of graceful retry
  395. # on error? Would require significant unwinding though
  396. my $file = "$location._comment";
  397. writefile($file, $config{srcdir}, $content);
  398. my $conflict;
  399. if ($config{rcs} and $config{comments_commit}) {
  400. my $message = gettext("Added a comment");
  401. if (defined $form->field('subject') &&
  402. length $form->field('subject')) {
  403. $message = sprintf(
  404. gettext("Added a comment: %s"),
  405. $form->field('subject'));
  406. }
  407. IkiWiki::rcs_add($file);
  408. IkiWiki::disable_commit_hook();
  409. $conflict = IkiWiki::rcs_commit_staged($message,
  410. $session->param('name'), $ENV{REMOTE_ADDR});
  411. IkiWiki::enable_commit_hook();
  412. IkiWiki::rcs_update();
  413. }
  414. # Now we need a refresh
  415. require IkiWiki::Render;
  416. IkiWiki::refresh();
  417. IkiWiki::saveindex();
  418. # this should never happen, unless a committer deliberately
  419. # breaks it or something
  420. error($conflict) if defined $conflict;
  421. # Jump to the new comment on the page.
  422. # The trailing question mark tries to avoid broken
  423. # caches and get the most recent version of the page.
  424. IkiWiki::redirect($cgi, urlto($page, undef, 1)."?updated#$location");
  425. }
  426. else {
  427. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  428. forcebaseurl => $baseurl);
  429. }
  430. exit;
  431. }
  432. sub commentmoderation ($$) {
  433. my $cgi=shift;
  434. my $session=shift;
  435. IkiWiki::needsignin($cgi, $session);
  436. if (! IkiWiki::is_admin($session->param("name"))) {
  437. error(gettext("you are not logged in as an admin"));
  438. }
  439. IkiWiki::decode_cgi_utf8($cgi);
  440. if (defined $cgi->param('sid')) {
  441. IkiWiki::checksessionexpiry($cgi, $session);
  442. my $rejectalldefer=$cgi->param('rejectalldefer');
  443. my %vars=$cgi->Vars;
  444. my $added=0;
  445. foreach my $id (keys %vars) {
  446. if ($id =~ /(.*)\Q._comment\E$/) {
  447. my $action=$cgi->param($id);
  448. next if $action eq 'Defer' && ! $rejectalldefer;
  449. # Make sure that the id is of a legal
  450. # pending comment before untainting.
  451. my ($f)= $id =~ /$config{wiki_file_regexp}/;
  452. if (! defined $f || ! length $f ||
  453. IkiWiki::file_pruned($f, $config{srcdir})) {
  454. error("illegal file");
  455. }
  456. my $page=IkiWiki::possibly_foolish_untaint(IkiWiki::dirname($1));
  457. my $file="$config{wikistatedir}/comments_pending/".
  458. IkiWiki::possibly_foolish_untaint($id);
  459. if ($action eq 'Accept') {
  460. my $content=eval { readfile($file) };
  461. next if $@; # file vanished since form was displayed
  462. my $dest=unique_comment_location($page, $config{srcdir})."._comment";
  463. writefile($dest, $config{srcdir}, $content);
  464. if ($config{rcs} and $config{comments_commit}) {
  465. IkiWiki::rcs_add($dest);
  466. }
  467. $added++;
  468. }
  469. # This removes empty subdirs, so the
  470. # .ikiwiki/comments_pending dir will
  471. # go away when all are moderated.
  472. require IkiWiki::Render;
  473. IkiWiki::prune($file);
  474. }
  475. }
  476. if ($added) {
  477. my $conflict;
  478. if ($config{rcs} and $config{comments_commit}) {
  479. my $message = gettext("Comment moderation");
  480. IkiWiki::disable_commit_hook();
  481. $conflict=IkiWiki::rcs_commit_staged($message,
  482. $session->param('name'), $ENV{REMOTE_ADDR});
  483. IkiWiki::enable_commit_hook();
  484. IkiWiki::rcs_update();
  485. }
  486. # Now we need a refresh
  487. require IkiWiki::Render;
  488. IkiWiki::refresh();
  489. IkiWiki::saveindex();
  490. error($conflict) if defined $conflict;
  491. }
  492. }
  493. my @comments=map {
  494. my ($id, $ctime)=@{$_};
  495. my $file="$config{wikistatedir}/comments_pending/$id";
  496. my $content=readfile($file);
  497. my $preview=previewcomment($content, $id,
  498. IkiWiki::dirname($_), $ctime);
  499. {
  500. id => $id,
  501. view => $preview,
  502. }
  503. } sort { $b->[1] <=> $a->[1] } comments_pending();
  504. my $template=template("commentmoderation.tmpl");
  505. $template->param(
  506. sid => $session->id,
  507. comments => \@comments,
  508. );
  509. IkiWiki::printheader($session);
  510. my $out=$template->output;
  511. IkiWiki::run_hooks(format => sub {
  512. $out = shift->(page => "", content => $out);
  513. });
  514. print IkiWiki::misctemplate(gettext("comment moderation"), $out);
  515. exit;
  516. }
  517. sub formbuilder_setup (@) {
  518. my %params=@_;
  519. my $form=$params{form};
  520. if ($form->title eq "preferences") {
  521. push @{$params{buttons}}, "Comment Moderation";
  522. if ($form->submitted && $form->submitted eq "Comment Moderation") {
  523. commentmoderation($params{cgi}, $params{session});
  524. }
  525. }
  526. }
  527. sub comments_pending () {
  528. my $dir="$config{wikistatedir}/comments_pending/";
  529. return unless -d $dir;
  530. my @ret;
  531. eval q{use File::Find};
  532. error($@) if $@;
  533. find({
  534. no_chdir => 1,
  535. wanted => sub {
  536. $_=decode_utf8($_);
  537. if (IkiWiki::file_pruned($_, $dir)) {
  538. $File::Find::prune=1;
  539. }
  540. elsif (! -l $_ && ! -d _) {
  541. $File::Find::prune=0;
  542. my ($f)=/$config{wiki_file_regexp}/; # untaint
  543. if (defined $f && $f =~ /\Q._comment\E$/) {
  544. my $ctime=(stat($f))[10];
  545. $f=~s/^\Q$dir\E\/?//;
  546. push @ret, [$f, $ctime];
  547. }
  548. }
  549. }
  550. }, $dir);
  551. return @ret;
  552. }
  553. sub previewcomment ($$$) {
  554. my $content=shift;
  555. my $location=shift;
  556. my $page=shift;
  557. my $time=shift;
  558. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  559. IkiWiki::linkify($location, $page,
  560. IkiWiki::preprocess($location, $page,
  561. IkiWiki::filter($location, $page, $content), 0, 1)));
  562. my $template = template("comment.tmpl");
  563. $template->param(content => $preview);
  564. $template->param(ctime => displaytime($time));
  565. IkiWiki::run_hooks(pagetemplate => sub {
  566. shift->(page => $location,
  567. destpage => $page,
  568. template => $template);
  569. });
  570. $template->param(have_actions => 0);
  571. return $template->output;
  572. }
  573. sub commentsshown ($) {
  574. my $page=shift;
  575. return ! pagespec_match($page, "*/$config{comments_pagename}*",
  576. location => $page) &&
  577. pagespec_match($page, $config{comments_pagespec},
  578. location => $page);
  579. }
  580. sub commentsopen ($) {
  581. my $page = shift;
  582. return length $config{cgiurl} > 0 &&
  583. (! length $config{comments_closed_pagespec} ||
  584. ! pagespec_match($page, $config{comments_closed_pagespec},
  585. location => $page));
  586. }
  587. sub pagetemplate (@) {
  588. my %params = @_;
  589. my $page = $params{page};
  590. my $template = $params{template};
  591. my $shown = ($template->query(name => 'commentslink') ||
  592. $template->query(name => 'commentsurl') ||
  593. $template->query(name => 'atomcommentsurl') ||
  594. $template->query(name => 'comments')) &&
  595. commentsshown($page);
  596. if ($template->query(name => 'comments')) {
  597. my $comments = undef;
  598. if ($shown) {
  599. $comments = IkiWiki::preprocess_inline(
  600. pages => "internal($page/$config{comments_pagename}*)",
  601. template => 'comment',
  602. show => 0,
  603. reverse => 'yes',
  604. page => $page,
  605. destpage => $params{destpage},
  606. feedfile => 'comments',
  607. emptyfeeds => 'no',
  608. );
  609. }
  610. if (defined $comments && length $comments) {
  611. $template->param(comments => $comments);
  612. }
  613. if ($shown && commentsopen($page)) {
  614. my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
  615. page => $page);
  616. $template->param(addcommenturl => $addcommenturl);
  617. }
  618. }
  619. if ($template->query(name => 'commentsurl')) {
  620. if ($shown) {
  621. $template->param(commentsurl =>
  622. urlto($page, undef, 1).'#comments');
  623. }
  624. }
  625. if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
  626. if ($shown) {
  627. # This will 404 until there are some comments, but I
  628. # think that's probably OK...
  629. $template->param(atomcommentsurl =>
  630. urlto($page, undef, 1).'comments.atom');
  631. }
  632. }
  633. if ($template->query(name => 'commentslink')) {
  634. # XXX Would be nice to say how many comments there are in
  635. # the link. But, to update the number, blog pages
  636. # would have to update whenever comments of any inlines
  637. # page are added, which is not currently done.
  638. if ($shown) {
  639. $template->param(commentslink =>
  640. htmllink($page, $params{destpage}, $page,
  641. linktext => gettext("Comments"),
  642. anchor => "comments",
  643. noimageinline => 1));
  644. }
  645. }
  646. # everything below this point is only relevant to the comments
  647. # themselves
  648. if (!exists $commentstate{$page}) {
  649. return;
  650. }
  651. if ($template->query(name => 'commentuser')) {
  652. $template->param(commentuser =>
  653. $commentstate{$page}{commentuser});
  654. }
  655. if ($template->query(name => 'commentopenid')) {
  656. $template->param(commentopenid =>
  657. $commentstate{$page}{commentopenid});
  658. }
  659. if ($template->query(name => 'commentip')) {
  660. $template->param(commentip =>
  661. $commentstate{$page}{commentip});
  662. }
  663. if ($template->query(name => 'commentauthor')) {
  664. $template->param(commentauthor =>
  665. $commentstate{$page}{commentauthor});
  666. }
  667. if ($template->query(name => 'commentauthorurl')) {
  668. $template->param(commentauthorurl =>
  669. $commentstate{$page}{commentauthorurl});
  670. }
  671. if ($template->query(name => 'removeurl') &&
  672. IkiWiki::Plugin::remove->can("check_canremove") &&
  673. length $config{cgiurl}) {
  674. $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
  675. page => $page));
  676. $template->param(have_actions => 1);
  677. }
  678. }
  679. sub unique_comment_location ($) {
  680. my $page=shift;
  681. my $dir=shift;
  682. my $location;
  683. my $i = 0;
  684. do {
  685. $i++;
  686. $location = "$page/$config{comments_pagename}$i";
  687. } while (-e "$dir/$location._comment");
  688. return $location;
  689. }
  690. package IkiWiki::PageSpec;
  691. sub match_postcomment ($$;@) {
  692. my $page = shift;
  693. my $glob = shift;
  694. if (! $postcomment) {
  695. return IkiWiki::FailReason->new("not posting a comment");
  696. }
  697. return match_glob($page, $glob);
  698. }
  699. 1