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