9 my (%backlinks, %rendered);
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 {
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 # Preprocess in scan-only mode.
178 preprocess($page, $page, $content, 1);
180 run_hooks(scan => sub {
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, @_);
270 my $dir=dirname($file);
271 while ((! defined $up_to || $dir =~ m{^\Q$up_to\E\/}) && rmdir($dir)) {
276 sub srcdir_check () {
277 # security check, avoid following symlinks in the srcdir path by default
278 my $test=$config{srcdir};
279 while (length $test) {
280 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
281 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
283 unless ($test=~s/\/+$//) {
284 $test=dirname($test);
290 sub find_src_files () {
293 eval q{use File::Find};
298 my $origdir=getcwd();
299 my $abssrcdir=Cwd::abs_path($config{srcdir});
301 my ($page, $underlay);
303 my $file=decode_utf8($_);
305 return if -l $file || -d _;
307 return if ! length $file;
308 $page = pagename($file);
309 if (! exists $pagesources{$page} &&
310 file_pruned($file)) {
311 $File::Find::prune=1;
315 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
317 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
322 # avoid underlaydir override attacks; see security.mdwn
323 if (! -l "$abssrcdir/$f" && ! -e _) {
324 if (! $pages{$page}) {
333 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
339 chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
344 chdir($origdir) || die "chdir $origdir: $!";
347 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
353 chdir($origdir) || die "chdir: $!";
357 return \@files, \%pages;
360 sub find_new_files ($) {
367 foreach my $file (@$files) {
368 my $page=pagename($file);
370 if ($config{rcs} && $config{gettime} &&
371 -e "$config{srcdir}/$file") {
372 if (! $times_noted) {
373 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
378 my $ctime=rcs_getctime($file);
380 $pagectime{$page}=$ctime;
388 $mtime=rcs_getmtime($file);
394 utime($mtime, $mtime, "$config{srcdir}/$file");
398 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
399 # the page has changed its type
400 $forcerebuild{$page}=1;
402 $pagesources{$page}=$file;
403 if (! $pagemtime{$page}) {
404 if (isinternal($page)) {
405 push @internal_new, $file;
410 $pagecase{lc $page}=$page;
411 if (! exists $pagectime{$page}) {
412 $pagectime{$page}=(srcfile_stat($file))[10];
417 return \@new, \@internal_new;
420 sub find_del_files ($) {
425 foreach my $page (keys %pagesources) {
426 if (! $pages->{$page}) {
427 if (isinternal($page)) {
428 push @internal_del, $pagesources{$page};
431 push @del, $pagesources{$page};
434 delete $typedlinks{$page};
435 $renderedfiles{$page}=[];
440 return \@del, \@internal_del;
444 foreach my $file (@_) {
445 my $page=pagename($file);
446 if (! isinternal($page)) {
447 debug(sprintf(gettext("removing obsolete %s"), $page));
450 foreach my $old (@{$oldrenderedfiles{$page}}) {
451 prune($config{destdir}."/".$old, $config{destdir});
454 foreach my $source (keys %destsources) {
455 if ($destsources{$source} eq $page) {
456 delete $destsources{$source};
460 delete $pagecase{lc $page};
461 $delpagesources{$page}=$pagesources{$page};
462 delete $pagesources{$page};
466 sub find_changed ($) {
469 my @internal_changed;
470 foreach my $file (@$files) {
471 my $page=pagename($file);
472 my ($srcfile, @stat)=srcfile_stat($file);
473 if (! exists $pagemtime{$page} ||
474 $stat[9] > $pagemtime{$page} ||
475 $forcerebuild{$page}) {
476 $pagemtime{$page}=$stat[9];
478 if (isinternal($page)) {
479 my $content = readfile($srcfile);
481 # Preprocess internal page in scan-only mode.
482 preprocess($page, $page, $content, 1);
484 run_hooks(scan => sub {
491 push @internal_changed, $file;
494 push @changed, $file;
498 return \@changed, \@internal_changed;
501 sub calculate_old_links ($$) {
502 my ($changed, $del)=@_;
504 foreach my $file (@$changed, @$del) {
505 my $page=pagename($file);
506 if (exists $oldlinks{$page}) {
507 foreach my $l (@{$oldlinks{$page}}) {
508 $oldlink_targets{$page}{$l}=bestlink($page, $l);
512 return \%oldlink_targets;
515 sub derender_internal ($) {
517 my $page=pagename($file);
518 delete $depends{$page};
519 delete $depends_simple{$page};
520 foreach my $old (@{$renderedfiles{$page}}) {
521 delete $destsources{$old};
523 $renderedfiles{$page}=[];
526 sub render_linkers ($) {
529 foreach my $page (keys %{$backlinks{$p}}) {
530 my $file=$pagesources{$page};
531 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
535 sub remove_unrendered () {
536 foreach my $src (keys %rendered) {
537 my $page=pagename($src);
538 foreach my $file (@{$oldrenderedfiles{$page}}) {
539 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
540 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
541 prune($config{destdir}."/".$file, $config{destdir});
547 sub link_types_changed ($$) {
548 # each is of the form { type => { link => 1 } }
552 return 0 if !defined $new && !defined $old;
553 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
555 while (my ($type, $links) = each %$new) {
556 foreach my $link (keys %$links) {
557 return 1 unless exists $old->{$type}{$link};
561 while (my ($type, $links) = each %$old) {
562 foreach my $link (keys %$links) {
563 return 1 unless exists $new->{$type}{$link};
570 sub calculate_changed_links ($$$) {
571 my ($changed, $del, $oldlink_targets)=@_;
573 my (%backlinkchanged, %linkchangers);
575 foreach my $file (@$changed, @$del) {
576 my $page=pagename($file);
578 if (exists $links{$page}) {
579 foreach my $l (@{$links{$page}}) {
580 my $target=bestlink($page, $l);
581 if (! exists $oldlink_targets->{$page}{$l} ||
582 $target ne $oldlink_targets->{$page}{$l}) {
583 $backlinkchanged{$target}=1;
584 $linkchangers{lc($page)}=1;
586 delete $oldlink_targets->{$page}{$l};
589 if (exists $oldlink_targets->{$page} &&
590 %{$oldlink_targets->{$page}}) {
591 foreach my $target (values %{$oldlink_targets->{$page}}) {
592 $backlinkchanged{$target}=1;
594 $linkchangers{lc($page)}=1;
597 # we currently assume that changing the type of a link doesn't
599 if (!exists $linkchangers{lc($page)}) {
600 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
601 $linkchangers{lc($page)}=1;
606 return \%backlinkchanged, \%linkchangers;
609 sub render_dependent ($$$$$$$) {
610 my ($files, $new, $internal_new, $del, $internal_del,
611 $internal_changed, $linkchangers)=@_;
613 my @changed=(keys %rendered, @$del);
614 my @exists_changed=(@$new, @$del);
616 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
617 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
619 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
620 if ($rendered{$p} || grep { $_ eq $p } @$del) {
621 foreach my $f (@$files) {
622 next if $rendered{$f};
623 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
629 foreach my $f (@$files) {
630 next if $rendered{$f};
634 if (exists $depends_simple{$p} && ! defined $reason) {
635 foreach my $d (keys %{$depends_simple{$p}}) {
636 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
639 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
640 $lc_exists_changed{$d})
642 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
651 if (exists $depends{$p} && ! defined $reason) {
652 foreach my $dep (keys %{$depends{$p}}) {
653 my $sub=pagespec_translate($dep);
654 next unless defined $sub;
656 # only consider internal files
657 # if the page explicitly depends
659 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
664 foreach my $file (@$list) {
666 my $page=pagename($file);
667 if ($sub->($page, location => $p)) {
668 if ($type == $IkiWiki::DEPEND_LINKS) {
669 next unless $linkchangers->{lc($page)};
678 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
679 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
680 last if $internal_dep && (
681 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
682 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
683 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
686 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
687 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
688 last if $internal_dep && (
689 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
690 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
693 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
694 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
695 last if $internal_dep && (
696 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
697 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
698 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
704 if (defined $reason) {
705 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
713 sub render_backlinks ($) {
714 my $backlinkchanged=shift;
715 foreach my $link (keys %$backlinkchanged) {
716 my $linkfile=$pagesources{$link};
717 if (defined $linkfile) {
718 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
723 sub gen_autofile ($$$) {
728 if (file_pruned($autofile)) {
732 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
733 if (! defined $file) {
737 # Remember autofiles that were tried, and never try them again later.
738 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
741 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
743 if (srcfile($autofile, 1) || file_pruned($autofile)) {
747 if (-l $file || -d _ || -e _) {
751 my $page = pagename($file);
752 if ($pages->{$page}) {
756 if (grep { $_ eq $autofile } @$del) {
760 $autofiles{$autofile}{generator}->();
768 run_hooks(refresh => sub { shift->() });
769 my ($files, $pages)=find_src_files();
770 my ($new, $internal_new)=find_new_files($files);
771 my ($del, $internal_del)=find_del_files($pages);
772 my ($changed, $internal_changed)=find_changed($files);
774 run_hooks(needsbuild => sub {
775 my $ret=shift->($changed, [@$del, @$internal_del]);
776 if (ref $ret eq 'ARRAY' && $ret != $changed) {
777 if (! %existingfiles) {
778 foreach my $f (@$files) {
779 $existingfiles{$f}=1;
782 @$changed=grep $existingfiles{$_}, @$ret;
785 my $oldlink_targets=calculate_old_links($changed, $del);
787 foreach my $file (@$changed) {
791 foreach my $autofile (keys %autofiles) {
792 if (gen_autofile($autofile, $pages, $del)) {
793 push @{$files}, $autofile;
794 push @{$new}, $autofile if find_new_files([$autofile]);
795 push @{$changed}, $autofile if find_changed([$autofile]);
803 remove_del(@$del, @$internal_del);
805 foreach my $file (@$changed) {
806 render($file, sprintf(gettext("building %s"), $file));
808 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
809 derender_internal($file);
812 run_hooks(build_affected => sub {
813 my %affected = shift->();
814 while (my ($page, $message) = each %affected) {
815 next unless exists $pagesources{$page};
816 render($pagesources{$page}, $message);
820 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
821 $del, $oldlink_targets);
823 foreach my $file (@$new, @$del) {
824 render_linkers($file);
827 if (@$changed || @$internal_changed ||
828 @$del || @$internal_del || @$internal_new) {
829 1 while render_dependent($files, $new, $internal_new,
830 $del, $internal_del, $internal_changed,
834 render_backlinks($backlinkchanged);
837 if (@$del || @$internal_del) {
838 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
841 run_hooks(rendered => sub { shift->(keys %rendered) });
842 run_hooks(change => sub { shift->(keys %rendered) }); # back-compat
844 my %all_changed = map { $_ => 1 }
845 @$new, @$changed, @$del,
846 @$internal_new, @$internal_changed, @$internal_del;
847 run_hooks(changes => sub { shift->(keys %all_changed) });
854 foreach my $page (keys %oldrenderedfiles) {
855 foreach my $file (@{$oldrenderedfiles{$page}}) {
856 prune($config{destdir}."/".$file, $config{destdir});
861 sub commandline_render () {
866 my $srcfile=possibly_foolish_untaint($config{render});
868 $file=~s/\Q$config{srcdir}\E\/?//;
870 my $type=pagetype($file);
871 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
872 my $content=readfile($srcfile);
873 my $page=pagename($file);
874 $pagesources{$page}=$file;
875 $content=filter($page, $page, $content);
876 $content=preprocess($page, $page, $content);
877 $content=linkify($page, $page, $content);
878 $content=htmlize($page, $page, $type, $content);
879 $pagemtime{$page}=(stat($srcfile))[9];
880 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
882 print genpage($page, $content);