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} || $a->{url} cmp $b->{url} } 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 ($config{rebuild} && $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 @stat=srcfile_stat($file, 1);
478 # For the creation time of the page, take the
479 # inode change time (not creation time!) or
480 # the modification time, whichever is older.
481 my $ctime=($stat[10] < $stat[9] ? $stat[10] : $stat[9]);
482 $pagectime{$page}=$ctime if defined $ctime;
487 return \@new, \@internal_new;
490 sub find_del_files ($) {
495 foreach my $page (keys %pagesources) {
496 if (! $pages->{$page}) {
497 if (isinternal($page)) {
498 push @internal_del, $pagesources{$page};
501 push @del, $pagesources{$page};
504 delete $typedlinks{$page};
505 $renderedfiles{$page}=[];
510 return \@del, \@internal_del;
514 foreach my $file (@_) {
515 my $page=pagename($file);
516 if (! isinternal($page)) {
517 debug(sprintf(gettext("removing obsolete %s"), $page));
520 foreach my $old (@{$oldrenderedfiles{$page}}) {
521 prune($config{destdir}."/".$old, $config{destdir});
524 foreach my $source (keys %destsources) {
525 if ($destsources{$source} eq $page) {
526 delete $destsources{$source};
530 delete $pagecase{lc $page};
531 $delpagesources{$page}=$pagesources{$page};
532 delete $pagesources{$page};
536 sub find_changed ($) {
539 my @internal_changed;
540 foreach my $file (@$files) {
541 my $page=pagename($file);
542 my ($srcfile, @stat)=srcfile_stat($file, 1);
543 if (defined $srcfile &&
544 (! exists $pagemtime{$page} ||
545 $stat[9] > $pagemtime{$page} ||
546 $forcerebuild{$page})) {
547 $pagemtime{$page}=$stat[9];
549 if (isinternal($page)) {
550 my $content = readfile($srcfile);
552 # Preprocess internal page in scan-only mode.
553 preprocess($page, $page, $content, 1);
555 run_hooks(scan => sub {
562 push @internal_changed, $file;
565 push @changed, $file;
569 return \@changed, \@internal_changed;
572 sub calculate_old_links ($$) {
573 my ($changed, $del)=@_;
575 foreach my $file (@$changed, @$del) {
576 my $page=pagename($file);
577 if (exists $oldlinks{$page}) {
578 foreach my $l (@{$oldlinks{$page}}) {
579 $oldlink_targets{$page}{$l}=bestlink($page, $l);
583 return \%oldlink_targets;
586 sub derender_internal ($) {
588 my $page=pagename($file);
589 delete $depends{$page};
590 delete $depends_simple{$page};
591 foreach my $old (@{$renderedfiles{$page}}) {
592 delete $destsources{$old};
594 $renderedfiles{$page}=[];
597 sub render_linkers ($) {
600 foreach my $page (keys %{$backlinks{$p}}) {
601 my $file=$pagesources{$page};
602 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
606 sub remove_unrendered () {
607 foreach my $src (keys %rendered) {
608 my $page=pagename($src);
609 foreach my $file (@{$oldrenderedfiles{$page}}) {
610 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
611 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
612 prune($config{destdir}."/".$file, $config{destdir});
618 sub link_types_changed ($$) {
619 # each is of the form { type => { link => 1 } }
623 return 0 if !defined $new && !defined $old;
624 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
626 while (my ($type, $links) = each %$new) {
627 foreach my $link (keys %$links) {
628 return 1 unless exists $old->{$type}{$link};
632 while (my ($type, $links) = each %$old) {
633 foreach my $link (keys %$links) {
634 return 1 unless exists $new->{$type}{$link};
641 sub calculate_changed_links ($$$) {
642 my ($changed, $del, $oldlink_targets)=@_;
644 my (%backlinkchanged, %linkchangers);
646 foreach my $file (@$changed, @$del) {
647 my $page=pagename($file);
649 if (exists $links{$page}) {
650 foreach my $l (@{$links{$page}}) {
651 my $target=bestlink($page, $l);
652 if (! exists $oldlink_targets->{$page}{$l} ||
653 $target ne $oldlink_targets->{$page}{$l}) {
654 $backlinkchanged{$target}=1;
655 $linkchangers{lc($page)}=1;
657 delete $oldlink_targets->{$page}{$l};
660 if (exists $oldlink_targets->{$page} &&
661 %{$oldlink_targets->{$page}}) {
662 foreach my $target (values %{$oldlink_targets->{$page}}) {
663 $backlinkchanged{$target}=1;
665 $linkchangers{lc($page)}=1;
668 # we currently assume that changing the type of a link doesn't
670 if (!exists $linkchangers{lc($page)}) {
671 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
672 $linkchangers{lc($page)}=1;
677 return \%backlinkchanged, \%linkchangers;
680 sub render_dependent ($$$$$$$) {
681 my ($files, $new, $internal_new, $del, $internal_del,
682 $internal_changed, $linkchangers)=@_;
684 my @changed=(keys %rendered, @$del);
685 my @exists_changed=(@$new, @$del);
687 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
688 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
690 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
691 if ($rendered{$p} || grep { $_ eq $p } @$del) {
692 foreach my $f (@$files) {
693 next if $rendered{$f};
694 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
700 foreach my $f (@$files) {
701 next if $rendered{$f};
705 if (exists $depends_simple{$p} && ! defined $reason) {
706 foreach my $d (keys %{$depends_simple{$p}}) {
707 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
710 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
711 $lc_exists_changed{$d})
713 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
722 if (exists $depends{$p} && ! defined $reason) {
723 foreach my $dep (keys %{$depends{$p}}) {
724 my $sub=pagespec_translate($dep);
725 next unless defined $sub;
727 # only consider internal files
728 # if the page explicitly depends
730 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
735 foreach my $file (@$list) {
737 my $page=pagename($file);
738 if ($sub->($page, location => $p)) {
739 if ($type == $IkiWiki::DEPEND_LINKS) {
740 next unless $linkchangers->{lc($page)};
749 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
750 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
751 last if $internal_dep && (
752 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
753 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
754 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
757 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
758 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
759 last if $internal_dep && (
760 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
761 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
764 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
765 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
766 last if $internal_dep && (
767 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
768 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
769 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
775 if (defined $reason) {
776 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
784 sub render_backlinks ($) {
785 my $backlinkchanged=shift;
786 foreach my $link (keys %$backlinkchanged) {
787 my $linkfile=$pagesources{$link};
788 if (defined $linkfile) {
789 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
794 sub gen_autofile ($$$) {
799 if (file_pruned($autofile)) {
803 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
804 if (! defined $file) {
808 # Remember autofiles that were tried, and never try them again later.
809 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
812 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
814 if (srcfile($autofile, 1) || file_pruned($autofile)) {
818 if (-l $file || -d _ || -e _) {
822 my $page = pagename($file);
823 if ($pages->{$page}) {
827 if (grep { $_ eq $autofile } @$del) {
831 $autofiles{$autofile}{generator}->();
840 run_hooks(refresh => sub { shift->() });
841 my ($files, $pages, $new, $internal_new, $del, $internal_del, $changed, $internal_changed);
842 my $want_find_changes=$config{only_committed_changes} &&
843 exists $IkiWiki::hooks{rcs}{rcs_find_changes} &&
844 exists $IkiWiki::hooks{rcs}{rcs_get_current_rev};
845 if (! $config{rebuild} && $want_find_changes && defined $IkiWiki::lastrev && length $IkiWiki::lastrev) {
846 my ($changed_raw, $del_raw);
847 ($changed_raw, $del_raw, $IkiWiki::lastrev) = $IkiWiki::hooks{rcs}{rcs_find_changes}{call}->($IkiWiki::lastrev);
848 ($files, $pages)=process_changed_files($changed_raw, $del_raw);
851 ($files, $pages)=find_src_files();
853 if ($want_find_changes) {
854 if (! defined($IkiWiki::lastrev) || ! length $IkiWiki::lastrev) {
855 $IkiWiki::lastrev=$IkiWiki::hooks{rcs}{rcs_get_current_rev}{call}->();
858 ($new, $internal_new)=find_new_files($files);
859 ($del, $internal_del)=find_del_files($pages);
860 ($changed, $internal_changed)=find_changed($files);
862 run_hooks(needsbuild => sub {
863 my $ret=shift->($changed, [@$del, @$internal_del]);
864 if (ref $ret eq 'ARRAY' && $ret != $changed) {
865 if (! %existingfiles) {
866 foreach my $f (@$files) {
867 $existingfiles{$f}=1;
870 @$changed=grep $existingfiles{$_}, @$ret;
873 my $oldlink_targets=calculate_old_links($changed, $del);
875 foreach my $file (@$changed) {
879 foreach my $autofile (keys %autofiles) {
880 if (gen_autofile($autofile, $pages, $del)) {
881 push @{$files}, $autofile;
882 push @{$new}, $autofile if find_new_files([$autofile]);
883 push @{$changed}, $autofile if find_changed([$autofile]);
891 # At this point it becomes OK to start matching pagespecs.
892 $phase = PHASE_RENDER;
893 # Save some memory in full rebuilds: we no longer need to keep
894 # track of which pages we've scanned, because we can assume the
895 # answer is "all of them".
896 %scanned = () if $config{rebuild};
898 remove_del(@$del, @$internal_del);
900 foreach my $file (@$changed) {
901 render($file, sprintf(gettext("building %s"), $file));
903 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
904 derender_internal($file);
907 run_hooks(build_affected => sub {
908 my %affected = shift->();
909 while (my ($page, $message) = each %affected) {
910 next unless exists $pagesources{$page};
911 render($pagesources{$page}, $message);
915 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
916 $del, $oldlink_targets);
918 foreach my $file (@$new, @$del) {
919 render_linkers($file);
922 if (@$changed || @$internal_changed ||
923 @$del || @$internal_del || @$internal_new) {
924 1 while render_dependent($files, $new, $internal_new,
925 $del, $internal_del, $internal_changed,
929 render_backlinks($backlinkchanged);
932 if (@$del || @$internal_del) {
933 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
936 run_hooks(rendered => sub { shift->(keys %rendered) });
937 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
939 my %all_changed = map { $_ => 1 }
940 @$new, @$changed, @$del,
941 @$internal_new, @$internal_changed, @$internal_del;
942 run_hooks(changes => sub { shift->(keys %all_changed) });
949 foreach my $page (keys %oldrenderedfiles) {
950 foreach my $file (@{$oldrenderedfiles{$page}}) {
951 prune($config{destdir}."/".$file, $config{destdir});
956 sub commandline_render () {
961 # This function behaves as though it's in the render phase;
962 # all other files are assumed to have been scanned last time.
963 $phase = PHASE_RENDER;
965 my $srcfile=possibly_foolish_untaint($config{render});
967 $file=~s/\Q$config{srcdir}\E\/?//;
969 my $type=pagetype($file);
970 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
971 my $content=readfile($srcfile);
972 my $page=pagename($file);
973 $pagesources{$page}=$file;
974 $content=filter($page, $page, $content);
975 $content=preprocess($page, $page, $content);
976 $content=linkify($page, $page, $content);
977 $content=htmlize($page, $page, $type, $content);
978 $pagemtime{$page}=(stat($srcfile))[9];
979 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
981 print genpage($page, $content);