]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
* Fix an issue with inlining a page with a rss feed inside another
[ikiwiki.git] / IkiWiki / Plugin / inline.pm
1 #!/usr/bin/perl
2 # Page inlining and blogging.
3 package IkiWiki::Plugin::inline;
4
5 use warnings;
6 use strict;
7 use IkiWiki 1.00;
8 use IkiWiki::Render; # for displaytime
9 use URI;
10
11 sub import { #{{{
12         hook(type => "preprocess", id => "inline", 
13                 call => \&IkiWiki::preprocess_inline);
14         hook(type => "pagetemplate", id => "inline",
15                 call => \&IkiWiki::pagetemplate_inline);
16         # Hook to change to do pinging since it's called late.
17         # This ensures each page only pings once and prevents slow
18         # pings interrupting page builds.
19         hook(type => "change", id => "inline", 
20                 call => \&IkiWiki::pingurl);
21 } # }}}
22
23 # Back to ikiwiki namespace for the rest, this code is very much
24 # internal to ikiwiki even though it's separated into a plugin.
25 package IkiWiki;
26
27 my %toping;
28 my %feedlinks;
29
30 sub yesno ($) { #{{{
31         my $val=shift;
32         return (defined $val && lc($val) eq "yes");
33 } #}}}
34
35 sub preprocess_inline (@) { #{{{
36         my %params=@_;
37         
38         if (! exists $params{pages}) {
39                 return "";
40         }
41         my $raw=yesno($params{raw});
42         my $archive=yesno($params{archive});
43         my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
44         my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
45         my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
46         my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
47         if (! exists $params{show} && ! $archive) {
48                 $params{show}=10;
49         }
50         my $desc;
51         if (exists $params{description}) {
52                 $desc = $params{description} 
53         } else {
54                 $desc = $config{wikiname};
55         }
56         my $actions=yesno($params{actions});
57
58         my @list;
59         foreach my $page (keys %pagesources) {
60                 next if $page eq $params{page};
61                 if (pagespec_match($page, $params{pages})) {
62                         push @list, $page;
63                 }
64         }
65
66         if (exists $params{sort} && $params{sort} eq 'title') {
67                 @list=sort @list;
68         }
69         elsif (! exists $params{sort} || $params{sort} eq 'age') {
70                 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
71         }
72         else {
73                 return "unknown sort type $params{sort}";
74         }
75
76         if (exists $params{skip}) {
77                 @list=@list[$params{skip} .. scalar @list - 1];
78         }
79         
80         if ($params{show} && @list > $params{show}) {
81                 @list=@list[0..$params{show} - 1];
82         }
83
84         add_depends($params{page}, $params{pages});
85
86         my $rssurl=rsspage(basename($params{page}));
87         my $atomurl=atompage(basename($params{page}));
88         my $ret="";
89
90         if (exists $params{rootpage} && $config{cgiurl}) {
91                 # Add a blog post form, with feed buttons.
92                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
93                 $formtemplate->param(cgiurl => $config{cgiurl});
94                 $formtemplate->param(rootpage => $params{rootpage});
95                 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
96                 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
97                 $ret.=$formtemplate->output;
98         }
99         elsif ($feeds) {
100                 # Add feed buttons.
101                 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
102                 $linktemplate->param(rssurl => $rssurl) if $rss;
103                 $linktemplate->param(atomurl => $atomurl) if $atom;
104                 $ret.=$linktemplate->output;
105         }
106         
107         my $template=template(
108                 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
109                 blind_cache => 1,
110         ) unless $raw;
111         
112         foreach my $page (@list) {
113                 my $file = $pagesources{$page};
114                 my $type = pagetype($file);
115                 if (! $raw || ($raw && ! defined $type)) {
116                         unless ($archive && $quick) {
117                                 # Get the content before populating the
118                                 # template, since getting the content uses
119                                 # the same template if inlines are nested.
120                                 my $content=get_inline_content($page, $params{destpage});
121                                 $template->param(content => $content);
122                         }
123                         # Don't use htmllink because this way the
124                         # title is separate and can be overridden by
125                         # other plugins.
126                         my $link=bestlink($params{page}, $page);
127                         $link=htmlpage($link) if defined $type;
128                         $link=abs2rel($link, dirname($params{destpage}));
129                         $template->param(pageurl => $link);
130                         $template->param(title => pagetitle(basename($page)));
131                         $template->param(ctime => displaytime($pagectime{$page}));
132
133                         if ($actions) {
134                                 my $file = $pagesources{$page};
135                                 my $type = pagetype($file);
136                                 if ($config{discussion}) {
137                                         $template->param(have_actions => 1);
138                                         $template->param(discussionlink => htmllink($page, $params{page}, "Discussion", 1, 1));
139                                 }
140                                 if (length $config{cgiurl} && defined $type) {
141                                         $template->param(have_actions => 1);
142                                         $template->param(editurl => cgiurl(do => "edit", page => $page));
143                                 }
144                         }
145
146                         run_hooks(pagetemplate => sub {
147                                 shift->(page => $page, destpage => $params{page},
148                                         template => $template,);
149                         });
150
151                         $ret.=$template->output;
152                         $template->clear_params;
153                 }
154                 else {
155                         if (defined $type) {
156                                 $ret.="\n".
157                                       linkify($page, $params{page},
158                                       preprocess($page, $params{page},
159                                       filter($page,
160                                       readfile(srcfile($file)))));
161                         }
162                 }
163         }
164         
165         if ($feeds && $rss) {
166                 will_render($params{page}, rsspage($params{page}));
167                 writefile(rsspage($params{page}), $config{destdir},
168                         genfeed("rss", $rssurl, $desc, $params{page}, @list));
169                 $toping{$params{page}}=1 unless $config{rebuild};
170                 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
171         }
172         if ($feeds && $atom) {
173                 will_render($params{page}, atompage($params{page}));
174                 writefile(atompage($params{page}), $config{destdir},
175                         genfeed("atom", $atomurl, $desc, $params{page}, @list));
176                 $toping{$params{page}}=1 unless $config{rebuild};
177                 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
178         }
179         
180         return $ret;
181 } #}}}
182
183 sub pagetemplate_inline (@) { #{{{
184         my %params=@_;
185         my $page=$params{page};
186         my $template=$params{template};
187
188         $template->param(feedlinks => $feedlinks{$page})
189                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
190 } #}}}
191
192 sub get_inline_content ($$) { #{{{
193         my $page=shift;
194         my $destpage=shift;
195         
196         my $file=$pagesources{$page};
197         my $type=pagetype($file);
198         if (defined $type) {
199                 return htmlize($page, $type,
200                        linkify($page, $destpage,
201                        preprocess($page, $destpage,
202                        filter($page,
203                        readfile(srcfile($file))))));
204         }
205         else {
206                 return "";
207         }
208 } #}}}
209
210 sub date_822 ($) { #{{{
211         my $time=shift;
212
213         eval q{use POSIX};
214         error($@) if $@;
215         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
216         POSIX::setlocale(&POSIX::LC_TIME, "C");
217         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
218         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
219         return $ret;
220 } #}}}
221
222 sub date_3339 ($) { #{{{
223         my $time=shift;
224
225         eval q{use POSIX};
226         error($@) if $@;
227         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
228         POSIX::setlocale(&POSIX::LC_TIME, "C");
229         my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
230         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
231         return $ret;
232 } #}}}
233
234 sub absolute_urls ($$) { #{{{
235         # sucky sub because rss sucks
236         my $content=shift;
237         my $baseurl=shift;
238
239         my $url=$baseurl;
240         $url=~s/[^\/]+$//;
241         
242         $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(#[^"]+)"/$1 href="$baseurl$2"/ig;
243         $content=~s/(<a(?:\s+(?:class|id)="?\w+"?)?)\s+href="(?![^:]+:\/\/)([^"]+)"/$1 href="$url$2"/ig;
244         $content=~s/(<img(?:\s+(?:class|id)="?\w+"?)?)\s+src="(?![^:]+:\/\/)([^"]+)"/$1 src="$url$2"/ig;
245         return $content;
246 } #}}}
247
248 sub rsspage ($) { #{{{
249         my $page=shift;
250
251         return $page.".rss";
252 } #}}}
253
254 sub atompage ($) { #{{{
255         my $page=shift;
256
257         return $page.".atom";
258 } #}}}
259
260 sub genfeed ($$$$@) { #{{{
261         my $feedtype=shift;
262         my $feedurl=shift;
263         my $feeddesc=shift;
264         my $page=shift;
265         my @pages=@_;
266         
267         my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
268         
269         my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
270         my $content="";
271         my $lasttime = 0;
272         foreach my $p (@pages) {
273                 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
274                 
275                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
276
277                 $itemtemplate->param(
278                         title => pagetitle(basename($p)),
279                         url => $u,
280                         permalink => $u,
281                         date_822 => date_822($pagectime{$p}),
282                         date_3339 => date_3339($pagectime{$p}),
283                 );
284
285                 if ($itemtemplate->query(name => "enclosure")) {
286                         my $file=$pagesources{$p};
287                         my $type=pagetype($file);
288                         if (defined $type) {
289                                 $itemtemplate->param(content => $pcontent);
290                         }
291                         else {
292                                 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
293                                 my $mime="unknown";
294                                 eval q{use File::MimeInfo};
295                                 if (! $@) {
296                                         $mime = mimetype($file);
297                                 }
298                                 $itemtemplate->param(
299                                         enclosure => $u,
300                                         type => $mime,
301                                         length => $size,
302                                 );
303                         }
304                 }
305                 else {
306                         $itemtemplate->param(content => $pcontent);
307                 }
308
309                 run_hooks(pagetemplate => sub {
310                         shift->(page => $p, destpage => $page,
311                                 template => $itemtemplate);
312                 });
313
314                 $content.=$itemtemplate->output;
315                 $itemtemplate->clear_params;
316
317                 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
318         }
319
320         my $template=template($feedtype."page.tmpl", blind_cache => 1);
321         $template->param(
322                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
323                 wikiname => $config{wikiname},
324                 pageurl => $url,
325                 content => $content,
326                 feeddesc => $feeddesc,
327                 feeddate => date_3339($lasttime),
328                 feedurl => $feedurl,
329                 version => $IkiWiki::version,
330         );
331         run_hooks(pagetemplate => sub {
332                 shift->(page => $page, destpage => $page,
333                         template => $template);
334         });
335         
336         return $template->output;
337 } #}}}
338
339 sub pingurl (@) { #{{{
340         return unless @{$config{pingurl}} && %toping;
341
342         eval q{require RPC::XML::Client};
343         if ($@) {
344                 debug("RPC::XML::Client not found, not pinging");
345                 return;
346         }
347
348         # daemonize here so slow pings don't slow down wiki updates
349         defined(my $pid = fork) or error("Can't fork: $!");
350         return if $pid;
351         chdir '/';
352         eval q{use POSIX ’setsid’};
353         setsid() or error("Can't start a new session: $!");
354         open STDIN, '/dev/null';
355         open STDOUT, '>/dev/null';
356         open STDERR, '>&STDOUT' or error("Can’t dup stdout: $!");
357
358         # Don't need to keep a lock on the wiki as a daemon.
359         IkiWiki::unlockwiki();
360
361         foreach my $page (keys %toping) {
362                 my $title=pagetitle(basename($page));
363                 my $url="$config{url}/".htmlpage($page);
364                 foreach my $pingurl (@{$config{pingurl}}) {
365                         debug("Pinging $pingurl for $page");
366                         eval {
367                                 my $client = RPC::XML::Client->new($pingurl);
368                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
369                                 $title, $url);
370                                 my $res = $client->send_request($req);
371                                 if (! ref $res) {
372                                         debug("Did not receive response to ping");
373                                 }
374                                 my $r=$res->value;
375                                 if (! exists $r->{flerror} || $r->{flerror}) {
376                                         debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
377                                 }
378                         };
379                         if ($@) {
380                                 debug "Ping failed: $@";
381                         }
382                 }
383         }
384
385         exit 0; # daemon done
386 } #}}}
387
388 1