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