]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
typos
[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} } 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         );
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         });
152         return $content;
155 sub scan ($) {
156         my $file=shift;
157         return if $phase > PHASE_SCAN || $scanned{$file};
158         $scanned{$file}=1;
160         debug(sprintf(gettext("scanning %s"), $file));
162         my $type=pagetype($file);
163         if (defined $type) {
164                 my $srcfile=srcfile($file);
165                 my $content=readfile($srcfile);
166                 my $page=pagename($file);
167                 will_render($page, htmlpage($page), 1);
169                 if ($config{discussion}) {
170                         # Discussion links are a special case since they're
171                         # not in the text of the page, but on its template.
172                         $links{$page}=[ $page."/".lc($config{discussionpage}) ];
173                 }
174                 else {
175                         $links{$page}=[];
176                 }
177                 delete $typedlinks{$page};
179                 # Preprocess in scan-only mode.
180                 preprocess($page, $page, $content, 1);
182                 run_hooks(scan => sub {
183                         shift->(
184                                 page => $page,
185                                 content => $content,
186                         );
187                 });
188         }
189         else {
190                 will_render($file, $file, 1);
191         }
194 sub fast_file_copy (@) {
195         my $srcfile=shift;
196         my $destfile=shift;
197         my $srcfd=shift;
198         my $destfd=shift;
199         my $cleanup=shift;
201         my $blksize = 16384;
202         my ($len, $buf, $written);
203         while ($len = sysread $srcfd, $buf, $blksize) {
204                 if (! defined $len) {
205                         next if $! =~ /^Interrupted/;
206                         error("failed to read $srcfile: $!", $cleanup);
207                 }
208                 my $offset = 0;
209                 while ($len) {
210                         defined($written = syswrite $destfd, $buf, $len, $offset)
211                                 or error("failed to write $destfile: $!", $cleanup);
212                         $len -= $written;
213                         $offset += $written;
214                 }
215         }
218 sub render ($$) {
219         my $file=shift;
220         return if $rendered{$file};
221         debug(shift);
222         $rendered{$file}=1;
223         
224         my $type=pagetype($file);
225         my $srcfile=srcfile($file);
226         if (defined $type) {
227                 my $page=pagename($file);
228                 delete $depends{$page};
229                 delete $depends_simple{$page};
230                 will_render($page, htmlpage($page), 1);
231                 return if $type=~/^_/;
232                 
233                 my $content=htmlize($page, $page, $type,
234                         linkify($page, $page,
235                         preprocess($page, $page,
236                         filter($page, $page,
237                         readfile($srcfile)))));
238                 
239                 my $output=htmlpage($page);
240                 writefile($output, $config{destdir}, genpage($page, $content));
241         }
242         else {
243                 delete $depends{$file};
244                 delete $depends_simple{$file};
245                 will_render($file, $file, 1);
246                 
247                 if ($config{hardlink}) {
248                         # only hardlink if owned by same user
249                         my @stat=stat($srcfile);
250                         if ($stat[4] == $>) {
251                                 prep_writefile($file, $config{destdir});
252                                 unlink($config{destdir}."/".$file);
253                                 if (link($srcfile, $config{destdir}."/".$file)) {
254                                         return;
255                                 }
256                         }
257                         # if hardlink fails, fall back to copying
258                 }
259                 
260                 my $srcfd=readfile($srcfile, 1, 1);
261                 writefile($file, $config{destdir}, undef, 1, sub {
262                         fast_file_copy($srcfile, $file, $srcfd, @_);
263                 });
264         }
267 sub prune ($;$) {
268         my $file=shift;
269         my $up_to=shift;
271         unlink($file);
272         my $dir=dirname($file);
273         while ((! defined $up_to || $dir =~ m{^\Q$up_to\E\/}) && rmdir($dir)) {
274                 $dir=dirname($dir);
275         }
278 sub srcdir_check () {
279         # security check, avoid following symlinks in the srcdir path by default
280         my $test=$config{srcdir};
281         while (length $test) {
282                 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
283                         error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
284                 }
285                 unless ($test=~s/\/+$//) {
286                         $test=dirname($test);
287                 }
288         }
289         
292 # Finds all files in the srcdir, and the underlaydirs.
293 # Returns the files, and their corresponding pages.
295 # When run in only_underlay mode, adds only the underlay files to
296 # the files and pages passed in.
297 sub find_src_files (;$$$) {
298         my $only_underlay=shift;
299         my @files;
300         if (defined $_[0]) {
301                 @files=@{shift()};
302         }
303         my %pages;
304         if (defined $_[0]) {
305                 %pages=%{shift()};
306         }
308         eval q{use File::Find};
309         error($@) if $@;
311         eval q{use Cwd};
312         die $@ if $@;
313         my $origdir=getcwd();
314         my $abssrcdir=Cwd::abs_path($config{srcdir});
315         
316         @IkiWiki::underlayfiles=();
318         my ($page, $underlay);
319         my $helper=sub {
320                 my $file=decode_utf8($_);
322                 return if -l $file || -d _;
323                 $file=~s/^\.\///;
324                 return if ! length $file;
325                 $page = pagename($file);
326                 if (! exists $pagesources{$page} &&
327                     file_pruned($file)) {
328                         $File::Find::prune=1;
329                         return;
330                 }
332                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
333                 if (! defined $f) {
334                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
335                         return;
336                 }
337         
338                 if ($underlay) {
339                         # avoid underlaydir override attacks; see security.mdwn
340                         if (! -l "$abssrcdir/$f" && ! -e _) {
341                                 if (! $pages{$page}) {
342                                         push @files, $f;
343                                         push @IkiWiki::underlayfiles, $f;
344                                         $pages{$page}=1;
345                                 }
346                         }
347                 }
348                 else {
349                         push @files, $f;
350                         if ($pages{$page}) {
351                                 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
352                         }
353                         $pages{$page}=1;
354                 }
355         };
357         unless ($only_underlay) {
358                 chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
359                 find({
360                         no_chdir => 1,
361                         wanted => $helper,
362                 }, '.');
363                 chdir($origdir) || die "chdir $origdir: $!";
364         }
366         $underlay=1;
367         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
368                 if (chdir($_)) {
369                         find({
370                                 no_chdir => 1,
371                                 wanted => $helper,
372                         }, '.');
373                         chdir($origdir) || die "chdir: $!";
374                 }
375         };
377         return \@files, \%pages;
380 # Given a hash of files that have changed, and a hash of files that were
381 # deleted, should return the same results as find_src_files, with the same
382 # sanity checks. But a lot faster!
383 sub process_changed_files ($$) {
384         my $changed_raw=shift;
385         my $deleted_raw=shift;
387         my @files;
388         my %pages;
390         foreach my $file (keys %$changed_raw) {
391                 my $page = pagename($file);
392                 next if ! exists $pagesources{$page} && file_pruned($file);
393                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
394                 if (! defined $f) {
395                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
396                         next;
397                 }
398                 push @files, $f;
399                 if ($pages{$page}) {
400                         debug(sprintf(gettext("%s has multiple possible source pages"), $page));
401                 }
402                 $pages{$page}=1;
403         }
405         # So far, we only have the changed files. Now add in all the old
406         # files that were not changed or deleted, excluding ones that came
407         # from the underlay.
408         my %old_underlay;
409         foreach my $f (@IkiWiki::underlayfiles) {
410                 $old_underlay{$f}=1;
411         }
412         foreach my $page (keys %pagesources) {
413                 my $f=$pagesources{$page};
414                 unless ($old_underlay{$f} || exists $pages{$page} || exists $deleted_raw->{$f}) {
415                         $pages{$page}=1;
416                         push @files, $f;
417                 }
418         }
420         # add in the underlay
421         find_src_files(1, \@files, \%pages);
424 sub find_new_files ($) {
425         my $files=shift;
426         my @new;
427         my @internal_new;
429         my $times_noted;
431         foreach my $file (@$files) {
432                 my $page=pagename($file);
434                 if ($config{rcs} && $config{gettime} &&
435                     -e "$config{srcdir}/$file") {
436                         if (! $times_noted) {
437                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
438                                 $times_noted=1;
439                         }
441                         eval {
442                                 my $ctime=rcs_getctime($file);
443                                 if ($ctime > 0) {
444                                         $pagectime{$page}=$ctime;
445                                 }
446                         };
447                         if ($@) {
448                                 print STDERR $@;
449                         }
450                         my $mtime;
451                         eval {
452                                 $mtime=rcs_getmtime($file);
453                         };
454                         if ($@) {
455                                 print STDERR $@;
456                         }
457                         elsif ($mtime > 0) {
458                                 utime($mtime, $mtime, "$config{srcdir}/$file");
459                         }
460                 }
462                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
463                         # the page has changed its type
464                         $forcerebuild{$page}=1;
465                 }
466                 $pagesources{$page}=$file;
467                 if (! $pagemtime{$page}) {
468                         if (isinternal($page)) {
469                                 push @internal_new, $file;
470                         }
471                         else {
472                                 push @new, $file;
473                         }
474                         $pagecase{lc $page}=$page;
475                         if (! exists $pagectime{$page}) {
476                                 $pagectime{$page}=(srcfile_stat($file))[10];
477                         }
478                 }
479         }
481         return \@new, \@internal_new;
484 sub find_del_files ($) {
485         my $pages=shift;
486         my @del;
487         my @internal_del;
489         foreach my $page (keys %pagesources) {
490                 if (! $pages->{$page}) {
491                         if (isinternal($page)) {
492                                 push @internal_del, $pagesources{$page};
493                         }
494                         else {
495                                 push @del, $pagesources{$page};
496                         }
497                         $links{$page}=[];
498                         delete $typedlinks{$page};
499                         $renderedfiles{$page}=[];
500                         $pagemtime{$page}=0;
501                 }
502         }
504         return \@del, \@internal_del;
507 sub remove_del (@) {
508         foreach my $file (@_) {
509                 my $page=pagename($file);
510                 if (! isinternal($page)) {
511                         debug(sprintf(gettext("removing obsolete %s"), $page));
512                 }
513         
514                 foreach my $old (@{$oldrenderedfiles{$page}}) {
515                         prune($config{destdir}."/".$old, $config{destdir});
516                 }
518                 foreach my $source (keys %destsources) {
519                         if ($destsources{$source} eq $page) {
520                                 delete $destsources{$source};
521                         }
522                 }
523         
524                 delete $pagecase{lc $page};
525                 $delpagesources{$page}=$pagesources{$page};
526                 delete $pagesources{$page};
527         }
530 sub find_changed ($) {
531         my $files=shift;
532         my @changed;
533         my @internal_changed;
534         foreach my $file (@$files) {
535                 my $page=pagename($file);
536                 my ($srcfile, @stat)=srcfile_stat($file);
537                 if (! exists $pagemtime{$page} ||
538                     $stat[9] > $pagemtime{$page} ||
539                     $forcerebuild{$page}) {
540                         $pagemtime{$page}=$stat[9];
542                         if (isinternal($page)) {
543                                 my $content = readfile($srcfile);
545                                 # Preprocess internal page in scan-only mode.
546                                 preprocess($page, $page, $content, 1);
548                                 run_hooks(scan => sub {
549                                         shift->(
550                                                 page => $page,
551                                                 content => $content,
552                                         );
553                                 });
555                                 push @internal_changed, $file;
556                         }
557                         else {
558                                 push @changed, $file;
559                         }
560                 }
561         }
562         return \@changed, \@internal_changed;
565 sub calculate_old_links ($$) {
566         my ($changed, $del)=@_;
567         my %oldlink_targets;
568         foreach my $file (@$changed, @$del) {
569                 my $page=pagename($file);
570                 if (exists $oldlinks{$page}) {
571                         foreach my $l (@{$oldlinks{$page}}) {
572                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
573                         }
574                 }
575         }
576         return \%oldlink_targets;
579 sub derender_internal ($) {
580         my $file=shift;
581         my $page=pagename($file);
582         delete $depends{$page};
583         delete $depends_simple{$page};
584         foreach my $old (@{$renderedfiles{$page}}) {
585                 delete $destsources{$old};
586         }
587         $renderedfiles{$page}=[];
590 sub render_linkers ($) {
591         my $f=shift;
592         my $p=pagename($f);
593         foreach my $page (keys %{$backlinks{$p}}) {
594                 my $file=$pagesources{$page};
595                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
596         }
599 sub remove_unrendered () {
600         foreach my $src (keys %rendered) {
601                 my $page=pagename($src);
602                 foreach my $file (@{$oldrenderedfiles{$page}}) {
603                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
604                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
605                                 prune($config{destdir}."/".$file, $config{destdir});
606                         }
607                 }
608         }
611 sub link_types_changed ($$) {
612         # each is of the form { type => { link => 1 } }
613         my $new = shift;
614         my $old = shift;
616         return 0 if !defined $new && !defined $old;
617         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
619         while (my ($type, $links) = each %$new) {
620                 foreach my $link (keys %$links) {
621                         return 1 unless exists $old->{$type}{$link};
622                 }
623         }
625         while (my ($type, $links) = each %$old) {
626                 foreach my $link (keys %$links) {
627                         return 1 unless exists $new->{$type}{$link};
628                 }
629         }
631         return 0;
634 sub calculate_changed_links ($$$) {
635         my ($changed, $del, $oldlink_targets)=@_;
637         my (%backlinkchanged, %linkchangers);
639         foreach my $file (@$changed, @$del) {
640                 my $page=pagename($file);
642                 if (exists $links{$page}) {
643                         foreach my $l (@{$links{$page}}) {
644                                 my $target=bestlink($page, $l);
645                                 if (! exists $oldlink_targets->{$page}{$l} ||
646                                     $target ne $oldlink_targets->{$page}{$l}) {
647                                         $backlinkchanged{$target}=1;
648                                         $linkchangers{lc($page)}=1;
649                                 }
650                                 delete $oldlink_targets->{$page}{$l};
651                         }
652                 }
653                 if (exists $oldlink_targets->{$page} &&
654                     %{$oldlink_targets->{$page}}) {
655                         foreach my $target (values %{$oldlink_targets->{$page}}) {
656                                 $backlinkchanged{$target}=1;
657                         }
658                         $linkchangers{lc($page)}=1;
659                 }
661                 # we currently assume that changing the type of a link doesn't
662                 # change backlinks
663                 if (!exists $linkchangers{lc($page)}) {
664                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
665                                 $linkchangers{lc($page)}=1;
666                         }
667                 }
668         }
670         return \%backlinkchanged, \%linkchangers;
673 sub render_dependent ($$$$$$$) {
674         my ($files, $new, $internal_new, $del, $internal_del,
675                 $internal_changed, $linkchangers)=@_;
677         my @changed=(keys %rendered, @$del);
678         my @exists_changed=(@$new, @$del);
679         
680         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
681         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
683         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
684                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
685                         foreach my $f (@$files) {
686                                 next if $rendered{$f};
687                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
688                         }
689                         return 0;
690                 }
691         }
692          
693         foreach my $f (@$files) {
694                 next if $rendered{$f};
695                 my $p=pagename($f);
696                 my $reason = undef;
698                 if (exists $depends_simple{$p} && ! defined $reason) {
699                         foreach my $d (keys %{$depends_simple{$p}}) {
700                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
701                                      $lc_changed{$d})
702                                     ||
703                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
704                                      $lc_exists_changed{$d})
705                                     ||
706                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
707                                      $linkchangers->{$d})
708                                 ) {
709                                         $reason = $d;
710                                         last;
711                                 }
712                         }
713                 }
714         
715                 if (exists $depends{$p} && ! defined $reason) {
716                         foreach my $dep (keys %{$depends{$p}}) {
717                                 my $sub=pagespec_translate($dep);
718                                 next unless defined $sub;
720                                 # only consider internal files
721                                 # if the page explicitly depends
722                                 # on such files
723                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
725                                 my $in=sub {
726                                         my $list=shift;
727                                         my $type=shift;
728                                         foreach my $file (@$list) {
729                                                 next if $file eq $f;
730                                                 my $page=pagename($file);
731                                                 if ($sub->($page, location => $p)) {
732                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
733                                                                 next unless $linkchangers->{lc($page)};
734                                                         }
735                                                         $reason=$page;
736                                                         return 1;
737                                                 }
738                                         }
739                                         return undef;
740                                 };
742                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
743                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
744                                         last if $internal_dep && (
745                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
746                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
747                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
748                                         );
749                                 }
750                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
751                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
752                                         last if $internal_dep && (
753                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
754                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
755                                         );
756                                 }
757                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
758                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
759                                         last if $internal_dep && (
760                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
761                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
762                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
763                                         );
764                                 }
765                         }
766                 }
767         
768                 if (defined $reason) {
769                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
770                         return 1;
771                 }
772         }
774         return 0;
777 sub render_backlinks ($) {
778         my $backlinkchanged=shift;
779         foreach my $link (keys %$backlinkchanged) {
780                 my $linkfile=$pagesources{$link};
781                 if (defined $linkfile) {
782                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
783                 }
784         }
787 sub gen_autofile ($$$) {
788         my $autofile=shift;
789         my $pages=shift;
790         my $del=shift;
792         if (file_pruned($autofile)) {
793                 return;
794         }
796         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
797         if (! defined $file) {
798                 return;
799         }
801         # Remember autofiles that were tried, and never try them again later.
802         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
803                 return;
804         }
805         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
807         if (srcfile($autofile, 1) || file_pruned($autofile)) {
808                 return;
809         }
810         
811         if (-l $file || -d _ || -e _) {
812                 return;
813         }
815         my $page = pagename($file);
816         if ($pages->{$page}) {
817                 return;
818         }
820         if (grep { $_ eq $autofile } @$del) {
821                 return;
822         }
824         $autofiles{$autofile}{generator}->();
825         $pages->{$page}=1;
826         return 1;
829 sub refresh () {
830         $phase = PHASE_SCAN;
832         srcdir_check();
833         run_hooks(refresh => sub { shift->() });
834         my ($files, $pages, $new, $internal_new, $del, $internal_del, $changed, $internal_changed);
835         my $want_find_changes=$config{only_committed_changes} &&
836                 exists $IkiWiki::hooks{rcs}{rcs_find_changes} &&
837                 exists $IkiWiki::hooks{rcs}{rcs_get_current_rev};
838         if (! $config{rebuild} && $want_find_changes && defined $IkiWiki::lastrev && length $IkiWiki::lastrev) {
839                 my ($changed_raw, $del_raw);
840                 ($changed_raw, $del_raw, $IkiWiki::lastrev) = $IkiWiki::hooks{rcs}{rcs_find_changes}{call}->($IkiWiki::lastrev);
841                 ($files, $pages)=process_changed_files($changed_raw, $del_raw);
842         }
843         else {
844                 ($files, $pages)=find_src_files();
845         }
846         if ($want_find_changes) {
847                 if (! defined($IkiWiki::lastrev) || ! length $IkiWiki::lastrev) {
848                         $IkiWiki::lastrev=$IkiWiki::hooks{rcs}{rcs_get_current_rev}{call}->();
849                 }
850         }
851         ($new, $internal_new)=find_new_files($files);
852         ($del, $internal_del)=find_del_files($pages);
853         ($changed, $internal_changed)=find_changed($files);
854         my %existingfiles;
855         run_hooks(needsbuild => sub {
856                 my $ret=shift->($changed, [@$del, @$internal_del]);
857                 if (ref $ret eq 'ARRAY' && $ret != $changed) {
858                         if (! %existingfiles) {
859                                 foreach my $f (@$files) {
860                                         $existingfiles{$f}=1;
861                                 }
862                         }
863                         @$changed=grep $existingfiles{$_}, @$ret;
864                 }
865         });
866         my $oldlink_targets=calculate_old_links($changed, $del);
868         foreach my $file (@$changed) {
869                 scan($file);
870         }
872         foreach my $autofile (keys %autofiles) {
873                 if (gen_autofile($autofile, $pages, $del)) {
874                         push @{$files}, $autofile;
875                         push @{$new}, $autofile if find_new_files([$autofile]);
876                         push @{$changed}, $autofile if find_changed([$autofile]);
877                         
878                         scan($autofile);
879                 }
880         }
882         calculate_links();
884         # At this point it becomes OK to start matching pagespecs.
885         $phase = PHASE_RENDER;
886         # Save some memory: we no longer need to keep track of which pages
887         # we've scanned
888         %scanned = ();
890         remove_del(@$del, @$internal_del);
892         foreach my $file (@$changed) {
893                 render($file, sprintf(gettext("building %s"), $file));
894         }
895         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
896                 derender_internal($file);
897         }
899         run_hooks(build_affected => sub {
900                 my %affected = shift->();
901                 while (my ($page, $message) = each %affected) {
902                         next unless exists $pagesources{$page};
903                         render($pagesources{$page}, $message);
904                 }
905         });
907         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
908                 $del, $oldlink_targets);
910         foreach my $file (@$new, @$del) {
911                 render_linkers($file);
912         }
914         if (@$changed || @$internal_changed ||
915             @$del || @$internal_del || @$internal_new) {
916                 1 while render_dependent($files, $new, $internal_new,
917                         $del, $internal_del, $internal_changed,
918                         $linkchangers);
919         }
921         render_backlinks($backlinkchanged);
922         remove_unrendered();
924         if (@$del || @$internal_del) {
925                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
926         }
927         if (%rendered) {
928                 run_hooks(rendered => sub { shift->(keys %rendered) });
929                 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
930         }
931         my %all_changed = map { $_ => 1 }
932                 @$new, @$changed, @$del,
933                 @$internal_new, @$internal_changed, @$internal_del;
934         run_hooks(changes => sub { shift->(keys %all_changed) });
937 sub clean_rendered {
938         lockwiki();
939         loadindex();
940         remove_unrendered();
941         foreach my $page (keys %oldrenderedfiles) {
942                 foreach my $file (@{$oldrenderedfiles{$page}}) {
943                         prune($config{destdir}."/".$file, $config{destdir});
944                 }
945         }
948 sub commandline_render () {
949         lockwiki();
950         loadindex();
951         unlockwiki();
953         # This function behaves as though it's in the render phase;
954         # all other files are assumed to have been scanned last time.
955         $phase = PHASE_RENDER;
957         my $srcfile=possibly_foolish_untaint($config{render});
958         my $file=$srcfile;
959         $file=~s/\Q$config{srcdir}\E\/?//;
961         my $type=pagetype($file);
962         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
963         my $content=readfile($srcfile);
964         my $page=pagename($file);
965         $pagesources{$page}=$file;
966         $content=filter($page, $page, $content);
967         $content=preprocess($page, $page, $content);
968         $content=linkify($page, $page, $content);
969         $content=htmlize($page, $page, $type, $content);
970         $pagemtime{$page}=(stat($srcfile))[9];
971         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
973         print genpage($page, $content);
974         exit 0;