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