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