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