10 my (%backlinks, %rendered);
12 my $links_calculated=0;
14 sub calculate_links () {
15 return if $links_calculated;
16 %backlinks=%brokenlinks=();
17 foreach my $page (keys %links) {
18 foreach my $link (@{$links{$page}}) {
19 my $bestlink=bestlink($page, $link);
20 if (length $bestlink) {
21 $backlinks{$bestlink}{$page}=1
22 if $bestlink ne $page;
25 push @{$brokenlinks{$link}}, $page;
32 sub backlink_pages ($) {
37 return keys %{$backlinks{$page}};
44 foreach my $p (backlink_pages($page)) {
45 my $href=urlto($p, $page);
47 # Trim common dir prefixes from both pages.
49 my $page_trimmed=$page;
51 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
53 $p_trimmed=~s/^\Q$dir\E// &&
54 $page_trimmed=~s/^\Q$dir\E//;
56 push @links, { url => $href, page => pagetitle($p_trimmed) };
65 run_hooks(indexhtml => sub {
66 shift->(page => $page, destpage => $page, content => $content);
70 run_hooks(templatefile => sub {
71 return if defined $templatefile;
72 my $file=shift->(page => $page);
73 if (defined $file && defined template_file($file)) {
78 if (defined $templatefile) {
79 $template=template_depends($templatefile, $page,
83 # no explicit depends as special case
84 $template=template('page.tmpl',
89 if (length $config{cgiurl}) {
90 if (IkiWiki->can("cgi_editpage")) {
91 $template->param(editurl => cgiurl(do => "edit", page => $page));
95 if (defined $config{historyurl} && length $config{historyurl}) {
96 my $u=$config{historyurl};
97 $u=~s/\[\[file\]\]/$pagesources{$page}/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 {
158 debug(sprintf(gettext("scanning %s"), $file));
160 my $type=pagetype($file);
162 my $srcfile=srcfile($file);
163 my $content=readfile($srcfile);
164 my $page=pagename($file);
165 will_render($page, htmlpage($page), 1);
167 if ($config{discussion}) {
168 # Discussion links are a special case since they're
169 # not in the text of the page, but on its template.
170 $links{$page}=[ $page."/".lc($config{discussionpage}) ];
175 delete $typedlinks{$page};
177 run_hooks(scan => sub {
184 # Preprocess in scan-only mode.
185 preprocess($page, $page, $content, 1);
188 will_render($file, $file, 1);
192 sub fast_file_copy (@) {
200 my ($len, $buf, $written);
201 while ($len = sysread $srcfd, $buf, $blksize) {
202 if (! defined $len) {
203 next if $! =~ /^Interrupted/;
204 error("failed to read $srcfile: $!", $cleanup);
208 defined($written = syswrite $destfd, $buf, $len, $offset)
209 or error("failed to write $destfile: $!", $cleanup);
218 return if $rendered{$file};
222 my $type=pagetype($file);
223 my $srcfile=srcfile($file);
225 my $page=pagename($file);
226 delete $depends{$page};
227 delete $depends_simple{$page};
228 will_render($page, htmlpage($page), 1);
229 return if $type=~/^_/;
231 my $content=htmlize($page, $page, $type,
232 linkify($page, $page,
233 preprocess($page, $page,
235 readfile($srcfile)))));
237 my $output=htmlpage($page);
238 writefile($output, $config{destdir}, genpage($page, $content));
241 delete $depends{$file};
242 delete $depends_simple{$file};
243 will_render($file, $file, 1);
245 if ($config{hardlink}) {
246 # only hardlink if owned by same user
247 my @stat=stat($srcfile);
248 if ($stat[4] == $>) {
249 prep_writefile($file, $config{destdir});
250 unlink($config{destdir}."/".$file);
251 if (link($srcfile, $config{destdir}."/".$file)) {
255 # if hardlink fails, fall back to copying
258 my $srcfd=readfile($srcfile, 1, 1);
259 writefile($file, $config{destdir}, undef, 1, sub {
260 fast_file_copy($srcfile, $file, $srcfd, @_);
269 my $dir=dirname($file);
270 while (rmdir($dir)) {
275 sub srcdir_check () {
276 # security check, avoid following symlinks in the srcdir path by default
277 my $test=$config{srcdir};
278 while (length $test) {
279 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
280 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
282 unless ($test=~s/\/+$//) {
283 $test=dirname($test);
289 sub find_src_files () {
292 eval q{use File::Find};
297 my $origdir=getcwd();
298 my $abssrcdir=Cwd::abs_path($config{srcdir});
300 my ($page, $underlay);
302 my $file=decode_utf8($_);
304 return if -l $file || -d _;
306 return if ! length $file;
307 $page = pagename($file);
308 if (! exists $pagesources{$page} &&
309 file_pruned($file)) {
310 $File::Find::prune=1;
314 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
316 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
321 # avoid underlaydir override attacks; see security.mdwn
322 if (! -l "$abssrcdir/$f" && ! -e _) {
323 if (! $pages{$page}) {
332 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
338 chdir($config{srcdir}) || die "chdir: $!";
343 chdir($origdir) || die "chdir: $!";
346 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
347 chdir($_) || die "chdir: $!";
352 chdir($origdir) || die "chdir: $!";
355 return \@files, \%pages;
358 sub find_new_files ($) {
365 foreach my $file (@$files) {
366 my $page=pagename($file);
367 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
368 # the page has changed its type
369 $forcerebuild{$page}=1;
371 $pagesources{$page}=$file;
372 if (! $pagemtime{$page}) {
373 if (isinternal($page)) {
374 push @internal_new, $file;
376 elsif ($config{rcs}) {
378 if ($config{gettime} && -e "$config{srcdir}/$file") {
379 if (! $times_noted) {
380 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
385 my $ctime=rcs_getctime("$config{srcdir}/$file");
387 $pagectime{$page}=$ctime;
395 $mtime=rcs_getmtime("$config{srcdir}/$file");
401 utime($mtime, $mtime, "$config{srcdir}/$file");
405 $pagecase{lc $page}=$page;
406 if (! exists $pagectime{$page}) {
407 $pagectime{$page}=(srcfile_stat($file))[10];
412 return \@new, \@internal_new;
415 sub find_del_files ($) {
420 foreach my $page (keys %pagesources) {
421 if (! $pages->{$page}) {
422 if (isinternal($page)) {
423 push @internal_del, $pagesources{$page};
426 push @del, $pagesources{$page};
429 delete $typedlinks{$page};
430 $renderedfiles{$page}=[];
435 return \@del, \@internal_del;
439 foreach my $file (@_) {
440 my $page=pagename($file);
441 if (! isinternal($page)) {
442 debug(sprintf(gettext("removing obsolete %s"), $page));
445 foreach my $old (@{$oldrenderedfiles{$page}}) {
446 prune($config{destdir}."/".$old);
449 foreach my $source (keys %destsources) {
450 if ($destsources{$source} eq $page) {
451 delete $destsources{$source};
455 delete $pagecase{lc $page};
456 $delpagesources{$page}=$pagesources{$page};
457 delete $pagesources{$page};
461 sub find_changed ($) {
464 my @internal_changed;
465 foreach my $file (@$files) {
466 my $page=pagename($file);
467 my ($srcfile, @stat)=srcfile_stat($file);
468 if (! exists $pagemtime{$page} ||
469 $stat[9] > $pagemtime{$page} ||
470 $forcerebuild{$page}) {
471 $pagemtime{$page}=$stat[9];
473 if (isinternal($page)) {
474 # Preprocess internal page in scan-only mode.
475 preprocess($page, $page, readfile($srcfile), 1);
476 push @internal_changed, $file;
479 push @changed, $file;
483 return \@changed, \@internal_changed;
486 sub calculate_old_links ($$) {
487 my ($changed, $del)=@_;
489 foreach my $file (@$changed, @$del) {
490 my $page=pagename($file);
491 if (exists $oldlinks{$page}) {
492 foreach my $l (@{$oldlinks{$page}}) {
493 $oldlink_targets{$page}{$l}=bestlink($page, $l);
497 return \%oldlink_targets;
500 sub derender_internal ($) {
502 my $page=pagename($file);
503 delete $depends{$page};
504 delete $depends_simple{$page};
505 foreach my $old (@{$renderedfiles{$page}}) {
506 delete $destsources{$old};
508 $renderedfiles{$page}=[];
511 sub render_linkers ($) {
514 foreach my $page (keys %{$backlinks{$p}}) {
515 my $file=$pagesources{$page};
516 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
520 sub remove_unrendered () {
521 foreach my $src (keys %rendered) {
522 my $page=pagename($src);
523 foreach my $file (@{$oldrenderedfiles{$page}}) {
524 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
525 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
526 prune($config{destdir}."/".$file);
532 sub link_types_changed ($$) {
533 # each is of the form { type => { link => 1 } }
537 return 0 if !defined $new && !defined $old;
538 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
540 while (my ($type, $links) = each %$new) {
541 foreach my $link (keys %$links) {
542 return 1 unless exists $old->{$type}{$link};
546 while (my ($type, $links) = each %$old) {
547 foreach my $link (keys %$links) {
548 return 1 unless exists $new->{$type}{$link};
555 sub calculate_changed_links ($$$) {
556 my ($changed, $del, $oldlink_targets)=@_;
558 my (%backlinkchanged, %linkchangers);
560 foreach my $file (@$changed, @$del) {
561 my $page=pagename($file);
563 if (exists $links{$page}) {
564 foreach my $l (@{$links{$page}}) {
565 my $target=bestlink($page, $l);
566 if (! exists $oldlink_targets->{$page}{$l} ||
567 $target ne $oldlink_targets->{$page}{$l}) {
568 $backlinkchanged{$target}=1;
569 $linkchangers{lc($page)}=1;
571 delete $oldlink_targets->{$page}{$l};
574 if (exists $oldlink_targets->{$page} &&
575 %{$oldlink_targets->{$page}}) {
576 foreach my $target (values %{$oldlink_targets->{$page}}) {
577 $backlinkchanged{$target}=1;
579 $linkchangers{lc($page)}=1;
582 # we currently assume that changing the type of a link doesn't
584 if (!exists $linkchangers{lc($page)}) {
585 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
586 $linkchangers{lc($page)}=1;
591 return \%backlinkchanged, \%linkchangers;
594 sub render_dependent ($$$$$$$) {
595 my ($files, $new, $internal_new, $del, $internal_del,
596 $internal_changed, $linkchangers)=@_;
598 my @changed=(keys %rendered, @$del);
599 my @exists_changed=(@$new, @$del);
601 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
602 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
604 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
605 if ($rendered{$p} || grep { $_ eq $p } @$del) {
606 foreach my $f (@$files) {
607 next if $rendered{$f};
608 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
614 foreach my $f (@$files) {
615 next if $rendered{$f};
619 if (exists $depends_simple{$p} && ! defined $reason) {
620 foreach my $d (keys %{$depends_simple{$p}}) {
621 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
624 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
625 $lc_exists_changed{$d})
627 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
636 if (exists $depends{$p} && ! defined $reason) {
637 foreach my $dep (keys %{$depends{$p}}) {
638 my $sub=pagespec_translate($dep);
639 next unless defined $sub;
641 # only consider internal files
642 # if the page explicitly depends
644 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
649 foreach my $file (@$list) {
651 my $page=pagename($file);
652 if ($sub->($page, location => $p)) {
653 if ($type == $IkiWiki::DEPEND_LINKS) {
654 next unless $linkchangers->{lc($page)};
663 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
664 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
665 last if $internal_dep && (
666 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
667 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
668 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
671 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
672 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
673 last if $internal_dep && (
674 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
675 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
678 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
679 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
680 last if $internal_dep && (
681 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
682 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
683 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
689 if (defined $reason) {
690 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
698 sub render_backlinks ($) {
699 my $backlinkchanged=shift;
700 foreach my $link (keys %$backlinkchanged) {
701 my $linkfile=$pagesources{$link};
702 if (defined $linkfile) {
703 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
708 sub gen_autofile ($$$) {
713 if (file_pruned($autofile)) {
717 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
718 if (! defined $file) {
722 # Remember autofiles that were tried, and never try them again later.
723 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
726 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
728 if (srcfile($autofile, 1) || file_pruned($autofile)) {
732 if (-l $file || -d _ || -e _) {
736 my $page = pagename($file);
737 if ($pages->{$page}) {
741 if (grep { $_ eq $autofile } @$del) {
745 $autofiles{$autofile}{generator}->();
753 run_hooks(refresh => sub { shift->() });
754 my ($files, $pages)=find_src_files();
755 my ($new, $internal_new)=find_new_files($files);
756 my ($del, $internal_del)=find_del_files($pages);
757 my ($changed, $internal_changed)=find_changed($files);
758 run_hooks(needsbuild => sub { shift->($changed) });
759 my $oldlink_targets=calculate_old_links($changed, $del);
761 foreach my $file (@$changed) {
765 foreach my $autofile (keys %autofiles) {
766 if (gen_autofile($autofile, $pages, $del)) {
767 push @{$files}, $autofile;
768 push @{$new}, $autofile if find_new_files([$autofile]);
769 push @{$changed}, $autofile if find_changed([$autofile]);
777 remove_del(@$del, @$internal_del);
779 foreach my $file (@$changed) {
780 render($file, sprintf(gettext("building %s"), $file));
782 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
783 derender_internal($file);
786 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
787 $del, $oldlink_targets);
789 foreach my $file (@$new, @$del) {
790 render_linkers($file);
793 if (@$changed || @$internal_changed ||
794 @$del || @$internal_del || @$internal_new) {
795 1 while render_dependent($files, $new, $internal_new,
796 $del, $internal_del, $internal_changed,
800 render_backlinks($backlinkchanged);
803 if (@$del || @$internal_del) {
804 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
807 run_hooks(change => sub { shift->(keys %rendered) });
815 foreach my $page (keys %oldrenderedfiles) {
816 foreach my $file (@{$oldrenderedfiles{$page}}) {
817 prune($config{destdir}."/".$file);
822 sub commandline_render () {
827 my $srcfile=possibly_foolish_untaint($config{render});
829 $file=~s/\Q$config{srcdir}\E\/?//;
831 my $type=pagetype($file);
832 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
833 my $content=readfile($srcfile);
834 my $page=pagename($file);
835 $pagesources{$page}=$file;
836 $content=filter($page, $page, $content);
837 $content=preprocess($page, $page, $content);
838 $content=linkify($page, $page, $content);
839 $content=htmlize($page, $page, $type, $content);
840 $pagemtime{$page}=(stat($srcfile))[9];
841 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
843 print genpage($page, $content);