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