]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
inline: The optimisation in 2.41 broke nested inlines. Detect those and avoid overopt...
[git.ikiwiki.info.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
3 package IkiWiki;
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
10 my %backlinks;
11 my $backlinks_calculated=0;
13 sub calculate_backlinks () { #{{{
14         return if $backlinks_calculated;
15         %backlinks=();
16         foreach my $page (keys %links) {
17                 foreach my $link (@{$links{$page}}) {
18                         my $bestlink=bestlink($page, $link);
19                         if (length $bestlink && $bestlink ne $page) {
20                                 $backlinks{$bestlink}{$page}=1;
21                         }
22                 }
23         }
24         $backlinks_calculated=1;
25 } #}}}
27 sub backlinks ($) { #{{{
28         my $page=shift;
30         calculate_backlinks();
32         my @links;
33         foreach my $p (keys %{$backlinks{$page}}) {
34                 my $href=urlto($p, $page);
35                 
36                 # Trim common dir prefixes from both pages.
37                 my $p_trimmed=$p;
38                 my $page_trimmed=$page;
39                 my $dir;
40                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
41                         defined $dir &&
42                         $p_trimmed=~s/^\Q$dir\E// &&
43                         $page_trimmed=~s/^\Q$dir\E//;
44                                
45                 push @links, { url => $href, page => pagetitle($p_trimmed) };
46         }
47         return @links;
48 } #}}}
50 sub parentlinks ($) { #{{{
51         my $page=shift;
52         
53         my @ret;
54         my $pagelink="";
55         my $path="";
56         my $title=$config{wikiname};
57         
58         foreach my $dir (split("/", $page)) {
59                 next if $dir eq 'index';
60                 push @ret, { url => urlto($path, $page), page => $title };
61                 $path.="/".$dir;
62                 $title=pagetitle($dir);
63         }
64         return @ret;
65 } #}}}
67 sub genpage ($$) { #{{{
68         my $page=shift;
69         my $content=shift;
71         my $templatefile;
72         run_hooks(templatefile => sub {
73                 return if defined $templatefile;
74                 my $file=shift->(page => $page);
75                 if (defined $file && defined template_file($file)) {
76                         $templatefile=$file;
77                 }
78         });
79         my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
80         my $actions=0;
82         if (length $config{cgiurl}) {
83                 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
84                 $template->param(prefsurl => cgiurl(do => "prefs"));
85                 $actions++;
86         }
87                 
88         if (length $config{historyurl}) {
89                 my $u=$config{historyurl};
90                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
91                 $template->param(historyurl => $u);
92                 $actions++;
93         }
94         if ($config{discussion}) {
95                 my $discussionlink=gettext("discussion");
96                 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
97                    (length $config{cgiurl} ||
98                     exists $links{$page."/".$discussionlink})) {
99                         $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
100                         $actions++;
101                 }
102         }
104         if ($actions) {
105                 $template->param(have_actions => 1);
106         }
108         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
109         my ($backlinks, $more_backlinks);
110         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
111                 $backlinks=\@backlinks;
112                 $more_backlinks=[];
113         }
114         else {
115                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
116                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
117         }
119         $template->param(
120                 title => $page eq 'index' 
121                         ? $config{wikiname} 
122                         : pagetitle(basename($page)),
123                 wikiname => $config{wikiname},
124                 parentlinks => [parentlinks($page)],
125                 content => $content,
126                 backlinks => $backlinks,
127                 more_backlinks => $more_backlinks,
128                 mtime => displaytime($pagemtime{$page}),
129                 ctime => displaytime($pagectime{$page}),
130                 baseurl => baseurl($page),
131         );
133         run_hooks(pagetemplate => sub {
134                 shift->(page => $page, destpage => $page, template => $template);
135         });
136         
137         $content=$template->output;
139         run_hooks(format => sub {
140                 $content=shift->(
141                         page => $page,
142                         content => $content,
143                 );
144         });
146         return $content;
147 } #}}}
149 sub scan ($) { #{{{
150         my $file=shift;
152         my $type=pagetype($file);
153         if (defined $type) {
154                 my $srcfile=srcfile($file);
155                 my $content=readfile($srcfile);
156                 my $page=pagename($file);
157                 will_render($page, htmlpage($page), 1);
159                 # Always needs to be done, since filters might add links
160                 # to the content.
161                 $content=filter($page, $page, $content);
162         
163                 if ($config{discussion}) {
164                         # Discussion links are a special case since they're
165                         # not in the text of the page, but on its template.
166                         $links{$page}=[ $page."/".gettext("discussion") ];
167                 }
168                 else {
169                         $links{$page}=[];
170                 }
172                 run_hooks(scan => sub {
173                         shift->(
174                                 page => $page,
175                                 content => $content,
176                         );
177                 });
179                 # Preprocess in scan-only mode.
180                 preprocess($page, $page, $content, 1);
181         }
182         else {
183                 will_render($file, $file, 1);
184         }
185 } #}}}
187 sub render ($) { #{{{
188         my $file=shift;
189         
190         my $type=pagetype($file);
191         my $srcfile=srcfile($file);
192         if (defined $type) {
193                 my $page=pagename($file);
194                 delete $depends{$page};
195                 will_render($page, htmlpage($page), 1);
196                 return if $type=~/^_/;
197                 
198                 my $content=htmlize($page, $type,
199                         linkify($page, $page,
200                         preprocess($page, $page,
201                         filter($page, $page,
202                         readfile($srcfile)))));
203                 
204                 my $output=htmlpage($page);
205                 writefile($output, $config{destdir}, genpage($page, $content));
206         }
207         else {
208                 delete $depends{$file};
209                 will_render($file, $file, 1);
210                 
211                 if ($config{hardlink}) {
212                         prep_writefile($file, $config{destdir});
213                         unlink($config{destdir}."/".$file);
214                         if (link($srcfile, $config{destdir}."/".$file)) {
215                                 return;
216                         }
217                         # if hardlink fails, fall back to copying
218                 }
219                 
220                 my $srcfd=readfile($srcfile, 1, 1);
221                 writefile($file, $config{destdir}, undef, 1, sub {
222                         my $destfd=shift;
223                         my $cleanup=shift;
225                         my $blksize = 16384;
226                         my ($len, $buf, $written);
227                         while ($len = sysread $srcfd, $buf, $blksize) {
228                                 if (! defined $len) {
229                                         next if $! =~ /^Interrupted/;
230                                         error("failed to read $srcfile: $!", $cleanup);
231                                 }
232                                 my $offset = 0;
233                                 while ($len) {
234                                         defined($written = syswrite $destfd, $buf, $len, $offset)
235                                                 or error("failed to write $file: $!", $cleanup);
236                                         $len -= $written;
237                                         $offset += $written;
238                                 }
239                         }
240                 });
241         }
242 } #}}}
244 sub prune ($) { #{{{
245         my $file=shift;
247         unlink($file);
248         my $dir=dirname($file);
249         while (rmdir($dir)) {
250                 $dir=dirname($dir);
251         }
252 } #}}}
254 sub refresh () { #{{{
255         # security check, avoid following symlinks in the srcdir path
256         my $test=$config{srcdir};
257         while (length $test) {
258                 if (-l $test) {
259                         error("symlink found in srcdir path ($test)");
260                 }
261                 unless ($test=~s/\/+$//) {
262                         $test=dirname($test);
263                 }
264         }
265         
266         run_hooks(refresh => sub { shift->() });
268         # find existing pages
269         my %exists;
270         my @files;
271         eval q{use File::Find};
272         error($@) if $@;
273         find({
274                 no_chdir => 1,
275                 wanted => sub {
276                         $_=decode_utf8($_);
277                         if (file_pruned($_, $config{srcdir})) {
278                                 $File::Find::prune=1;
279                         }
280                         elsif (! -l $_ && ! -d _) {
281                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
282                                 if (! defined $f) {
283                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
284                                 }
285                                 else {
286                                         $f=~s/^\Q$config{srcdir}\E\/?//;
287                                         push @files, $f;
288                                         $exists{pagename($f)}=1;
289                                 }
290                         }
291                 },
292         }, $config{srcdir});
293         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
294                 find({
295                         no_chdir => 1,
296                         wanted => sub {
297                                 $_=decode_utf8($_);
298                                 if (file_pruned($_, $dir)) {
299                                         $File::Find::prune=1;
300                                 }
301                                 elsif (! -l $_ && ! -d _) {
302                                         my ($f)=/$config{wiki_file_regexp}/; # untaint
303                                         if (! defined $f) {
304                                                 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
305                                         }
306                                         else {
307                                                 $f=~s/^\Q$dir\E\/?//;
308                                                 # avoid underlaydir
309                                                 # override attacks; see
310                                                 # security.mdwn
311                                                 if (! -l "$config{srcdir}/$f" && 
312                                                     ! -e _) {
313                                                         my $page=pagename($f);
314                                                         if (! $exists{$page}) {
315                                                                 push @files, $f;
316                                                                 $exists{$page}=1;
317                                                         }
318                                                 }
319                                         }
320                                 }
321                         },
322                 }, $dir);
323         };
325         my (%rendered, @add, @del, @internal);
326         # check for added or removed pages
327         foreach my $file (@files) {
328                 my $page=pagename($file);
329                 $pagesources{$page}=$file;
330                 if (! $pagemtime{$page}) {
331                         if (isinternal($page)) {
332                                 push @internal, $file;
333                         }
334                         else {
335                                 push @add, $file;
336                                 if ($config{getctime} && -e "$config{srcdir}/$file") {
337                                         eval {
338                                                 my $time=rcs_getctime("$config{srcdir}/$file");
339                                                 $pagectime{$page}=$time;
340                                         };
341                                         if ($@) {
342                                                 print STDERR $@;
343                                         }
344                                 }
345                         }
346                         $pagecase{lc $page}=$page;
347                         if (! exists $pagectime{$page}) {
348                                 $pagectime{$page}=(srcfile_stat($file))[10];
349                         }
350                 }
351         }
352         foreach my $page (keys %pagemtime) {
353                 if (! $exists{$page}) {
354                         if (isinternal($page)) {
355                                 push @internal, $pagesources{$page};
356                         }
357                         else {
358                                 debug(sprintf(gettext("removing old page %s"), $page));
359                                 push @del, $pagesources{$page};
360                         }
361                         $links{$page}=[];
362                         $renderedfiles{$page}=[];
363                         $pagemtime{$page}=0;
364                         prune($config{destdir}."/".$_)
365                                 foreach @{$oldrenderedfiles{$page}};
366                         delete $pagesources{$page};
367                         foreach (keys %destsources) {
368                                 if ($destsources{$_} eq $page) {
369                                         delete $destsources{$_};
370                                 }
371                         }
372                 }
373         }
375         # find changed and new files
376         my @needsbuild;
377         foreach my $file (@files) {
378                 my $page=pagename($file);
379                 my ($srcfile, @stat)=srcfile_stat($file);
380                 if (! exists $pagemtime{$page} ||
381                     $stat[9] > $pagemtime{$page} ||
382                     $forcerebuild{$page}) {
383                         $pagemtime{$page}=$stat[9];
384                         if (isinternal($page)) {
385                                 push @internal, $file;
386                                 # Preprocess internal page in scan-only mode.
387                                 preprocess($page, $page, readfile($srcfile), 1);
388                         }
389                         else {
390                                 push @needsbuild, $file;
391                         }
392                 }
393         }
394         run_hooks(needsbuild => sub { shift->(\@needsbuild) });
396         # scan and render files
397         foreach my $file (@needsbuild) {
398                 debug(sprintf(gettext("scanning %s"), $file));
399                 scan($file);
400         }
401         calculate_backlinks();
402         foreach my $file (@needsbuild) {
403                 debug(sprintf(gettext("rendering %s"), $file));
404                 render($file);
405                 $rendered{$file}=1;
406         }
407         foreach my $file (@internal) {
408                 # internal pages are not rendered
409                 my $page=pagename($file);
410                 delete $depends{$page};
411                 foreach my $old (@{$renderedfiles{$page}}) {
412                         delete $destsources{$old};
413                 }
414                 $renderedfiles{$page}=[];
415         }
416         
417         # rebuild pages that link to added or removed pages
418         if (@add || @del) {
419                 foreach my $f (@add, @del) {
420                         my $p=pagename($f);
421                         foreach my $page (keys %{$backlinks{$p}}) {
422                                 my $file=$pagesources{$page};
423                                 next if $rendered{$file};
424                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
425                                 render($file);
426                                 $rendered{$file}=1;
427                         }
428                 }
429         }
431         if (%rendered || @del || @internal) {
432                 my @changed=(keys %rendered, @del);
434                 # rebuild dependant pages
435                 foreach my $f (@files) {
436                         next if $rendered{$f};
437                         my $p=pagename($f);
438                         if (exists $depends{$p}) {
439                                 # only consider internal files
440                                 # if the page explicitly depends on such files
441                                 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
442                                         next if $f eq $file;
443                                         my $page=pagename($file);
444                                         if (pagespec_match($page, $depends{$p}, location => $p)) {
445                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
446                                                 render($f);
447                                                 $rendered{$f}=1;
448                                                 last;
449                                         }
450                                 }
451                         }
452                 }
453                 
454                 # handle backlinks; if a page has added/removed links,
455                 # update the pages it links to
456                 my %linkchanged;
457                 foreach my $file (@changed) {
458                         my $page=pagename($file);
459                         
460                         if (exists $links{$page}) {
461                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
462                                         if (length $link &&
463                                             (! exists $oldlinks{$page} ||
464                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
465                                                 $linkchanged{$link}=1;
466                                         }
467                                 }
468                         }
469                         if (exists $oldlinks{$page}) {
470                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
471                                         if (length $link &&
472                                             (! exists $links{$page} || 
473                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
474                                                 $linkchanged{$link}=1;
475                                         }
476                                 }
477                         }
478                 }
480                 foreach my $link (keys %linkchanged) {
481                         my $linkfile=$pagesources{$link};
482                         if (defined $linkfile) {
483                                 next if $rendered{$linkfile};
484                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
485                                 render($linkfile);
486                                 $rendered{$linkfile}=1;
487                         }
488                 }
489         }
491         # remove no longer rendered files
492         foreach my $src (keys %rendered) {
493                 my $page=pagename($src);
494                 foreach my $file (@{$oldrenderedfiles{$page}}) {
495                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
496                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
497                                 prune($config{destdir}."/".$file);
498                         }
499                 }
500         }
502         if (@del) {
503                 run_hooks(delete => sub { shift->(@del) });
504         }
505         if (%rendered) {
506                 run_hooks(change => sub { shift->(keys %rendered) });
507         }
508 } #}}}
510 sub commandline_render () { #{{{
511         lockwiki();
512         loadindex();
513         unlockwiki();
515         my $srcfile=possibly_foolish_untaint($config{render});
516         my $file=$srcfile;
517         $file=~s/\Q$config{srcdir}\E\/?//;
519         my $type=pagetype($file);
520         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
521         my $content=readfile($srcfile);
522         my $page=pagename($file);
523         $pagesources{$page}=$file;
524         $content=filter($page, $page, $content);
525         $content=preprocess($page, $page, $content);
526         $content=linkify($page, $page, $content);
527         $content=htmlize($page, $type, $content);
528         $pagemtime{$page}=(stat($srcfile))[9];
530         print genpage($page, $content);
531         exit 0;
532 } #}}}