]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Add a manual test for reverting git commits
[git.ikiwiki.info.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
3 package IkiWiki;
5 use warnings;
6 use strict;
7 use IkiWiki;
9 my (%backlinks, %rendered, %scanned);
10 our %brokenlinks;
11 my $links_calculated=0;
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 }
31 sub backlink_pages ($) {
32         my $page=shift;
34         calculate_links();
36         return keys %{$backlinks{$page}};
37 }
39 sub backlinks ($) {
40         my $page=shift;
42         my @links;
43         foreach my $p (backlink_pages($page)) {
44                 my $href=urlto($p, $page);
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 }
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         });
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         }
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);
114         my @backlinks=sort { $a->{page} cmp $b->{page} || $a->{url} cmp $b->{url} } 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         }
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                 responsive_layout => $config{responsive_layout},
138         );
140         run_hooks(pagetemplate => sub {
141                 shift->(page => $page, destpage => $page, template => $template);
142         });
143         
144         $content=$template->output;
145         
146         run_hooks(format => sub {
147                 $content=shift->(
148                         page => $page,
149                         content => $content,
150                 );
151         });
153         return $content;
156 sub scan ($) {
157         my $file=shift;
158         return if ($config{rebuild} && $phase > PHASE_SCAN) || $scanned{$file};
159         $scanned{$file}=1;
161         debug(sprintf(gettext("scanning %s"), $file));
163         my $type=pagetype($file);
164         if (defined $type) {
165                 my $srcfile=srcfile($file);
166                 my $content=readfile($srcfile);
167                 my $page=pagename($file);
168                 will_render($page, htmlpage($page), 1);
170                 if ($config{discussion}) {
171                         # Discussion links are a special case since they're
172                         # not in the text of the page, but on its template.
173                         $links{$page}=[ $page."/".lc($config{discussionpage}) ];
174                 }
175                 else {
176                         $links{$page}=[];
177                 }
178                 delete $typedlinks{$page};
180                 # Preprocess in scan-only mode.
181                 preprocess($page, $page, $content, 1);
183                 run_hooks(scan => sub {
184                         shift->(
185                                 page => $page,
186                                 content => $content,
187                         );
188                 });
189         }
190         else {
191                 will_render($file, $file, 1);
192         }
195 sub fast_file_copy (@) {
196         my $srcfile=shift;
197         my $destfile=shift;
198         my $srcfd=shift;
199         my $destfd=shift;
200         my $cleanup=shift;
202         my $blksize = 16384;
203         my ($len, $buf, $written);
204         while ($len = sysread $srcfd, $buf, $blksize) {
205                 if (! defined $len) {
206                         next if $! =~ /^Interrupted/;
207                         error("failed to read $srcfile: $!", $cleanup);
208                 }
209                 my $offset = 0;
210                 while ($len) {
211                         defined($written = syswrite $destfd, $buf, $len, $offset)
212                                 or error("failed to write $destfile: $!", $cleanup);
213                         $len -= $written;
214                         $offset += $written;
215                 }
216         }
219 sub render ($$) {
220         my $file=shift;
221         return if $rendered{$file};
222         debug(shift);
223         $rendered{$file}=1;
224         
225         my $type=pagetype($file);
226         my $srcfile=srcfile($file);
227         if (defined $type) {
228                 my $page=pagename($file);
229                 delete $depends{$page};
230                 delete $depends_simple{$page};
231                 will_render($page, htmlpage($page), 1);
232                 return if $type=~/^_/;
233                 
234                 my $content=htmlize($page, $page, $type,
235                         linkify($page, $page,
236                         preprocess($page, $page,
237                         filter($page, $page,
238                         readfile($srcfile)))));
239                 
240                 my $output=htmlpage($page);
241                 writefile($output, $config{destdir}, genpage($page, $content));
242         }
243         else {
244                 delete $depends{$file};
245                 delete $depends_simple{$file};
246                 will_render($file, $file, 1);
247                 
248                 if ($config{hardlink}) {
249                         # only hardlink if owned by same user
250                         my @stat=stat($srcfile);
251                         if ($stat[4] == $>) {
252                                 prep_writefile($file, $config{destdir});
253                                 unlink($config{destdir}."/".$file);
254                                 if (link($srcfile, $config{destdir}."/".$file)) {
255                                         return;
256                                 }
257                         }
258                         # if hardlink fails, fall back to copying
259                 }
260                 
261                 my $srcfd=readfile($srcfile, 1, 1);
262                 writefile($file, $config{destdir}, undef, 1, sub {
263                         fast_file_copy($srcfile, $file, $srcfd, @_);
264                 });
265         }
268 sub prune ($;$) {
269         my $file=shift;
270         my $up_to=shift;
272         unlink($file);
273         my $dir=dirname($file);
274         while ((! defined $up_to || $dir =~ m{^\Q$up_to\E\/}) && rmdir($dir)) {
275                 $dir=dirname($dir);
276         }
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         
293 # Finds all files in the srcdir, and the underlaydirs.
294 # Returns the files, and their corresponding pages.
296 # When run in only_underlay mode, adds only the underlay files to
297 # the files and pages passed in.
298 sub find_src_files (;$$$) {
299         my $only_underlay=shift;
300         my @files;
301         if (defined $_[0]) {
302                 @files=@{shift()};
303         }
304         my %pages;
305         if (defined $_[0]) {
306                 %pages=%{shift()};
307         }
309         eval q{use File::Find};
310         error($@) if $@;
312         eval q{use Cwd};
313         die $@ if $@;
314         my $origdir=getcwd();
315         my $abssrcdir=Cwd::abs_path($config{srcdir});
316         
317         @IkiWiki::underlayfiles=();
319         my ($page, $underlay);
320         my $helper=sub {
321                 my $file=decode_utf8($_);
323                 return if -l $file || -d _;
324                 $file=~s/^\.\///;
325                 return if ! length $file;
326                 $page = pagename($file);
327                 if (! exists $pagesources{$page} &&
328                     file_pruned($file)) {
329                         no warnings 'once';
330                         $File::Find::prune=1;
331                         return;
332                 }
334                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
335                 if (! defined $f) {
336                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
337                         return;
338                 }
339         
340                 if ($underlay) {
341                         # avoid underlaydir override attacks; see security.mdwn
342                         if (! -l "$abssrcdir/$f" && ! -e _) {
343                                 if (! $pages{$page}) {
344                                         push @files, $f;
345                                         push @IkiWiki::underlayfiles, $f;
346                                         $pages{$page}=1;
347                                 }
348                         }
349                 }
350                 else {
351                         push @files, $f;
352                         if ($pages{$page}) {
353                                 debug(sprintf(gettext("%s has multiple possible source files; one will be chosen at random"), $page));
354                         }
355                         $pages{$page}=1;
356                 }
357         };
359         unless ($only_underlay) {
360                 chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
361                 find({
362                         no_chdir => 1,
363                         wanted => $helper,
364                 }, '.');
365                 chdir($origdir) || die "chdir $origdir: $!";
366         }
368         $underlay=1;
369         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
370                 if (chdir($_)) {
371                         find({
372                                 no_chdir => 1,
373                                 wanted => $helper,
374                         }, '.');
375                         chdir($origdir) || die "chdir: $!";
376                 }
377         };
379         return \@files, \%pages;
382 # Given a hash of files that have changed, and a hash of files that were
383 # deleted, should return the same results as find_src_files, with the same
384 # sanity checks. But a lot faster!
385 sub process_changed_files ($$) {
386         my $changed_raw=shift;
387         my $deleted_raw=shift;
389         my @files;
390         my %pages;
392         foreach my $file (keys %$changed_raw) {
393                 my $page = pagename($file);
394                 next if ! exists $pagesources{$page} && file_pruned($file);
395                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
396                 if (! defined $f) {
397                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
398                         next;
399                 }
400                 push @files, $f;
401                 if ($pages{$page}) {
402                         debug(sprintf(gettext("%s has multiple possible source files; one will be chosen at random"), $page));
403                 }
404                 $pages{$page}=1;
405         }
407         # So far, we only have the changed files. Now add in all the old
408         # files that were not changed or deleted, excluding ones that came
409         # from the underlay.
410         my %old_underlay;
411         foreach my $f (@IkiWiki::underlayfiles) {
412                 $old_underlay{$f}=1;
413         }
414         foreach my $page (keys %pagesources) {
415                 my $f=$pagesources{$page};
416                 unless ($old_underlay{$f} || exists $pages{$page} || exists $deleted_raw->{$f}) {
417                         $pages{$page}=1;
418                         push @files, $f;
419                 }
420         }
422         # add in the underlay
423         find_src_files(1, \@files, \%pages);
426 sub find_new_files ($) {
427         my $files=shift;
428         my @new;
429         my @internal_new;
431         my $times_noted;
433         foreach my $file (@$files) {
434                 my $page=pagename($file);
436                 if ($config{rcs} && $config{gettime} &&
437                     -e "$config{srcdir}/$file") {
438                         if (! $times_noted) {
439                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
440                                 $times_noted=1;
441                         }
443                         eval {
444                                 my $ctime=rcs_getctime($file);
445                                 if ($ctime > 0) {
446                                         $pagectime{$page}=$ctime;
447                                 }
448                         };
449                         if ($@) {
450                                 print STDERR $@;
451                         }
452                         my $mtime;
453                         eval {
454                                 $mtime=rcs_getmtime($file);
455                         };
456                         if ($@) {
457                                 print STDERR $@;
458                         }
459                         elsif ($mtime > 0) {
460                                 utime($mtime, $mtime, "$config{srcdir}/$file");
461                         }
462                 }
464                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
465                         # the page has changed its type
466                         $forcerebuild{$page}=1;
467                 }
468                 $pagesources{$page}=$file;
469                 if (! $pagemtime{$page}) {
470                         if (isinternal($page)) {
471                                 push @internal_new, $file;
472                         }
473                         else {
474                                 push @new, $file;
475                         }
476                         $pagecase{lc $page}=$page;
477                         if (! exists $pagectime{$page}) {
478                                 my @stat=srcfile_stat($file, 1);
479                                 # For the creation time of the page, take the
480                                 # inode change time (not creation time!) or
481                                 # the modification time, whichever is older.
482                                 my $ctime=($stat[10] < $stat[9] ? $stat[10] : $stat[9]);
483                                 $pagectime{$page}=$ctime if defined $ctime;
484                         }
485                 }
486         }
488         return \@new, \@internal_new;
491 sub find_del_files ($) {
492         my $pages=shift;
493         my @del;
494         my @internal_del;
496         foreach my $page (keys %pagesources) {
497                 if (! $pages->{$page}) {
498                         if (isinternal($page)) {
499                                 push @internal_del, $pagesources{$page};
500                         }
501                         else {
502                                 push @del, $pagesources{$page};
503                         }
504                         $links{$page}=[];
505                         delete $typedlinks{$page};
506                         $renderedfiles{$page}=[];
507                         $pagemtime{$page}=0;
508                 }
509         }
511         return \@del, \@internal_del;
514 sub remove_del (@) {
515         foreach my $file (@_) {
516                 my $page=pagename($file);
517                 if (! isinternal($page)) {
518                         debug(sprintf(gettext("removing obsolete %s"), $page));
519                 }
520         
521                 foreach my $old (@{$oldrenderedfiles{$page}}) {
522                         prune($config{destdir}."/".$old, $config{destdir});
523                 }
525                 foreach my $source (keys %destsources) {
526                         if ($destsources{$source} eq $page) {
527                                 delete $destsources{$source};
528                         }
529                 }
530         
531                 delete $pagecase{lc $page};
532                 $delpagesources{$page}=$pagesources{$page};
533                 delete $pagesources{$page};
534         }
537 sub find_changed ($) {
538         my $files=shift;
539         my @changed;
540         my @internal_changed;
541         foreach my $file (@$files) {
542                 my $page=pagename($file);
543                 my ($srcfile, @stat)=srcfile_stat($file, 1);
544                 if (defined $srcfile && 
545                     (! exists $pagemtime{$page} ||
546                      $stat[9] > $pagemtime{$page} ||
547                      $forcerebuild{$page})) {
548                         $pagemtime{$page}=$stat[9];
550                         if (isinternal($page)) {
551                                 my $content = readfile($srcfile);
553                                 # Preprocess internal page in scan-only mode.
554                                 preprocess($page, $page, $content, 1);
556                                 run_hooks(scan => sub {
557                                         shift->(
558                                                 page => $page,
559                                                 content => $content,
560                                         );
561                                 });
563                                 push @internal_changed, $file;
564                         }
565                         else {
566                                 push @changed, $file;
567                         }
568                 }
569         }
570         return \@changed, \@internal_changed;
573 sub calculate_old_links ($$) {
574         my ($changed, $del)=@_;
575         my %oldlink_targets;
576         foreach my $file (@$changed, @$del) {
577                 my $page=pagename($file);
578                 if (exists $oldlinks{$page}) {
579                         foreach my $l (@{$oldlinks{$page}}) {
580                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
581                         }
582                 }
583         }
584         return \%oldlink_targets;
587 sub derender_internal ($) {
588         my $file=shift;
589         my $page=pagename($file);
590         delete $depends{$page};
591         delete $depends_simple{$page};
592         foreach my $old (@{$renderedfiles{$page}}) {
593                 delete $destsources{$old};
594         }
595         $renderedfiles{$page}=[];
598 sub render_linkers ($) {
599         my $f=shift;
600         my $p=pagename($f);
601         foreach my $page (keys %{$backlinks{$p}}) {
602                 my $file=$pagesources{$page};
603                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
604         }
607 sub remove_unrendered () {
608         foreach my $src (keys %rendered) {
609                 my $page=pagename($src);
610                 foreach my $file (@{$oldrenderedfiles{$page}}) {
611                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
612                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
613                                 prune($config{destdir}."/".$file, $config{destdir});
614                         }
615                 }
616         }
619 sub link_types_changed ($$) {
620         # each is of the form { type => { link => 1 } }
621         my $new = shift;
622         my $old = shift;
624         return 0 if !defined $new && !defined $old;
625         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
627         while (my ($type, $links) = each %$new) {
628                 foreach my $link (keys %$links) {
629                         return 1 unless exists $old->{$type}{$link};
630                 }
631         }
633         while (my ($type, $links) = each %$old) {
634                 foreach my $link (keys %$links) {
635                         return 1 unless exists $new->{$type}{$link};
636                 }
637         }
639         return 0;
642 sub calculate_changed_links ($$$) {
643         my ($changed, $del, $oldlink_targets)=@_;
645         my (%backlinkchanged, %linkchangers);
647         foreach my $file (@$changed, @$del) {
648                 my $page=pagename($file);
650                 if (exists $links{$page}) {
651                         foreach my $l (@{$links{$page}}) {
652                                 my $target=bestlink($page, $l);
653                                 if (! exists $oldlink_targets->{$page}{$l} ||
654                                     $target ne $oldlink_targets->{$page}{$l}) {
655                                         $backlinkchanged{$target}=1;
656                                         $linkchangers{lc($page)}=1;
657                                 }
658                                 delete $oldlink_targets->{$page}{$l};
659                         }
660                 }
661                 if (exists $oldlink_targets->{$page} &&
662                     %{$oldlink_targets->{$page}}) {
663                         foreach my $target (values %{$oldlink_targets->{$page}}) {
664                                 $backlinkchanged{$target}=1;
665                         }
666                         $linkchangers{lc($page)}=1;
667                 }
669                 # we currently assume that changing the type of a link doesn't
670                 # change backlinks
671                 if (!exists $linkchangers{lc($page)}) {
672                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
673                                 $linkchangers{lc($page)}=1;
674                         }
675                 }
676         }
678         return \%backlinkchanged, \%linkchangers;
681 sub render_dependent ($$$$$$$) {
682         my ($files, $new, $internal_new, $del, $internal_del,
683                 $internal_changed, $linkchangers)=@_;
685         my @changed=(keys %rendered, @$del);
686         my @exists_changed=(@$new, @$del);
687         
688         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
689         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
691         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
692                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
693                         foreach my $f (@$files) {
694                                 next if $rendered{$f};
695                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
696                         }
697                         return 0;
698                 }
699         }
700          
701         foreach my $f (@$files) {
702                 next if $rendered{$f};
703                 my $p=pagename($f);
704                 my $reason = undef;
706                 if (exists $depends_simple{$p} && ! defined $reason) {
707                         foreach my $d (keys %{$depends_simple{$p}}) {
708                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
709                                      $lc_changed{$d})
710                                     ||
711                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
712                                      $lc_exists_changed{$d})
713                                     ||
714                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
715                                      $linkchangers->{$d})
716                                 ) {
717                                         $reason = $d;
718                                         last;
719                                 }
720                         }
721                 }
722         
723                 if (exists $depends{$p} && ! defined $reason) {
724                         foreach my $dep (keys %{$depends{$p}}) {
725                                 my $sub=pagespec_translate($dep);
726                                 next unless defined $sub;
728                                 # only consider internal files
729                                 # if the page explicitly depends
730                                 # on such files
731                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
733                                 my $in=sub {
734                                         my $list=shift;
735                                         my $type=shift;
736                                         foreach my $file (@$list) {
737                                                 next if $file eq $f;
738                                                 my $page=pagename($file);
739                                                 if ($sub->($page, location => $p)) {
740                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
741                                                                 next unless $linkchangers->{lc($page)};
742                                                         }
743                                                         $reason=$page;
744                                                         return 1;
745                                                 }
746                                         }
747                                         return undef;
748                                 };
750                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
751                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
752                                         last if $internal_dep && (
753                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
754                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
755                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
756                                         );
757                                 }
758                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
759                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
760                                         last if $internal_dep && (
761                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
762                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
763                                         );
764                                 }
765                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
766                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
767                                         last if $internal_dep && (
768                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
769                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
770                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
771                                         );
772                                 }
773                         }
774                 }
775         
776                 if (defined $reason) {
777                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
778                         return 1;
779                 }
780         }
782         return 0;
785 sub render_backlinks ($) {
786         my $backlinkchanged=shift;
787         foreach my $link (keys %$backlinkchanged) {
788                 my $linkfile=$pagesources{$link};
789                 if (defined $linkfile) {
790                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
791                 }
792         }
795 sub gen_autofile ($$$) {
796         my $autofile=shift;
797         my $pages=shift;
798         my $del=shift;
800         if (file_pruned($autofile)) {
801                 return;
802         }
804         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
805         if (! defined $file) {
806                 return;
807         }
809         # Remember autofiles that were tried, and never try them again later.
810         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
811                 return;
812         }
813         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
815         if (srcfile($autofile, 1) || file_pruned($autofile)) {
816                 return;
817         }
818         
819         if (-l $file || -d _ || -e _) {
820                 return;
821         }
823         my $page = pagename($file);
824         if ($pages->{$page}) {
825                 return;
826         }
828         if (grep { $_ eq $autofile } @$del) {
829                 return;
830         }
832         $autofiles{$autofile}{generator}->();
833         $pages->{$page}=1;
834         return 1;
837 sub refresh () {
838         $phase = PHASE_SCAN;
840         srcdir_check();
841         run_hooks(refresh => sub { shift->() });
842         my ($files, $pages, $new, $internal_new, $del, $internal_del, $changed, $internal_changed);
843         my $want_find_changes=$config{only_committed_changes} &&
844                 exists $IkiWiki::hooks{rcs}{rcs_find_changes} &&
845                 exists $IkiWiki::hooks{rcs}{rcs_get_current_rev};
846         if (! $config{rebuild} && $want_find_changes && defined $IkiWiki::lastrev && length $IkiWiki::lastrev) {
847                 my ($changed_raw, $del_raw);
848                 ($changed_raw, $del_raw, $IkiWiki::lastrev) = $IkiWiki::hooks{rcs}{rcs_find_changes}{call}->($IkiWiki::lastrev);
849                 ($files, $pages)=process_changed_files($changed_raw, $del_raw);
850         }
851         else {
852                 ($files, $pages)=find_src_files();
853         }
854         if ($want_find_changes) {
855                 if (! defined($IkiWiki::lastrev) || ! length $IkiWiki::lastrev) {
856                         $IkiWiki::lastrev=$IkiWiki::hooks{rcs}{rcs_get_current_rev}{call}->();
857                 }
858         }
859         ($new, $internal_new)=find_new_files($files);
860         ($del, $internal_del)=find_del_files($pages);
861         ($changed, $internal_changed)=find_changed($files);
862         my %existingfiles;
863         run_hooks(needsbuild => sub {
864                 my $ret=shift->($changed, [@$del, @$internal_del]);
865                 if (ref $ret eq 'ARRAY' && $ret != $changed) {
866                         if (! %existingfiles) {
867                                 foreach my $f (@$files) {
868                                         $existingfiles{$f}=1;
869                                 }
870                         }
871                         @$changed=grep $existingfiles{$_}, @$ret;
872                 }
873         });
874         my $oldlink_targets=calculate_old_links($changed, $del);
876         foreach my $file (@$changed) {
877                 scan($file);
878         }
880         foreach my $autofile (keys %autofiles) {
881                 if (gen_autofile($autofile, $pages, $del)) {
882                         push @{$files}, $autofile;
883                         push @{$new}, $autofile if find_new_files([$autofile]);
884                         push @{$changed}, $autofile if find_changed([$autofile]);
885                         
886                         scan($autofile);
887                 }
888         }
890         calculate_links();
892         # At this point it becomes OK to start matching pagespecs.
893         $phase = PHASE_RENDER;
894         # Save some memory in full rebuilds: we no longer need to keep
895         # track of which pages we've scanned, because we can assume the
896         # answer is "all of them".
897         %scanned = () if $config{rebuild};
899         remove_del(@$del, @$internal_del);
901         foreach my $file (@$changed) {
902                 render($file, sprintf(gettext("building %s"), $file));
903         }
904         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
905                 derender_internal($file);
906         }
908         run_hooks(build_affected => sub {
909                 my %affected = shift->();
910                 while (my ($page, $message) = each %affected) {
911                         next unless exists $pagesources{$page};
912                         render($pagesources{$page}, $message);
913                 }
914         });
916         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
917                 $del, $oldlink_targets);
919         foreach my $file (@$new, @$del) {
920                 render_linkers($file);
921         }
923         if (@$changed || @$internal_changed ||
924             @$del || @$internal_del || @$internal_new) {
925                 1 while render_dependent($files, $new, $internal_new,
926                         $del, $internal_del, $internal_changed,
927                         $linkchangers);
928         }
930         render_backlinks($backlinkchanged);
931         remove_unrendered();
933         if (@$del || @$internal_del) {
934                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
935         }
936         if (%rendered) {
937                 run_hooks(rendered => sub { shift->(keys %rendered) });
938                 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
939         }
940         my %all_changed = map { $_ => 1 }
941                 @$new, @$changed, @$del,
942                 @$internal_new, @$internal_changed, @$internal_del;
943         run_hooks(changes => sub { shift->(keys %all_changed) });
946 sub clean_rendered {
947         lockwiki();
948         loadindex();
949         remove_unrendered();
950         foreach my $page (keys %oldrenderedfiles) {
951                 foreach my $file (@{$oldrenderedfiles{$page}}) {
952                         prune($config{destdir}."/".$file, $config{destdir});
953                 }
954         }
957 sub commandline_render () {
958         lockwiki();
959         loadindex();
960         unlockwiki();
962         # This function behaves as though it's in the render phase;
963         # all other files are assumed to have been scanned last time.
964         $phase = PHASE_RENDER;
966         my $srcfile=possibly_foolish_untaint($config{render});
967         my $file=$srcfile;
968         $file=~s/\Q$config{srcdir}\E\/?//;
970         my $type=pagetype($file);
971         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
972         my $content=readfile($srcfile);
973         my $page=pagename($file);
974         $pagesources{$page}=$file;
975         $content=filter($page, $page, $content);
976         $content=preprocess($page, $page, $content);
977         $content=linkify($page, $page, $content);
978         $content=htmlize($page, $page, $type, $content);
979         $pagemtime{$page}=(stat($srcfile))[9];
980         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
982         print genpage($page, $content);
983         exit 0;