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