]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
Merge remote-tracking branch 'refs/remotes/dgit/dgit/sid'
[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                                 my $ctime=(srcfile_stat($file, 1))[10];
477                                 $pagectime{$page}=$ctime if defined $ctime;
478                         }
479                 }
480         }
482         return \@new, \@internal_new;
485 sub find_del_files ($) {
486         my $pages=shift;
487         my @del;
488         my @internal_del;
490         foreach my $page (keys %pagesources) {
491                 if (! $pages->{$page}) {
492                         if (isinternal($page)) {
493                                 push @internal_del, $pagesources{$page};
494                         }
495                         else {
496                                 push @del, $pagesources{$page};
497                         }
498                         $links{$page}=[];
499                         delete $typedlinks{$page};
500                         $renderedfiles{$page}=[];
501                         $pagemtime{$page}=0;
502                 }
503         }
505         return \@del, \@internal_del;
508 sub remove_del (@) {
509         foreach my $file (@_) {
510                 my $page=pagename($file);
511                 if (! isinternal($page)) {
512                         debug(sprintf(gettext("removing obsolete %s"), $page));
513                 }
514         
515                 foreach my $old (@{$oldrenderedfiles{$page}}) {
516                         prune($config{destdir}."/".$old, $config{destdir});
517                 }
519                 foreach my $source (keys %destsources) {
520                         if ($destsources{$source} eq $page) {
521                                 delete $destsources{$source};
522                         }
523                 }
524         
525                 delete $pagecase{lc $page};
526                 $delpagesources{$page}=$pagesources{$page};
527                 delete $pagesources{$page};
528         }
531 sub find_changed ($) {
532         my $files=shift;
533         my @changed;
534         my @internal_changed;
535         foreach my $file (@$files) {
536                 my $page=pagename($file);
537                 my ($srcfile, @stat)=srcfile_stat($file, 1);
538                 if (defined $srcfile && 
539                     (! exists $pagemtime{$page} ||
540                      $stat[9] > $pagemtime{$page} ||
541                      $forcerebuild{$page})) {
542                         $pagemtime{$page}=$stat[9];
544                         if (isinternal($page)) {
545                                 my $content = readfile($srcfile);
547                                 # Preprocess internal page in scan-only mode.
548                                 preprocess($page, $page, $content, 1);
550                                 run_hooks(scan => sub {
551                                         shift->(
552                                                 page => $page,
553                                                 content => $content,
554                                         );
555                                 });
557                                 push @internal_changed, $file;
558                         }
559                         else {
560                                 push @changed, $file;
561                         }
562                 }
563         }
564         return \@changed, \@internal_changed;
567 sub calculate_old_links ($$) {
568         my ($changed, $del)=@_;
569         my %oldlink_targets;
570         foreach my $file (@$changed, @$del) {
571                 my $page=pagename($file);
572                 if (exists $oldlinks{$page}) {
573                         foreach my $l (@{$oldlinks{$page}}) {
574                                 $oldlink_targets{$page}{$l}=bestlink($page, $l);
575                         }
576                 }
577         }
578         return \%oldlink_targets;
581 sub derender_internal ($) {
582         my $file=shift;
583         my $page=pagename($file);
584         delete $depends{$page};
585         delete $depends_simple{$page};
586         foreach my $old (@{$renderedfiles{$page}}) {
587                 delete $destsources{$old};
588         }
589         $renderedfiles{$page}=[];
592 sub render_linkers ($) {
593         my $f=shift;
594         my $p=pagename($f);
595         foreach my $page (keys %{$backlinks{$p}}) {
596                 my $file=$pagesources{$page};
597                 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
598         }
601 sub remove_unrendered () {
602         foreach my $src (keys %rendered) {
603                 my $page=pagename($src);
604                 foreach my $file (@{$oldrenderedfiles{$page}}) {
605                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
606                                 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
607                                 prune($config{destdir}."/".$file, $config{destdir});
608                         }
609                 }
610         }
613 sub link_types_changed ($$) {
614         # each is of the form { type => { link => 1 } }
615         my $new = shift;
616         my $old = shift;
618         return 0 if !defined $new && !defined $old;
619         return 1 if (!defined $new && %$old) || (!defined $old && %$new);
621         while (my ($type, $links) = each %$new) {
622                 foreach my $link (keys %$links) {
623                         return 1 unless exists $old->{$type}{$link};
624                 }
625         }
627         while (my ($type, $links) = each %$old) {
628                 foreach my $link (keys %$links) {
629                         return 1 unless exists $new->{$type}{$link};
630                 }
631         }
633         return 0;
636 sub calculate_changed_links ($$$) {
637         my ($changed, $del, $oldlink_targets)=@_;
639         my (%backlinkchanged, %linkchangers);
641         foreach my $file (@$changed, @$del) {
642                 my $page=pagename($file);
644                 if (exists $links{$page}) {
645                         foreach my $l (@{$links{$page}}) {
646                                 my $target=bestlink($page, $l);
647                                 if (! exists $oldlink_targets->{$page}{$l} ||
648                                     $target ne $oldlink_targets->{$page}{$l}) {
649                                         $backlinkchanged{$target}=1;
650                                         $linkchangers{lc($page)}=1;
651                                 }
652                                 delete $oldlink_targets->{$page}{$l};
653                         }
654                 }
655                 if (exists $oldlink_targets->{$page} &&
656                     %{$oldlink_targets->{$page}}) {
657                         foreach my $target (values %{$oldlink_targets->{$page}}) {
658                                 $backlinkchanged{$target}=1;
659                         }
660                         $linkchangers{lc($page)}=1;
661                 }
663                 # we currently assume that changing the type of a link doesn't
664                 # change backlinks
665                 if (!exists $linkchangers{lc($page)}) {
666                         if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
667                                 $linkchangers{lc($page)}=1;
668                         }
669                 }
670         }
672         return \%backlinkchanged, \%linkchangers;
675 sub render_dependent ($$$$$$$) {
676         my ($files, $new, $internal_new, $del, $internal_del,
677                 $internal_changed, $linkchangers)=@_;
679         my @changed=(keys %rendered, @$del);
680         my @exists_changed=(@$new, @$del);
681         
682         my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
683         my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
685         foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
686                 if ($rendered{$p} || grep { $_ eq $p } @$del) {
687                         foreach my $f (@$files) {
688                                 next if $rendered{$f};
689                                 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
690                         }
691                         return 0;
692                 }
693         }
694          
695         foreach my $f (@$files) {
696                 next if $rendered{$f};
697                 my $p=pagename($f);
698                 my $reason = undef;
700                 if (exists $depends_simple{$p} && ! defined $reason) {
701                         foreach my $d (keys %{$depends_simple{$p}}) {
702                                 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
703                                      $lc_changed{$d})
704                                     ||
705                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
706                                      $lc_exists_changed{$d})
707                                     ||
708                                     ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
709                                      $linkchangers->{$d})
710                                 ) {
711                                         $reason = $d;
712                                         last;
713                                 }
714                         }
715                 }
716         
717                 if (exists $depends{$p} && ! defined $reason) {
718                         foreach my $dep (keys %{$depends{$p}}) {
719                                 my $sub=pagespec_translate($dep);
720                                 next unless defined $sub;
722                                 # only consider internal files
723                                 # if the page explicitly depends
724                                 # on such files
725                                 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
727                                 my $in=sub {
728                                         my $list=shift;
729                                         my $type=shift;
730                                         foreach my $file (@$list) {
731                                                 next if $file eq $f;
732                                                 my $page=pagename($file);
733                                                 if ($sub->($page, location => $p)) {
734                                                         if ($type == $IkiWiki::DEPEND_LINKS) {
735                                                                 next unless $linkchangers->{lc($page)};
736                                                         }
737                                                         $reason=$page;
738                                                         return 1;
739                                                 }
740                                         }
741                                         return undef;
742                                 };
744                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
745                                         last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
746                                         last if $internal_dep && (
747                                                 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
748                                                 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
749                                                 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
750                                         );
751                                 }
752                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
753                                         last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
754                                         last if $internal_dep && (
755                                                 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
756                                                 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
757                                         );
758                                 }
759                                 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
760                                         last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
761                                         last if $internal_dep && (
762                                                 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
763                                                 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
764                                                 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
765                                         );
766                                 }
767                         }
768                 }
769         
770                 if (defined $reason) {
771                         render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
772                         return 1;
773                 }
774         }
776         return 0;
779 sub render_backlinks ($) {
780         my $backlinkchanged=shift;
781         foreach my $link (keys %$backlinkchanged) {
782                 my $linkfile=$pagesources{$link};
783                 if (defined $linkfile) {
784                         render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
785                 }
786         }
789 sub gen_autofile ($$$) {
790         my $autofile=shift;
791         my $pages=shift;
792         my $del=shift;
794         if (file_pruned($autofile)) {
795                 return;
796         }
798         my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
799         if (! defined $file) {
800                 return;
801         }
803         # Remember autofiles that were tried, and never try them again later.
804         if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
805                 return;
806         }
807         $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
809         if (srcfile($autofile, 1) || file_pruned($autofile)) {
810                 return;
811         }
812         
813         if (-l $file || -d _ || -e _) {
814                 return;
815         }
817         my $page = pagename($file);
818         if ($pages->{$page}) {
819                 return;
820         }
822         if (grep { $_ eq $autofile } @$del) {
823                 return;
824         }
826         $autofiles{$autofile}{generator}->();
827         $pages->{$page}=1;
828         return 1;
831 sub refresh () {
832         $phase = PHASE_SCAN;
834         srcdir_check();
835         run_hooks(refresh => sub { shift->() });
836         my ($files, $pages, $new, $internal_new, $del, $internal_del, $changed, $internal_changed);
837         my $want_find_changes=$config{only_committed_changes} &&
838                 exists $IkiWiki::hooks{rcs}{rcs_find_changes} &&
839                 exists $IkiWiki::hooks{rcs}{rcs_get_current_rev};
840         if (! $config{rebuild} && $want_find_changes && defined $IkiWiki::lastrev && length $IkiWiki::lastrev) {
841                 my ($changed_raw, $del_raw);
842                 ($changed_raw, $del_raw, $IkiWiki::lastrev) = $IkiWiki::hooks{rcs}{rcs_find_changes}{call}->($IkiWiki::lastrev);
843                 ($files, $pages)=process_changed_files($changed_raw, $del_raw);
844         }
845         else {
846                 ($files, $pages)=find_src_files();
847         }
848         if ($want_find_changes) {
849                 if (! defined($IkiWiki::lastrev) || ! length $IkiWiki::lastrev) {
850                         $IkiWiki::lastrev=$IkiWiki::hooks{rcs}{rcs_get_current_rev}{call}->();
851                 }
852         }
853         ($new, $internal_new)=find_new_files($files);
854         ($del, $internal_del)=find_del_files($pages);
855         ($changed, $internal_changed)=find_changed($files);
856         my %existingfiles;
857         run_hooks(needsbuild => sub {
858                 my $ret=shift->($changed, [@$del, @$internal_del]);
859                 if (ref $ret eq 'ARRAY' && $ret != $changed) {
860                         if (! %existingfiles) {
861                                 foreach my $f (@$files) {
862                                         $existingfiles{$f}=1;
863                                 }
864                         }
865                         @$changed=grep $existingfiles{$_}, @$ret;
866                 }
867         });
868         my $oldlink_targets=calculate_old_links($changed, $del);
870         foreach my $file (@$changed) {
871                 scan($file);
872         }
874         foreach my $autofile (keys %autofiles) {
875                 if (gen_autofile($autofile, $pages, $del)) {
876                         push @{$files}, $autofile;
877                         push @{$new}, $autofile if find_new_files([$autofile]);
878                         push @{$changed}, $autofile if find_changed([$autofile]);
879                         
880                         scan($autofile);
881                 }
882         }
884         calculate_links();
886         # At this point it becomes OK to start matching pagespecs.
887         $phase = PHASE_RENDER;
888         # Save some memory: we no longer need to keep track of which pages
889         # we've scanned
890         %scanned = ();
892         remove_del(@$del, @$internal_del);
894         foreach my $file (@$changed) {
895                 render($file, sprintf(gettext("building %s"), $file));
896         }
897         foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
898                 derender_internal($file);
899         }
901         run_hooks(build_affected => sub {
902                 my %affected = shift->();
903                 while (my ($page, $message) = each %affected) {
904                         next unless exists $pagesources{$page};
905                         render($pagesources{$page}, $message);
906                 }
907         });
909         my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
910                 $del, $oldlink_targets);
912         foreach my $file (@$new, @$del) {
913                 render_linkers($file);
914         }
916         if (@$changed || @$internal_changed ||
917             @$del || @$internal_del || @$internal_new) {
918                 1 while render_dependent($files, $new, $internal_new,
919                         $del, $internal_del, $internal_changed,
920                         $linkchangers);
921         }
923         render_backlinks($backlinkchanged);
924         remove_unrendered();
926         if (@$del || @$internal_del) {
927                 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
928         }
929         if (%rendered) {
930                 run_hooks(rendered => sub { shift->(keys %rendered) });
931                 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
932         }
933         my %all_changed = map { $_ => 1 }
934                 @$new, @$changed, @$del,
935                 @$internal_new, @$internal_changed, @$internal_del;
936         run_hooks(changes => sub { shift->(keys %all_changed) });
939 sub clean_rendered {
940         lockwiki();
941         loadindex();
942         remove_unrendered();
943         foreach my $page (keys %oldrenderedfiles) {
944                 foreach my $file (@{$oldrenderedfiles{$page}}) {
945                         prune($config{destdir}."/".$file, $config{destdir});
946                 }
947         }
950 sub commandline_render () {
951         lockwiki();
952         loadindex();
953         unlockwiki();
955         # This function behaves as though it's in the render phase;
956         # all other files are assumed to have been scanned last time.
957         $phase = PHASE_RENDER;
959         my $srcfile=possibly_foolish_untaint($config{render});
960         my $file=$srcfile;
961         $file=~s/\Q$config{srcdir}\E\/?//;
963         my $type=pagetype($file);
964         die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
965         my $content=readfile($srcfile);
966         my $page=pagename($file);
967         $pagesources{$page}=$file;
968         $content=filter($page, $page, $content);
969         $content=preprocess($page, $page, $content);
970         $content=linkify($page, $page, $content);
971         $content=htmlize($page, $page, $type, $content);
972         $pagemtime{$page}=(stat($srcfile))[9];
973         $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
975         print genpage($page, $content);
976         exit 0;