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