]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Merge branch 'master' of ssh://git.ikiwiki.info
[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                 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 $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                         $File::Find::prune=1;
330                         return;
331                 }
333                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
334                 if (! defined $f) {
335                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
336                         return;
337                 }
338         
339                 if ($underlay) {
340                         # avoid underlaydir override attacks; see security.mdwn
341                         if (! -l "$abssrcdir/$f" && ! -e _) {
342                                 if (! $pages{$page}) {
343                                         push @files, $f;
344                                         push @IkiWiki::underlayfiles, $f;
345                                         $pages{$page}=1;
346                                 }
347                         }
348                 }
349                 else {
350                         push @files, $f;
351                         if ($pages{$page}) {
352                                 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
353                         }
354                         $pages{$page}=1;
355                 }
356         };
358         unless ($only_underlay) {
359                 chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
360                 find({
361                         no_chdir => 1,
362                         wanted => $helper,
363                 }, '.');
364                 chdir($origdir) || die "chdir $origdir: $!";
365         }
367         $underlay=1;
368         foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
369                 if (chdir($_)) {
370                         find({
371                                 no_chdir => 1,
372                                 wanted => $helper,
373                         }, '.');
374                         chdir($origdir) || die "chdir: $!";
375                 }
376         };
378         return \@files, \%pages;
381 # Given a hash of files that have changed, and a hash of files that were
382 # deleted, should return the same results as find_src_files, with the same
383 # sanity checks. But a lot faster!
384 sub process_changed_files ($$) {
385         my $changed_raw=shift;
386         my $deleted_raw=shift;
388         my @files;
389         my %pages;
391         foreach my $file (keys %$changed_raw) {
392                 my $page = pagename($file);
393                 next if ! exists $pagesources{$page} && file_pruned($file);
394                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
395                 if (! defined $f) {
396                         warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
397                         next;
398                 }
399                 push @files, $f;
400                 if ($pages{$page}) {
401                         debug(sprintf(gettext("%s has multiple possible source pages"), $page));
402                 }
403                 $pages{$page}=1;
404         }
406         # So far, we only have the changed files. Now add in all the old
407         # files that were not changed or deleted, excluding ones that came
408         # from the underlay.
409         my %old_underlay;
410         foreach my $f (@IkiWiki::underlayfiles) {
411                 $old_underlay{$f}=1;
412         }
413         foreach my $page (keys %pagesources) {
414                 my $f=$pagesources{$page};
415                 unless ($old_underlay{$f} || exists $pages{$page} || exists $deleted_raw->{$f}) {
416                         $pages{$page}=1;
417                         push @files, $f;
418                 }
419         }
421         # add in the underlay
422         find_src_files(1, \@files, \%pages);
425 sub find_new_files ($) {
426         my $files=shift;
427         my @new;
428         my @internal_new;
430         my $times_noted;
432         foreach my $file (@$files) {
433                 my $page=pagename($file);
435                 if ($config{rcs} && $config{gettime} &&
436                     -e "$config{srcdir}/$file") {
437                         if (! $times_noted) {
438                                 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
439                                 $times_noted=1;
440                         }
442                         eval {
443                                 my $ctime=rcs_getctime($file);
444                                 if ($ctime > 0) {
445                                         $pagectime{$page}=$ctime;
446                                 }
447                         };
448                         if ($@) {
449                                 print STDERR $@;
450                         }
451                         my $mtime;
452                         eval {
453                                 $mtime=rcs_getmtime($file);
454                         };
455                         if ($@) {
456                                 print STDERR $@;
457                         }
458                         elsif ($mtime > 0) {
459                                 utime($mtime, $mtime, "$config{srcdir}/$file");
460                         }
461                 }
463                 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
464                         # the page has changed its type
465                         $forcerebuild{$page}=1;
466                 }
467                 $pagesources{$page}=$file;
468                 if (! $pagemtime{$page}) {
469                         if (isinternal($page)) {
470                                 push @internal_new, $file;
471                         }
472                         else {
473                                 push @new, $file;
474                         }
475                         $pagecase{lc $page}=$page;
476                         if (! exists $pagectime{$page}) {
477                                 my $ctime=(srcfile_stat($file, 1))[10];
478                                 $pagectime{$page}=$ctime if defined $ctime;
479                         }
480                 }
481         }
483         return \@new, \@internal_new;
486 sub find_del_files ($) {
487         my $pages=shift;
488         my @del;
489         my @internal_del;
491         foreach my $page (keys %pagesources) {
492                 if (! $pages->{$page}) {
493                         if (isinternal($page)) {
494                                 push @internal_del, $pagesources{$page};
495                         }
496                         else {
497                                 push @del, $pagesources{$page};
498                         }
499                         $links{$page}=[];
500                         delete $typedlinks{$page};
501                         $renderedfiles{$page}=[];
502                         $pagemtime{$page}=0;
503                 }
504         }
506         return \@del, \@internal_del;
509 sub remove_del (@) {
510         foreach my $file (@_) {
511                 my $page=pagename($file);
512                 if (! isinternal($page)) {
513                         debug(sprintf(gettext("removing obsolete %s"), $page));
514                 }
515         
516                 foreach my $old (@{$oldrenderedfiles{$page}}) {
517                         prune($config{destdir}."/".$old, $config{destdir});
518                 }
520                 foreach my $source (keys %destsources) {
521                         if ($destsources{$source} eq $page) {
522                                 delete $destsources{$source};
523                         }
524                 }
525         
526                 delete $pagecase{lc $page};
527                 $delpagesources{$page}=$pagesources{$page};
528                 delete $pagesources{$page};
529         }
532 sub find_changed ($) {
533         my $files=shift;
534         my @changed;
535         my @internal_changed;
536         foreach my $file (@$files) {
537                 my $page=pagename($file);
538                 my ($srcfile, @stat)=srcfile_stat($file, 1);
539                 if (defined $srcfile && 
540                     (! exists $pagemtime{$page} ||
541                      $stat[9] > $pagemtime{$page} ||
542                      $forcerebuild{$page})) {
543                         $pagemtime{$page}=$stat[9];
545                         if (isinternal($page)) {
546                                 my $content = readfile($srcfile);
548                                 # Preprocess internal page in scan-only mode.
549                                 preprocess($page, $page, $content, 1);
551                                 run_hooks(scan => sub {
552                                         shift->(
553                                                 page => $page,
554                                                 content => $content,
555                                         );
556                                 });
558                                 push @internal_changed, $file;
559                         }
560                         else {
561                                 push @changed, $file;
562                         }
563                 }
564         }
565         return \@changed, \@internal_changed;
568 sub calculate_old_links ($$) {
569         my ($changed, $del)=@_;
570         my %oldlink_targets;
571         foreach my $file (@$changed, @$del) {
572                 my $page=pagename($file);
573                 if (exists $oldlinks{$page}) {
574                         foreach my $l (@{$oldlinks{$page}}) {
575                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
576                         }
577                 }
578         }
579         return \%oldlink_targets;
582 sub derender_internal ($) {
583         my $file=shift;
584         my $page=pagename($file);
585         delete $depends{$page};
586         delete $depends_simple{$page};
587         foreach my $old (@{$renderedfiles{$page}}) {
588                 delete $destsources{$old};
589         }
590         $renderedfiles{$page}=[];
593 sub render_linkers ($) {
594         my $f=shift;
595         my $p=pagename($f);
596         foreach my $page (keys %{$backlinks{$p}}) {
597                 my $file=$pagesources{$page};
598                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
599         }
602 sub remove_unrendered () {
603         foreach my $src (keys %rendered) {
604                 my $page=pagename($src);
605                 foreach my $file (@{$oldrenderedfiles{$page}}) {
606                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
607                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
608                                 prune($config{destdir}."/".$file, $config{destdir});
609                         }
610                 }
611         }
614 sub link_types_changed ($$) {
615         # each is of the form { type => { link => 1 } }
616         my $new = shift;
617         my $old = shift;
619         return 0 if !defined $new && !defined $old;
620         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
622         while (my ($type, $links) = each %$new) {
623                 foreach my $link (keys %$links) {
624                         return 1 unless exists $old->{$type}{$link};
625                 }
626         }
628         while (my ($type, $links) = each %$old) {
629                 foreach my $link (keys %$links) {
630                         return 1 unless exists $new->{$type}{$link};
631                 }
632         }
634         return 0;
637 sub calculate_changed_links ($$$) {
638         my ($changed, $del, $oldlink_targets)=@_;
640         my (%backlinkchanged, %linkchangers);
642         foreach my $file (@$changed, @$del) {
643                 my $page=pagename($file);
645                 if (exists $links{$page}) {
646                         foreach my $l (@{$links{$page}}) {
647                                 my $target=bestlink($page, $l);
648                                 if (! exists $oldlink_targets->{$page}{$l} ||
649                                     $target ne $oldlink_targets->{$page}{$l}) {
650                                         $backlinkchanged{$target}=1;
651                                         $linkchangers{lc($page)}=1;
652                                 }
653                                 delete $oldlink_targets->{$page}{$l};
654                         }
655                 }
656                 if (exists $oldlink_targets->{$page} &&
657                     %{$oldlink_targets->{$page}}) {
658                         foreach my $target (values %{$oldlink_targets->{$page}}) {
659                                 $backlinkchanged{$target}=1;
660                         }
661                         $linkchangers{lc($page)}=1;
662                 }
664                 # we currently assume that changing the type of a link doesn't
665                 # change backlinks
666                 if (!exists $linkchangers{lc($page)}) {
667                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
668                                 $linkchangers{lc($page)}=1;
669                         }
670                 }
671         }
673         return \%backlinkchanged, \%linkchangers;
676 sub render_dependent ($$$$$$$) {
677         my ($files, $new, $internal_new, $del, $internal_del,
678                 $internal_changed, $linkchangers)=@_;
680         my @changed=(keys %rendered, @$del);
681         my @exists_changed=(@$new, @$del);
682         
683         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
684         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
686         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
687                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
688                         foreach my $f (@$files) {
689                                 next if $rendered{$f};
690                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
691                         }
692                         return 0;
693                 }
694         }
695          
696         foreach my $f (@$files) {
697                 next if $rendered{$f};
698                 my $p=pagename($f);
699                 my $reason = undef;
701                 if (exists $depends_simple{$p} && ! defined $reason) {
702                         foreach my $d (keys %{$depends_simple{$p}}) {
703                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
704                                      $lc_changed{$d})
705                                     ||
706                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
707                                      $lc_exists_changed{$d})
708                                     ||
709                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
710                                      $linkchangers->{$d})
711                                 ) {
712                                         $reason = $d;
713                                         last;
714                                 }
715                         }
716                 }
717         
718                 if (exists $depends{$p} && ! defined $reason) {
719                         foreach my $dep (keys %{$depends{$p}}) {
720                                 my $sub=pagespec_translate($dep);
721                                 next unless defined $sub;
723                                 # only consider internal files
724                                 # if the page explicitly depends
725                                 # on such files
726                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
728                                 my $in=sub {
729                                         my $list=shift;
730                                         my $type=shift;
731                                         foreach my $file (@$list) {
732                                                 next if $file eq $f;
733                                                 my $page=pagename($file);
734                                                 if ($sub->($page, location => $p)) {
735                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
736                                                                 next unless $linkchangers->{lc($page)};
737                                                         }
738                                                         $reason=$page;
739                                                         return 1;
740                                                 }
741                                         }
742                                         return undef;
743                                 };
745                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
746                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
747                                         last if $internal_dep && (
748                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
749                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
750                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
751                                         );
752                                 }
753                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
754                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
755                                         last if $internal_dep && (
756                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
757                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
758                                         );
759                                 }
760                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
761                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
762                                         last if $internal_dep && (
763                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
764                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
765                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
766                                         );
767                                 }
768                         }
769                 }
770         
771                 if (defined $reason) {
772                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
773                         return 1;
774                 }
775         }
777         return 0;
780 sub render_backlinks ($) {
781         my $backlinkchanged=shift;
782         foreach my $link (keys %$backlinkchanged) {
783                 my $linkfile=$pagesources{$link};
784                 if (defined $linkfile) {
785                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
786                 }
787         }
790 sub gen_autofile ($$$) {
791         my $autofile=shift;
792         my $pages=shift;
793         my $del=shift;
795         if (file_pruned($autofile)) {
796                 return;
797         }
799         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
800         if (! defined $file) {
801                 return;
802         }
804         # Remember autofiles that were tried, and never try them again later.
805         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
806                 return;
807         }
808         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
810         if (srcfile($autofile, 1) || file_pruned($autofile)) {
811                 return;
812         }
813         
814         if (-l $file || -d _ || -e _) {
815                 return;
816         }
818         my $page = pagename($file);
819         if ($pages->{$page}) {
820                 return;
821         }
823         if (grep { $_ eq $autofile } @$del) {
824                 return;
825         }
827         $autofiles{$autofile}{generator}->();
828         $pages->{$page}=1;
829         return 1;
832 sub refresh () {
833         $phase = PHASE_SCAN;
835         srcdir_check();
836         run_hooks(refresh => sub { shift->() });
837         my ($files, $pages, $new, $internal_new, $del, $internal_del, $changed, $internal_changed);
838         my $want_find_changes=$config{only_committed_changes} &&
839                 exists $IkiWiki::hooks{rcs}{rcs_find_changes} &&
840                 exists $IkiWiki::hooks{rcs}{rcs_get_current_rev};
841         if (! $config{rebuild} && $want_find_changes && defined $IkiWiki::lastrev && length $IkiWiki::lastrev) {
842                 my ($changed_raw, $del_raw);
843                 ($changed_raw, $del_raw, $IkiWiki::lastrev) = $IkiWiki::hooks{rcs}{rcs_find_changes}{call}->($IkiWiki::lastrev);
844                 ($files, $pages)=process_changed_files($changed_raw, $del_raw);
845         }
846         else {
847                 ($files, $pages)=find_src_files();
848         }
849         if ($want_find_changes) {
850                 if (! defined($IkiWiki::lastrev) || ! length $IkiWiki::lastrev) {
851                         $IkiWiki::lastrev=$IkiWiki::hooks{rcs}{rcs_get_current_rev}{call}->();
852                 }
853         }
854         ($new, $internal_new)=find_new_files($files);
855         ($del, $internal_del)=find_del_files($pages);
856         ($changed, $internal_changed)=find_changed($files);
857         my %existingfiles;
858         run_hooks(needsbuild => sub {
859                 my $ret=shift->($changed, [@$del, @$internal_del]);
860                 if (ref $ret eq 'ARRAY' && $ret != $changed) {
861                         if (! %existingfiles) {
862                                 foreach my $f (@$files) {
863                                         $existingfiles{$f}=1;
864                                 }
865                         }
866                         @$changed=grep $existingfiles{$_}, @$ret;
867                 }
868         });
869         my $oldlink_targets=calculate_old_links($changed, $del);
871         foreach my $file (@$changed) {
872                 scan($file);
873         }
875         foreach my $autofile (keys %autofiles) {
876                 if (gen_autofile($autofile, $pages, $del)) {
877                         push @{$files}, $autofile;
878                         push @{$new}, $autofile if find_new_files([$autofile]);
879                         push @{$changed}, $autofile if find_changed([$autofile]);
880                         
881                         scan($autofile);
882                 }
883         }
885         calculate_links();
887         # At this point it becomes OK to start matching pagespecs.
888         $phase = PHASE_RENDER;
889         # Save some memory: we no longer need to keep track of which pages
890         # we've scanned
891         %scanned = ();
893         remove_del(@$del, @$internal_del);
895         foreach my $file (@$changed) {
896                 render($file, sprintf(gettext("building %s"), $file));
897         }
898         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
899                 derender_internal($file);
900         }
902         run_hooks(build_affected => sub {
903                 my %affected = shift->();
904                 while (my ($page, $message) = each %affected) {
905                         next unless exists $pagesources{$page};
906                         render($pagesources{$page}, $message);
907                 }
908         });
910         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
911                 $del, $oldlink_targets);
913         foreach my $file (@$new, @$del) {
914                 render_linkers($file);
915         }
917         if (@$changed || @$internal_changed ||
918             @$del || @$internal_del || @$internal_new) {
919                 1 while render_dependent($files, $new, $internal_new,
920                         $del, $internal_del, $internal_changed,
921                         $linkchangers);
922         }
924         render_backlinks($backlinkchanged);
925         remove_unrendered();
927         if (@$del || @$internal_del) {
928                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
929         }
930         if (%rendered) {
931                 run_hooks(rendered => sub { shift->(keys %rendered) });
932                 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
933         }
934         my %all_changed = map { $_ => 1 }
935                 @$new, @$changed, @$del,
936                 @$internal_new, @$internal_changed, @$internal_del;
937         run_hooks(changes => sub { shift->(keys %all_changed) });
940 sub clean_rendered {
941         lockwiki();
942         loadindex();
943         remove_unrendered();
944         foreach my $page (keys %oldrenderedfiles) {
945                 foreach my $file (@{$oldrenderedfiles{$page}}) {
946                         prune($config{destdir}."/".$file, $config{destdir});
947                 }
948         }
951 sub commandline_render () {
952         lockwiki();
953         loadindex();
954         unlockwiki();
956         # This function behaves as though it's in the render phase;
957         # all other files are assumed to have been scanned last time.
958         $phase = PHASE_RENDER;
960         my $srcfile=possibly_foolish_untaint($config{render});
961         my $file=$srcfile;
962         $file=~s/\Q$config{srcdir}\E\/?//;
964         my $type=pagetype($file);
965         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
966         my $content=readfile($srcfile);
967         my $page=pagename($file);
968         $pagesources{$page}=$file;
969         $content=filter($page, $page, $content);
970         $content=preprocess($page, $page, $content);
971         $content=linkify($page, $page, $content);
972         $content=htmlize($page, $page, $type, $content);
973         $pagemtime{$page}=(stat($srcfile))[9];
974         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
976         print genpage($page, $content);
977         exit 0;