740bb52b0fc6d9ea1797a08080a273f5fb3701f6
[ikiwiki.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
9
10 my (%backlinks, %rendered);
11 our %brokenlinks;
12 my $links_calculated=0;
13
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 }
31
32 sub backlink_pages ($) {
33         my $page=shift;
34
35         calculate_links();
36
37         return keys %{$backlinks{$page}};
38 }
39
40 sub backlinks ($) {
41         my $page=shift;
42
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 }
60
61 sub genpage ($$) {
62         my $page=shift;
63         my $content=shift;
64         
65         run_hooks(indexhtml => sub {
66                 shift->(page => $page, destpage => $page, content => $content);
67         });
68
69         my $templatefile;
70         run_hooks(templatefile => sub {
71                 return if defined $templatefile;
72                 my $file=shift->(page => $page);
73                 if (defined $file && defined template_file($file)) {
74                         $templatefile=$file;
75                 }
76         });
77         my $template;
78         if (defined $templatefile) {
79                 $template=template_depends($templatefile, $page,
80                         blind_cache => 1);
81         }
82         else {
83                 # no explicit depends as special case
84                 $template=template('page.tmpl', 
85                         blind_cache => 1);
86         }
87
88         my $actions=0;
89         if (length $config{cgiurl}) {
90                 if (IkiWiki->can("cgi_editpage")) {
91                         $template->param(editurl => cgiurl(do => "edit", page => $page));
92                         $actions++;
93                 }
94         }
95         if (defined $config{historyurl} && length $config{historyurl}) {
96                 my $u=$config{historyurl};
97                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
98                 $template->param(historyurl => $u);
99                 $actions++;
100         }
101         if ($config{discussion}) {
102                 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
103                    (length $config{cgiurl} ||
104                     exists $links{$page."/".$config{discussionpage}})) {
105                         $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
106                         $actions++;
107                 }
108         }
109         if ($actions) {
110                 $template->param(have_actions => 1);
111         }
112         templateactions($template, $page);
113
114         my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
115         my ($backlinks, $more_backlinks);
116         if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
117                 $backlinks=\@backlinks;
118                 $more_backlinks=[];
119         }
120         else {
121                 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
122                 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
123         }
124
125         $template->param(
126                 title => $page eq 'index' 
127                         ? $config{wikiname} 
128                         : pagetitle(basename($page)),
129                 wikiname => $config{wikiname},
130                 content => $content,
131                 backlinks => $backlinks,
132                 more_backlinks => $more_backlinks,
133                 mtime => displaytime($pagemtime{$page}),
134                 ctime => displaytime($pagectime{$page}, undef, 1),
135                 baseurl => baseurl($page),
136                 html5 => $config{html5},
137         );
138
139         run_hooks(pagetemplate => sub {
140                 shift->(page => $page, destpage => $page, template => $template);
141         });
142         
143         $content=$template->output;
144         
145         run_hooks(format => sub {
146                 $content=shift->(
147                         page => $page,
148                         content => $content,
149                 );
150         });
151
152         return $content;
153 }
154
155 sub scan ($) {
156         my $file=shift;
157
158         debug(sprintf(gettext("scanning %s"), $file));
159
160         my $type=pagetype($file);
161         if (defined $type) {
162                 my $srcfile=srcfile($file);
163                 my $content=readfile($srcfile);
164                 my $page=pagename($file);
165                 will_render($page, htmlpage($page), 1);
166
167                 if ($config{discussion}) {
168                         # Discussion links are a special case since they're
169                         # not in the text of the page, but on its template.
170                         $links{$page}=[ $page."/".lc($config{discussionpage}) ];
171                 }
172                 else {
173                         $links{$page}=[];
174                 }
175                 delete $typedlinks{$page};
176
177                 run_hooks(scan => sub {
178                         shift->(
179                                 page => $page,
180                                 content => $content,
181                         );
182                 });
183
184                 # Preprocess in scan-only mode.
185                 preprocess($page, $page, $content, 1);
186         }
187         else {
188                 will_render($file, $file, 1);
189         }
190 }
191
192 sub fast_file_copy (@) {
193         my $srcfile=shift;
194         my $destfile=shift;
195         my $srcfd=shift;
196         my $destfd=shift;
197         my $cleanup=shift;
198
199         my $blksize = 16384;
200         my ($len, $buf, $written);
201         while ($len = sysread $srcfd, $buf, $blksize) {
202                 if (! defined $len) {
203                         next if $! =~ /^Interrupted/;
204                         error("failed to read $srcfile: $!", $cleanup);
205                 }
206                 my $offset = 0;
207                 while ($len) {
208                         defined($written = syswrite $destfd, $buf, $len, $offset)
209                                 or error("failed to write $destfile: $!", $cleanup);
210                         $len -= $written;
211                         $offset += $written;
212                 }
213         }
214 }
215
216 sub render ($$) {
217         my $file=shift;
218         return if $rendered{$file};
219         debug(shift);
220         $rendered{$file}=1;
221         
222         my $type=pagetype($file);
223         my $srcfile=srcfile($file);
224         if (defined $type) {
225                 my $page=pagename($file);
226                 delete $depends{$page};
227                 delete $depends_simple{$page};
228                 will_render($page, htmlpage($page), 1);
229                 return if $type=~/^_/;
230                 
231                 my $content=htmlize($page, $page, $type,
232                         linkify($page, $page,
233                         preprocess($page, $page,
234                         filter($page, $page,
235                         readfile($srcfile)))));
236                 
237                 my $output=htmlpage($page);
238                 writefile($output, $config{destdir}, genpage($page, $content));
239         }
240         else {
241                 delete $depends{$file};
242                 delete $depends_simple{$file};
243                 will_render($file, $file, 1);
244                 
245                 if ($config{hardlink}) {
246                         # only hardlink if owned by same user
247                         my @stat=stat($srcfile);
248                         if ($stat[4] == $>) {
249                                 prep_writefile($file, $config{destdir});
250                                 unlink($config{destdir}."/".$file);
251                                 if (link($srcfile, $config{destdir}."/".$file)) {
252                                         return;
253                                 }
254                         }
255                         # if hardlink fails, fall back to copying
256                 }
257                 
258                 my $srcfd=readfile($srcfile, 1, 1);
259                 writefile($file, $config{destdir}, undef, 1, sub {
260                         fast_file_copy($srcfile, $file, $srcfd, @_);
261                 });
262         }
263 }
264
265 sub prune ($) {
266         my $file=shift;
267
268         unlink($file);
269         my $dir=dirname($file);
270         while (rmdir($dir)) {
271                 $dir=dirname($dir);
272         }
273 }
274
275 sub srcdir_check () {
276         # security check, avoid following symlinks in the srcdir path by default
277         my $test=$config{srcdir};
278         while (length $test) {
279                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
280                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
281                 }
282                 unless ($test=~s/\/+$//) {
283                         $test=dirname($test);
284                 }
285         }
286         
287 }
288
289 sub find_src_files () {
290         my @files;
291         my %pages;
292         eval q{use File::Find};
293         error($@) if $@;
294
295         eval q{use Cwd};
296         die $@ if $@;
297         my $origdir=getcwd();
298         my $abssrcdir=Cwd::abs_path($config{srcdir});
299
300         my ($page, $underlay);
301         my $helper=sub {
302                 my $file=decode_utf8($_);
303                 return if -l $file || -d _;
304                 $file=~s/^\.\///;
305                 return if ! length $file;
306                 $page = pagename($file);
307                 if (! exists $pagesources{$page} &&
308                     file_pruned($file)) {
309                         $File::Find::prune=1;
310                         return;
311                 }
312
313                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
314                 if (! defined $f) {
315                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
316                         return;
317                 }
318         
319                 if ($underlay) {
320                         # avoid underlaydir override attacks; see security.mdwn
321                         if (! -l "$abssrcdir/$f" && ! -e _) {
322                                 if (! $pages{$page}) {
323                                         push @files, $f;
324                                         $pages{$page}=1;
325                                 }
326                         }
327                 }
328                 else {
329                         push @files, $f;
330                         if ($pages{$page}) {
331                                 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
332                         }
333                         $pages{$page}=1;
334                 }
335         };
336
337         chdir($config{srcdir}) || die "chdir: $!";
338         find({
339                 no_chdir => 1,
340                 wanted => $helper,
341         }, '.');
342         chdir($origdir) || die "chdir: $!";
343
344         $underlay=1;
345         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
346                 chdir($_) || die "chdir: $!";
347                 find({
348                         no_chdir => 1,
349                         wanted => $helper,
350                 }, '.');
351                 chdir($origdir) || die "chdir: $!";
352         };
353
354         return \@files, \%pages;
355 }
356
357 sub find_new_files ($) {
358         my $files=shift;
359         my @new;
360         my @internal_new;
361
362         my $times_noted;
363
364         foreach my $file (@$files) {
365                 my $page=pagename($file);
366                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
367                         # the page has changed its type
368                         $forcerebuild{$page}=1;
369                 }
370                 $pagesources{$page}=$file;
371                 if (! $pagemtime{$page}) {
372                         if (isinternal($page)) {
373                                 push @internal_new, $file;
374                         }
375                         elsif ($config{rcs}) {
376                                 push @new, $file;
377                                 if ($config{gettime} && -e "$config{srcdir}/$file") {
378                                         if (! $times_noted) {
379                                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
380                                                 $times_noted=1;
381                                         }
382
383                                         eval {
384                                                 my $ctime=rcs_getctime("$config{srcdir}/$file");
385                                                 if ($ctime > 0) {
386                                                         $pagectime{$page}=$ctime;
387                                                 }
388                                         };
389                                         if ($@) {
390                                                 print STDERR $@;
391                                         }
392                                         my $mtime;
393                                         eval {
394                                                 $mtime=rcs_getmtime("$config{srcdir}/$file");
395                                         };
396                                         if ($@) {
397                                                 print STDERR $@;
398                                         }
399                                         elsif ($mtime > 0) {
400                                                 utime($mtime, $mtime, "$config{srcdir}/$file");
401                                         }
402                                 }
403                         }
404                         $pagecase{lc $page}=$page;
405                         if (! exists $pagectime{$page}) {
406                                 $pagectime{$page}=(srcfile_stat($file))[10];
407                         }
408                 }
409         }
410
411         return \@new, \@internal_new;
412 }
413
414 sub find_del_files ($) {
415         my $pages=shift;
416         my @del;
417         my @internal_del;
418
419         foreach my $page (keys %pagesources) {
420                 if (! $pages->{$page}) {
421                         if (isinternal($page)) {
422                                 push @internal_del, $pagesources{$page};
423                         }
424                         else {
425                                 push @del, $pagesources{$page};
426                         }
427                         $links{$page}=[];
428                         delete $typedlinks{$page};
429                         $renderedfiles{$page}=[];
430                         $pagemtime{$page}=0;
431                 }
432         }
433
434         return \@del, \@internal_del;
435 }
436
437 sub remove_del (@) {
438         foreach my $file (@_) {
439                 my $page=pagename($file);
440                 if (! isinternal($page)) {
441                         debug(sprintf(gettext("removing obsolete %s"), $page));
442                 }
443         
444                 foreach my $old (@{$oldrenderedfiles{$page}}) {
445                         prune($config{destdir}."/".$old);
446                 }
447
448                 foreach my $source (keys %destsources) {
449                         if ($destsources{$source} eq $page) {
450                                 delete $destsources{$source};
451                         }
452                 }
453         
454                 delete $pagecase{lc $page};
455                 $delpagesources{$page}=$pagesources{$page};
456                 delete $pagesources{$page};
457         }
458 }
459
460 sub find_changed ($) {
461         my $files=shift;
462         my @changed;
463         my @internal_changed;
464         foreach my $file (@$files) {
465                 my $page=pagename($file);
466                 my ($srcfile, @stat)=srcfile_stat($file);
467                 if (! exists $pagemtime{$page} ||
468                     $stat[9] > $pagemtime{$page} ||
469                     $forcerebuild{$page}) {
470                         $pagemtime{$page}=$stat[9];
471
472                         if (isinternal($page)) {
473                                 # Preprocess internal page in scan-only mode.
474                                 preprocess($page, $page, readfile($srcfile), 1);
475                                 push @internal_changed, $file;
476                         }
477                         else {
478                                 push @changed, $file;
479                         }
480                 }
481         }
482         return \@changed, \@internal_changed;
483 }
484
485 sub calculate_old_links ($$) {
486         my ($changed, $del)=@_;
487         my %oldlink_targets;
488         foreach my $file (@$changed, @$del) {
489                 my $page=pagename($file);
490                 if (exists $oldlinks{$page}) {
491                         foreach my $l (@{$oldlinks{$page}}) {
492                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
493                         }
494                 }
495         }
496         return \%oldlink_targets;
497 }
498
499 sub derender_internal ($) {
500         my $file=shift;
501         my $page=pagename($file);
502         delete $depends{$page};
503         delete $depends_simple{$page};
504         foreach my $old (@{$renderedfiles{$page}}) {
505                 delete $destsources{$old};
506         }
507         $renderedfiles{$page}=[];
508 }
509
510 sub render_linkers ($) {
511         my $f=shift;
512         my $p=pagename($f);
513         foreach my $page (keys %{$backlinks{$p}}) {
514                 my $file=$pagesources{$page};
515                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
516         }
517 }
518
519 sub remove_unrendered () {
520         foreach my $src (keys %rendered) {
521                 my $page=pagename($src);
522                 foreach my $file (@{$oldrenderedfiles{$page}}) {
523                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
524                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
525                                 prune($config{destdir}."/".$file);
526                         }
527                 }
528         }
529 }
530
531 sub link_types_changed ($$) {
532         # each is of the form { type => { link => 1 } }
533         my $new = shift;
534         my $old = shift;
535
536         return 0 if !defined $new && !defined $old;
537         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
538
539         while (my ($type, $links) = each %$new) {
540                 foreach my $link (keys %$links) {
541                         return 1 unless exists $old->{$type}{$link};
542                 }
543         }
544
545         while (my ($type, $links) = each %$old) {
546                 foreach my $link (keys %$links) {
547                         return 1 unless exists $new->{$type}{$link};
548                 }
549         }
550
551         return 0;
552 }
553
554 sub calculate_changed_links ($$$) {
555         my ($changed, $del, $oldlink_targets)=@_;
556
557         my (%backlinkchanged, %linkchangers);
558
559         foreach my $file (@$changed, @$del) {
560                 my $page=pagename($file);
561
562                 if (exists $links{$page}) {
563                         foreach my $l (@{$links{$page}}) {
564                                 my $target=bestlink($page, $l);
565                                 if (! exists $oldlink_targets->{$page}{$l} ||
566                                     $target ne $oldlink_targets->{$page}{$l}) {
567                                         $backlinkchanged{$target}=1;
568                                         $linkchangers{lc($page)}=1;
569                                 }
570                                 delete $oldlink_targets->{$page}{$l};
571                         }
572                 }
573                 if (exists $oldlink_targets->{$page} &&
574                     %{$oldlink_targets->{$page}}) {
575                         foreach my $target (values %{$oldlink_targets->{$page}}) {
576                                 $backlinkchanged{$target}=1;
577                         }
578                         $linkchangers{lc($page)}=1;
579                 }
580
581                 # we currently assume that changing the type of a link doesn't
582                 # change backlinks
583                 if (!exists $linkchangers{lc($page)}) {
584                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
585                                 $linkchangers{lc($page)}=1;
586                         }
587                 }
588         }
589
590         return \%backlinkchanged, \%linkchangers;
591 }
592
593 sub render_dependent ($$$$$$$) {
594         my ($files, $new, $internal_new, $del, $internal_del,
595                 $internal_changed, $linkchangers)=@_;
596
597         my @changed=(keys %rendered, @$del);
598         my @exists_changed=(@$new, @$del);
599         
600         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
601         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
602
603         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
604                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
605                         foreach my $f (@$files) {
606                                 next if $rendered{$f};
607                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
608                         }
609                         return 0;
610                 }
611         }
612          
613         foreach my $f (@$files) {
614                 next if $rendered{$f};
615                 my $p=pagename($f);
616                 my $reason = undef;
617
618                 if (exists $depends_simple{$p} && ! defined $reason) {
619                         foreach my $d (keys %{$depends_simple{$p}}) {
620                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
621                                      $lc_changed{$d})
622                                     ||
623                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
624                                      $lc_exists_changed{$d})
625                                     ||
626                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
627                                      $linkchangers->{$d})
628                                 ) {
629                                         $reason = $d;
630                                         last;
631                                 }
632                         }
633                 }
634         
635                 if (exists $depends{$p} && ! defined $reason) {
636                         foreach my $dep (keys %{$depends{$p}}) {
637                                 my $sub=pagespec_translate($dep);
638                                 next unless defined $sub;
639
640                                 # only consider internal files
641                                 # if the page explicitly depends
642                                 # on such files
643                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
644
645                                 my $in=sub {
646                                         my $list=shift;
647                                         my $type=shift;
648                                         foreach my $file (@$list) {
649                                                 next if $file eq $f;
650                                                 my $page=pagename($file);
651                                                 if ($sub->($page, location => $p)) {
652                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
653                                                                 next unless $linkchangers->{lc($page)};
654                                                         }
655                                                         $reason=$page;
656                                                         return 1;
657                                                 }
658                                         }
659                                         return undef;
660                                 };
661
662                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
663                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
664                                         last if $internal_dep && (
665                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
666                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
667                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
668                                         );
669                                 }
670                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
671                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
672                                         last if $internal_dep && (
673                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
674                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
675                                         );
676                                 }
677                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
678                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
679                                         last if $internal_dep && (
680                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
681                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
682                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
683                                         );
684                                 }
685                         }
686                 }
687         
688                 if (defined $reason) {
689                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
690                         return 1;
691                 }
692         }
693
694         return 0;
695 }
696
697 sub render_backlinks ($) {
698         my $backlinkchanged=shift;
699         foreach my $link (keys %$backlinkchanged) {
700                 my $linkfile=$pagesources{$link};
701                 if (defined $linkfile) {
702                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
703                 }
704         }
705 }
706
707 sub gen_autofile ($$$) {
708         my $autofile=shift;
709         my $pages=shift;
710         my $del=shift;
711
712         if (file_pruned($autofile)) {
713                 return;
714         }
715
716         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
717         if (! defined $file) {
718                 return;
719         }
720
721         # Remember autofiles that were tried, and never try them again later.
722         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
723                 return;
724         }
725         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
726
727         if (srcfile($autofile, 1) || file_pruned($autofile)) {
728                 return;
729         }
730         
731         if (-l $file || -d _ || -e _) {
732                 return;
733         }
734
735         my $page = pagename($file);
736         if ($pages->{$page}) {
737                 return;
738         }
739
740         if (grep { $_ eq $autofile } @$del) {
741                 return;
742         }
743
744         $autofiles{$autofile}{generator}->();
745         $pages->{$page}=1;
746         return 1;
747 }
748
749
750 sub refresh () {
751         srcdir_check();
752         run_hooks(refresh => sub { shift->() });
753         my ($files, $pages)=find_src_files();
754         my ($new, $internal_new)=find_new_files($files);
755         my ($del, $internal_del)=find_del_files($pages);
756         my ($changed, $internal_changed)=find_changed($files);
757         run_hooks(needsbuild => sub { shift->($changed) });
758         my $oldlink_targets=calculate_old_links($changed, $del);
759
760         foreach my $file (@$changed) {
761                 scan($file);
762         }
763
764         foreach my $autofile (keys %autofiles) {
765                 if (gen_autofile($autofile, $pages, $del)) {
766                         push @{$files}, $autofile;
767                         push @{$new}, $autofile if find_new_files([$autofile]);
768                         push @{$changed}, $autofile if find_changed([$autofile]);
769                         
770                         scan($autofile);
771                 }
772         }
773
774         calculate_links();
775         
776         remove_del(@$del, @$internal_del);
777
778         foreach my $file (@$changed) {
779                 render($file, sprintf(gettext("building %s"), $file));
780         }
781         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
782                 derender_internal($file);
783         }
784
785         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
786                 $del, $oldlink_targets);
787
788         foreach my $file (@$new, @$del) {
789                 render_linkers($file);
790         }
791
792         if (@$changed || @$internal_changed ||
793             @$del || @$internal_del || @$internal_new) {
794                 1 while render_dependent($files, $new, $internal_new,
795                         $del, $internal_del, $internal_changed,
796                         $linkchangers);
797         }
798
799         render_backlinks($backlinkchanged);
800         remove_unrendered();
801
802         if (@$del || @$internal_del) {
803                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
804         }
805         if (%rendered) {
806                 run_hooks(change => sub { shift->(keys %rendered) });
807         }
808 }
809
810 sub clean_rendered {
811         lockwiki();
812         loadindex();
813         remove_unrendered();
814         foreach my $page (keys %oldrenderedfiles) {
815                 foreach my $file (@{$oldrenderedfiles{$page}}) {
816                         prune($config{destdir}."/".$file);
817                 }
818         }
819 }
820
821 sub commandline_render () {
822         lockwiki();
823         loadindex();
824         unlockwiki();
825
826         my $srcfile=possibly_foolish_untaint($config{render});
827         my $file=$srcfile;
828         $file=~s/\Q$config{srcdir}\E\/?//;
829
830         my $type=pagetype($file);
831         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
832         my $content=readfile($srcfile);
833         my $page=pagename($file);
834         $pagesources{$page}=$file;
835         $content=filter($page, $page, $content);
836         $content=preprocess($page, $page, $content);
837         $content=linkify($page, $page, $content);
838         $content=htmlize($page, $page, $type, $content);
839         $pagemtime{$page}=(stat($srcfile))[9];
840         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
841
842         print genpage($page, $content);
843         exit 0;
844 }
845
846 1