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