]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
* Avoid syntax errors in templates used by the template plugin crashing
[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 $feeds=exists $params{feeds} ? yesno($params{feeds}) : 1;
46         if (! exists $params{show} && ! $archive) {
47                 $params{show}=10;
48         }
49         my $desc;
50         if (exists $params{description}) {
51                 $desc = $params{description} 
52         } else {
53                 $desc = $config{wikiname};
54         }
55         my $actions=yesno($params{actions});
56
57         my @list;
58         foreach my $page (keys %pagesources) {
59                 next if $page eq $params{page};
60                 if (pagespec_match($page, $params{pages})) {
61                         push @list, $page;
62                 }
63         }
64
65         if (exists $params{sort} && $params{sort} eq 'title') {
66                 @list=sort @list;
67         }
68         elsif (! exists $params{sort} || $params{sort} eq 'age') {
69                 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
70         }
71         else {
72                 return "unknown sort type $params{sort}";
73         }
74
75         if ($params{show} && @list > $params{show}) {
76                 @list=@list[0..$params{show} - 1];
77         }
78
79         add_depends($params{page}, $params{pages});
80
81         my $rssurl=rsspage(basename($params{page}));
82         my $atomurl=atompage(basename($params{page}));
83         my $ret="";
84
85         if (exists $params{rootpage} && $config{cgiurl}) {
86                 # Add a blog post form, with feed buttons.
87                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
88                 $formtemplate->param(cgiurl => $config{cgiurl});
89                 $formtemplate->param(rootpage => $params{rootpage});
90                 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
91                 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
92                 $ret.=$formtemplate->output;
93         }
94         elsif ($feeds) {
95                 # Add feed buttons.
96                 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
97                 $linktemplate->param(rssurl => $rssurl) if $rss;
98                 $linktemplate->param(atomurl => $atomurl) if $atom;
99                 $ret.=$linktemplate->output;
100         }
101         
102         my $template=template(
103                 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
104                 blind_cache => 1,
105         ) unless $raw;
106         
107         foreach my $page (@list) {
108                 my $file = $pagesources{$page};
109                 my $type = pagetype($file);
110                 if (! $raw || ($raw && ! defined $type)) {
111                         # Get the content before populating the template,
112                         # since getting the content uses the same template
113                         # if inlines are nested.
114                         # TODO: if $archive=1, the only reason to do this
115                         # is to let the meta plugin get page title info; so stop
116                         # calling this next line then once the meta plugin can
117                         # store that accross runs (also tags plugin).
118                         my $content=get_inline_content($page, $params{destpage});
119                         # Don't use htmllink because this way the title is separate
120                         # and can be overridden by other plugins.
121                         my $link=bestlink($params{page}, $page);
122                         $link=htmlpage($link) if defined $type;
123                         $link=abs2rel($link, dirname($params{destpage}));
124                         $template->param(pageurl => $link);
125                         $template->param(title => pagetitle(basename($page)));
126                         $template->param(content => $content);
127                         $template->param(ctime => displaytime($pagectime{$page}));
128
129                         if ($actions) {
130                                 my $file = $pagesources{$page};
131                                 my $type = pagetype($file);
132                                 if ($config{discussion}) {
133                                         $template->param(have_actions => 1);
134                                         $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
135                                 }
136                                 if (length $config{cgiurl} && defined $type) {
137                                         $template->param(have_actions => 1);
138                                         $template->param(editurl => cgiurl(do => "edit", page => $page));
139                                 }
140                         }
141
142                         run_hooks(pagetemplate => sub {
143                                 shift->(page => $page, destpage => $params{page},
144                                         template => $template,);
145                         });
146
147                         $ret.=$template->output;
148                         $template->clear_params;
149                 }
150                 else {
151                         if (defined $type) {
152                                 $ret.="\n".
153                                       linkify($page, $params{page},
154                                       preprocess($page, $params{page},
155                                       filter($page,
156                                       readfile(srcfile($file)))));
157                         }
158                 }
159         }
160         
161         if ($feeds && $rss) {
162                 will_render($params{page}, rsspage($params{page}));
163                 writefile(rsspage($params{page}), $config{destdir},
164                         genfeed("rss", $rssurl, $desc, $params{page}, @list));
165                 $toping{$params{page}}=1 unless $config{rebuild};
166                 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
167         }
168         if ($feeds && $atom) {
169                 will_render($params{page}, atompage($params{page}));
170                 writefile(atompage($params{page}), $config{destdir},
171                         genfeed("atom", $atomurl, $desc, $params{page}, @list));
172                 $toping{$params{page}}=1 unless $config{rebuild};
173                 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
174         }
175         
176         return $ret;
177 } #}}}
178
179 sub pagetemplate_inline (@) { #{{{
180         my %params=@_;
181         my $page=$params{page};
182         my $template=$params{template};
183
184         $template->param(feedlinks => $feedlinks{$page})
185                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
186 } #}}}
187
188 sub get_inline_content ($$) { #{{{
189         my $page=shift;
190         my $destpage=shift;
191         
192         my $file=$pagesources{$page};
193         my $type=pagetype($file);
194         if (defined $type) {
195                 return htmlize($page, $type,
196                        linkify($page, $destpage,
197                        preprocess($page, $destpage,
198                        filter($page,
199                        readfile(srcfile($file))))));
200         }
201         else {
202                 return "";
203         }
204 } #}}}
205
206 sub date_822 ($) { #{{{
207         my $time=shift;
208
209         eval q{use POSIX};
210         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
211         POSIX::setlocale(&POSIX::LC_TIME, "C");
212         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
213         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
214         return $ret;
215 } #}}}
216
217 sub date_3339 ($) { #{{{
218         my $time=shift;
219
220         eval q{use POSIX};
221         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
222         POSIX::setlocale(&POSIX::LC_TIME, "C");
223         my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
224         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
225         return $ret;
226 } #}}}
227
228 sub absolute_urls ($$) { #{{{
229         # sucky sub because rss sucks
230         my $content=shift;
231         my $url=shift;
232
233         $url=~s/[^\/]+$//;
234         
235         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
236         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
237         return $content;
238 } #}}}
239
240 sub rsspage ($) { #{{{
241         my $page=shift;
242
243         return $page.".rss";
244 } #}}}
245
246 sub atompage ($) { #{{{
247         my $page=shift;
248
249         return $page.".atom";
250 } #}}}
251
252 sub genfeed ($$$$@) { #{{{
253         my $feedtype=shift;
254         my $feedurl=shift;
255         my $feeddesc=shift;
256         my $page=shift;
257         my @pages=@_;
258         
259         my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
260         
261         my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
262         my $content="";
263         my $lasttime = 0;
264         foreach my $p (@pages) {
265                 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
266
267                 $itemtemplate->param(
268                         title => pagetitle(basename($p)),
269                         url => $u,
270                         permalink => $u,
271                         date_822 => date_822($pagectime{$p}),
272                         date_3339 => date_3339($pagectime{$p}),
273                 );
274
275                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
276                 if ($itemtemplate->query(name => "enclosure")) {
277                         my $file=$pagesources{$p};
278                         my $type=pagetype($file);
279                         if (defined $type) {
280                                 $itemtemplate->param(content => $pcontent);
281                         }
282                         else {
283                                 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
284                                 my $mime="unknown";
285                                 eval q{use File::MimeInfo};
286                                 if (! $@) {
287                                         $mime = mimetype($file);
288                                 }
289                                 $itemtemplate->param(
290                                         enclosure => $u,
291                                         type => $mime,
292                                         length => $size,
293                                 );
294                         }
295                 }
296                 else {
297                         $itemtemplate->param(content => $pcontent);
298                 }
299
300                 run_hooks(pagetemplate => sub {
301                         shift->(page => $p, destpage => $page,
302                                 template => $itemtemplate);
303                 });
304
305                 $content.=$itemtemplate->output;
306                 $itemtemplate->clear_params;
307
308                 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
309         }
310
311         my $template=template($feedtype."page.tmpl", blind_cache => 1);
312         $template->param(
313                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
314                 wikiname => $config{wikiname},
315                 pageurl => $url,
316                 content => $content,
317                 feeddesc => $feeddesc,
318                 feeddate => date_3339($lasttime),
319                 feedurl => $feedurl,
320                 version => $IkiWiki::version,
321         );
322         run_hooks(pagetemplate => sub {
323                 shift->(page => $page, destpage => $page,
324                         template => $template);
325         });
326         
327         return $template->output;
328 } #}}}
329
330 sub pingurl (@) { #{{{
331         return unless $config{pingurl} && %toping;
332
333         eval q{require RPC::XML::Client};
334         if ($@) {
335                 debug("RPC::XML::Client not found, not pinging");
336                 return;
337         }
338
339         # TODO: daemonize here so slow pings don't slow down wiki updates
340
341         foreach my $page (keys %toping) {
342                 my $title=pagetitle(basename($page));
343                 my $url="$config{url}/".htmlpage($page);
344                 foreach my $pingurl (@{$config{pingurl}}) {
345                         debug("Pinging $pingurl for $page");
346                         eval {
347                                 my $client = RPC::XML::Client->new($pingurl);
348                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
349                                 $title, $url);
350                                 my $res = $client->send_request($req);
351                                 if (! ref $res) {
352                                         debug("Did not receive response to ping");
353                                 }
354                                 my $r=$res->value;
355                                 if (! exists $r->{flerror} || $r->{flerror}) {
356                                         debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
357                                 }
358                         };
359                         if ($@) {
360                                 debug "Ping failed: $@";
361                         }
362                 }
363         }
364 } #}}}
365
366 1