Added a "changes" hook. Renamed the "change" hook to "rendered", but
[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 => "rendered", 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 = grep { $_ ne '' } 
198                         map { bestlink($params{page}, $_) }
199                         split ' ', $params{pagenames};
200
201                 if (yesno($params{reverse})) {
202                         @list=reverse(@list);
203                 }
204
205                 foreach my $p (@list) {
206                         add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
207                 }
208         }
209         else {
210                 my $num=0;
211                 if ($params{show}) {
212                         $num=$params{show};
213                 }
214                 if ($params{feedshow} && $num < $params{feedshow} && $num > 0) {
215                         $num=$params{feedshow};
216                 }
217                 if ($params{skip} && $num) {
218                         $num+=$params{skip};
219                 }
220
221                 @list = pagespec_match_list($params{page}, $params{pages},
222                         deptype => deptype($quick ? "presence" : "content"),
223                         filter => sub { $_[0] eq $params{page} },
224                         sort => exists $params{sort} ? $params{sort} : "age",
225                         reverse => yesno($params{reverse}),
226                         ($num ? (num => $num) : ()),
227                 );
228         }
229
230         if (exists $params{skip}) {
231                 @list=@list[$params{skip} .. $#list];
232         }
233         
234         my @feedlist;
235         if ($feeds) {
236                 if (exists $params{feedshow} &&
237                     $params{feedshow} && @list > $params{feedshow}) {
238                         @feedlist=@list[0..$params{feedshow} - 1];
239                 }
240                 else {
241                         @feedlist=@list;
242                 }
243         }
244         
245         if ($params{show} && @list > $params{show}) {
246                 @list=@list[0..$params{show} - 1];
247         }
248
249         if ($feeds && exists $params{feedpages}) {
250                 @feedlist = pagespec_match_list(
251                         $params{page}, "($params{pages}) and ($params{feedpages})",
252                         deptype => deptype($quick ? "presence" : "content"),
253                         list => \@feedlist,
254                 );
255         }
256
257         my ($feedbase, $feednum);
258         if ($feeds) {
259                 # Ensure that multiple feeds on a page go to unique files.
260                 
261                 # Feedfile can lead to conflicts if usedirs is not enabled,
262                 # so avoid supporting it in that case.
263                 delete $params{feedfile} if ! $config{usedirs};
264                 # Tight limits on legal feedfiles, to avoid security issues
265                 # and conflicts.
266                 if (defined $params{feedfile}) {
267                         if ($params{feedfile} =~ /\// ||
268                             $params{feedfile} !~ /$config{wiki_file_regexp}/) {
269                                 error("illegal feedfile");
270                         }
271                         $params{feedfile}=possibly_foolish_untaint($params{feedfile});
272                 }
273                 $feedbase=targetpage($params{page}, "", $params{feedfile});
274
275                 my $feedid=join("\0", $feedbase, map { $_."\0".$params{$_} } sort keys %params);
276                 if (exists $knownfeeds{$feedid}) {
277                         $feednum=$knownfeeds{$feedid};
278                 }
279                 else {
280                         if (exists $page_numfeeds{$params{destpage}}{$feedbase}) {
281                                 if ($feeds) {
282                                         $feednum=$knownfeeds{$feedid}=++$page_numfeeds{$params{destpage}}{$feedbase};
283                                 }
284                         }
285                         else {
286                                 $feednum=$knownfeeds{$feedid}="";
287                                 if ($feeds) {
288                                         $page_numfeeds{$params{destpage}}{$feedbase}=1;
289                                 }
290                         }
291                 }
292         }
293
294         my ($rssurl, $atomurl, $rssdesc, $atomdesc);
295         if ($feeds) {
296                 if ($rss) {
297                         $rssurl=abs2rel($feedbase."rss".$feednum, dirname(htmlpage($params{destpage})));
298                         $rssdesc = sprintf(gettext("%s (RSS feed)"), $desc);
299                 }
300                 if ($atom) {
301                         $atomurl=abs2rel($feedbase."atom".$feednum, dirname(htmlpage($params{destpage})));
302                         $atomdesc = sprintf(gettext("%s (Atom feed)"), $desc);
303                 }
304         }
305
306         my $ret="";
307
308         if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
309             (exists $params{postform} && yesno($params{postform}))) &&
310             IkiWiki->can("cgi_editpage")) {
311                 # Add a blog post form, with feed buttons.
312                 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
313                 $formtemplate->param(cgiurl => IkiWiki::cgiurl());
314                 $formtemplate->param(rootpage => rootpage(%params));
315                 if ($feeds) {
316                         if ($rss) {
317                                 $formtemplate->param(rssurl => $rssurl);
318                                 $formtemplate->param(rssdesc => $rssdesc);
319                         }
320                         if ($atom) {
321                                 $formtemplate->param(atomurl => $atomurl);
322                                 $formtemplate->param(atomdesc => $atomdesc);
323                         }
324                 }
325                 if (exists $params{postformtext}) {
326                         $formtemplate->param(postformtext =>
327                                 $params{postformtext});
328                 }
329                 else {
330                         $formtemplate->param(postformtext =>
331                                 gettext("Add a new post titled:"));
332                 }
333                 if (exists $params{id}) {
334                         $formtemplate->param(postformid =>
335                                 $params{id});
336                 }
337                 $ret.=$formtemplate->output;
338                 
339                 # The post form includes the feed buttons, so
340                 # emptyfeeds cannot be hidden.
341                 $emptyfeeds=1;
342         }
343         elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
344                 # Add feed buttons.
345                 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
346                 if ($rss) {
347                         $linktemplate->param(rssurl => $rssurl);
348                         $linktemplate->param(rssdesc => $rssdesc);
349                 }
350                 if ($atom) {
351                         $linktemplate->param(atomurl => $atomurl);
352                         $linktemplate->param(atomdesc => $atomdesc);
353                 }
354                 if (exists $params{id}) {
355                         $linktemplate->param(id => $params{id});
356                 }
357                 $ret.=$linktemplate->output;
358         }
359         
360         if (! $feedonly) {
361                 my $template;
362                 if (! $raw) {
363                         # cannot use wiki pages as templates; template not sanitized due to
364                         # format hook hack
365                         eval {
366                                 $template=template_depends($params{template}.".tmpl", $params{page},
367                                         blind_cache => 1);
368                         };
369                         if ($@) {
370                                 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $@";
371                         }
372                 }
373                 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
374         
375                 foreach my $page (@list) {
376                         my $file = $pagesources{$page};
377                         my $type = pagetype($file);
378                         if (! $raw) {
379                                 if ($needcontent) {
380                                         # Get the content before populating the
381                                         # template, since getting the content uses
382                                         # the same template if inlines are nested.
383                                         my $content=get_inline_content($page, $params{destpage});
384                                         $template->param(content => $content);
385                                 }
386                                 $template->param(pageurl => urlto($page, $params{destpage}));
387                                 $template->param(inlinepage => $page);
388                                 $template->param(title => pagetitle(basename($page)));
389                                 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
390                                 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
391                                 $template->param(first => 1) if $page eq $list[0];
392                                 $template->param(last => 1) if $page eq $list[$#list];
393                                 $template->param(html5 => $config{html5});
394         
395                                 if ($actions) {
396                                         my $file = $pagesources{$page};
397                                         my $type = pagetype($file);
398                                         if ($config{discussion}) {
399                                                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
400                                                     (length $config{cgiurl} ||
401                                                      exists $pagesources{$page."/".lc($config{discussionpage})})) {
402                                                         $template->param(have_actions => 1);
403                                                         $template->param(discussionlink =>
404                                                                 htmllink($page,
405                                                                         $params{destpage},
406                                                                         $config{discussionpage},
407                                                                         noimageinline => 1,
408                                                                         forcesubpage => 1));
409                                                 }
410                                         }
411                                         if (length $config{cgiurl} &&
412                                             defined $type &&
413                                             IkiWiki->can("cgi_editpage")) {
414                                                 $template->param(have_actions => 1);
415                                                 $template->param(editurl => cgiurl(do => "edit", page => $page));
416
417                                         }
418                                 }
419         
420                                 run_hooks(pagetemplate => sub {
421                                         shift->(page => $page, destpage => $params{destpage},
422                                                 template => $template,);
423                                 });
424         
425                                 $ret.=$template->output;
426                                 $template->clear_params;
427                         }
428                         else {
429                                 if (defined $type) {
430                                         $ret.="\n".
431                                               linkify($page, $params{destpage},
432                                               preprocess($page, $params{destpage},
433                                               filter($page, $params{destpage},
434                                               readfile(srcfile($file)))));
435                                 }
436                                 else {
437                                         $ret.="\n".
438                                               readfile(srcfile($file));
439                                 }
440                         }
441                 }
442         }
443         
444         if ($feeds && ($emptyfeeds || @feedlist)) {
445                 if ($rss) {
446                         my $rssp=$feedbase."rss".$feednum;
447                         will_render($params{destpage}, $rssp);
448                         if (! $params{preview}) {
449                                 writefile($rssp, $config{destdir},
450                                         genfeed("rss",
451                                                 $config{url}."/".$rssp, $desc, $params{guid}, $params{page}, @feedlist));
452                                 $toping{$params{destpage}}=1 unless $config{rebuild};
453                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" href="$rssurl" />};
454                         }
455                 }
456                 if ($atom) {
457                         my $atomp=$feedbase."atom".$feednum;
458                         will_render($params{destpage}, $atomp);
459                         if (! $params{preview}) {
460                                 writefile($atomp, $config{destdir},
461                                         genfeed("atom", $config{url}."/".$atomp, $desc, $params{guid}, $params{page}, @feedlist));
462                                 $toping{$params{destpage}}=1 unless $config{rebuild};
463                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
464                         }
465                 }
466         }
467         
468         clear_inline_content_cache();
469
470         return $ret if $raw || $nested;
471         push @inline, $ret;
472         return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
473 }
474
475 sub pagetemplate_inline (@) {
476         my %params=@_;
477         my $page=$params{page};
478         my $template=$params{template};
479
480         $template->param(feedlinks => $feedlinks{$page})
481                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
482 }
483
484 {
485 my %inline_content;
486 my $cached_destpage="";
487
488 sub get_inline_content ($$) {
489         my $page=shift;
490         my $destpage=shift;
491         
492         if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
493                 return $inline_content{$page};
494         }
495
496         my $file=$pagesources{$page};
497         my $type=pagetype($file);
498         my $ret="";
499         if (defined $type) {
500                 $nested++;
501                 $ret=htmlize($page, $destpage, $type,
502                        linkify($page, $destpage,
503                        preprocess($page, $destpage,
504                        filter($page, $destpage,
505                        readfile(srcfile($file))))));
506                 $nested--;
507                 if (isinternal($page)) {
508                         # make inlined text of internal pages searchable
509                         run_hooks(indexhtml => sub {
510                                 shift->(page => $page, destpage => $destpage,
511                                         content => $ret);
512                         });
513                 }
514         }
515         
516         if ($cached_destpage ne $destpage) {
517                 clear_inline_content_cache();
518                 $cached_destpage=$destpage;
519         }
520         return $inline_content{$page}=$ret;
521 }
522
523 sub clear_inline_content_cache () {
524         %inline_content=();
525 }
526
527 }
528
529 sub date_822 ($) {
530         my $time=shift;
531
532         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
533         POSIX::setlocale(&POSIX::LC_TIME, "C");
534         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
535         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
536         return $ret;
537 }
538
539 sub absolute_urls ($$) {
540         # needed because rss sucks
541         my $html=shift;
542         my $baseurl=shift;
543
544         my $url=$baseurl;
545         $url=~s/[^\/]+$//;
546         my $urltop; # calculated if needed
547
548         my $ret="";
549
550         eval q{use HTML::Parser; use HTML::Tagset};
551         die $@ if $@;
552         my $p = HTML::Parser->new(api_version => 3);
553         $p->handler(default => sub { $ret.=join("", @_) }, "text");
554         $p->handler(start => sub {
555                 my ($tagname, $pos, $text) = @_;
556                 if (ref $HTML::Tagset::linkElements{$tagname}) {
557                         while (4 <= @$pos) {
558                                 # use attribute sets from right to left
559                                 # to avoid invalidating the offsets
560                                 # when replacing the values
561                                 my ($k_offset, $k_len, $v_offset, $v_len) =
562                                         splice(@$pos, -4);
563                                 my $attrname = lc(substr($text, $k_offset, $k_len));
564                                 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
565                                 next unless $v_offset; # 0 v_offset means no value
566                                 my $v = substr($text, $v_offset, $v_len);
567                                 $v =~ s/^([\'\"])(.*)\1$/$2/;
568                                 eval q{use HTML::Entities};
569                                 my $dv = decode_entities($v);
570                                 if ($dv=~/^#/) {
571                                         $v=$baseurl.$v; # anchor
572                                 }
573                                 elsif ($dv=~/^(?!\w+:)[^\/]/) {
574                                         $v=$url.$v; # relative url
575                                 }
576                                 elsif ($dv=~/^\//) {
577                                         if (! defined $urltop) {
578                                                 # what is the non path part of the url?
579                                                 my $top_uri = URI->new($url);
580                                                 $top_uri->path_query(""); # reset the path
581                                                 $urltop = $top_uri->as_string;
582                                         }
583                                         $v=$urltop.$v; # url relative to top of site
584                                 }
585                                 $v =~ s/\"/&quot;/g; # since we quote with ""
586                                 substr($text, $v_offset, $v_len) = qq("$v");
587                         }
588                 }
589                 $ret.=$text;
590         }, "tagname, tokenpos, text");
591         $p->parse($html);
592         $p->eof;
593
594         return $ret;
595 }
596
597 sub genfeed ($$$$$@) {
598         my $feedtype=shift;
599         my $feedurl=shift;
600         my $feeddesc=shift;
601         my $guid=shift;
602         my $page=shift;
603         my @pages=@_;
604         
605         my $url=URI->new(encode_utf8(urlto($page,"",1)));
606         
607         my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
608         my $content="";
609         my $lasttime = 0;
610         foreach my $p (@pages) {
611                 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
612                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
613
614                 $itemtemplate->param(
615                         title => pagetitle(basename($p)),
616                         url => $u,
617                         permalink => $u,
618                         cdate_822 => date_822($pagectime{$p}),
619                         mdate_822 => date_822($pagemtime{$p}),
620                         cdate_3339 => date_3339($pagectime{$p}),
621                         mdate_3339 => date_3339($pagemtime{$p}),
622                 );
623
624                 if (exists $pagestate{$p}) {
625                         if (exists $pagestate{$p}{meta}{guid}) {
626                                 eval q{use HTML::Entities};
627                                 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
628                         }
629
630                         if (exists $pagestate{$p}{meta}{updated}) {
631                                 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
632                                 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
633                         }
634                 }
635
636                 if ($itemtemplate->query(name => "enclosure")) {
637                         my $file=$pagesources{$p};
638                         my $type=pagetype($file);
639                         if (defined $type) {
640                                 $itemtemplate->param(content => $pcontent);
641                         }
642                         else {
643                                 my $size=(srcfile_stat($file))[8];
644                                 my $mime="unknown";
645                                 eval q{use File::MimeInfo};
646                                 if (! $@) {
647                                         $mime = mimetype($file);
648                                 }
649                                 $itemtemplate->param(
650                                         enclosure => $u,
651                                         type => $mime,
652                                         length => $size,
653                                 );
654                         }
655                 }
656                 else {
657                         $itemtemplate->param(content => $pcontent);
658                 }
659
660                 run_hooks(pagetemplate => sub {
661                         shift->(page => $p, destpage => $page,
662                                 template => $itemtemplate);
663                 });
664
665                 $content.=$itemtemplate->output;
666                 $itemtemplate->clear_params;
667
668                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
669         }
670
671         my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
672         $template->param(
673                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
674                 wikiname => $config{wikiname},
675                 pageurl => $url,
676                 content => $content,
677                 feeddesc => $feeddesc,
678                 guid => $guid,
679                 feeddate => date_3339($lasttime),
680                 feedurl => $feedurl,
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