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