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