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 $pagectime{$page}=(srcfile_stat($file))[10];
481 return \@new, \@internal_new;
484 sub find_del_files ($) {
489 foreach my $page (keys %pagesources) {
490 if (! $pages->{$page}) {
491 if (isinternal($page)) {
492 push @internal_del, $pagesources{$page};
495 push @del, $pagesources{$page};
498 delete $typedlinks{$page};
499 $renderedfiles{$page}=[];
504 return \@del, \@internal_del;
508 foreach my $file (@_) {
509 my $page=pagename($file);
510 if (! isinternal($page)) {
511 debug(sprintf(gettext("removing obsolete %s"), $page));
514 foreach my $old (@{$oldrenderedfiles{$page}}) {
515 prune($config{destdir}."/".$old, $config{destdir});
518 foreach my $source (keys %destsources) {
519 if ($destsources{$source} eq $page) {
520 delete $destsources{$source};
524 delete $pagecase{lc $page};
525 $delpagesources{$page}=$pagesources{$page};
526 delete $pagesources{$page};
530 sub find_changed ($) {
533 my @internal_changed;
534 foreach my $file (@$files) {
535 my $page=pagename($file);
536 my ($srcfile, @stat)=srcfile_stat($file);
537 if (! exists $pagemtime{$page} ||
538 $stat[9] > $pagemtime{$page} ||
539 $forcerebuild{$page}) {
540 $pagemtime{$page}=$stat[9];
542 if (isinternal($page)) {
543 my $content = readfile($srcfile);
545 # Preprocess internal page in scan-only mode.
546 preprocess($page, $page, $content, 1);
548 run_hooks(scan => sub {
555 push @internal_changed, $file;
558 push @changed, $file;
562 return \@changed, \@internal_changed;
565 sub calculate_old_links ($$) {
566 my ($changed, $del)=@_;
568 foreach my $file (@$changed, @$del) {
569 my $page=pagename($file);
570 if (exists $oldlinks{$page}) {
571 foreach my $l (@{$oldlinks{$page}}) {
572 $oldlink_targets{$page}{$l}=bestlink($page, $l);
576 return \%oldlink_targets;
579 sub derender_internal ($) {
581 my $page=pagename($file);
582 delete $depends{$page};
583 delete $depends_simple{$page};
584 foreach my $old (@{$renderedfiles{$page}}) {
585 delete $destsources{$old};
587 $renderedfiles{$page}=[];
590 sub render_linkers ($) {
593 foreach my $page (keys %{$backlinks{$p}}) {
594 my $file=$pagesources{$page};
595 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
599 sub remove_unrendered () {
600 foreach my $src (keys %rendered) {
601 my $page=pagename($src);
602 foreach my $file (@{$oldrenderedfiles{$page}}) {
603 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
604 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
605 prune($config{destdir}."/".$file, $config{destdir});
611 sub link_types_changed ($$) {
612 # each is of the form { type => { link => 1 } }
616 return 0 if !defined $new && !defined $old;
617 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
619 while (my ($type, $links) = each %$new) {
620 foreach my $link (keys %$links) {
621 return 1 unless exists $old->{$type}{$link};
625 while (my ($type, $links) = each %$old) {
626 foreach my $link (keys %$links) {
627 return 1 unless exists $new->{$type}{$link};
634 sub calculate_changed_links ($$$) {
635 my ($changed, $del, $oldlink_targets)=@_;
637 my (%backlinkchanged, %linkchangers);
639 foreach my $file (@$changed, @$del) {
640 my $page=pagename($file);
642 if (exists $links{$page}) {
643 foreach my $l (@{$links{$page}}) {
644 my $target=bestlink($page, $l);
645 if (! exists $oldlink_targets->{$page}{$l} ||
646 $target ne $oldlink_targets->{$page}{$l}) {
647 $backlinkchanged{$target}=1;
648 $linkchangers{lc($page)}=1;
650 delete $oldlink_targets->{$page}{$l};
653 if (exists $oldlink_targets->{$page} &&
654 %{$oldlink_targets->{$page}}) {
655 foreach my $target (values %{$oldlink_targets->{$page}}) {
656 $backlinkchanged{$target}=1;
658 $linkchangers{lc($page)}=1;
661 # we currently assume that changing the type of a link doesn't
663 if (!exists $linkchangers{lc($page)}) {
664 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
665 $linkchangers{lc($page)}=1;
670 return \%backlinkchanged, \%linkchangers;
673 sub render_dependent ($$$$$$$) {
674 my ($files, $new, $internal_new, $del, $internal_del,
675 $internal_changed, $linkchangers)=@_;
677 my @changed=(keys %rendered, @$del);
678 my @exists_changed=(@$new, @$del);
680 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
681 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
683 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
684 if ($rendered{$p} || grep { $_ eq $p } @$del) {
685 foreach my $f (@$files) {
686 next if $rendered{$f};
687 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
693 foreach my $f (@$files) {
694 next if $rendered{$f};
698 if (exists $depends_simple{$p} && ! defined $reason) {
699 foreach my $d (keys %{$depends_simple{$p}}) {
700 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
703 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
704 $lc_exists_changed{$d})
706 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
715 if (exists $depends{$p} && ! defined $reason) {
716 foreach my $dep (keys %{$depends{$p}}) {
717 my $sub=pagespec_translate($dep);
718 next unless defined $sub;
720 # only consider internal files
721 # if the page explicitly depends
723 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
728 foreach my $file (@$list) {
730 my $page=pagename($file);
731 if ($sub->($page, location => $p)) {
732 if ($type == $IkiWiki::DEPEND_LINKS) {
733 next unless $linkchangers->{lc($page)};
742 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
743 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
744 last if $internal_dep && (
745 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
746 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
747 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
750 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
751 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
752 last if $internal_dep && (
753 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
754 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
757 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
758 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
759 last if $internal_dep && (
760 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
761 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
762 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
768 if (defined $reason) {
769 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
777 sub render_backlinks ($) {
778 my $backlinkchanged=shift;
779 foreach my $link (keys %$backlinkchanged) {
780 my $linkfile=$pagesources{$link};
781 if (defined $linkfile) {
782 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
787 sub gen_autofile ($$$) {
792 if (file_pruned($autofile)) {
796 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
797 if (! defined $file) {
801 # Remember autofiles that were tried, and never try them again later.
802 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
805 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
807 if (srcfile($autofile, 1) || file_pruned($autofile)) {
811 if (-l $file || -d _ || -e _) {
815 my $page = pagename($file);
816 if ($pages->{$page}) {
820 if (grep { $_ eq $autofile } @$del) {
824 $autofiles{$autofile}{generator}->();
833 run_hooks(refresh => sub { shift->() });
834 my ($files, $pages, $new, $internal_new, $del, $internal_del, $changed, $internal_changed);
835 my $want_find_changes=$config{only_committed_changes} &&
836 exists $IkiWiki::hooks{rcs}{rcs_find_changes} &&
837 exists $IkiWiki::hooks{rcs}{rcs_get_current_rev};
838 if (! $config{rebuild} && $want_find_changes && defined $IkiWiki::lastrev && length $IkiWiki::lastrev) {
839 my ($changed_raw, $del_raw);
840 ($changed_raw, $del_raw, $IkiWiki::lastrev) = $IkiWiki::hooks{rcs}{rcs_find_changes}{call}->($IkiWiki::lastrev);
841 ($files, $pages)=process_changed_files($changed_raw, $del_raw);
844 ($files, $pages)=find_src_files();
846 if ($want_find_changes) {
847 if (! defined($IkiWiki::lastrev) || ! length $IkiWiki::lastrev) {
848 $IkiWiki::lastrev=$IkiWiki::hooks{rcs}{rcs_get_current_rev}{call}->();
851 ($new, $internal_new)=find_new_files($files);
852 ($del, $internal_del)=find_del_files($pages);
853 ($changed, $internal_changed)=find_changed($files);
855 run_hooks(needsbuild => sub {
856 my $ret=shift->($changed, [@$del, @$internal_del]);
857 if (ref $ret eq 'ARRAY' && $ret != $changed) {
858 if (! %existingfiles) {
859 foreach my $f (@$files) {
860 $existingfiles{$f}=1;
863 @$changed=grep $existingfiles{$_}, @$ret;
866 my $oldlink_targets=calculate_old_links($changed, $del);
868 foreach my $file (@$changed) {
872 foreach my $autofile (keys %autofiles) {
873 if (gen_autofile($autofile, $pages, $del)) {
874 push @{$files}, $autofile;
875 push @{$new}, $autofile if find_new_files([$autofile]);
876 push @{$changed}, $autofile if find_changed([$autofile]);
884 # At this point it becomes OK to start matching pagespecs.
885 $phase = PHASE_RENDER;
886 # Save some memory: we no longer need to keep track of which pages
890 remove_del(@$del, @$internal_del);
892 foreach my $file (@$changed) {
893 render($file, sprintf(gettext("building %s"), $file));
895 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
896 derender_internal($file);
899 run_hooks(build_affected => sub {
900 my %affected = shift->();
901 while (my ($page, $message) = each %affected) {
902 next unless exists $pagesources{$page};
903 render($pagesources{$page}, $message);
907 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
908 $del, $oldlink_targets);
910 foreach my $file (@$new, @$del) {
911 render_linkers($file);
914 if (@$changed || @$internal_changed ||
915 @$del || @$internal_del || @$internal_new) {
916 1 while render_dependent($files, $new, $internal_new,
917 $del, $internal_del, $internal_changed,
921 render_backlinks($backlinkchanged);
924 if (@$del || @$internal_del) {
925 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
928 run_hooks(rendered => sub { shift->(keys %rendered) });
929 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
931 my %all_changed = map { $_ => 1 }
932 @$new, @$changed, @$del,
933 @$internal_new, @$internal_changed, @$internal_del;
934 run_hooks(changes => sub { shift->(keys %all_changed) });
941 foreach my $page (keys %oldrenderedfiles) {
942 foreach my $file (@{$oldrenderedfiles{$page}}) {
943 prune($config{destdir}."/".$file, $config{destdir});
948 sub commandline_render () {
953 # This function behaves as though it's in the render phase;
954 # all other files are assumed to have been scanned last time.
955 $phase = PHASE_RENDER;
957 my $srcfile=possibly_foolish_untaint($config{render});
959 $file=~s/\Q$config{srcdir}\E\/?//;
961 my $type=pagetype($file);
962 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
963 my $content=readfile($srcfile);
964 my $page=pagename($file);
965 $pagesources{$page}=$file;
966 $content=filter($page, $page, $content);
967 $content=preprocess($page, $page, $content);
968 $content=linkify($page, $page, $content);
969 $content=htmlize($page, $page, $type, $content);
970 $pagemtime{$page}=(stat($srcfile))[9];
971 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
973 print genpage($page, $content);