]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Optimize away most expensive file prune calls, when refreshing
[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 our %brokenlinks;
12 my $links_calculated=0;
14 sub calculate_links () {
15         return if $links_calculated;
16         %backlinks=%brokenlinks=();
17         foreach my $page (keys %links) {
18                 foreach my $link (@{$links{$page}}) {
19                         my $bestlink=bestlink($page, $link);
20                         if (length $bestlink) {
21                                 $backlinks{$bestlink}{$page}=1
22                                         if $bestlink ne $page;
23                         }
24                         else {
25                                 push @{$brokenlinks{$link}}, $page;
26                         }
27                 }
28         }
29         $links_calculated=1;
30 }
32 sub backlink_pages ($) {
33         my $page=shift;
35         calculate_links();
37         return keys %{$backlinks{$page}};
38 }
40 sub backlinks ($) {
41         my $page=shift;
43         my @links;
44         foreach my $p (backlink_pages($page)) {
45                 my $href=urlto($p, $page);
46                 
47                 # Trim common dir prefixes from both pages.
48                 my $p_trimmed=$p;
49                 my $page_trimmed=$page;
50                 my $dir;
51                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
52                         defined $dir &&
53                         $p_trimmed=~s/^\Q$dir\E// &&
54                         $page_trimmed=~s/^\Q$dir\E//;
55                                
56                 push @links, { url => $href, page => pagetitle($p_trimmed) };
57         }
58         return @links;
59 }
61 sub genpage ($$) {
62         my $page=shift;
63         my $content=shift;
65         my $templatefile;
66         run_hooks(templatefile => sub {
67                 return if defined $templatefile;
68                 my $file=shift->(page => $page);
69                 if (defined $file && defined template_file($file)) {
70                         $templatefile=$file;
71                 }
72         });
73         my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
74         my $actions=0;
76         if (length $config{cgiurl}) {
77                 $template->param(editurl => cgiurl(do => "edit", page => $page))
78                         if IkiWiki->can("cgi_editpage");
79                 $template->param(prefsurl => cgiurl(do => "prefs"))
80                         if exists $hooks{auth};
81                 $actions++;
82         }
83                 
84         if (defined $config{historyurl} && length $config{historyurl}) {
85                 my $u=$config{historyurl};
86                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
87                 $template->param(historyurl => $u);
88                 $actions++;
89         }
90         if ($config{discussion}) {
91                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
92                    (length $config{cgiurl} ||
93                     exists $links{$page."/".$config{discussionpage}})) {
94                         $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
95                         $actions++;
96                 }
97         }
99         if ($actions) {
100                 $template->param(have_actions => 1);
101         }
103         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
104         my ($backlinks, $more_backlinks);
105         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
106                 $backlinks=\@backlinks;
107                 $more_backlinks=[];
108         }
109         else {
110                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
111                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
112         }
114         $template->param(
115                 title => $page eq 'index' 
116                         ? $config{wikiname} 
117                         : pagetitle(basename($page)),
118                 wikiname => $config{wikiname},
119                 content => $content,
120                 backlinks => $backlinks,
121                 more_backlinks => $more_backlinks,
122                 mtime => displaytime($pagemtime{$page}),
123                 ctime => displaytime($pagectime{$page}),
124                 baseurl => baseurl($page),
125         );
127         run_hooks(pagetemplate => sub {
128                 shift->(page => $page, destpage => $page, template => $template);
129         });
130         
131         $content=$template->output;
132         
133         run_hooks(postscan => sub {
134                 shift->(page => $page, content => $content);
135         });
137         run_hooks(format => sub {
138                 $content=shift->(
139                         page => $page,
140                         content => $content,
141                 );
142         });
144         return $content;
147 sub scan ($) {
148         my $file=shift;
150         my $type=pagetype($file);
151         if (defined $type) {
152                 my $srcfile=srcfile($file);
153                 my $content=readfile($srcfile);
154                 my $page=pagename($file);
155                 will_render($page, htmlpage($page), 1);
157                 if ($config{discussion}) {
158                         # Discussion links are a special case since they're
159                         # not in the text of the page, but on its template.
160                         $links{$page}=[ $page."/".lc($config{discussionpage}) ];
161                 }
162                 else {
163                         $links{$page}=[];
164                 }
166                 run_hooks(scan => sub {
167                         shift->(
168                                 page => $page,
169                                 content => $content,
170                         );
171                 });
173                 # Preprocess in scan-only mode.
174                 preprocess($page, $page, $content, 1);
175         }
176         else {
177                 will_render($file, $file, 1);
178         }
181 sub fast_file_copy (@) {
182         my $srcfile=shift;
183         my $destfile=shift;
184         my $srcfd=shift;
185         my $destfd=shift;
186         my $cleanup=shift;
188         my $blksize = 16384;
189         my ($len, $buf, $written);
190         while ($len = sysread $srcfd, $buf, $blksize) {
191                 if (! defined $len) {
192                         next if $! =~ /^Interrupted/;
193                         error("failed to read $srcfile: $!", $cleanup);
194                 }
195                 my $offset = 0;
196                 while ($len) {
197                         defined($written = syswrite $destfd, $buf, $len, $offset)
198                                 or error("failed to write $destfile: $!", $cleanup);
199                         $len -= $written;
200                         $offset += $written;
201                 }
202         }
205 sub render ($) {
206         my $file=shift;
207         
208         my $type=pagetype($file);
209         my $srcfile=srcfile($file);
210         if (defined $type) {
211                 my $page=pagename($file);
212                 delete $depends{$page};
213                 delete $depends_simple{$page};
214                 will_render($page, htmlpage($page), 1);
215                 return if $type=~/^_/;
216                 
217                 my $content=htmlize($page, $page, $type,
218                         linkify($page, $page,
219                         preprocess($page, $page,
220                         filter($page, $page,
221                         readfile($srcfile)))));
222                 
223                 my $output=htmlpage($page);
224                 writefile($output, $config{destdir}, genpage($page, $content));
225         }
226         else {
227                 delete $depends{$file};
228                 delete $depends_simple{$file};
229                 will_render($file, $file, 1);
230                 
231                 if ($config{hardlink}) {
232                         # only hardlink if owned by same user
233                         my @stat=stat($srcfile);
234                         if ($stat[4] == $>) {
235                                 prep_writefile($file, $config{destdir});
236                                 unlink($config{destdir}."/".$file);
237                                 if (link($srcfile, $config{destdir}."/".$file)) {
238                                         return;
239                                 }
240                         }
241                         # if hardlink fails, fall back to copying
242                 }
243                 
244                 my $srcfd=readfile($srcfile, 1, 1);
245                 writefile($file, $config{destdir}, undef, 1, sub {
246                         fast_file_copy($srcfile, $file, $srcfd, @_);
247                 });
248         }
251 sub prune ($) {
252         my $file=shift;
254         unlink($file);
255         my $dir=dirname($file);
256         while (rmdir($dir)) {
257                 $dir=dirname($dir);
258         }
261 sub srcdir_check () {
262         # security check, avoid following symlinks in the srcdir path by default
263         my $test=$config{srcdir};
264         while (length $test) {
265                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
266                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
267                 }
268                 unless ($test=~s/\/+$//) {
269                         $test=dirname($test);
270                 }
271         }
272         
275 sub find_src_files () {
276         my (@files, %pages);
277         eval q{use File::Find};
278         error($@) if $@;
279         find({
280                 no_chdir => 1,
281                 wanted => sub {
282                         my $file=decode_utf8($_);
283                         $file=~s/^\Q$config{srcdir}\E\/?//;
284                         my $page = pagename($file);
285                         if (! exists $pagesources{$page} &&
286                             file_pruned($file)) {
287                                 $File::Find::prune=1;
288                                 return;
289                         }
290                         return if -l $_ || -d _ || ! length $file;
292                         my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
293                         if (! defined $f) {
294                                 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
295                         }
296                         else {
297                                 push @files, $f;
298                                 if ($pages{$page}) {
299                                         debug(sprintf(gettext("%s has multiple possible source pages"), $page));
300                                 }
301                                 $pages{$page}=1;
302                         }
303                 },
304         }, $config{srcdir});
305         foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
306                 find({
307                         no_chdir => 1,
308                         wanted => sub {
309                                 my $file=decode_utf8($_);
310                                 $file=~s/^\Q$dir\E\/?//;
311                                 my $page=pagename($file);
312                                 if (! exists $pagesources{$page} &&
313                                     file_pruned($file)) {
314                                         $File::Find::prune=1;
315                                         return;
316                                 }
317                                 return if -l $_ || -d _ || ! length $file;
319                                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
320                                 if (! defined $f) {
321                                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
322                                 }
323                                 else {
324                                         # avoid underlaydir override
325                                         # attacks; see security.mdwn
326                                         if (! -l "$config{srcdir}/$f" && 
327                                             ! -e _) {
328                                                 if (! $pages{$page}) {
329                                                         push @files, $f;
330                                                         $pages{$page}=1;
331                                                 }
332                                         }
333                                 }
334                         },
335                 }, $dir);
336         };
338         # Returns a list of all source files found, and a hash of 
339         # the corresponding page names.
340         return \@files, \%pages;
343 sub refresh () {
344         srcdir_check();
345         run_hooks(refresh => sub { shift->() });
346         my ($files, $exists)=find_src_files();
348         my (%rendered, @add, @del, @internal);
349         # check for added or removed pages
350         foreach my $file (@$files) {
351                 my $page=pagename($file);
352                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
353                         # the page has changed its type
354                         $forcerebuild{$page}=1;
355                 }
356                 $pagesources{$page}=$file;
357                 if (! $pagemtime{$page}) {
358                         if (isinternal($page)) {
359                                 push @internal, $file;
360                         }
361                         else {
362                                 push @add, $file;
363                                 if ($config{getctime} && -e "$config{srcdir}/$file") {
364                                         eval {
365                                                 my $time=rcs_getctime("$config{srcdir}/$file");
366                                                 $pagectime{$page}=$time;
367                                         };
368                                         if ($@) {
369                                                 print STDERR $@;
370                                         }
371                                 }
372                         }
373                         $pagecase{lc $page}=$page;
374                         if (! exists $pagectime{$page}) {
375                                 $pagectime{$page}=(srcfile_stat($file))[10];
376                         }
377                 }
378         }
379         foreach my $page (keys %pagemtime) {
380                 if (! $exists->{$page}) {
381                         if (isinternal($page)) {
382                                 push @internal, $pagesources{$page};
383                         }
384                         else {
385                                 debug(sprintf(gettext("removing old page %s"), $page));
386                                 push @del, $pagesources{$page};
387                         }
388                         $links{$page}=[];
389                         $renderedfiles{$page}=[];
390                         $pagemtime{$page}=0;
391                         foreach my $old (@{$oldrenderedfiles{$page}}) {
392                                 prune($config{destdir}."/".$old);
393                         }
394                         delete $pagesources{$page};
395                         foreach my $source (keys %destsources) {
396                                 if ($destsources{$source} eq $page) {
397                                         delete $destsources{$source};
398                                 }
399                         }
400                 }
401         }
403         # find changed and new files
404         my @needsbuild;
405         foreach my $file (@$files) {
406                 my $page=pagename($file);
407                 my ($srcfile, @stat)=srcfile_stat($file);
408                 if (! exists $pagemtime{$page} ||
409                     $stat[9] > $pagemtime{$page} ||
410                     $forcerebuild{$page}) {
411                         $pagemtime{$page}=$stat[9];
412                         if (isinternal($page)) {
413                                 push @internal, $file;
414                                 # Preprocess internal page in scan-only mode.
415                                 preprocess($page, $page, readfile($srcfile), 1);
416                         }
417                         else {
418                                 push @needsbuild, $file;
419                         }
420                 }
421         }
422         run_hooks(needsbuild => sub { shift->(\@needsbuild) });
424         # scan and render files
425         foreach my $file (@needsbuild) {
426                 debug(sprintf(gettext("scanning %s"), $file));
427                 scan($file);
428         }
429         calculate_links();
430         foreach my $file (@needsbuild) {
431                 debug(sprintf(gettext("building %s"), $file));
432                 render($file);
433                 $rendered{$file}=1;
434         }
435         foreach my $file (@internal) {
436                 # internal pages are not rendered
437                 my $page=pagename($file);
438                 delete $depends{$page};
439                 delete $depends_simple{$page};
440                 foreach my $old (@{$renderedfiles{$page}}) {
441                         delete $destsources{$old};
442                 }
443                 $renderedfiles{$page}=[];
444         }
445         
446         # rebuild pages that link to added or removed pages
447         if (@add || @del) {
448                 foreach my $f (@add, @del) {
449                         my $p=pagename($f);
450                         foreach my $page (keys %{$backlinks{$p}}) {
451                                 my $file=$pagesources{$page};
452                                 next if $rendered{$file};
453                                 debug(sprintf(gettext("building %s, which links to %s"), $file, $p));
454                                 render($file);
455                                 $rendered{$file}=1;
456                         }
457                 }
458         }
460         if (%rendered || @del || @internal) {
461                 my @changed=(keys %rendered, @del);
463                 my %lcchanged = map { lc(pagename($_)) => 1 } @changed;
464  
465                 # rebuild dependant pages
466                 foreach my $f (@$files) {
467                         next if $rendered{$f};
468                         my $p=pagename($f);
469                         my $reason = undef;
471                         if (exists $depends_simple{$p}) {
472                                 foreach my $d (keys %{$depends_simple{$p}}) {
473                                         if (exists $lcchanged{$d}) {
474                                                 $reason = $d;
475                                                 last;
476                                         }
477                                 }
478                         }
480                         if (exists $depends{$p} && ! defined $reason) {
481                                 D: foreach my $d (keys %{$depends{$p}}) {
482                                         my $sub=pagespec_translate($d);
483                                         next if $@ || ! defined $sub;
485                                         # only consider internal files
486                                         # if the page explicitly depends
487                                         # on such files
488                                         foreach my $file (@changed, $d =~ /internal\(/ ? @internal : ()) {
489                                                 next if $file eq $f;
490                                                 my $page=pagename($file);
491                                                 if ($sub->($page, location => $p)) {
492                                                         $reason = $page;
493                                                         last D;
494                                                 }
495                                         }
496                                 }
497                         }
499                         if (defined $reason) {
500                                 debug(sprintf(gettext("building %s, which depends on %s"), $f, $reason));
501                                 render($f);
502                                 $rendered{$f}=1;
503                         }
504                 }
505                 
506                 # handle backlinks; if a page has added/removed links,
507                 # update the pages it links to
508                 my %linkchanged;
509                 foreach my $file (@changed) {
510                         my $page=pagename($file);
511                         
512                         if (exists $links{$page}) {
513                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
514                                         if (length $link &&
515                                             (! exists $oldlinks{$page} ||
516                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
517                                                 $linkchanged{$link}=1;
518                                         }
519                                 }
520                         }
521                         if (exists $oldlinks{$page}) {
522                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
523                                         if (length $link &&
524                                             (! exists $links{$page} || 
525                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
526                                                 $linkchanged{$link}=1;
527                                         }
528                                 }
529                         }
530                 }
532                 foreach my $link (keys %linkchanged) {
533                         my $linkfile=$pagesources{$link};
534                         if (defined $linkfile) {
535                                 next if $rendered{$linkfile};
536                                 debug(sprintf(gettext("building %s, to update its backlinks"), $linkfile));
537                                 render($linkfile);
538                                 $rendered{$linkfile}=1;
539                         }
540                 }
541         }
543         # remove no longer rendered files
544         foreach my $src (keys %rendered) {
545                 my $page=pagename($src);
546                 foreach my $file (@{$oldrenderedfiles{$page}}) {
547                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
548                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
549                                 prune($config{destdir}."/".$file);
550                         }
551                 }
552         }
554         if (@del) {
555                 run_hooks(delete => sub { shift->(@del) });
556         }
557         if (%rendered) {
558                 run_hooks(change => sub { shift->(keys %rendered) });
559         }
562 sub commandline_render () {
563         lockwiki();
564         loadindex();
565         unlockwiki();
567         my $srcfile=possibly_foolish_untaint($config{render});
568         my $file=$srcfile;
569         $file=~s/\Q$config{srcdir}\E\/?//;
571         my $type=pagetype($file);
572         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
573         my $content=readfile($srcfile);
574         my $page=pagename($file);
575         $pagesources{$page}=$file;
576         $content=filter($page, $page, $content);
577         $content=preprocess($page, $page, $content);
578         $content=linkify($page, $page, $content);
579         $content=htmlize($page, $page, $type, $content);
580         $pagemtime{$page}=(stat($srcfile))[9];
581         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
583         print genpage($page, $content);
584         exit 0;