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