Merge branch 'master' of ssh://git.ikiwiki.info
[ikiwiki.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8
9 my (%backlinks, %rendered);
10 our %brokenlinks;
11 my $links_calculated=0;
12
13 sub calculate_links () {
14         return if $links_calculated;
15         %backlinks=%brokenlinks=();
16         foreach my $page (keys %links) {
17                 foreach my $link (@{$links{$page}}) {
18                         my $bestlink=bestlink($page, $link);
19                         if (length $bestlink) {
20                                 $backlinks{$bestlink}{$page}=1
21                                         if $bestlink ne $page;
22                         }
23                         else {
24                                 push @{$brokenlinks{$link}}, $page;
25                         }
26                 }
27         }
28         $links_calculated=1;
29 }
30
31 sub backlink_pages ($) {
32         my $page=shift;
33
34         calculate_links();
35
36         return keys %{$backlinks{$page}};
37 }
38
39 sub backlinks ($) {
40         my $page=shift;
41
42         my @links;
43         foreach my $p (backlink_pages($page)) {
44                 my $href=urlto($p, $page);
45
46                 # Trim common dir prefixes from both pages.
47                 my $p_trimmed=$p;
48                 my $page_trimmed=$page;
49                 my $dir;
50                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
51                         defined $dir &&
52                         $p_trimmed=~s/^\Q$dir\E// &&
53                         $page_trimmed=~s/^\Q$dir\E//;
54                                
55                 push @links, { url => $href, page => pagetitle($p_trimmed) };
56         }
57         return @links;
58 }
59
60 sub genpage ($$) {
61         my $page=shift;
62         my $content=shift;
63         
64         run_hooks(indexhtml => sub {
65                 shift->(page => $page, destpage => $page, content => $content);
66         });
67
68         my $templatefile;
69         run_hooks(templatefile => sub {
70                 return if defined $templatefile;
71                 my $file=shift->(page => $page);
72                 if (defined $file && defined template_file($file)) {
73                         $templatefile=$file;
74                 }
75         });
76         my $template;
77         if (defined $templatefile) {
78                 $template=template_depends($templatefile, $page,
79                         blind_cache => 1);
80         }
81         else {
82                 # no explicit depends as special case
83                 $template=template('page.tmpl', 
84                         blind_cache => 1);
85         }
86
87         my $actions=0;
88         if (length $config{cgiurl}) {
89                 if (IkiWiki->can("cgi_editpage")) {
90                         $template->param(editurl => cgiurl(do => "edit", page => $page));
91                         $actions++;
92                 }
93         }
94         if (defined $config{historyurl} && length $config{historyurl}) {
95                 my $u=$config{historyurl};
96                 my $p=uri_escape_utf8($pagesources{$page}, '^A-Za-z0-9\-\._~/');
97                 $u=~s/\[\[file\]\]/$p/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                 # Preprocess in scan-only mode.
178                 preprocess($page, $page, $content, 1);
179
180                 run_hooks(scan => sub {
181                         shift->(
182                                 page => $page,
183                                 content => $content,
184                         );
185                 });
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         my $up_to=shift;
268
269         unlink($file);
270         my $dir=dirname($file);
271         while ((! defined $up_to || $dir =~ m{^\Q$up_to\E\/}) && rmdir($dir)) {
272                 $dir=dirname($dir);
273         }
274 }
275
276 sub srcdir_check () {
277         # security check, avoid following symlinks in the srcdir path by default
278         my $test=$config{srcdir};
279         while (length $test) {
280                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
281                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
282                 }
283                 unless ($test=~s/\/+$//) {
284                         $test=dirname($test);
285                 }
286         }
287         
288 }
289
290 sub find_src_files () {
291         my @files;
292         my %pages;
293         eval q{use File::Find};
294         error($@) if $@;
295
296         eval q{use Cwd};
297         die $@ if $@;
298         my $origdir=getcwd();
299         my $abssrcdir=Cwd::abs_path($config{srcdir});
300
301         my ($page, $underlay);
302         my $helper=sub {
303                 my $file=decode_utf8($_);
304
305                 return if -l $file || -d _;
306                 $file=~s/^\.\///;
307                 return if ! length $file;
308                 $page = pagename($file);
309                 if (! exists $pagesources{$page} &&
310                     file_pruned($file)) {
311                         $File::Find::prune=1;
312                         return;
313                 }
314
315                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
316                 if (! defined $f) {
317                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
318                         return;
319                 }
320         
321                 if ($underlay) {
322                         # avoid underlaydir override attacks; see security.mdwn
323                         if (! -l "$abssrcdir/$f" && ! -e _) {
324                                 if (! $pages{$page}) {
325                                         push @files, $f;
326                                         $pages{$page}=1;
327                                 }
328                         }
329                 }
330                 else {
331                         push @files, $f;
332                         if ($pages{$page}) {
333                                 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
334                         }
335                         $pages{$page}=1;
336                 }
337         };
338
339         chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
340         find({
341                 no_chdir => 1,
342                 wanted => $helper,
343         }, '.');
344         chdir($origdir) || die "chdir $origdir: $!";
345
346         $underlay=1;
347         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
348                 if (chdir($_)) {
349                         find({
350                                 no_chdir => 1,
351                                 wanted => $helper,
352                         }, '.');
353                         chdir($origdir) || die "chdir: $!";
354                 }
355         };
356
357         return \@files, \%pages;
358 }
359
360 sub find_new_files ($) {
361         my $files=shift;
362         my @new;
363         my @internal_new;
364
365         my $times_noted;
366
367         foreach my $file (@$files) {
368                 my $page=pagename($file);
369
370                 if ($config{rcs} && $config{gettime} &&
371                     -e "$config{srcdir}/$file") {
372                         if (! $times_noted) {
373                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
374                                 $times_noted=1;
375                         }
376
377                         eval {
378                                 my $ctime=rcs_getctime($file);
379                                 if ($ctime > 0) {
380                                         $pagectime{$page}=$ctime;
381                                 }
382                         };
383                         if ($@) {
384                                 print STDERR $@;
385                         }
386                         my $mtime;
387                         eval {
388                                 $mtime=rcs_getmtime($file);
389                         };
390                         if ($@) {
391                                 print STDERR $@;
392                         }
393                         elsif ($mtime > 0) {
394                                 utime($mtime, $mtime, "$config{srcdir}/$file");
395                         }
396                 }
397
398                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
399                         # the page has changed its type
400                         $forcerebuild{$page}=1;
401                 }
402                 $pagesources{$page}=$file;
403                 if (! $pagemtime{$page}) {
404                         if (isinternal($page)) {
405                                 push @internal_new, $file;
406                         }
407                         else {
408                                 push @new, $file;
409                         }
410                         $pagecase{lc $page}=$page;
411                         if (! exists $pagectime{$page}) {
412                                 $pagectime{$page}=(srcfile_stat($file))[10];
413                         }
414                 }
415         }
416
417         return \@new, \@internal_new;
418 }
419
420 sub find_del_files ($) {
421         my $pages=shift;
422         my @del;
423         my @internal_del;
424
425         foreach my $page (keys %pagesources) {
426                 if (! $pages->{$page}) {
427                         if (isinternal($page)) {
428                                 push @internal_del, $pagesources{$page};
429                         }
430                         else {
431                                 push @del, $pagesources{$page};
432                         }
433                         $links{$page}=[];
434                         delete $typedlinks{$page};
435                         $renderedfiles{$page}=[];
436                         $pagemtime{$page}=0;
437                 }
438         }
439
440         return \@del, \@internal_del;
441 }
442
443 sub remove_del (@) {
444         foreach my $file (@_) {
445                 my $page=pagename($file);
446                 if (! isinternal($page)) {
447                         debug(sprintf(gettext("removing obsolete %s"), $page));
448                 }
449         
450                 foreach my $old (@{$oldrenderedfiles{$page}}) {
451                         prune($config{destdir}."/".$old, $config{destdir});
452                 }
453
454                 foreach my $source (keys %destsources) {
455                         if ($destsources{$source} eq $page) {
456                                 delete $destsources{$source};
457                         }
458                 }
459         
460                 delete $pagecase{lc $page};
461                 $delpagesources{$page}=$pagesources{$page};
462                 delete $pagesources{$page};
463         }
464 }
465
466 sub find_changed ($) {
467         my $files=shift;
468         my @changed;
469         my @internal_changed;
470         foreach my $file (@$files) {
471                 my $page=pagename($file);
472                 my ($srcfile, @stat)=srcfile_stat($file);
473                 if (! exists $pagemtime{$page} ||
474                     $stat[9] > $pagemtime{$page} ||
475                     $forcerebuild{$page}) {
476                         $pagemtime{$page}=$stat[9];
477
478                         if (isinternal($page)) {
479                                 my $content = readfile($srcfile);
480
481                                 # Preprocess internal page in scan-only mode.
482                                 preprocess($page, $page, $content, 1);
483
484                                 run_hooks(scan => sub {
485                                         shift->(
486                                                 page => $page,
487                                                 content => $content,
488                                         );
489                                 });
490
491                                 push @internal_changed, $file;
492                         }
493                         else {
494                                 push @changed, $file;
495                         }
496                 }
497         }
498         return \@changed, \@internal_changed;
499 }
500
501 sub calculate_old_links ($$) {
502         my ($changed, $del)=@_;
503         my %oldlink_targets;
504         foreach my $file (@$changed, @$del) {
505                 my $page=pagename($file);
506                 if (exists $oldlinks{$page}) {
507                         foreach my $l (@{$oldlinks{$page}}) {
508                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
509                         }
510                 }
511         }
512         return \%oldlink_targets;
513 }
514
515 sub derender_internal ($) {
516         my $file=shift;
517         my $page=pagename($file);
518         delete $depends{$page};
519         delete $depends_simple{$page};
520         foreach my $old (@{$renderedfiles{$page}}) {
521                 delete $destsources{$old};
522         }
523         $renderedfiles{$page}=[];
524 }
525
526 sub render_linkers ($) {
527         my $f=shift;
528         my $p=pagename($f);
529         foreach my $page (keys %{$backlinks{$p}}) {
530                 my $file=$pagesources{$page};
531                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
532         }
533 }
534
535 sub remove_unrendered () {
536         foreach my $src (keys %rendered) {
537                 my $page=pagename($src);
538                 foreach my $file (@{$oldrenderedfiles{$page}}) {
539                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
540                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
541                                 prune($config{destdir}."/".$file, $config{destdir});
542                         }
543                 }
544         }
545 }
546
547 sub link_types_changed ($$) {
548         # each is of the form { type => { link => 1 } }
549         my $new = shift;
550         my $old = shift;
551
552         return 0 if !defined $new && !defined $old;
553         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
554
555         while (my ($type, $links) = each %$new) {
556                 foreach my $link (keys %$links) {
557                         return 1 unless exists $old->{$type}{$link};
558                 }
559         }
560
561         while (my ($type, $links) = each %$old) {
562                 foreach my $link (keys %$links) {
563                         return 1 unless exists $new->{$type}{$link};
564                 }
565         }
566
567         return 0;
568 }
569
570 sub calculate_changed_links ($$$) {
571         my ($changed, $del, $oldlink_targets)=@_;
572
573         my (%backlinkchanged, %linkchangers);
574
575         foreach my $file (@$changed, @$del) {
576                 my $page=pagename($file);
577
578                 if (exists $links{$page}) {
579                         foreach my $l (@{$links{$page}}) {
580                                 my $target=bestlink($page, $l);
581                                 if (! exists $oldlink_targets->{$page}{$l} ||
582                                     $target ne $oldlink_targets->{$page}{$l}) {
583                                         $backlinkchanged{$target}=1;
584                                         $linkchangers{lc($page)}=1;
585                                 }
586                                 delete $oldlink_targets->{$page}{$l};
587                         }
588                 }
589                 if (exists $oldlink_targets->{$page} &&
590                     %{$oldlink_targets->{$page}}) {
591                         foreach my $target (values %{$oldlink_targets->{$page}}) {
592                                 $backlinkchanged{$target}=1;
593                         }
594                         $linkchangers{lc($page)}=1;
595                 }
596
597                 # we currently assume that changing the type of a link doesn't
598                 # change backlinks
599                 if (!exists $linkchangers{lc($page)}) {
600                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
601                                 $linkchangers{lc($page)}=1;
602                         }
603                 }
604         }
605
606         return \%backlinkchanged, \%linkchangers;
607 }
608
609 sub render_dependent ($$$$$$$) {
610         my ($files, $new, $internal_new, $del, $internal_del,
611                 $internal_changed, $linkchangers)=@_;
612
613         my @changed=(keys %rendered, @$del);
614         my @exists_changed=(@$new, @$del);
615         
616         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
617         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
618
619         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
620                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
621                         foreach my $f (@$files) {
622                                 next if $rendered{$f};
623                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
624                         }
625                         return 0;
626                 }
627         }
628          
629         foreach my $f (@$files) {
630                 next if $rendered{$f};
631                 my $p=pagename($f);
632                 my $reason = undef;
633
634                 if (exists $depends_simple{$p} && ! defined $reason) {
635                         foreach my $d (keys %{$depends_simple{$p}}) {
636                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
637                                      $lc_changed{$d})
638                                     ||
639                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
640                                      $lc_exists_changed{$d})
641                                     ||
642                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
643                                      $linkchangers->{$d})
644                                 ) {
645                                         $reason = $d;
646                                         last;
647                                 }
648                         }
649                 }
650         
651                 if (exists $depends{$p} && ! defined $reason) {
652                         foreach my $dep (keys %{$depends{$p}}) {
653                                 my $sub=pagespec_translate($dep);
654                                 next unless defined $sub;
655
656                                 # only consider internal files
657                                 # if the page explicitly depends
658                                 # on such files
659                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
660
661                                 my $in=sub {
662                                         my $list=shift;
663                                         my $type=shift;
664                                         foreach my $file (@$list) {
665                                                 next if $file eq $f;
666                                                 my $page=pagename($file);
667                                                 if ($sub->($page, location => $p)) {
668                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
669                                                                 next unless $linkchangers->{lc($page)};
670                                                         }
671                                                         $reason=$page;
672                                                         return 1;
673                                                 }
674                                         }
675                                         return undef;
676                                 };
677
678                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
679                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
680                                         last if $internal_dep && (
681                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
682                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
683                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
684                                         );
685                                 }
686                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
687                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
688                                         last if $internal_dep && (
689                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
690                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
691                                         );
692                                 }
693                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
694                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
695                                         last if $internal_dep && (
696                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
697                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
698                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
699                                         );
700                                 }
701                         }
702                 }
703         
704                 if (defined $reason) {
705                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
706                         return 1;
707                 }
708         }
709
710         return 0;
711 }
712
713 sub render_backlinks ($) {
714         my $backlinkchanged=shift;
715         foreach my $link (keys %$backlinkchanged) {
716                 my $linkfile=$pagesources{$link};
717                 if (defined $linkfile) {
718                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
719                 }
720         }
721 }
722
723 sub gen_autofile ($$$) {
724         my $autofile=shift;
725         my $pages=shift;
726         my $del=shift;
727
728         if (file_pruned($autofile)) {
729                 return;
730         }
731
732         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
733         if (! defined $file) {
734                 return;
735         }
736
737         # Remember autofiles that were tried, and never try them again later.
738         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
739                 return;
740         }
741         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
742
743         if (srcfile($autofile, 1) || file_pruned($autofile)) {
744                 return;
745         }
746         
747         if (-l $file || -d _ || -e _) {
748                 return;
749         }
750
751         my $page = pagename($file);
752         if ($pages->{$page}) {
753                 return;
754         }
755
756         if (grep { $_ eq $autofile } @$del) {
757                 return;
758         }
759
760         $autofiles{$autofile}{generator}->();
761         $pages->{$page}=1;
762         return 1;
763 }
764
765
766 sub refresh () {
767         srcdir_check();
768         run_hooks(refresh => sub { shift->() });
769         my ($files, $pages)=find_src_files();
770         my ($new, $internal_new)=find_new_files($files);
771         my ($del, $internal_del)=find_del_files($pages);
772         my ($changed, $internal_changed)=find_changed($files);
773         my %existingfiles;
774         run_hooks(needsbuild => sub {
775                 my $ret=shift->($changed, [@$del, @$internal_del]);
776                 if (ref $ret eq 'ARRAY') {
777                         if (! %existingfiles) {
778                                 foreach my $f (@$files) {
779                                         $existingfiles{$f}=1;
780                                 }
781                         }
782                         @$changed=grep $existingfiles{$_}, @$ret;
783                 }
784         });
785         my $oldlink_targets=calculate_old_links($changed, $del);
786
787         foreach my $file (@$changed) {
788                 scan($file);
789         }
790
791         foreach my $autofile (keys %autofiles) {
792                 if (gen_autofile($autofile, $pages, $del)) {
793                         push @{$files}, $autofile;
794                         push @{$new}, $autofile if find_new_files([$autofile]);
795                         push @{$changed}, $autofile if find_changed([$autofile]);
796                         
797                         scan($autofile);
798                 }
799         }
800
801         calculate_links();
802         
803         remove_del(@$del, @$internal_del);
804
805         foreach my $file (@$changed) {
806                 render($file, sprintf(gettext("building %s"), $file));
807         }
808         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
809                 derender_internal($file);
810         }
811
812         run_hooks(build_affected => sub {
813                 my %affected = shift->();
814                 while (my ($page, $message) = each %affected) {
815                         next unless exists $pagesources{$page};
816                         render($pagesources{$page}, $message);
817                 }
818         });
819
820         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
821                 $del, $oldlink_targets);
822
823         foreach my $file (@$new, @$del) {
824                 render_linkers($file);
825         }
826
827         if (@$changed || @$internal_changed ||
828             @$del || @$internal_del || @$internal_new) {
829                 1 while render_dependent($files, $new, $internal_new,
830                         $del, $internal_del, $internal_changed,
831                         $linkchangers);
832         }
833
834         render_backlinks($backlinkchanged);
835         remove_unrendered();
836
837         if (@$del || @$internal_del) {
838                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
839         }
840         if (%rendered) {
841                 run_hooks(rendered => sub { shift->(keys %rendered) });
842                 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
843         }
844         my %all_changed = map { $_ => 1 }
845                 @$new, @$changed, @$del,
846                 @$internal_new, @$internal_changed, @$internal_del;
847         run_hooks(changes => sub { shift->(keys %all_changed) });
848 }
849
850 sub clean_rendered {
851         lockwiki();
852         loadindex();
853         remove_unrendered();
854         foreach my $page (keys %oldrenderedfiles) {
855                 foreach my $file (@{$oldrenderedfiles{$page}}) {
856                         prune($config{destdir}."/".$file, $config{destdir});
857                 }
858         }
859 }
860
861 sub commandline_render () {
862         lockwiki();
863         loadindex();
864         unlockwiki();
865
866         my $srcfile=possibly_foolish_untaint($config{render});
867         my $file=$srcfile;
868         $file=~s/\Q$config{srcdir}\E\/?//;
869
870         my $type=pagetype($file);
871         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
872         my $content=readfile($srcfile);
873         my $page=pagename($file);
874         $pagesources{$page}=$file;
875         $content=filter($page, $page, $content);
876         $content=preprocess($page, $page, $content);
877         $content=linkify($page, $page, $content);
878         $content=htmlize($page, $page, $type, $content);
879         $pagemtime{$page}=(stat($srcfile))[9];
880         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
881
882         print genpage($page, $content);
883         exit 0;
884 }
885
886 1