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