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