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)) {
330 $File::Find::prune=1;
334 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
336 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
341 # avoid underlaydir override attacks; see security.mdwn
342 if (! -l "$abssrcdir/$f" && ! -e _) {
343 if (! $pages{$page}) {
345 push @IkiWiki::underlayfiles, $f;
353 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
359 unless ($only_underlay) {
360 chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
365 chdir($origdir) || die "chdir $origdir: $!";
369 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
375 chdir($origdir) || die "chdir: $!";
379 return \@files, \%pages;
382 # Given a hash of files that have changed, and a hash of files that were
383 # deleted, should return the same results as find_src_files, with the same
384 # sanity checks. But a lot faster!
385 sub process_changed_files ($$) {
386 my $changed_raw=shift;
387 my $deleted_raw=shift;
392 foreach my $file (keys %$changed_raw) {
393 my $page = pagename($file);
394 next if ! exists $pagesources{$page} && file_pruned($file);
395 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
397 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
402 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
407 # So far, we only have the changed files. Now add in all the old
408 # files that were not changed or deleted, excluding ones that came
411 foreach my $f (@IkiWiki::underlayfiles) {
414 foreach my $page (keys %pagesources) {
415 my $f=$pagesources{$page};
416 unless ($old_underlay{$f} || exists $pages{$page} || exists $deleted_raw->{$f}) {
422 # add in the underlay
423 find_src_files(1, \@files, \%pages);
426 sub find_new_files ($) {
433 foreach my $file (@$files) {
434 my $page=pagename($file);
436 if ($config{rcs} && $config{gettime} &&
437 -e "$config{srcdir}/$file") {
438 if (! $times_noted) {
439 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
444 my $ctime=rcs_getctime($file);
446 $pagectime{$page}=$ctime;
454 $mtime=rcs_getmtime($file);
460 utime($mtime, $mtime, "$config{srcdir}/$file");
464 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
465 # the page has changed its type
466 $forcerebuild{$page}=1;
468 $pagesources{$page}=$file;
469 if (! $pagemtime{$page}) {
470 if (isinternal($page)) {
471 push @internal_new, $file;
476 $pagecase{lc $page}=$page;
477 if (! exists $pagectime{$page}) {
478 my @stat=srcfile_stat($file, 1);
479 # For the creation time of the page, take the
480 # inode change time (not creation time!) or
481 # the modification time, whichever is older.
482 my $ctime=($stat[10] < $stat[9] ? $stat[10] : $stat[9]);
483 $pagectime{$page}=$ctime if defined $ctime;
488 return \@new, \@internal_new;
491 sub find_del_files ($) {
496 foreach my $page (keys %pagesources) {
497 if (! $pages->{$page}) {
498 if (isinternal($page)) {
499 push @internal_del, $pagesources{$page};
502 push @del, $pagesources{$page};
505 delete $typedlinks{$page};
506 $renderedfiles{$page}=[];
511 return \@del, \@internal_del;
515 foreach my $file (@_) {
516 my $page=pagename($file);
517 if (! isinternal($page)) {
518 debug(sprintf(gettext("removing obsolete %s"), $page));
521 foreach my $old (@{$oldrenderedfiles{$page}}) {
522 prune($config{destdir}."/".$old, $config{destdir});
525 foreach my $source (keys %destsources) {
526 if ($destsources{$source} eq $page) {
527 delete $destsources{$source};
531 delete $pagecase{lc $page};
532 $delpagesources{$page}=$pagesources{$page};
533 delete $pagesources{$page};
537 sub find_changed ($) {
540 my @internal_changed;
541 foreach my $file (@$files) {
542 my $page=pagename($file);
543 my ($srcfile, @stat)=srcfile_stat($file, 1);
544 if (defined $srcfile &&
545 (! exists $pagemtime{$page} ||
546 $stat[9] > $pagemtime{$page} ||
547 $forcerebuild{$page})) {
548 $pagemtime{$page}=$stat[9];
550 if (isinternal($page)) {
551 my $content = readfile($srcfile);
553 # Preprocess internal page in scan-only mode.
554 preprocess($page, $page, $content, 1);
556 run_hooks(scan => sub {
563 push @internal_changed, $file;
566 push @changed, $file;
570 return \@changed, \@internal_changed;
573 sub calculate_old_links ($$) {
574 my ($changed, $del)=@_;
576 foreach my $file (@$changed, @$del) {
577 my $page=pagename($file);
578 if (exists $oldlinks{$page}) {
579 foreach my $l (@{$oldlinks{$page}}) {
580 $oldlink_targets{$page}{$l}=bestlink($page, $l);
584 return \%oldlink_targets;
587 sub derender_internal ($) {
589 my $page=pagename($file);
590 delete $depends{$page};
591 delete $depends_simple{$page};
592 foreach my $old (@{$renderedfiles{$page}}) {
593 delete $destsources{$old};
595 $renderedfiles{$page}=[];
598 sub render_linkers ($) {
601 foreach my $page (keys %{$backlinks{$p}}) {
602 my $file=$pagesources{$page};
603 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
607 sub remove_unrendered () {
608 foreach my $src (keys %rendered) {
609 my $page=pagename($src);
610 foreach my $file (@{$oldrenderedfiles{$page}}) {
611 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
612 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
613 prune($config{destdir}."/".$file, $config{destdir});
619 sub link_types_changed ($$) {
620 # each is of the form { type => { link => 1 } }
624 return 0 if !defined $new && !defined $old;
625 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
627 while (my ($type, $links) = each %$new) {
628 foreach my $link (keys %$links) {
629 return 1 unless exists $old->{$type}{$link};
633 while (my ($type, $links) = each %$old) {
634 foreach my $link (keys %$links) {
635 return 1 unless exists $new->{$type}{$link};
642 sub calculate_changed_links ($$$) {
643 my ($changed, $del, $oldlink_targets)=@_;
645 my (%backlinkchanged, %linkchangers);
647 foreach my $file (@$changed, @$del) {
648 my $page=pagename($file);
650 if (exists $links{$page}) {
651 foreach my $l (@{$links{$page}}) {
652 my $target=bestlink($page, $l);
653 if (! exists $oldlink_targets->{$page}{$l} ||
654 $target ne $oldlink_targets->{$page}{$l}) {
655 $backlinkchanged{$target}=1;
656 $linkchangers{lc($page)}=1;
658 delete $oldlink_targets->{$page}{$l};
661 if (exists $oldlink_targets->{$page} &&
662 %{$oldlink_targets->{$page}}) {
663 foreach my $target (values %{$oldlink_targets->{$page}}) {
664 $backlinkchanged{$target}=1;
666 $linkchangers{lc($page)}=1;
669 # we currently assume that changing the type of a link doesn't
671 if (!exists $linkchangers{lc($page)}) {
672 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
673 $linkchangers{lc($page)}=1;
678 return \%backlinkchanged, \%linkchangers;
681 sub render_dependent ($$$$$$$) {
682 my ($files, $new, $internal_new, $del, $internal_del,
683 $internal_changed, $linkchangers)=@_;
685 my @changed=(keys %rendered, @$del);
686 my @exists_changed=(@$new, @$del);
688 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
689 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
691 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
692 if ($rendered{$p} || grep { $_ eq $p } @$del) {
693 foreach my $f (@$files) {
694 next if $rendered{$f};
695 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
701 foreach my $f (@$files) {
702 next if $rendered{$f};
706 if (exists $depends_simple{$p} && ! defined $reason) {
707 foreach my $d (keys %{$depends_simple{$p}}) {
708 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
711 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
712 $lc_exists_changed{$d})
714 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
723 if (exists $depends{$p} && ! defined $reason) {
724 foreach my $dep (keys %{$depends{$p}}) {
725 my $sub=pagespec_translate($dep);
726 next unless defined $sub;
728 # only consider internal files
729 # if the page explicitly depends
731 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
736 foreach my $file (@$list) {
738 my $page=pagename($file);
739 if ($sub->($page, location => $p)) {
740 if ($type == $IkiWiki::DEPEND_LINKS) {
741 next unless $linkchangers->{lc($page)};
750 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
751 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
752 last if $internal_dep && (
753 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
754 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
755 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
758 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
759 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
760 last if $internal_dep && (
761 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
762 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
765 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
766 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
767 last if $internal_dep && (
768 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
769 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
770 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
776 if (defined $reason) {
777 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
785 sub render_backlinks ($) {
786 my $backlinkchanged=shift;
787 foreach my $link (keys %$backlinkchanged) {
788 my $linkfile=$pagesources{$link};
789 if (defined $linkfile) {
790 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
795 sub gen_autofile ($$$) {
800 if (file_pruned($autofile)) {
804 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
805 if (! defined $file) {
809 # Remember autofiles that were tried, and never try them again later.
810 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
813 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
815 if (srcfile($autofile, 1) || file_pruned($autofile)) {
819 if (-l $file || -d _ || -e _) {
823 my $page = pagename($file);
824 if ($pages->{$page}) {
828 if (grep { $_ eq $autofile } @$del) {
832 $autofiles{$autofile}{generator}->();
841 run_hooks(refresh => sub { shift->() });
842 my ($files, $pages, $new, $internal_new, $del, $internal_del, $changed, $internal_changed);
843 my $want_find_changes=$config{only_committed_changes} &&
844 exists $IkiWiki::hooks{rcs}{rcs_find_changes} &&
845 exists $IkiWiki::hooks{rcs}{rcs_get_current_rev};
846 if (! $config{rebuild} && $want_find_changes && defined $IkiWiki::lastrev && length $IkiWiki::lastrev) {
847 my ($changed_raw, $del_raw);
848 ($changed_raw, $del_raw, $IkiWiki::lastrev) = $IkiWiki::hooks{rcs}{rcs_find_changes}{call}->($IkiWiki::lastrev);
849 ($files, $pages)=process_changed_files($changed_raw, $del_raw);
852 ($files, $pages)=find_src_files();
854 if ($want_find_changes) {
855 if (! defined($IkiWiki::lastrev) || ! length $IkiWiki::lastrev) {
856 $IkiWiki::lastrev=$IkiWiki::hooks{rcs}{rcs_get_current_rev}{call}->();
859 ($new, $internal_new)=find_new_files($files);
860 ($del, $internal_del)=find_del_files($pages);
861 ($changed, $internal_changed)=find_changed($files);
863 run_hooks(needsbuild => sub {
864 my $ret=shift->($changed, [@$del, @$internal_del]);
865 if (ref $ret eq 'ARRAY' && $ret != $changed) {
866 if (! %existingfiles) {
867 foreach my $f (@$files) {
868 $existingfiles{$f}=1;
871 @$changed=grep $existingfiles{$_}, @$ret;
874 my $oldlink_targets=calculate_old_links($changed, $del);
876 foreach my $file (@$changed) {
880 foreach my $autofile (keys %autofiles) {
881 if (gen_autofile($autofile, $pages, $del)) {
882 push @{$files}, $autofile;
883 push @{$new}, $autofile if find_new_files([$autofile]);
884 push @{$changed}, $autofile if find_changed([$autofile]);
892 # At this point it becomes OK to start matching pagespecs.
893 $phase = PHASE_RENDER;
894 # Save some memory in full rebuilds: we no longer need to keep
895 # track of which pages we've scanned, because we can assume the
896 # answer is "all of them".
897 %scanned = () if $config{rebuild};
899 remove_del(@$del, @$internal_del);
901 foreach my $file (@$changed) {
902 render($file, sprintf(gettext("building %s"), $file));
904 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
905 derender_internal($file);
908 run_hooks(build_affected => sub {
909 my %affected = shift->();
910 while (my ($page, $message) = each %affected) {
911 next unless exists $pagesources{$page};
912 render($pagesources{$page}, $message);
916 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
917 $del, $oldlink_targets);
919 foreach my $file (@$new, @$del) {
920 render_linkers($file);
923 if (@$changed || @$internal_changed ||
924 @$del || @$internal_del || @$internal_new) {
925 1 while render_dependent($files, $new, $internal_new,
926 $del, $internal_del, $internal_changed,
930 render_backlinks($backlinkchanged);
933 if (@$del || @$internal_del) {
934 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
937 run_hooks(rendered => sub { shift->(keys %rendered) });
938 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
940 my %all_changed = map { $_ => 1 }
941 @$new, @$changed, @$del,
942 @$internal_new, @$internal_changed, @$internal_del;
943 run_hooks(changes => sub { shift->(keys %all_changed) });
950 foreach my $page (keys %oldrenderedfiles) {
951 foreach my $file (@{$oldrenderedfiles{$page}}) {
952 prune($config{destdir}."/".$file, $config{destdir});
957 sub commandline_render () {
962 # This function behaves as though it's in the render phase;
963 # all other files are assumed to have been scanned last time.
964 $phase = PHASE_RENDER;
966 my $srcfile=possibly_foolish_untaint($config{render});
968 $file=~s/\Q$config{srcdir}\E\/?//;
970 my $type=pagetype($file);
971 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
972 my $content=readfile($srcfile);
973 my $page=pagename($file);
974 $pagesources{$page}=$file;
975 $content=filter($page, $page, $content);
976 $content=preprocess($page, $page, $content);
977 $content=linkify($page, $page, $content);
978 $content=htmlize($page, $page, $type, $content);
979 $pagemtime{$page}=(stat($srcfile))[9];
980 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
982 print genpage($page, $content);