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