]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
Merge branch 'master' of ssh://git.ikiwiki.info
[ikiwiki.git] / 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                         section => "core",
53                 },
54                 rss => {
55                         type => "boolean",
56                         example => 0,
57                         description => "enable rss feeds by default?",
58                         safe => 1,
59                         rebuild => 1,
60                 },
61                 atom => {
62                         type => "boolean",
63                         example => 0,
64                         description => "enable atom feeds by default?",
65                         safe => 1,
66                         rebuild => 1,
67                 },
68                 allowrss => {
69                         type => "boolean",
70                         example => 0,
71                         description => "allow rss feeds to be used?",
72                         safe => 1,
73                         rebuild => 1,
74                 },
75                 allowatom => {
76                         type => "boolean",
77                         example => 0,
78                         description => "allow atom feeds to be used?",
79                         safe => 1,
80                         rebuild => 1,
81                 },
82                 pingurl => {
83                         type => "string",
84                         example => "http://rpc.technorati.com/rpc/ping",
85                         description => "urls to ping (using XML-RPC) on feed update",
86                         safe => 1,
87                         rebuild => 0,
88                 },
89 }
90
91 sub checkconfig () {
92         if (($config{rss} || $config{atom}) && ! length $config{url}) {
93                 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
94         }
95         if ($config{rss}) {
96                 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
97         }
98         if ($config{atom}) {
99                 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
100         }
101         if (! exists $config{pingurl}) {
102                 $config{pingurl}=[];
103         }
104 }
105
106 sub format (@) {
107         my %params=@_;
108
109         # Fill in the inline content generated earlier. This is actually an
110         # optimisation.
111         $params{content}=~s{<div class="inline" id="([^"]+)"></div>}{
112                 delete @inline[$1,]
113         }eg;
114         return $params{content};
115 }
116
117 sub sessioncgi ($$) {
118         my $q=shift;
119         my $session=shift;
120
121         if ($q->param('do') eq 'blog') {
122                 my $page=titlepage(decode_utf8($q->param('title')));
123                 $page=~s/(\/)/"__".ord($1)."__"/eg; # don't create subdirs
124                 # if the page already exists, munge it to be unique
125                 my $from=$q->param('from');
126                 my $add="";
127                 while (exists $IkiWiki::pagecase{lc($from."/".$page.$add)}) {
128                         $add=1 unless length $add;
129                         $add++;
130                 }
131                 $q->param('page', "/$from/$page$add");
132                 # now go create the page
133                 $q->param('do', 'create');
134                 # make sure the editpage plugin is loaded
135                 if (IkiWiki->can("cgi_editpage")) {
136                         IkiWiki::cgi_editpage($q, $session);
137                 }
138                 else {
139                         error(gettext("page editing not allowed"));
140                 }
141                 exit;
142         }
143 }
144
145 # Back to ikiwiki namespace for the rest, this code is very much
146 # internal to ikiwiki even though it's separated into a plugin.
147 package IkiWiki;
148
149 my %toping;
150 my %feedlinks;
151
152 sub preprocess_inline (@) {
153         my %params=@_;
154         
155         if (! exists $params{pages} && ! exists $params{pagenames}) {
156                 error gettext("missing pages parameter");
157         }
158         my $raw=yesno($params{raw});
159         my $archive=yesno($params{archive});
160         my $rss=(($config{rss} || $config{allowrss}) && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
161         my $atom=(($config{atom} || $config{allowatom}) && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
162         my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
163         my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick && ! $raw;
164         my $emptyfeeds=exists $params{emptyfeeds} ? yesno($params{emptyfeeds}) : 1;
165         my $feedonly=yesno($params{feedonly});
166         if (! exists $params{show} && ! $archive) {
167                 $params{show}=10;
168         }
169         if (! exists $params{feedshow} && exists $params{show}) {
170                 $params{feedshow}=$params{show};
171         }
172         my $desc;
173         if (exists $params{description}) {
174                 $desc = $params{description} 
175         }
176         else {
177                 $desc = $config{wikiname};
178         }
179         my $actions=yesno($params{actions});
180         if (exists $params{template}) {
181                 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
182         }
183         else {
184                 $params{template} = $archive ? "archivepage" : "inlinepage";
185         }
186
187         my @list;
188
189         if (exists $params{pagenames}) {
190                 foreach my $p (qw(sort pages)) {
191                         if (exists $params{$p}) {
192                                 error sprintf(gettext("the %s and %s parameters cannot be used together"),
193                                         "pagenames", $p);
194                         }
195                 }
196
197                 @list = map { bestlink($params{page}, $_) }
198                         split ' ', $params{pagenames};
199
200                 if (yesno($params{reverse})) {
201                         @list=reverse(@list);
202                 }
203
204                 foreach my $p (@list) {
205                         add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
206                 }
207         }
208         else {
209                 my $num=0;
210                 if ($params{show}) {
211                         $num=$params{show};
212                 }
213                 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
214                         $num=$params{feedshow};
215                 }
216                 if ($params{skip} && $num) {
217                         $num+=$params{skip};
218                 }
219
220                 @list = pagespec_match_list($params{page}, $params{pages},
221                         deptype => deptype($quick ? "presence" : "content"),
222                         filter => sub { $_[0] eq $params{page} },
223                         sort => exists $params{sort} ? $params{sort} : "age",
224                         reverse => yesno($params{reverse}),
225                         ($num ? (num => $num) : ()),
226                 );
227         }
228
229         if (exists $params{skip}) {
230                 @list=@list[$params{skip} .. $#list];
231         }
232         
233         my @feedlist;
234         if ($feeds) {
235                 if (exists $params{feedshow} &&
236                     $params{feedshow} && @list > $params{feedshow}) {
237                         @feedlist=@list[0..$params{feedshow} - 1];
238                 }
239                 else {
240                         @feedlist=@list;
241                 }
242         }
243         
244         if ($params{show} && @list > $params{show}) {
245                 @list=@list[0..$params{show} - 1];
246         }
247
248         if ($feeds && exists $params{feedpages}) {
249                 @feedlist = pagespec_match_list(
250                         $params{page}, "($params{pages}) and ($params{feedpages})",
251                         deptype => deptype($quick ? "presence" : "content"),
252                         list => \@feedlist,
253                 );
254         }
255
256         my ($feedbase, $feednum);
257         if ($feeds) {
258                 # Ensure that multiple feeds on a page go to unique files.
259                 
260                 # Feedfile can lead to conflicts if usedirs is not enabled,
261                 # so avoid supporting it in that case.
262                 delete $params{feedfile} if ! $config{usedirs};
263                 # Tight limits on legal feedfiles, to avoid security issues
264                 # and conflicts.
265                 if (defined $params{feedfile}) {
266                         if ($params{feedfile} =~ /\// ||
267                             $params{feedfile} !~ /$config{wiki_file_regexp}/) {
268                                 error("illegal feedfile");
269                         }
270                         $params{feedfile}=possibly_foolish_untaint($params{feedfile});
271                 }
272                 $feedbase=targetpage($params{page}, "", $params{feedfile});
273
274                 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
275                 if (exists $knownfeeds{$feedid}) {
276                         $feednum=$knownfeeds{$feedid};
277                 }
278                 else {
279                         if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
280                                 if ($feeds) {
281                                         $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
282                                 }
283                         }
284                         else {
285                                 $feednum=$knownfeeds{$feedid}="";
286                                 if ($feeds) {
287                                         $page_numfeeds{$params{destpage}}{$feedbase}=1;
288                                 }
289                         }
290                 }
291         }
292
293         my ($rssurl, $atomurl, $rssdesc, $atomdesc);
294         if ($feeds) {
295                 if ($rss) {
296                         $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage})));
297                         $rssdesc = sprintf(gettext("%s (RSS feed)"), $desc);
298                 }
299                 if ($atom) {
300                         $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage})));
301                         $atomdesc = sprintf(gettext("%s (Atom feed)"), $desc);
302                 }
303         }
304
305         my $ret="";
306
307         if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
308             (exists $params{postform} && yesno($params{postform}))) &&
309             IkiWiki->can("cgi_editpage")) {
310                 # Add a blog post form, with feed buttons.
311                 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
312                 $formtemplate->param(cgiurl => IkiWiki::cgiurl());
313                 $formtemplate->param(rootpage => rootpage(%params));
314                 if ($feeds) {
315                         if ($rss) {
316                                 $formtemplate->param(rssurl => $rssurl);
317                                 $formtemplate->param(rssdesc => $rssdesc);
318                         }
319                         if ($atom) {
320                                 $formtemplate->param(atomurl => $atomurl);
321                                 $formtemplate->param(atomdesc => $atomdesc);
322                         }
323                 }
324                 if (exists $params{postformtext}) {
325                         $formtemplate->param(postformtext =>
326                                 $params{postformtext});
327                 }
328                 else {
329                         $formtemplate->param(postformtext =>
330                                 gettext("Add a new post titled:"));
331                 }
332                 if (exists $params{id}) {
333                         $formtemplate->param(postformid =>
334                                 $params{id});
335                 }
336                 $ret.=$formtemplate->output;
337                 
338                 # The post form includes the feed buttons, so
339                 # emptyfeeds cannot be hidden.
340                 $emptyfeeds=1;
341         }
342         elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
343                 # Add feed buttons.
344                 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
345                 if ($rss) {
346                         $linktemplate->param(rssurl => $rssurl);
347                         $linktemplate->param(rssdesc => $rssdesc);
348                 }
349                 if ($atom) {
350                         $linktemplate->param(atomurl => $atomurl);
351                         $linktemplate->param(atomdesc => $atomdesc);
352                 }
353                 if (exists $params{id}) {
354                         $linktemplate->param(id => $params{id});
355                 }
356                 $ret.=$linktemplate->output;
357         }
358         
359         if (! $feedonly) {
360                 my $template;
361                 if (! $raw) {
362                         # cannot use wiki pages as templates; template not sanitized due to
363                         # format hook hack
364                         eval {
365                                 $template=template_depends($params{template}.".tmpl", $params{page},
366                                         blind_cache => 1);
367                         };
368                         if ($@) {
369                                 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $@";
370                         }
371                 }
372                 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
373         
374                 foreach my $page (@list) {
375                         my $file = $pagesources{$page};
376                         my $type = pagetype($file);
377                         if (! $raw) {
378                                 if ($needcontent) {
379                                         # Get the content before populating the
380                                         # template, since getting the content uses
381                                         # the same template if inlines are nested.
382                                         my $content=get_inline_content($page, $params{destpage});
383                                         $template->param(content => $content);
384                                 }
385                                 $template->param(pageurl => urlto($page, $params{destpage}));
386                                 $template->param(inlinepage => $page);
387                                 $template->param(title => pagetitle(basename($page)));
388                                 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
389                                 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
390                                 $template->param(first => 1) if $page eq $list[0];
391                                 $template->param(last => 1) if $page eq $list[$#list];
392                                 $template->param(html5 => $config{html5});
393         
394                                 if ($actions) {
395                                         my $file = $pagesources{$page};
396                                         my $type = pagetype($file);
397                                         if ($config{discussion}) {
398                                                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
399                                                     (length $config{cgiurl} ||
400                                                      exists $pagesources{$page."/".lc($config{discussionpage})})) {
401                                                         $template->param(have_actions => 1);
402                                                         $template->param(discussionlink =>
403                                                                 htmllink($page,
404                                                                         $params{destpage},
405                                                                         $config{discussionpage},
406                                                                         noimageinline => 1,
407                                                                         forcesubpage => 1));
408                                                 }
409                                         }
410                                         if (length $config{cgiurl} &&
411                                             defined $type &&
412                                             IkiWiki->can("cgi_editpage")) {
413                                                 $template->param(have_actions => 1);
414                                                 $template->param(editurl => cgiurl(do => "edit", page => $page));
415
416                                         }
417                                 }
418         
419                                 run_hooks(pagetemplate => sub {
420                                         shift->(page => $page, destpage => $params{destpage},
421                                                 template => $template,);
422                                 });
423         
424                                 $ret.=$template->output;
425                                 $template->clear_params;
426                         }
427                         else {
428                                 if (defined $type) {
429                                         $ret.="\n".
430                                               linkify($page, $params{destpage},
431                                               preprocess($page, $params{destpage},
432                                               filter($page, $params{destpage},
433                                               readfile(srcfile($file)))));
434                                 }
435                                 else {
436                                         $ret.="\n".
437                                               readfile(srcfile($file));
438                                 }
439                         }
440                 }
441         }
442         
443         if ($feeds && ($emptyfeeds || @feedlist)) {
444                 if ($rss) {
445                         my $rssp=$feedbase."rss".$feednum;
446                         will_render($params{destpage}, $rssp);
447                         if (! $params{preview}) {
448                                 writefile($rssp, $config{destdir},
449                                         genfeed("rss",
450                                                 $config{url}."/".$rssp, $desc, $params{guid}, $params{page}, @feedlist));
451                                 $toping{$params{destpage}}=1 unless $config{rebuild};
452                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" href="$rssurl" />};
453                         }
454                 }
455                 if ($atom) {
456                         my $atomp=$feedbase."atom".$feednum;
457                         will_render($params{destpage}, $atomp);
458                         if (! $params{preview}) {
459                                 writefile($atomp, $config{destdir},
460                                         genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{page}, @feedlist));
461                                 $toping{$params{destpage}}=1 unless $config{rebuild};
462                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
463                         }
464                 }
465         }
466         
467         clear_inline_content_cache();
468
469         return $ret if $raw || $nested;
470         push @inline, $ret;
471         return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
472 }
473
474 sub pagetemplate_inline (@) {
475         my %params=@_;
476         my $page=$params{page};
477         my $template=$params{template};
478
479         $template->param(feedlinks => $feedlinks{$page})
480                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
481 }
482
483 {
484 my %inline_content;
485 my $cached_destpage="";
486
487 sub get_inline_content ($$) {
488         my $page=shift;
489         my $destpage=shift;
490         
491         if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
492                 return $inline_content{$page};
493         }
494
495         my $file=$pagesources{$page};
496         my $type=pagetype($file);
497         my $ret="";
498         if (defined $type) {
499                 $nested++;
500                 $ret=htmlize($page, $destpage, $type,
501                        linkify($page, $destpage,
502                        preprocess($page, $destpage,
503                        filter($page, $destpage,
504                        readfile(srcfile($file))))));
505                 $nested--;
506                 if (isinternal($page)) {
507                         # make inlined text of internal pages searchable
508                         run_hooks(indexhtml => sub {
509                                 shift->(page => $page, destpage => $destpage,
510                                         content => $ret);
511                         });
512                 }
513         }
514         
515         if ($cached_destpage ne $destpage) {
516                 clear_inline_content_cache();
517                 $cached_destpage=$destpage;
518         }
519         return $inline_content{$page}=$ret;
520 }
521
522 sub clear_inline_content_cache () {
523         %inline_content=();
524 }
525
526 }
527
528 sub date_822 ($) {
529         my $time=shift;
530
531         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
532         POSIX::setlocale(&POSIX::LC_TIME, "C");
533         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
534         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
535         return $ret;
536 }
537
538 sub absolute_urls ($$) {
539         # needed because rss sucks
540         my $html=shift;
541         my $baseurl=shift;
542
543         my $url=$baseurl;
544         $url=~s/[^\/]+$//;
545         my $urltop; # calculated if needed
546
547         my $ret="";
548
549         eval q{use HTML::Parser; use HTML::Tagset};
550         die $@ if $@;
551         my $p = HTML::Parser->new(api_version => 3);
552         $p->handler(default => sub { $ret.=join("", @_) }, "text");
553         $p->handler(start => sub {
554                 my ($tagname, $pos, $text) = @_;
555                 if (ref $HTML::Tagset::linkElements{$tagname}) {
556                         while (4 <= @$pos) {
557                                 # use attribute sets from right to left
558                                 # to avoid invalidating the offsets
559                                 # when replacing the values
560                                 my ($k_offset, $k_len, $v_offset, $v_len) =
561                                         splice(@$pos, -4);
562                                 my $attrname = lc(substr($text, $k_offset, $k_len));
563                                 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
564                                 next unless $v_offset; # 0 v_offset means no value
565                                 my $v = substr($text, $v_offset, $v_len);
566                                 $v =~ s/^([\'\"])(.*)\1$/$2/;
567                                 eval q{use HTML::Entities};
568                                 my $dv = decode_entities($v);
569                                 if ($dv=~/^#/) {
570                                         $v=$baseurl.$v; # anchor
571                                 }
572                                 elsif ($dv=~/^(?!\w+:)[^\/]/) {
573                                         $v=$url.$v; # relative url
574                                 }
575                                 elsif ($dv=~/^\//) {
576                                         if (! defined $urltop) {
577                                                 # what is the non path part of the url?
578                                                 my $top_uri = URI->new($url);
579                                                 $top_uri->path_query(""); # reset the path
580                                                 $urltop = $top_uri->as_string;
581                                         }
582                                         $v=$urltop.$v; # url relative to top of site
583                                 }
584                                 $v =~ s/\"/&quot;/g; # since we quote with ""
585                                 substr($text, $v_offset, $v_len) = qq("$v");
586                         }
587                 }
588                 $ret.=$text;
589         }, "tagname, tokenpos, text");
590         $p->parse($html);
591         $p->eof;
592
593         return $ret;
594 }
595
596 sub genfeed ($$$$$@) {
597         my $feedtype=shift;
598         my $feedurl=shift;
599         my $feeddesc=shift;
600         my $guid=shift;
601         my $page=shift;
602         my @pages=@_;
603         
604         my $url=URI->new(encode_utf8(urlto($page,"",1)));
605         
606         my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
607         my $content="";
608         my $lasttime = 0;
609         foreach my $p (@pages) {
610                 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
611                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
612
613                 $itemtemplate->param(
614                         title => pagetitle(basename($p)),
615                         url => $u,
616                         permalink => $u,
617                         cdate_822 => date_822($pagectime{$p}),
618                         mdate_822 => date_822($pagemtime{$p}),
619                         cdate_3339 => date_3339($pagectime{$p}),
620                         mdate_3339 => date_3339($pagemtime{$p}),
621                 );
622
623                 if (exists $pagestate{$p}) {
624                         if (exists $pagestate{$p}{meta}{guid}) {
625                                 eval q{use HTML::Entities};
626                                 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
627                         }
628
629                         if (exists $pagestate{$p}{meta}{updated}) {
630                                 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
631                                 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
632                         }
633                 }
634
635                 if ($itemtemplate->query(name => "enclosure")) {
636                         my $file=$pagesources{$p};
637                         my $type=pagetype($file);
638                         if (defined $type) {
639                                 $itemtemplate->param(content => $pcontent);
640                         }
641                         else {
642                                 my $size=(srcfile_stat($file))[8];
643                                 my $mime="unknown";
644                                 eval q{use File::MimeInfo};
645                                 if (! $@) {
646                                         $mime = mimetype($file);
647                                 }
648                                 $itemtemplate->param(
649                                         enclosure => $u,
650                                         type => $mime,
651                                         length => $size,
652                                 );
653                         }
654                 }
655                 else {
656                         $itemtemplate->param(content => $pcontent);
657                 }
658
659                 run_hooks(pagetemplate => sub {
660                         shift->(page => $p, destpage => $page,
661                                 template => $itemtemplate);
662                 });
663
664                 $content.=$itemtemplate->output;
665                 $itemtemplate->clear_params;
666
667                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
668         }
669
670         my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
671         $template->param(
672                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
673                 wikiname => $config{wikiname},
674                 pageurl => $url,
675                 content => $content,
676                 feeddesc => $feeddesc,
677                 guid => $guid,
678                 feeddate => date_3339($lasttime),
679                 feedurl => $feedurl,
680                 version => $IkiWiki::version,
681         );
682         run_hooks(pagetemplate => sub {
683                 shift->(page => $page, destpage => $page,
684                         template => $template);
685         });
686         
687         return $template->output;
688 }
689
690 sub pingurl (@) {
691         return unless @{$config{pingurl}} && %toping;
692
693         eval q{require RPC::XML::Client};
694         if ($@) {
695                 debug(gettext("RPC::XML::Client not found, not pinging"));
696                 return;
697         }
698
699         # daemonize here so slow pings don't slow down wiki updates
700         defined(my $pid = fork) or error("Can't fork: $!");
701         return if $pid;
702         chdir '/';
703         POSIX::setsid() or error("Can't start a new session: $!");
704         open STDIN, '/dev/null';
705         open STDOUT, '>/dev/null';
706         open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
707
708         # Don't need to keep a lock on the wiki as a daemon.
709         IkiWiki::unlockwiki();
710
711         foreach my $page (keys %toping) {
712                 my $title=pagetitle(basename($page), 0);
713                 my $url=urlto($page, "", 1);
714                 foreach my $pingurl (@{$config{pingurl}}) {
715                         debug("Pinging $pingurl for $page");
716                         eval {
717                                 my $client = RPC::XML::Client->new($pingurl);
718                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
719                                         $title, $url);
720                                 my $res = $client->send_request($req);
721                                 if (! ref $res) {
722                                         error("Did not receive response to ping");
723                                 }
724                                 my $r=$res->value;
725                                 if (! exists $r->{flerror} || $r->{flerror}) {
726                                         error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
727                                 }
728                         };
729                         if ($@) {
730                                 error "Ping failed: $@";
731                         }
732                 }
733         }
734
735         exit 0; # daemon done
736 }
737
738
739 sub rootpage (@) {
740         my %params=@_;
741
742         my $rootpage;
743         if (exists $params{rootpage}) {
744                 $rootpage=bestlink($params{page}, $params{rootpage});
745                 if (!length $rootpage) {
746                         $rootpage=$params{rootpage};
747                 }
748         }
749         else {
750                 $rootpage=$params{page};
751         }
752         return $rootpage;
753 }
754
755 1