9 my (%backlinks, %rendered, %scanned);
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;
24 push @{$brokenlinks{$link}}, $page;
31 sub backlink_pages ($) {
36 return keys %{$backlinks{$page}};
43 foreach my $p (backlink_pages($page)) {
44 my $href=urlto($p, $page);
46 # Trim common dir prefixes from both pages.
48 my $page_trimmed=$page;
50 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
52 $p_trimmed=~s/^\Q$dir\E// &&
53 $page_trimmed=~s/^\Q$dir\E//;
55 push @links, { url => $href, page => pagetitle($p_trimmed) };
64 run_hooks(indexhtml => sub {
65 shift->(page => $page, destpage => $page, content => $content);
69 run_hooks(templatefile => sub {
70 return if defined $templatefile;
71 my $file=shift->(page => $page);
72 if (defined $file && defined template_file($file)) {
77 if (defined $templatefile) {
78 $template=template_depends($templatefile, $page,
82 # no explicit depends as special case
83 $template=template('page.tmpl',
88 if (length $config{cgiurl}) {
89 if (IkiWiki->can("cgi_editpage")) {
90 $template->param(editurl => cgiurl(do => "edit", page => $page));
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);
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));
110 $template->param(have_actions => 1);
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;
121 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
122 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
126 title => $page eq 'index'
128 : pagetitle(basename($page)),
129 wikiname => $config{wikiname},
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},
140 run_hooks(pagetemplate => sub {
141 shift->(page => $page, destpage => $page, template => $template);
144 $content=$template->output;
146 run_hooks(format => sub {
158 return if $phase > PHASE_SCAN || $scanned{$file};
161 debug(sprintf(gettext("scanning %s"), $file));
163 my $type=pagetype($file);
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}) ];
178 delete $typedlinks{$page};
180 # Preprocess in scan-only mode.
181 preprocess($page, $page, $content, 1);
183 run_hooks(scan => sub {
191 will_render($file, $file, 1);
195 sub fast_file_copy (@) {
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);
211 defined($written = syswrite $destfd, $buf, $len, $offset)
212 or error("failed to write $destfile: $!", $cleanup);
221 return if $rendered{$file};
225 my $type=pagetype($file);
226 my $srcfile=srcfile($file);
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=~/^_/;
234 my $content=htmlize($page, $page, $type,
235 linkify($page, $page,
236 preprocess($page, $page,
238 readfile($srcfile)))));
240 my $output=htmlpage($page);
241 writefile($output, $config{destdir}, genpage($page, $content));
244 delete $depends{$file};
245 delete $depends_simple{$file};
246 will_render($file, $file, 1);
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)) {
258 # if hardlink fails, fall back to copying
261 my $srcfd=readfile($srcfile, 1, 1);
262 writefile($file, $config{destdir}, undef, 1, sub {
263 fast_file_copy($srcfile, $file, $srcfd, @_);
273 my $dir=dirname($file);
274 while ((! defined $up_to || $dir =~ m{^\Q$up_to\E\/}) && rmdir($dir)) {
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));
286 unless ($test=~s/\/+$//) {
287 $test=dirname($test);
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;
309 eval q{use File::Find};
314 my $origdir=getcwd();
315 my $abssrcdir=Cwd::abs_path($config{srcdir});
317 @IkiWiki::underlayfiles=();
319 my ($page, $underlay);
321 my $file=decode_utf8($_);
323 return if -l $file || -d _;
325 return if ! length $file;
326 $page = pagename($file);
327 if (! exists $pagesources{$page} &&
328 file_pruned($file)) {
329 $File::Find::prune=1;
333 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
335 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
340 # avoid underlaydir override attacks; see security.mdwn
341 if (! -l "$abssrcdir/$f" && ! -e _) {
342 if (! $pages{$page}) {
344 push @IkiWiki::underlayfiles, $f;
352 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
358 unless ($only_underlay) {
359 chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
364 chdir($origdir) || die "chdir $origdir: $!";
368 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
374 chdir($origdir) || die "chdir: $!";
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;
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
396 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
401 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
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
410 foreach my $f (@IkiWiki::underlayfiles) {
413 foreach my $page (keys %pagesources) {
414 my $f=$pagesources{$page};
415 unless ($old_underlay{$f} || exists $pages{$page} || exists $deleted_raw->{$f}) {
421 # add in the underlay
422 find_src_files(1, \@files, \%pages);
425 sub find_new_files ($) {
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}));
443 my $ctime=rcs_getctime($file);
445 $pagectime{$page}=$ctime;
453 $mtime=rcs_getmtime($file);
459 utime($mtime, $mtime, "$config{srcdir}/$file");
463 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
464 # the page has changed its type
465 $forcerebuild{$page}=1;
467 $pagesources{$page}=$file;
468 if (! $pagemtime{$page}) {
469 if (isinternal($page)) {
470 push @internal_new, $file;
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;
483 return \@new, \@internal_new;
486 sub find_del_files ($) {
491 foreach my $page (keys %pagesources) {
492 if (! $pages->{$page}) {
493 if (isinternal($page)) {
494 push @internal_del, $pagesources{$page};
497 push @del, $pagesources{$page};
500 delete $typedlinks{$page};
501 $renderedfiles{$page}=[];
506 return \@del, \@internal_del;
510 foreach my $file (@_) {
511 my $page=pagename($file);
512 if (! isinternal($page)) {
513 debug(sprintf(gettext("removing obsolete %s"), $page));
516 foreach my $old (@{$oldrenderedfiles{$page}}) {
517 prune($config{destdir}."/".$old, $config{destdir});
520 foreach my $source (keys %destsources) {
521 if ($destsources{$source} eq $page) {
522 delete $destsources{$source};
526 delete $pagecase{lc $page};
527 $delpagesources{$page}=$pagesources{$page};
528 delete $pagesources{$page};
532 sub find_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 {
558 push @internal_changed, $file;
561 push @changed, $file;
565 return \@changed, \@internal_changed;
568 sub calculate_old_links ($$) {
569 my ($changed, $del)=@_;
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);
579 return \%oldlink_targets;
582 sub derender_internal ($) {
584 my $page=pagename($file);
585 delete $depends{$page};
586 delete $depends_simple{$page};
587 foreach my $old (@{$renderedfiles{$page}}) {
588 delete $destsources{$old};
590 $renderedfiles{$page}=[];
593 sub render_linkers ($) {
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));
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});
614 sub link_types_changed ($$) {
615 # each is of the form { type => { link => 1 } }
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};
628 while (my ($type, $links) = each %$old) {
629 foreach my $link (keys %$links) {
630 return 1 unless exists $new->{$type}{$link};
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;
653 delete $oldlink_targets->{$page}{$l};
656 if (exists $oldlink_targets->{$page} &&
657 %{$oldlink_targets->{$page}}) {
658 foreach my $target (values %{$oldlink_targets->{$page}}) {
659 $backlinkchanged{$target}=1;
661 $linkchangers{lc($page)}=1;
664 # we currently assume that changing the type of a link doesn't
666 if (!exists $linkchangers{lc($page)}) {
667 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
668 $linkchangers{lc($page)}=1;
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);
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));
696 foreach my $f (@$files) {
697 next if $rendered{$f};
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 &&
706 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
707 $lc_exists_changed{$d})
709 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
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
726 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
731 foreach my $file (@$list) {
733 my $page=pagename($file);
734 if ($sub->($page, location => $p)) {
735 if ($type == $IkiWiki::DEPEND_LINKS) {
736 next unless $linkchangers->{lc($page)};
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)
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)
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)
771 if (defined $reason) {
772 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
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));
790 sub gen_autofile ($$$) {
795 if (file_pruned($autofile)) {
799 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
800 if (! defined $file) {
804 # Remember autofiles that were tried, and never try them again later.
805 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
808 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
810 if (srcfile($autofile, 1) || file_pruned($autofile)) {
814 if (-l $file || -d _ || -e _) {
818 my $page = pagename($file);
819 if ($pages->{$page}) {
823 if (grep { $_ eq $autofile } @$del) {
827 $autofiles{$autofile}{generator}->();
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);
847 ($files, $pages)=find_src_files();
849 if ($want_find_changes) {
850 if (! defined($IkiWiki::lastrev) || ! length $IkiWiki::lastrev) {
851 $IkiWiki::lastrev=$IkiWiki::hooks{rcs}{rcs_get_current_rev}{call}->();
854 ($new, $internal_new)=find_new_files($files);
855 ($del, $internal_del)=find_del_files($pages);
856 ($changed, $internal_changed)=find_changed($files);
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;
866 @$changed=grep $existingfiles{$_}, @$ret;
869 my $oldlink_targets=calculate_old_links($changed, $del);
871 foreach my $file (@$changed) {
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]);
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
893 remove_del(@$del, @$internal_del);
895 foreach my $file (@$changed) {
896 render($file, sprintf(gettext("building %s"), $file));
898 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
899 derender_internal($file);
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);
910 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
911 $del, $oldlink_targets);
913 foreach my $file (@$new, @$del) {
914 render_linkers($file);
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,
924 render_backlinks($backlinkchanged);
927 if (@$del || @$internal_del) {
928 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
931 run_hooks(rendered => sub { shift->(keys %rendered) });
932 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
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) });
944 foreach my $page (keys %oldrenderedfiles) {
945 foreach my $file (@{$oldrenderedfiles{$page}}) {
946 prune($config{destdir}."/".$file, $config{destdir});
951 sub commandline_render () {
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});
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);