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},
139 run_hooks(pagetemplate => sub {
140 shift->(page => $page, destpage => $page, template => $template);
143 $content=$template->output;
145 run_hooks(format => sub {
157 return if $phase > PHASE_SCAN || $scanned{$file};
160 debug(sprintf(gettext("scanning %s"), $file));
162 my $type=pagetype($file);
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}) ];
177 delete $typedlinks{$page};
179 # Preprocess in scan-only mode.
180 preprocess($page, $page, $content, 1);
182 run_hooks(scan => sub {
190 will_render($file, $file, 1);
194 sub fast_file_copy (@) {
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);
210 defined($written = syswrite $destfd, $buf, $len, $offset)
211 or error("failed to write $destfile: $!", $cleanup);
220 return if $rendered{$file};
224 my $type=pagetype($file);
225 my $srcfile=srcfile($file);
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=~/^_/;
233 my $content=htmlize($page, $page, $type,
234 linkify($page, $page,
235 preprocess($page, $page,
237 readfile($srcfile)))));
239 my $output=htmlpage($page);
240 writefile($output, $config{destdir}, genpage($page, $content));
243 delete $depends{$file};
244 delete $depends_simple{$file};
245 will_render($file, $file, 1);
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)) {
257 # if hardlink fails, fall back to copying
260 my $srcfd=readfile($srcfile, 1, 1);
261 writefile($file, $config{destdir}, undef, 1, sub {
262 fast_file_copy($srcfile, $file, $srcfd, @_);
272 my $dir=dirname($file);
273 while ((! defined $up_to || $dir =~ m{^\Q$up_to\E\/}) && rmdir($dir)) {
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));
285 unless ($test=~s/\/+$//) {
286 $test=dirname($test);
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;
308 eval q{use File::Find};
313 my $origdir=getcwd();
314 my $abssrcdir=Cwd::abs_path($config{srcdir});
316 @IkiWiki::underlayfiles=();
318 my ($page, $underlay);
320 my $file=decode_utf8($_);
322 return if -l $file || -d _;
324 return if ! length $file;
325 $page = pagename($file);
326 if (! exists $pagesources{$page} &&
327 file_pruned($file)) {
328 $File::Find::prune=1;
332 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
334 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
339 # avoid underlaydir override attacks; see security.mdwn
340 if (! -l "$abssrcdir/$f" && ! -e _) {
341 if (! $pages{$page}) {
343 push @IkiWiki::underlayfiles, $f;
351 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
357 unless ($only_underlay) {
358 chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
363 chdir($origdir) || die "chdir $origdir: $!";
367 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
373 chdir($origdir) || die "chdir: $!";
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;
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
395 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
400 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
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
409 foreach my $f (@IkiWiki::underlayfiles) {
412 foreach my $page (keys %pagesources) {
413 my $f=$pagesources{$page};
414 unless ($old_underlay{$f} || exists $pages{$page} || exists $deleted_raw->{$f}) {
420 # add in the underlay
421 find_src_files(1, \@files, \%pages);
424 sub find_new_files ($) {
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}));
442 my $ctime=rcs_getctime($file);
444 $pagectime{$page}=$ctime;
452 $mtime=rcs_getmtime($file);
458 utime($mtime, $mtime, "$config{srcdir}/$file");
462 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
463 # the page has changed its type
464 $forcerebuild{$page}=1;
466 $pagesources{$page}=$file;
467 if (! $pagemtime{$page}) {
468 if (isinternal($page)) {
469 push @internal_new, $file;
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;
482 return \@new, \@internal_new;
485 sub find_del_files ($) {
490 foreach my $page (keys %pagesources) {
491 if (! $pages->{$page}) {
492 if (isinternal($page)) {
493 push @internal_del, $pagesources{$page};
496 push @del, $pagesources{$page};
499 delete $typedlinks{$page};
500 $renderedfiles{$page}=[];
505 return \@del, \@internal_del;
509 foreach my $file (@_) {
510 my $page=pagename($file);
511 if (! isinternal($page)) {
512 debug(sprintf(gettext("removing obsolete %s"), $page));
515 foreach my $old (@{$oldrenderedfiles{$page}}) {
516 prune($config{destdir}."/".$old, $config{destdir});
519 foreach my $source (keys %destsources) {
520 if ($destsources{$source} eq $page) {
521 delete $destsources{$source};
525 delete $pagecase{lc $page};
526 $delpagesources{$page}=$pagesources{$page};
527 delete $pagesources{$page};
531 sub find_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 {
557 push @internal_changed, $file;
560 push @changed, $file;
564 return \@changed, \@internal_changed;
567 sub calculate_old_links ($$) {
568 my ($changed, $del)=@_;
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);
578 return \%oldlink_targets;
581 sub derender_internal ($) {
583 my $page=pagename($file);
584 delete $depends{$page};
585 delete $depends_simple{$page};
586 foreach my $old (@{$renderedfiles{$page}}) {
587 delete $destsources{$old};
589 $renderedfiles{$page}=[];
592 sub render_linkers ($) {
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));
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});
613 sub link_types_changed ($$) {
614 # each is of the form { type => { link => 1 } }
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};
627 while (my ($type, $links) = each %$old) {
628 foreach my $link (keys %$links) {
629 return 1 unless exists $new->{$type}{$link};
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;
652 delete $oldlink_targets->{$page}{$l};
655 if (exists $oldlink_targets->{$page} &&
656 %{$oldlink_targets->{$page}}) {
657 foreach my $target (values %{$oldlink_targets->{$page}}) {
658 $backlinkchanged{$target}=1;
660 $linkchangers{lc($page)}=1;
663 # we currently assume that changing the type of a link doesn't
665 if (!exists $linkchangers{lc($page)}) {
666 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
667 $linkchangers{lc($page)}=1;
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);
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));
695 foreach my $f (@$files) {
696 next if $rendered{$f};
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 &&
705 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
706 $lc_exists_changed{$d})
708 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
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
725 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
730 foreach my $file (@$list) {
732 my $page=pagename($file);
733 if ($sub->($page, location => $p)) {
734 if ($type == $IkiWiki::DEPEND_LINKS) {
735 next unless $linkchangers->{lc($page)};
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)
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)
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)
770 if (defined $reason) {
771 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
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));
789 sub gen_autofile ($$$) {
794 if (file_pruned($autofile)) {
798 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
799 if (! defined $file) {
803 # Remember autofiles that were tried, and never try them again later.
804 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
807 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
809 if (srcfile($autofile, 1) || file_pruned($autofile)) {
813 if (-l $file || -d _ || -e _) {
817 my $page = pagename($file);
818 if ($pages->{$page}) {
822 if (grep { $_ eq $autofile } @$del) {
826 $autofiles{$autofile}{generator}->();
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);
846 ($files, $pages)=find_src_files();
848 if ($want_find_changes) {
849 if (! defined($IkiWiki::lastrev) || ! length $IkiWiki::lastrev) {
850 $IkiWiki::lastrev=$IkiWiki::hooks{rcs}{rcs_get_current_rev}{call}->();
853 ($new, $internal_new)=find_new_files($files);
854 ($del, $internal_del)=find_del_files($pages);
855 ($changed, $internal_changed)=find_changed($files);
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;
865 @$changed=grep $existingfiles{$_}, @$ret;
868 my $oldlink_targets=calculate_old_links($changed, $del);
870 foreach my $file (@$changed) {
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]);
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
892 remove_del(@$del, @$internal_del);
894 foreach my $file (@$changed) {
895 render($file, sprintf(gettext("building %s"), $file));
897 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
898 derender_internal($file);
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);
909 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
910 $del, $oldlink_targets);
912 foreach my $file (@$new, @$del) {
913 render_linkers($file);
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,
923 render_backlinks($backlinkchanged);
926 if (@$del || @$internal_del) {
927 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
930 run_hooks(rendered => sub { shift->(keys %rendered) });
931 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
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) });
943 foreach my $page (keys %oldrenderedfiles) {
944 foreach my $file (@{$oldrenderedfiles{$page}}) {
945 prune($config{destdir}."/".$file, $config{destdir});
950 sub commandline_render () {
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});
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);