]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/inline.pm
f0c37c1e9886544921cba5893cd158bf98d511a3
[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 Encode;
8 use IkiWiki 2.00;
9 use URI;
10
11 sub import { #{{{
12         hook(type => "getopt", id => "inline", call => \&getopt);
13         hook(type => "checkconfig", id => "inline", call => \&checkconfig);
14         hook(type => "sessioncgi", id => "skeleton", call => \&sessioncgi);
15         hook(type => "preprocess", id => "inline", 
16                 call => \&IkiWiki::preprocess_inline);
17         hook(type => "pagetemplate", id => "inline",
18                 call => \&IkiWiki::pagetemplate_inline);
19         # Hook to change to do pinging since it's called late.
20         # This ensures each page only pings once and prevents slow
21         # pings interrupting page builds.
22         hook(type => "change", id => "inline", 
23                 call => \&IkiWiki::pingurl);
24
25 } # }}}
26
27 sub getopt () { #{{{
28         eval q{use Getopt::Long};
29         error($@) if $@;
30         Getopt::Long::Configure('pass_through');
31         GetOptions(
32                 "rss!" => \$config{rss},
33                 "atom!" => \$config{atom},
34         );
35 }
36
37 sub checkconfig () { #{{{
38         if (($config{rss} || $config{atom}) && ! length $config{url}) {
39                 error(gettext("Must specify url to wiki with --url when using --rss or --atom"));
40         }
41         if ($config{rss}) {
42                 push @{$config{wiki_file_prune_regexps}}, qr/\.rss$/;
43         }
44         if ($config{atom}) {
45                 push @{$config{wiki_file_prune_regexps}}, qr/\.atom$/;
46         }
47 } #}}}
48
49 sub sessioncgi () { #{{{
50         my $q=shift;
51         my $session=shift;
52
53         if ($q->param('do') eq 'blog') {
54                 my $page=decode_utf8($q->param('title'));
55                 $page=~s/\///g; # no slashes in blog posts
56                 # if the page already exists, munge it to be unique
57                 my $from=$q->param('from');
58                 my $add="";
59                 while (exists $IkiWiki::pagecase{lc($from."/".IkiWiki::titlepage($page).$add)}) {
60                         $add=1 unless length $add;
61                         $add++;
62                 }
63                 $q->param('page', $page.$add);
64                 # now go create the page
65                 $q->param('do', 'create');
66                 IkiWiki::cgi_editpage($q, $session);
67                 exit;
68         }
69 }
70
71 # Back to ikiwiki namespace for the rest, this code is very much
72 # internal to ikiwiki even though it's separated into a plugin.
73 package IkiWiki;
74
75 my %toping;
76 my %feedlinks;
77
78 sub yesno ($) { #{{{
79         my $val=shift;
80         return (defined $val && lc($val) eq "yes");
81 } #}}}
82
83 sub preprocess_inline (@) { #{{{
84         my %params=@_;
85         
86         if (! exists $params{pages}) {
87                 return "";
88         }
89         my $raw=yesno($params{raw});
90         my $archive=yesno($params{archive});
91         my $rss=($config{rss} && exists $params{rss}) ? yesno($params{rss}) : $config{rss};
92         my $atom=($config{atom} && exists $params{atom}) ? yesno($params{atom}) : $config{atom};
93         my $quick=exists $params{quick} ? yesno($params{quick}) : 0;
94         my $feeds=exists $params{feeds} ? yesno($params{feeds}) : !$quick;
95         $feeds=0 if $params{preview};
96         if (! exists $params{show} && ! $archive) {
97                 $params{show}=10;
98         }
99         my $desc;
100         if (exists $params{description}) {
101                 $desc = $params{description} 
102         } else {
103                 $desc = $config{wikiname};
104         }
105         my $actions=yesno($params{actions});
106         if (exists $params{template}) {
107                 $params{template}=~s/[^-_a-zA-Z0-9]+//g;
108         }
109         else {
110                 $params{template} = $archive ? "archivepage" : "inlinepage";
111         }
112
113         my @list;
114         foreach my $page (keys %pagesources) {
115                 next if $page eq $params{page};
116                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
117                         push @list, $page;
118                 }
119         }
120
121         if (exists $params{sort} && $params{sort} eq 'title') {
122                 @list=sort @list;
123         }
124         elsif (exists $params{sort} && $params{sort} eq 'mtime') {
125                 @list=sort { $pagemtime{$b} <=> $pagemtime{$a} } @list;
126         }
127         elsif (! exists $params{sort} || $params{sort} eq 'age') {
128                 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
129         }
130         else {
131                 return sprintf(gettext("unknown sort type %s"), $params{sort});
132         }
133
134         if (yesno($params{reverse})) {
135                 @list=reverse(@list);
136         }
137
138         if (exists $params{skip}) {
139                 @list=@list[$params{skip} .. scalar @list - 1];
140         }
141         
142         if ($params{show} && @list > $params{show}) {
143                 @list=@list[0..$params{show} - 1];
144         }
145
146         add_depends($params{page}, $params{pages});
147         # Explicitly add all currently displayed pages as dependencies, so
148         # that if they are removed or otherwise changed, the inline will be
149         # sure to be updated.
150         add_depends($params{page}, join(" or ", @list));
151
152         my $rssurl=basename(rsspage($params{page}));
153         my $atomurl=basename(atompage($params{page}));
154         my $ret="";
155
156         if ($config{cgiurl} && (exists $params{rootpage} ||
157                         (exists $params{postform} && yesno($params{postform})))) {
158                 # Add a blog post form, with feed buttons.
159                 my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
160                 $formtemplate->param(cgiurl => $config{cgiurl});
161                 $formtemplate->param(rootpage => 
162                         exists $params{rootpage} ? $params{rootpage} : $params{page});
163                 $formtemplate->param(rssurl => $rssurl) if $feeds && $rss;
164                 $formtemplate->param(atomurl => $atomurl) if $feeds && $atom;
165                 if (exists $params{postformtext}) {
166                         $formtemplate->param(postformtext =>
167                                 $params{postformtext});
168                 }
169                 else {
170                         $formtemplate->param(postformtext =>
171                                 gettext("Add a new post titled:"));
172                 }
173                 $ret.=$formtemplate->output;
174         }
175         elsif ($feeds) {
176                 # Add feed buttons.
177                 my $linktemplate=template("feedlink.tmpl", blind_cache => 1);
178                 $linktemplate->param(rssurl => $rssurl) if $rss;
179                 $linktemplate->param(atomurl => $atomurl) if $atom;
180                 $ret.=$linktemplate->output;
181         }
182         
183         require HTML::Template;
184         my @params=IkiWiki::template_params($params{template}.".tmpl", blind_cache => 1);
185         if (! @params) {
186                 return sprintf(gettext("nonexistant template %s"), $params{template});
187         }
188         my $template=HTML::Template->new(@params) unless $raw;
189         
190         foreach my $page (@list) {
191                 my $file = $pagesources{$page};
192                 my $type = pagetype($file);
193                 if (! $raw || ($raw && ! defined $type)) {
194                         unless ($archive && $quick) {
195                                 # Get the content before populating the
196                                 # template, since getting the content uses
197                                 # the same template if inlines are nested.
198                                 my $content=get_inline_content($page, $params{destpage});
199                                 $template->param(content => $content);
200                         }
201                         $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
202                         $template->param(title => pagetitle(basename($page)));
203                         $template->param(ctime => displaytime($pagectime{$page}));
204
205                         if ($actions) {
206                                 my $file = $pagesources{$page};
207                                 my $type = pagetype($file);
208                                 if ($config{discussion}) {
209                                         my $discussionlink=gettext("discussion");
210                                         if ($page !~ /.*\/\Q$discussionlink\E$/ &&
211                                             (length $config{cgiurl} ||
212                                              exists $links{$page."/".$discussionlink})) {
213                                                 $template->param(have_actions => 1);
214                                                 $template->param(discussionlink =>
215                                                         htmllink($page,
216                                                                 $params{page},
217                                                                 gettext("Discussion"),
218                                                                 noimageinline => 1,
219                                                                 forcesubpage => 1));
220                                         }
221                                 }
222                                 if (length $config{cgiurl} && defined $type) {
223                                         $template->param(have_actions => 1);
224                                         $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
225                                 }
226                         }
227
228                         run_hooks(pagetemplate => sub {
229                                 shift->(page => $page, destpage => $params{page},
230                                         template => $template,);
231                         });
232
233                         $ret.=$template->output;
234                         $template->clear_params;
235                 }
236                 else {
237                         if (defined $type) {
238                                 $ret.="\n".
239                                       linkify($page, $params{page},
240                                       preprocess($page, $params{page},
241                                       filter($page, $params{page},
242                                       readfile(srcfile($file)))));
243                         }
244                 }
245         }
246         
247         if ($feeds) {
248                 if (exists $params{feedshow} && @list > $params{feedshow}) {
249                         @list=@list[0..$params{feedshow} - 1];
250                 }
251                 if (exists $params{feedpages}) {
252                         @list=grep { pagespec_match($_, $params{feedpages}, location => $params{page}) } @list;
253                 }
254         
255                 if ($rss) {
256                         my $rssp=rsspage($params{page});
257                         will_render($params{page}, $rssp);
258                         writefile($rssp, $config{destdir},
259                                 genfeed("rss", $rssurl, $desc, $params{page}, @list));
260                         $toping{$params{page}}=1 unless $config{rebuild};
261                         $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
262                 }
263                 if ($atom) {
264                         my $atomp=atompage($params{page});
265                         will_render($params{page}, $atomp);
266                         writefile($atomp, $config{destdir},
267                                 genfeed("atom", $atomurl, $desc, $params{page}, @list));
268                         $toping{$params{page}}=1 unless $config{rebuild};
269                         $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
270                 }
271         }
272         
273         return $ret;
274 } #}}}
275
276 sub pagetemplate_inline (@) { #{{{
277         my %params=@_;
278         my $page=$params{page};
279         my $template=$params{template};
280
281         $template->param(feedlinks => $feedlinks{$page})
282                 if exists $feedlinks{$page} && $template->query(name => "feedlinks");
283 } #}}}
284
285 sub get_inline_content ($$) { #{{{
286         my $page=shift;
287         my $destpage=shift;
288         
289         my $file=$pagesources{$page};
290         my $type=pagetype($file);
291         if (defined $type) {
292                 return htmlize($page, $type,
293                        linkify($page, $destpage,
294                        preprocess($page, $destpage,
295                        filter($page, $destpage,
296                        readfile(srcfile($file))))));
297         }
298         else {
299                 return "";
300         }
301 } #}}}
302
303 sub date_822 ($) { #{{{
304         my $time=shift;
305
306         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
307         POSIX::setlocale(&POSIX::LC_TIME, "C");
308         my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
309         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
310         return $ret;
311 } #}}}
312
313 sub date_3339 ($) { #{{{
314         my $time=shift;
315
316         my $lc_time=POSIX::setlocale(&POSIX::LC_TIME);
317         POSIX::setlocale(&POSIX::LC_TIME, "C");
318         my $ret=POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", localtime($time));
319         POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
320         return $ret;
321 } #}}}
322
323 sub absolute_urls ($$) { #{{{
324         # sucky sub because rss sucks
325         my $content=shift;
326         my $baseurl=shift;
327
328         my $url=$baseurl;
329         $url=~s/[^\/]+$//;
330         
331         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(#[^"]+)"/$1 href="$baseurl$2"/mig;
332         $content=~s/(<a(?:\s+(?:class|id)\s*="?\w+"?)?)\s+href=\s*"(?!\w+:\/\/)([^"]+)"/$1 href="$url$2"/mig;
333         $content=~s/(<img(?:\s+(?:class|id|width|height)\s*="?\w+"?)*)\s+src=\s*"(?!\w+:\/\/)([^"]+)"/$1 src="$url$2"/mig;
334         return $content;
335 } #}}}
336
337 sub rsspage ($) { #{{{
338         return targetpage(shift, "rss");
339 } #}}}
340
341 sub atompage ($) { #{{{
342         return targetpage(shift, "atom");
343 } #}}}
344
345 sub genfeed ($$$$@) { #{{{
346         my $feedtype=shift;
347         my $feedurl=shift;
348         my $feeddesc=shift;
349         my $page=shift;
350         my @pages=@_;
351         
352         my $url=URI->new(encode_utf8($config{url}."/".urlto($page,"")));
353         
354         my $itemtemplate=template($feedtype."item.tmpl", blind_cache => 1);
355         my $content="";
356         my $lasttime = 0;
357         foreach my $p (@pages) {
358                 my $u=URI->new(encode_utf8($config{url}."/".urlto($p, "")));
359                 my $pcontent = absolute_urls(get_inline_content($p, $page), $url);
360
361                 $itemtemplate->param(
362                         title => pagetitle(basename($p)),
363                         url => $u,
364                         permalink => $u,
365                         cdate_822 => date_822($pagectime{$p}),
366                         mdate_822 => date_822($pagemtime{$p}),
367                         cdate_3339 => date_3339($pagectime{$p}),
368                         mdate_3339 => date_3339($pagemtime{$p}),
369                 );
370
371                 if ($itemtemplate->query(name => "enclosure")) {
372                         my $file=$pagesources{$p};
373                         my $type=pagetype($file);
374                         if (defined $type) {
375                                 $itemtemplate->param(content => $pcontent);
376                         }
377                         else {
378                                 my ($a, $b, $c, $d, $e, $f, $g, $size) = stat(srcfile($file));
379                                 my $mime="unknown";
380                                 eval q{use File::MimeInfo};
381                                 if (! $@) {
382                                         $mime = mimetype($file);
383                                 }
384                                 $itemtemplate->param(
385                                         enclosure => $u,
386                                         type => $mime,
387                                         length => $size,
388                                 );
389                         }
390                 }
391                 else {
392                         $itemtemplate->param(content => $pcontent);
393                 }
394
395                 run_hooks(pagetemplate => sub {
396                         shift->(page => $p, destpage => $page,
397                                 template => $itemtemplate);
398                 });
399
400                 $content.=$itemtemplate->output;
401                 $itemtemplate->clear_params;
402
403                 $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime;
404         }
405
406         my $template=template($feedtype."page.tmpl", blind_cache => 1);
407         $template->param(
408                 title => $page ne "index" ? pagetitle($page) : $config{wikiname},
409                 wikiname => $config{wikiname},
410                 pageurl => $url,
411                 content => $content,
412                 feeddesc => $feeddesc,
413                 feeddate => date_3339($lasttime),
414                 feedurl => $feedurl,
415                 version => $IkiWiki::version,
416         );
417         run_hooks(pagetemplate => sub {
418                 shift->(page => $page, destpage => $page,
419                         template => $template);
420         });
421         
422         return $template->output;
423 } #}}}
424
425 sub pingurl (@) { #{{{
426         return unless @{$config{pingurl}} && %toping;
427
428         eval q{require RPC::XML::Client};
429         if ($@) {
430                 debug(gettext("RPC::XML::Client not found, not pinging"));
431                 return;
432         }
433
434         # daemonize here so slow pings don't slow down wiki updates
435         defined(my $pid = fork) or error("Can't fork: $!");
436         return if $pid;
437         chdir '/';
438         setsid() or error("Can't start a new session: $!");
439         open STDIN, '/dev/null';
440         open STDOUT, '>/dev/null';
441         open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
442
443         # Don't need to keep a lock on the wiki as a daemon.
444         IkiWiki::unlockwiki();
445
446         foreach my $page (keys %toping) {
447                 my $title=pagetitle(basename($page), 0);
448                 my $url="$config{url}/".urlto($page, "");
449                 foreach my $pingurl (@{$config{pingurl}}) {
450                         debug("Pinging $pingurl for $page");
451                         eval {
452                                 my $client = RPC::XML::Client->new($pingurl);
453                                 my $req = RPC::XML::request->new('weblogUpdates.ping',
454                                         $title, $url);
455                                 my $res = $client->send_request($req);
456                                 if (! ref $res) {
457                                         debug("Did not receive response to ping");
458                                 }
459                                 my $r=$res->value;
460                                 if (! exists $r->{flerror} || $r->{flerror}) {
461                                         debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
462                                 }
463                         };
464                         if ($@) {
465                                 debug "Ping failed: $@";
466                         }
467                 }
468         }
469
470         exit 0; # daemon done
471 } #}}}
472
473 1