* Patch from Ethan Glasser Camp to add a skip option to the inline plugin.
[ikiwiki] / 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 (exists $params{skip}) {
76                 @list=@list[$params{skip} .. scalar @list - 1];
77         }
78         
79         if ($params{show} && @list > $params{show}) {
80                 @list=@list[0..$params{show} - 1];
81         }
82
83         add_depends($params{page}, $params{pages});
84
85         my $rssurl=rsspage(basename($params{page}));
86         my $atomurl=atompage(basename($params{page}));
87         my $ret="";
88
89         if (exists $params{rootpage} && $config{cgiurl}) {
90                 # Add a blog post form, with feed buttons.
91                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
92                 $formtemplate->param(cgiurl => $config{cgiurl});
93                 $formtemplate->param(rootpage => $params{rootpage});
94                 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
95                 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
96                 $ret.=$formtemplate->output;
97         }
98         elsif ($feeds) {
99                 # Add feed buttons.
100                 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
101                 $linktemplate->param(rssurl => $rssurl) if $rss;
102                 $linktemplate->param(atomurl => $atomurl) if $atom;
103                 $ret.=$linktemplate->output;
104         }
105         
106         my $template=template(
107                 ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
108                 blind_cache => 1,
109         ) unless $raw;
110         
111         foreach my $page (@list) {
112                 my $file = $pagesources{$page};
113                 my $type = pagetype($file);
114                 if (! $raw || ($raw && ! defined $type)) {
115                         # Get the content before populating the template,
116                         # since getting the content uses the same template
117                         # if inlines are nested.
118                         # TODO: if $archive=1, the only reason to do this
119                         # is to let the meta plugin get page title info; so stop
120                         # calling this next line then once the meta plugin can
121                         # store that accross runs (also tags plugin).
122                         my $content=get_inline_content($page, $params{destpage});
123                         # Don't use htmllink because this way the title is separate
124                         # and can be overridden by other plugins.
125                         my $link=bestlink($params{page}, $page);
126                         $link=htmlpage($link) if defined $type;
127                         $link=abs2rel($link, dirname($params{destpage}));
128                         $template->param(pageurl => $link);
129                         $template->param(title => pagetitle(basename($page)));
130                         $template->param(content => $content);
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, $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         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
215         POSIX::setlocale(&POSIX::LC_TIME, "C");
216         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
217         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
218         return $ret;
219 } #}}}
220
221 sub date_3339 ($) { #{{{
222         my $time=shift;
223
224         eval q{use POSIX};
225         my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
226         POSIX::setlocale(&POSIX::LC_TIME, "C");
227         my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
228         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
229         return $ret;
230 } #}}}
231
232 sub absolute_urls ($$) { #{{{
233         # sucky sub because rss sucks
234         my $content=shift;
235         my $url=shift;
236
237         $url=~s/[^\/]+$//;
238         
239         $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
240         $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
241         return $content;
242 } #}}}
243
244 sub rsspage ($) { #{{{
245         my $page=shift;
246
247         return $page.".rss";
248 } #}}}
249
250 sub atompage ($) { #{{{
251         my $page=shift;
252
253         return $page.".atom";
254 } #}}}
255
256 sub genfeed ($$$$@) { #{{{
257         my $feedtype=shift;
258         my $feedurl=shift;
259         my $feeddesc=shift;
260         my $page=shift;
261         my @pages=@_;
262         
263         my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
264         
265         my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
266         my $content="";
267         my $lasttime = 0;
268         foreach my $p (@pages) {
269                 my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
270
271                 $itemtemplate->param(
272                         title => pagetitle(basename($p)),
273                         url => $u,
274                         permalink => $u,
275                         date_822 => date_822($pagectime{$p}),
276                         date_3339 => date_3339($pagectime{$p}),
277                 );
278
279                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
280                 if ($itemtemplate->query(name => "enclosure")) {
281                         my $file=$pagesources{$p};
282                         my $type=pagetype($file);
283                         if (defined $type) {
284                                 $itemtemplate->param(content => $pcontent);
285                         }
286                         else {
287                                 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
288                                 my $mime="unknown";
289                                 eval q{use File::MimeInfo};
290                                 if (! $@) {
291                                         $mime = mimetype($file);
292                                 }
293                                 $itemtemplate->param(
294                                         enclosure => $u,
295                                         type => $mime,
296                                         length => $size,
297                                 );
298                         }
299                 }
300                 else {
301                         $itemtemplate->param(content => $pcontent);
302                 }
303
304                 run_hooks(pagetemplate => sub {
305                         shift->(page => $p, destpage => $page,
306                                 template => $itemtemplate);
307                 });
308
309                 $content.=$itemtemplate->output;
310                 $itemtemplate->clear_params;
311
312                 $lasttime = $pagectime{$p} if $pagectime{$p} > $lasttime;
313         }
314
315         my $template=template($feedtype."page.tmpl", blind_cache => 1);
316         $template->param(
317                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
318                 wikiname => $config{wikiname},
319                 pageurl => $url,
320                 content => $content,
321                 feeddesc => $feeddesc,
322                 feeddate => date_3339($lasttime),
323                 feedurl => $feedurl,
324                 version => $IkiWiki::version,
325         );
326         run_hooks(pagetemplate => sub {
327                 shift->(page => $page, destpage => $page,
328                         template => $template);
329         });
330         
331         return $template->output;
332 } #}}}
333
334 sub pingurl (@) { #{{{
335         return unless $config{pingurl} && %toping;
336
337         eval q{require RPC::XML::Client};
338         if ($@) {
339                 debug("RPC::XML::Client not found, not pinging");
340                 return;
341         }
342
343         # TODO: daemonize here so slow pings don't slow down wiki updates
344
345         foreach my $page (keys %toping) {
346                 my $title=pagetitle(basename($page));
347                 my $url="$config{url}/".htmlpage($page);
348                 foreach my $pingurl (@{$config{pingurl}}) {
349                         debug("Pinging $pingurl for $page");
350                         eval {
351                                 my $client = RPC::XML::Client->new($pingurl);
352                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
353                                 $title, $url);
354                                 my $res = $client->send_request($req);
355                                 if (! ref $res) {
356                                         debug("Did not receive response to ping");
357                                 }
358                                 my $r=$res->value;
359                                 if (! exists $r->{flerror} || $r->{flerror}) {
360                                         debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
361                                 }
362                         };
363                         if ($@) {
364                                 debug "Ping failed: $@";
365                         }
366                 }
367         }
368 } #}}}
369
370 1