comments: Fix anchor ids to be legal xhtml. Closes: #521339
[ikiwiki] / IkiWiki / Plugin / inline.pm
1 #!/usr/bin/perl
2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
4
5 use warnings;
6 use strict;
7 use Encode;
8 use IkiWiki 3.00;
9 use URI;
10
11 my %knownfeeds;
12 my %page_numfeeds;
13 my @inline;
14 my $nested=0;
15
16 sub import {
17         hook(type => "getopt", id => "inline", call => \&getopt);
18         hook(type => "getsetup", id => "inline", call => \&getsetup);
19         hook(type => "checkconfig", id => "inline", call => \&checkconfig);
20         hook(type => "sessioncgi", id => "inline", call => \&sessioncgi);
21         hook(type => "preprocess", id => "inline", 
22                 call => \&IkiWiki::preprocess_inline);
23         hook(type => "pagetemplate", id => "inline",
24                 call => \&IkiWiki::pagetemplate_inline);
25         hook(type => "format", id => "inline", call => \&format, first => 1);
26         # Hook to change to do pinging since it's called late.
27         # This ensures each page only pings once and prevents slow
28         # pings interrupting page builds.
29         hook(type => "change", id => "inline", call => \&IkiWiki::pingurl);
30 }
31
32 sub getopt () {
33         eval q{use Getopt::Long};
34         error($@) if $@;
35         Getopt::Long::Configure('pass_through');
36         GetOptions(
37                 "rss!" => \$config{rss},
38                 "atom!" => \$config{atom},
39                 "allowrss!" => \$config{allowrss},
40                 "allowatom!" => \$config{allowatom},
41                 "pingurl=s" => sub {
42                         push @{$config{pingurl}}, $_[1];
43                 },      
44         );
45 }
46
47 sub getsetup () {
48         return
49                 plugin => {
50                         safe => 1,
51                         rebuild => undef,
52                 },
53                 rss => {
54                         type => "boolean",
55                         example => 0,
56                         description => "enable rss feeds by default?",
57                         safe => 1,
58                         rebuild => 1,
59                 },
60                 atom => {
61                         type => "boolean",
62                         example => 0,
63                         description => "enable atom feeds by default?",
64                         safe => 1,
65                         rebuild => 1,
66                 },
67                 allowrss => {
68                         type => "boolean",
69                         example => 0,
70                         description => "allow rss feeds to be used?",
71                         safe => 1,
72                         rebuild => 1,
73                 },
74                 allowatom => {
75                         type => "boolean",
76                         example => 0,
77                         description => "allow atom feeds to be used?",
78                         safe => 1,
79                         rebuild => 1,
80                 },
81                 pingurl => {
82                         type => "string",
83                         example => "http://rpc.technorati.com/rpc/ping",
84                         description => "urls to ping (using XML-RPC) on feed update",
85                         safe => 1,
86                         rebuild => 0,
87                 },
88 }
89
90 sub checkconfig () {
91         if (($config{rss} || $config{atom}) && ! length $config{url}) {
92                 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
93         }
94         if ($config{rss}) {
95                 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
96         }
97         if ($config{atom}) {
98                 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
99         }
100         if (! exists $config{pingurl}) {
101                 $config{pingurl}=[];
102         }
103 }
104
105 sub format (@) {
106         my %params=@_;
107
108         # Fill in the inline content generated earlier. This is actually an
109         # optimisation.
110         $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
111                 delete @inline[$1,]
112         }eg;
113         return $params{content};
114 }
115
116 sub sessioncgi ($$) {
117         my $q=shift;
118         my $session=shift;
119
120         if ($q->param('do') eq 'blog') {
121                 my $page=titlepage(decode_utf8($q->param('title')));
122                 $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
123                 # if the page already exists, munge it to be unique
124                 my $from=$q->param('from');
125                 my $add="";
126                 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
127                         $add=1 unless length $add;
128                         $add++;
129                 }
130                 $q->param('page', $page.$add);
131                 # now go create the page
132                 $q->param('do', 'create');
133                 # make sure the editpage plugin in loaded
134                 if (IkiWiki->can("cgi_editpage")) {
135                         IkiWiki::cgi_editpage($q, $session);
136                 }
137                 else {
138                         error(gettext("page editing not allowed"));
139                 }
140                 exit;
141         }
142 }
143
144 # Back to ikiwiki namespace for the rest, this code is very much
145 # internal to ikiwiki even though it's separated into a plugin.
146 package IkiWiki;
147
148 my %toping;
149 my %feedlinks;
150
151 sub preprocess_inline (@) {
152         my %params=@_;
153         
154         if (! exists $params{pages}) {
155                 error gettext("missing pages parameter");
156         }
157         my $raw=yesno($params{raw});
158         my $archive=yesno($params{archive});
159         my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
160         my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
161         my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
162         my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
163         my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
164         my $feedonly=yesno($params{feedonly});
165         if (! exists $params{show} && ! $archive) {
166                 $params{show}=10;
167         }
168         if (! exists $params{feedshow} && exists $params{show}) {
169                 $params{feedshow}=$params{show};
170         }
171         my $desc;
172         if (exists $params{description}) {
173                 $desc = $params{description} 
174         }
175         else {
176                 $desc = $config{wikiname};
177         }
178         my $actions=yesno($params{actions});
179         if (exists $params{template}) {
180                 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
181         }
182         else {
183                 $params{template} = $archive ? "archivepage" : "inlinepage";
184         }
185
186         my @list;
187         foreach my $page (keys %pagesources) {
188                 next if $page eq $params{page};
189                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
190                         push @list, $page;
191                 }
192         }
193
194         if (exists $params{sort} && $params{sort} eq 'title') {
195                 @list=sort { pagetitle(basename($a)) cmp pagetitle(basename($b)) } @list;
196         }
197         elsif (exists $params{sort} && $params{sort} eq 'title_natural') {
198                 eval q{use Sort::Naturally};
199                 if ($@) {
200                         error(gettext("Sort::Naturally needed for title_natural sort"));
201                 }
202                 @list=sort { Sort::Naturally::ncmp(pagetitle(basename($a)), pagetitle(basename($b))) } @list;
203         }
204         elsif (exists $params{sort} && $params{sort} eq 'mtime') {
205                 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
206         }
207         elsif (! exists $params{sort} || $params{sort} eq 'age') {
208                 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
209         }
210         else {
211                 error sprintf(gettext("unknown sort type %s"), $params{sort});
212         }
213
214         if (yesno($params{reverse})) {
215                 @list=reverse(@list);
216         }
217
218         if (exists $params{skip}) {
219                 @list=@list[$params{skip} .. scalar @list - 1];
220         }
221         
222         my @feedlist;
223         if ($feeds) {
224                 if (exists $params{feedshow} &&
225                     $params{feedshow} && @list > $params{feedshow}) {
226                         @feedlist=@list[0..$params{feedshow} - 1];
227                 }
228                 else {
229                         @feedlist=@list;
230                 }
231         }
232         
233         if ($params{show} && @list > $params{show}) {
234                 @list=@list[0..$params{show} - 1];
235         }
236
237         add_depends($params{page}, $params{pages});
238         # Explicitly add all currently displayed pages as dependencies, so
239         # that if they are removed or otherwise changed, the inline will be
240         # sure to be updated.
241         add_depends($params{page}, join(" or ", $#list >= $#feedlist ? @list : @feedlist));
242         
243         if ($feeds && exists $params{feedpages}) {
244                 @feedlist=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @feedlist;
245         }
246
247         my ($feedbase, $feednum);
248         if ($feeds) {
249                 # Ensure that multiple feeds on a page go to unique files.
250                 
251                 # Feedfile can lead to conflicts if usedirs is not enabled,
252                 # so avoid supporting it in that case.
253                 delete $params{feedfile} if ! $config{usedirs};
254                 # Tight limits on legal feedfiles, to avoid security issues
255                 # and conflicts.
256                 if (defined $params{feedfile}) {
257                         if ($params{feedfile} =~ /\// ||
258                             $params{feedfile} !~ /$config{wiki_file_regexp}/) {
259                                 error("illegal feedfile");
260                         }
261                         $params{feedfile}=possibly_foolish_untaint($params{feedfile});
262                 }
263                 $feedbase=targetpage($params{destpage}, "", $params{feedfile});
264
265                 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
266                 if (exists $knownfeeds{$feedid}) {
267                         $feednum=$knownfeeds{$feedid};
268                 }
269                 else {
270                         if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
271                                 if ($feeds) {
272                                         $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
273                                 }
274                         }
275                         else {
276                                 $feednum=$knownfeeds{$feedid}="";
277                                 if ($feeds) {
278                                         $page_numfeeds{$params{destpage}}{$feedbase}=1;
279                                 }
280                         }
281                 }
282         }
283
284         my $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $rss;
285         my $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage}))) if $feeds && $atom;
286
287         my $ret="";
288
289         if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
290             (exists $params{postform} && yesno($params{postform}))) &&
291             IkiWiki->can("cgi_editpage")) {
292                 # Add a blog post form, with feed buttons.
293                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
294                 $formtemplate->param(cgiurl => $config{cgiurl});
295                 my $rootpage;
296                 if (exists $params{rootpage}) {
297                         $rootpage=bestlink($params{page}, $params{rootpage});
298                         if (!length $rootpage) {
299                                 $rootpage=$params{rootpage};
300                         }
301                 }
302                 else {
303                         $rootpage=$params{page};
304                 }
305                 $formtemplate->param(rootpage => $rootpage);
306                 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
307                 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
308                 if (exists $params{postformtext}) {
309                         $formtemplate->param(postformtext =>
310                                 $params{postformtext});
311                 }
312                 else {
313                         $formtemplate->param(postformtext =>
314                                 gettext("Add a new post titled:"));
315                 }
316                 $ret.=$formtemplate->output;
317                 
318                 # The post form includes the feed buttons, so
319                 # emptyfeeds cannot be hidden.
320                 $emptyfeeds=1;
321         }
322         elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
323                 # Add feed buttons.
324                 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
325                 $linktemplate->param(rssurl => $rssurl) if $rss;
326                 $linktemplate->param(atomurl => $atomurl) if $atom;
327                 $ret.=$linktemplate->output;
328         }
329         
330         if (! $feedonly) {
331                 require HTML::Template;
332                 my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
333                 if (! @params) {
334                         error sprintf(gettext("nonexistant template %s"), $params{template});
335                 }
336                 my $template=HTML::Template->new(@params) unless $raw;
337         
338                 foreach my $page (@list) {
339                         my $file = $pagesources{$page};
340                         my $type = pagetype($file);
341                         if (! $raw || ($raw && ! defined $type)) {
342                                 unless ($archive && $quick) {
343                                         # Get the content before populating the
344                                         # template, since getting the content uses
345                                         # the same template if inlines are nested.
346                                         my $content=get_inline_content($page, $params{destpage});
347                                         $template->param(content => $content);
348                                 }
349                                 $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
350                                 $template->param(inlinepage => $page);
351                                 $template->param(title => pagetitle(basename($page)));
352                                 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}));
353                                 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
354                                 $template->param(first => 1) if $page eq $list[0];
355                                 $template->param(last => 1) if $page eq $list[$#list];
356         
357                                 if ($actions) {
358                                         my $file = $pagesources{$page};
359                                         my $type = pagetype($file);
360                                         if ($config{discussion}) {
361                                                 my $discussionlink=gettext("discussion");
362                                                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
363                                                     (length $config{cgiurl} ||
364                                                      exists $links{$page."/".$discussionlink})) {
365                                                         $template->param(have_actions => 1);
366                                                         $template->param(discussionlink =>
367                                                                 htmllink($page,
368                                                                         $params{destpage},
369                                                                         gettext("Discussion"),
370                                                                         noimageinline => 1,
371                                                                         forcesubpage => 1));
372                                                 }
373                                         }
374                                         if (length $config{cgiurl} && defined $type) {
375                                                 $template->param(have_actions => 1);
376                                                 $template->param(editurl => cgiurl(do => "edit", page => $page));
377                                         }
378                                 }
379         
380                                 run_hooks(pagetemplate => sub {
381                                         shift->(page => $page, destpage => $params{destpage},
382                                                 template => $template,);
383                                 });
384         
385                                 $ret.=$template->output;
386                                 $template->clear_params;
387                         }
388                         else {
389                                 if (defined $type) {
390                                         $ret.="\n".
391                                               linkify($page, $params{destpage},
392                                               preprocess($page, $params{destpage},
393                                               filter($page, $params{destpage},
394                                               readfile(srcfile($file)))));
395                                 }
396                         }
397                 }
398         }
399         
400         if ($feeds && ($emptyfeeds || @feedlist)) {
401                 if ($rss) {
402                         my $rssp=$feedbase."rss".$feednum;
403                         will_render($params{destpage}, $rssp);
404                         if (! $params{preview}) {
405                                 writefile($rssp, $config{destdir},
406                                         genfeed("rss",
407                                                 $config{url}."/".$rssp, $desc, $params{guid}, $params{destpage}, @feedlist));
408                                 $toping{$params{destpage}}=1 unless $config{rebuild};
409                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$desc (RSS)" href="$rssurl" />};
410                         }
411                 }
412                 if ($atom) {
413                         my $atomp=$feedbase."atom".$feednum;
414                         will_render($params{destpage}, $atomp);
415                         if (! $params{preview}) {
416                                 writefile($atomp, $config{destdir},
417                                         genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{destpage}, @feedlist));
418                                 $toping{$params{destpage}}=1 unless $config{rebuild};
419                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$desc (Atom)" href="$atomurl" />};
420                         }
421                 }
422         }
423         
424         return $ret if $raw || $nested;
425         push @inline, $ret;
426         return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
427 }
428
429 sub pagetemplate_inline (@) {
430         my %params=@_;
431         my $page=$params{page};
432         my $template=$params{template};
433
434         $template->param(feedlinks => $feedlinks{$page})
435                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
436 }
437
438 sub get_inline_content ($$) {
439         my $page=shift;
440         my $destpage=shift;
441         
442         my $file=$pagesources{$page};
443         my $type=pagetype($file);
444         if (defined $type) {
445                 $nested++;
446                 my $ret=htmlize($page, $destpage, $type,
447                        linkify($page, $destpage,
448                        preprocess($page, $destpage,
449                        filter($page, $destpage,
450                        readfile(srcfile($file))))));
451                 $nested--;
452                 return $ret;
453         }
454         else {
455                 return "";
456         }
457 }
458
459 sub date_822 ($) {
460         my $time=shift;
461
462         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
463         POSIX::setlocale(&POSIX::LC_TIME, "C");
464         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
465         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
466         return $ret;
467 }
468
469 sub date_3339 ($) {
470         my $time=shift;
471
472         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
473         POSIX::setlocale(&POSIX::LC_TIME, "C");
474         my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", gmtime($time));
475         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
476         return $ret;
477 }
478
479 sub absolute_urls ($$) {
480         # sucky sub because rss sucks
481         my $content=shift;
482         my $baseurl=shift;
483
484         my $url=$baseurl;
485         $url=~s/[^\/]+$//;
486
487         # what is the non path part of the url?
488         my $top_uri = URI->new($url);
489         $top_uri->path_query(""); # reset the path
490         my $urltop = $top_uri->as_string;
491
492         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
493         # relative to another wiki page
494         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)([^\/][^"]*)"/$1 href="$url$2"/mig;
495         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)([^\/][^"]*)"/$1 src="$url$2"/mig;
496         # relative to the top of the site
497         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:)(\/[^"]*)"/$1 href="$urltop$2"/mig;
498         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:)(\/[^"]*)"/$1 src="$urltop$2"/mig;
499         return $content;
500 }
501
502 sub genfeed ($$$$$@) {
503         my $feedtype=shift;
504         my $feedurl=shift;
505         my $feeddesc=shift;
506         my $guid=shift;
507         my $page=shift;
508         my @pages=@_;
509         
510         my $url=URI->new(encode_utf8(urlto($page,"",1)));
511         
512         my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
513         my $content="";
514         my $lasttime = 0;
515         foreach my $p (@pages) {
516                 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
517                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
518
519                 $itemtemplate->param(
520                         title => pagetitle(basename($p)),
521                         url => $u,
522                         permalink => $u,
523                         cdate_822 => date_822($pagectime{$p}),
524                         mdate_822 => date_822($pagemtime{$p}),
525                         cdate_3339 => date_3339($pagectime{$p}),
526                         mdate_3339 => date_3339($pagemtime{$p}),
527                 );
528
529                 if (exists $pagestate{$p}) {
530                         if (exists $pagestate{$p}{meta}{guid}) {
531                                 $itemtemplate->param(guid => $pagestate{$p}{meta}{guid});
532                         }
533
534                         if (exists $pagestate{$p}{meta}{updated}) {
535                                 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
536                                 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
537                         }
538                 }
539
540                 if ($itemtemplate->query(name => "enclosure")) {
541                         my $file=$pagesources{$p};
542                         my $type=pagetype($file);
543                         if (defined $type) {
544                                 $itemtemplate->param(content => $pcontent);
545                         }
546                         else {
547                                 my $size=(srcfile_stat($file))[8];
548                                 my $mime="unknown";
549                                 eval q{use File::MimeInfo};
550                                 if (! $@) {
551                                         $mime = mimetype($file);
552                                 }
553                                 $itemtemplate->param(
554                                         enclosure => $u,
555                                         type => $mime,
556                                         length => $size,
557                                 );
558                         }
559                 }
560                 else {
561                         $itemtemplate->param(content => $pcontent);
562                 }
563
564                 run_hooks(pagetemplate => sub {
565                         shift->(page => $p, destpage => $page,
566                                 template => $itemtemplate);
567                 });
568
569                 $content.=$itemtemplate->output;
570                 $itemtemplate->clear_params;
571
572                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
573         }
574
575         my $template=template($feedtype."page.tmpl", blind_cache => 1);
576         $template->param(
577                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
578                 wikiname => $config{wikiname},
579                 pageurl => $url,
580                 content => $content,
581                 feeddesc => $feeddesc,
582                 guid => $guid,
583                 feeddate => date_3339($lasttime),
584                 feedurl => $feedurl,
585                 version => $IkiWiki::version,
586         );
587         run_hooks(pagetemplate => sub {
588                 shift->(page => $page, destpage => $page,
589                         template => $template);
590         });
591         
592         return $template->output;
593 }
594
595 sub pingurl (@) {
596         return unless @{$config{pingurl}} && %toping;
597
598         eval q{require RPC::XML::Client};
599         if ($@) {
600                 debug(gettext("RPC::XML::Client not found, not pinging"));
601                 return;
602         }
603
604         # daemonize here so slow pings don't slow down wiki updates
605         defined(my $pid = fork) or error("Can't fork: $!");
606         return if $pid;
607         chdir '/';
608         POSIX::setsid() or error("Can't start a new session: $!");
609         open STDIN, '/dev/null';
610         open STDOUT, '>/dev/null';
611         open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
612
613         # Don't need to keep a lock on the wiki as a daemon.
614         IkiWiki::unlockwiki();
615
616         foreach my $page (keys %toping) {
617                 my $title=pagetitle(basename($page), 0);
618                 my $url=urlto($page, "", 1);
619                 foreach my $pingurl (@{$config{pingurl}}) {
620                         debug("Pinging $pingurl for $page");
621                         eval {
622                                 my $client = RPC::XML::Client->new($pingurl);
623                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
624                                         $title, $url);
625                                 my $res = $client->send_request($req);
626                                 if (! ref $res) {
627                                         error("Did not receive response to ping");
628                                 }
629                                 my $r=$res->value;
630                                 if (! exists $r->{flerror} || $r->{flerror}) {
631                                         error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
632                                 }
633                         };
634                         if ($@) {
635                                 error "Ping failed: $@";
636                         }
637                 }
638         }
639
640         exit 0; # daemon done
641 }
642
643 1