+ @$changed=grep $existingfiles{$_}, @$ret;
+ }
+ });
+ my $oldlink_targets=calculate_old_links($changed, $del);
+
+ foreach my $file (@$changed) {
+ scan($file);
+ }
+
+ foreach my $autofile (keys %autofiles) {
+ if (gen_autofile($autofile, $pages, $del)) {
+ push @{$files}, $autofile;
+ push @{$new}, $autofile if find_new_files([$autofile]);
+ push @{$changed}, $autofile if find_changed([$autofile]);
+
+ scan($autofile);
+ }
+ }
+
+ calculate_links();
+
+ # At this point it becomes OK to start matching pagespecs.
+ $phase = PHASE_RENDER;
+ # Save some memory in full rebuilds: we no longer need to keep
+ # track of which pages we've scanned, because we can assume the
+ # answer is "all of them".
+ %scanned = () if $config{rebuild};
+
+ remove_del(@$del, @$internal_del);
+
+ foreach my $file (@$changed) {
+ render($file, sprintf(gettext("building %s"), $file));
+ }
+ foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
+ derender_internal($file);
+ }
+
+ run_hooks(build_affected => sub {
+ my %affected = shift->();
+ while (my ($page, $message) = each %affected) {
+ next unless exists $pagesources{$page};
+ render($pagesources{$page}, $message);
+ }
+ });
+
+ my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
+ $del, $oldlink_targets);
+
+ foreach my $file (@$new, @$del) {
+ render_linkers($file);
+ }
+
+ if (@$changed || @$internal_changed ||
+ @$del || @$internal_del || @$internal_new) {
+ 1 while render_dependent($files, $new, $internal_new,
+ $del, $internal_del, $internal_changed,
+ $linkchangers);
+ }
+
+ render_backlinks($backlinkchanged);
+ remove_unrendered();
+
+ if (@$del || @$internal_del) {
+ run_hooks(delete => sub { shift->(@$del, @$internal_del) });
+ }
+ if (%rendered) {
+ run_hooks(rendered => sub { shift->(keys %rendered) });
+ run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
+ }
+ my %all_changed = map { $_ => 1 }
+ @$new, @$changed, @$del,
+ @$internal_new, @$internal_changed, @$internal_del;
+ run_hooks(changes => sub { shift->(keys %all_changed) });
+}
+
+sub clean_rendered {
+ lockwiki();
+ loadindex();
+ remove_unrendered();
+ foreach my $page (keys %oldrenderedfiles) {
+ foreach my $file (@{$oldrenderedfiles{$page}}) {
+ prune($config{destdir}."/".$file, $config{destdir});