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