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