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