]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/inline.pm
Simplifying code
[git.ikiwiki.info.git] / IkiWiki / Plugin / inline.pm
1 #!/usr/bin/perl
2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
5 use warnings;
6 use strict;
7 use Encode;
8 use IkiWiki 3.00;
9 use URI;
11 my %knownfeeds;
12 my %page_numfeeds;
13 my @inline;
14 my $nested=0;
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 }
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 }
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 }
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         }
106 sub format (@) {
107         my %params=@_;
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};
117 sub sessioncgi ($$) {
118         my $q=shift;
119         my $session=shift;
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         }
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;
149 my %toping;
150 my %feedlinks;
152 sub preprocess_inline (@) {
153         my %params=@_;
154         
155         if (! exists $params{pages} && ! exists $params{pagenames}) {
156                 error gettext("missing pages parameter");
157         }
159         if (! defined wantarray) {
160                 # Running in scan mode: only do the essentials
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                 }
168                 return;
169         }
171         if (yesno($params{trail}) && IkiWiki::Plugin::trail->can("preprocess_trailitems")) {
172                 scalar IkiWiki::Plugin::trail::preprocess_trailitems(sort => 'age', %params);
173         }
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         }
211         my @list;
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                 }
221                 @list = split ' ', $params{pagenames};
223                 if (yesno($params{reverse})) {
224                         @list=reverse(@list);
225                 }
227                 foreach my $p (@list) {
228                         add_depends($params{page}, $p, deptype($quick ? "presence" : "content"));
229                 }
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                 }
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         }
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         }
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         }
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});
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         }
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         }
330         my $ret="";
332         if (length $config{cgiurl} && ! $params{preview} && (exists $params{rootpage} ||
333             (exists $params{postform} && yesno($params{postform}))) &&
334             IkiWiki->can("cgi_editpage")) {
335                 # Add a blog post form, with feed buttons.
336                 my $formtemplate=template_depends("blogpost.tmpl", $params{page}, blind_cache => 1);
337                 $formtemplate->param(cgiurl => IkiWiki::cgiurl());
338                 $formtemplate->param(rootpage => rootpage(%params));
339                 if ($feeds) {
340                         if ($rss) {
341                                 $formtemplate->param(rssurl => $rssurl);
342                                 $formtemplate->param(rssdesc => $rssdesc);
343                         }
344                         if ($atom) {
345                                 $formtemplate->param(atomurl => $atomurl);
346                                 $formtemplate->param(atomdesc => $atomdesc);
347                         }
348                 }
349                 if (exists $params{postformtext}) {
350                         $formtemplate->param(postformtext =>
351                                 $params{postformtext});
352                 }
353                 else {
354                         $formtemplate->param(postformtext =>
355                                 gettext("Add a new post titled:"));
356                 }
357                 if (exists $params{id}) {
358                         $formtemplate->param(postformid =>
359                                 $params{id});
360                 }
361                 $ret.=$formtemplate->output;
362                 
363                 # The post form includes the feed buttons, so
364                 # emptyfeeds cannot be hidden.
365                 $emptyfeeds=1;
366         }
367         elsif ($feeds && !$params{preview} && ($emptyfeeds || @feedlist)) {
368                 # Add feed buttons.
369                 my $linktemplate=template_depends("feedlink.tmpl", $params{page}, blind_cache => 1);
370                 if ($rss) {
371                         $linktemplate->param(rssurl => $rssurl);
372                         $linktemplate->param(rssdesc => $rssdesc);
373                 }
374                 if ($atom) {
375                         $linktemplate->param(atomurl => $atomurl);
376                         $linktemplate->param(atomdesc => $atomdesc);
377                 }
378                 if (exists $params{id}) {
379                         $linktemplate->param(id => $params{id});
380                 }
381                 $ret.=$linktemplate->output;
382         }
383         
384         if (! $feedonly) {
385                 my $template;
386                 if (! $raw) {
387                         # cannot use wiki pages as templates; template not sanitized due to
388                         # format hook hack
389                         eval {
390                                 $template=template_depends($params{template}.".tmpl", $params{page},
391                                         blind_cache => 1);
392                         };
393                         if ($@) {
394                                 # gettext can clobber $@
395                                 my $error = $@;
396                                 error sprintf(gettext("failed to process template %s"), $params{template}.".tmpl").": $error";
397                         }
398                 }
399                 my $needcontent=$raw || (!($archive && $quick) && $template->query(name => 'content'));
400         
401                 foreach my $page (@list) {
402                         my $file = $pagesources{$page};
403                         my $type = pagetype($file);
404                         if (! $raw) {
405                                 if ($needcontent) {
406                                         # Get the content before populating the
407                                         # template, since getting the content uses
408                                         # the same template if inlines are nested.
409                                         my $content=get_inline_content($page, $params{destpage});
410                                         $template->param(content => $content);
411                                 }
412                                 $template->param(pageurl => urlto($page, $params{destpage}));
413                                 $template->param(inlinepage => $page);
414                                 $template->param(title => pagetitle(basename($page)));
415                                 $template->param(ctime => displaytime($pagectime{$page}, $params{timeformat}, 1));
416                                 $template->param(mtime => displaytime($pagemtime{$page}, $params{timeformat}));
417                                 $template->param(first => 1) if $page eq $list[0];
418                                 $template->param(last => 1) if $page eq $list[$#list];
419                                 $template->param(html5 => $config{html5});
420         
421                                 if ($actions) {
422                                         my $file = $pagesources{$page};
423                                         my $type = pagetype($file);
424                                         if ($config{discussion}) {
425                                                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
426                                                     (length $config{cgiurl} ||
427                                                      exists $pagesources{$page."/".lc($config{discussionpage})})) {
428                                                         $template->param(have_actions => 1);
429                                                         $template->param(discussionlink =>
430                                                                 htmllink($page,
431                                                                         $params{destpage},
432                                                                         $config{discussionpage},
433                                                                         noimageinline => 1,
434                                                                         forcesubpage => 1));
435                                                 }
436                                         }
437                                         if (length $config{cgiurl} &&
438                                             defined $type &&
439                                             IkiWiki->can("cgi_editpage")) {
440                                                 $template->param(have_actions => 1);
441                                                 $template->param(editurl => cgiurl(do => "edit", page => $page));
443                                         }
444                                 }
445         
446                                 run_hooks(pagetemplate => sub {
447                                         shift->(page => $page, destpage => $params{destpage},
448                                                 template => $template,);
449                                 });
450         
451                                 $ret.=$template->output;
452                                 $template->clear_params;
453                         }
454                         else {
455                                 if (defined $type) {
456                                         $ret.="\n".
457                                               linkify($page, $params{destpage},
458                                               preprocess($page, $params{destpage},
459                                               filter($page, $params{destpage},
460                                               readfile(srcfile($file)))));
461                                 }
462                                 else {
463                                         $ret.="\n".
464                                               readfile(srcfile($file));
465                                 }
466                         }
467                 }
468         }
469         
470         if ($feeds && ($emptyfeeds || @feedlist)) {
471                 if ($rss) {
472                         my $rssp=$feedbase."rss".$feednum;
473                         will_render($params{destpage}, $rssp);
474                         if (! $params{preview}) {
475                                 writefile($rssp, $config{destdir},
476                                         genfeed("rss",
477                                                 $config{url}."/".$rssp, $title, $desc, $params{guid}, $params{page}, @feedlist));
478                                 $toping{$params{destpage}}=1 unless $config{rebuild};
479                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/rss+xml" title="$rssdesc" href="$rssurl" />};
480                         }
481                 }
482                 if ($atom) {
483                         my $atomp=$feedbase."atom".$feednum;
484                         will_render($params{destpage}, $atomp);
485                         if (! $params{preview}) {
486                                 writefile($atomp, $config{destdir},
487                                         genfeed("atom", $config{url}."/".$atomp, $title, $desc, $params{guid}, $params{page}, @feedlist));
488                                 $toping{$params{destpage}}=1 unless $config{rebuild};
489                                 $feedlinks{$params{destpage}}.=qq{<link rel="alternate" type="application/atom+xml" title="$atomdesc" href="$atomurl" />};
490                         }
491                 }
492         }
493         
494         clear_inline_content_cache();
496         return $ret if $raw || $nested;
497         push @inline, $ret;
498         return "<div class=\"inline\" id=\"$#inline\"></div>\n\n";
501 sub pagetemplate_inline (@) {
502         my %params=@_;
503         my $page=$params{page};
504         my $template=$params{template};
506         $template->param(feedlinks => $feedlinks{$page})
507                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
511 my %inline_content;
512 my $cached_destpage="";
514 sub get_inline_content ($$) {
515         my $page=shift;
516         my $destpage=shift;
517         
518         if (exists $inline_content{$page} && $cached_destpage eq $destpage) {
519                 return $inline_content{$page};
520         }
522         my $file=$pagesources{$page};
523         my $type=pagetype($file);
524         my $ret="";
525         if (defined $type) {
526                 $nested++;
527                 $ret=htmlize($page, $destpage, $type,
528                        linkify($page, $destpage,
529                        preprocess($page, $destpage,
530                        filter($page, $destpage,
531                        readfile(srcfile($file))))));
532                 $nested--;
533                 if (isinternal($page)) {
534                         # make inlined text of internal pages searchable
535                         run_hooks(indexhtml => sub {
536                                 shift->(page => $page, destpage => $destpage,
537                                         content => $ret);
538                         });
539                 }
540         }
541         
542         if ($cached_destpage ne $destpage) {
543                 clear_inline_content_cache();
544                 $cached_destpage=$destpage;
545         }
546         return $inline_content{$page}=$ret;
549 sub clear_inline_content_cache () {
550         %inline_content=();
555 sub date_822 ($) {
556         my $time=shift;
558         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
559         POSIX::setlocale(&POSIX::LC_TIME, "C");
560         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
561         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
562         return $ret;
565 sub absolute_urls ($$) {
566         # needed because rss sucks
567         my $html=shift;
568         my $baseurl=shift;
570         my $url=$baseurl;
571         $url=~s/[^\/]+$//;
572         my $urltop; # calculated if needed
574         my $ret="";
576         eval q{use HTML::Parser; use HTML::Tagset};
577         die $@ if $@;
578         my $p = HTML::Parser->new(api_version => 3);
579         $p->handler(default => sub { $ret.=join("", @_) }, "text");
580         $p->handler(start => sub {
581                 my ($tagname, $pos, $text) = @_;
582                 if (ref $HTML::Tagset::linkElements{$tagname}) {
583                         while (4 <= @$pos) {
584                                 # use attribute sets from right to left
585                                 # to avoid invalidating the offsets
586                                 # when replacing the values
587                                 my ($k_offset, $k_len, $v_offset, $v_len) =
588                                         splice(@$pos, -4);
589                                 my $attrname = lc(substr($text, $k_offset, $k_len));
590                                 next unless grep { $_ eq $attrname } @{$HTML::Tagset::linkElements{$tagname}};
591                                 next unless $v_offset; # 0 v_offset means no value
592                                 my $v = substr($text, $v_offset, $v_len);
593                                 $v =~ s/^([\'\"])(.*)\1$/$2/;
594                                 eval q{use HTML::Entities};
595                                 my $dv = decode_entities($v);
596                                 if ($dv=~/^#/) {
597                                         $v=$baseurl.$v; # anchor
598                                 }
599                                 elsif ($dv=~/^(?!\w+:)[^\/]/) {
600                                         $v=$url.$v; # relative url
601                                 }
602                                 elsif ($dv=~/^\//) {
603                                         if (! defined $urltop) {
604                                                 # what is the non path part of the url?
605                                                 my $top_uri = URI->new($url);
606                                                 $top_uri->path_query(""); # reset the path
607                                                 $urltop = $top_uri->as_string;
608                                         }
609                                         $v=$urltop.$v; # url relative to top of site
610                                 }
611                                 $v =~ s/\"/&quot;/g; # since we quote with ""
612                                 substr($text, $v_offset, $v_len) = qq("$v");
613                         }
614                 }
615                 $ret.=$text;
616         }, "tagname, tokenpos, text");
617         $p->parse($html);
618         $p->eof;
620         return $ret;
623 sub genenclosure {
624         my $itemtemplate=shift;
625         my $url=shift;
626         my $file=shift;
628         return unless $itemtemplate->query(name => "enclosure");
630         my $size=(srcfile_stat($file))[8];
631         my $mime="unknown";
632         eval q{use File::MimeInfo};
633         if (! $@) {
634                 $mime = mimetype($file);
635         }
636         $itemtemplate->param(
637                 enclosure => $url,
638                 type => $mime,
639                 length => $size,
640         );
643 sub genfeed ($$$$$@) {
644         my $feedtype=shift;
645         my $feedurl=shift;
646         my $feedtitle=shift;
647         my $feeddesc=shift;
648         my $guid=shift;
649         my $page=shift;
650         my @pages=@_;
651         
652         my $url=URI->new(encode_utf8(urlto($page,"",1)));
653         
654         my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1);
655         my $content="";
656         my $lasttime = 0;
657         foreach my $p (@pages) {
658                 my $u=URI->new(encode_utf8(urlto($p, "", 1)));
659                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
660                 my $fancy_enclosure_seen = 0;
662                 $itemtemplate->param(
663                         title => pagetitle(basename($p)),
664                         url => $u,
665                         permalink => $u,
666                         cdate_822 => date_822($pagectime{$p}),
667                         mdate_822 => date_822($pagemtime{$p}),
668                         cdate_3339 => date_3339($pagectime{$p}),
669                         mdate_3339 => date_3339($pagemtime{$p}),
670                 );
672                 if (exists $pagestate{$p}) {
673                         if (exists $pagestate{$p}{meta}{guid}) {
674                                 eval q{use HTML::Entities};
675                                 $itemtemplate->param(guid => HTML::Entities::encode_numeric($pagestate{$p}{meta}{guid}));
676                         }
678                         if (exists $pagestate{$p}{meta}{updated}) {
679                                 $itemtemplate->param(mdate_822 => date_822($pagestate{$p}{meta}{updated}));
680                                 $itemtemplate->param(mdate_3339 => date_3339($pagestate{$p}{meta}{updated}));
681                         }
683                         if (exists $pagestate{$p}{meta}{enclosure}) {
684                                 my $absurl = $pagestate{$p}{meta}{enclosure};
685                                 my $file = $pagestate{$p}{meta}{enclosurefile};
686                                 genenclosure($itemtemplate, $absurl, $file);
687                                 $fancy_enclosure_seen = 1;
688                         }
689                 }
691                 my $file=$pagesources{$p};
692                 unless ($fancy_enclosure_seen || defined(pagetype($file))) {
693                         genenclosure($itemtemplate, $u, $file);
694                         $itemtemplate->param(simplepodcast => 1);
695                 }
697                 $itemtemplate->param(content => $pcontent);
699                 run_hooks(pagetemplate => sub {
700                         shift->(page => $p, destpage => $page,
701                                 template => $itemtemplate);
702                 });
704                 $content.=$itemtemplate->output;
705                 $itemtemplate->clear_params;
707                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
708         }
710         my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1);
711         $template->param(
712                 title => $feedtitle,
713                 wikiname => $config{wikiname},
714                 pageurl => $url,
715                 content => $content,
716                 feeddesc => $feeddesc,
717                 guid => $guid,
718                 feeddate => date_3339($lasttime),
719                 feeddate_822 => date_822($lasttime),
720                 feedurl => $feedurl,
721         );
722         run_hooks(pagetemplate => sub {
723                 shift->(page => $page, destpage => $page,
724                         template => $template);
725         });
726         
727         return $template->output;
730 sub pingurl (@) {
731         return unless @{$config{pingurl}} && %toping;
733         eval q{require RPC::XML::Client};
734         if ($@) {
735                 debug(gettext("RPC::XML::Client not found, not pinging"));
736                 return;
737         }
739         # daemonize here so slow pings don't slow down wiki updates
740         defined(my $pid = fork) or error("Can't fork: $!");
741         return if $pid;
742         chdir '/';
743         POSIX::setsid() or error("Can't start a new session: $!");
744         open STDIN, '/dev/null';
745         open STDOUT, '>/dev/null';
746         open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
748         # Don't need to keep a lock on the wiki as a daemon.
749         IkiWiki::unlockwiki();
751         foreach my $page (keys %toping) {
752                 my $title=pagetitle(basename($page), 0);
753                 my $url=urlto($page, "", 1);
754                 foreach my $pingurl (@{$config{pingurl}}) {
755                         debug("Pinging $pingurl for $page");
756                         eval {
757                                 my $client = RPC::XML::Client->new($pingurl);
758                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
759                                         $title, $url);
760                                 my $res = $client->send_request($req);
761                                 if (! ref $res) {
762                                         error("Did not receive response to ping");
763                                 }
764                                 my $r=$res->value;
765                                 if (! exists $r->{flerror} || $r->{flerror}) {
766                                         error("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
767                                 }
768                         };
769                         if ($@) {
770                                 error "Ping failed: $@";
771                         }
772                 }
773         }
775         exit 0; # daemon done
779 sub rootpage (@) {
780         my %params=@_;
782         my $rootpage;
783         if (exists $params{rootpage}) {
784                 $rootpage=bestlink($params{page}, $params{rootpage});
785                 if (!length $rootpage) {
786                         $rootpage=$params{rootpage};
787                 }
788         }
789         else {
790                 $rootpage=$params{page};
791         }
792         return $rootpage;