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