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);
187 run_hooks(rescan => sub {
196 will_render($file, $file, 1);
200 sub fast_file_copy (@) {
208 my ($len, $buf, $written);
209 while ($len = sysread $srcfd, $buf, $blksize) {
210 if (! defined $len) {
211 next if $! =~ /^Interrupted/;
212 error("failed to read $srcfile: $!", $cleanup);
216 defined($written = syswrite $destfd, $buf, $len, $offset)
217 or error("failed to write $destfile: $!", $cleanup);
226 return if $rendered{$file};
230 my $type=pagetype($file);
231 my $srcfile=srcfile($file);
233 my $page=pagename($file);
234 delete $depends{$page};
235 delete $depends_simple{$page};
236 will_render($page, htmlpage($page), 1);
237 return if $type=~/^_/;
239 my $content=htmlize($page, $page, $type,
240 linkify($page, $page,
241 preprocess($page, $page,
243 readfile($srcfile)))));
245 my $output=htmlpage($page);
246 writefile($output, $config{destdir}, genpage($page, $content));
249 delete $depends{$file};
250 delete $depends_simple{$file};
251 will_render($file, $file, 1);
253 if ($config{hardlink}) {
254 # only hardlink if owned by same user
255 my @stat=stat($srcfile);
256 if ($stat[4] == $>) {
257 prep_writefile($file, $config{destdir});
258 unlink($config{destdir}."/".$file);
259 if (link($srcfile, $config{destdir}."/".$file)) {
263 # if hardlink fails, fall back to copying
266 my $srcfd=readfile($srcfile, 1, 1);
267 writefile($file, $config{destdir}, undef, 1, sub {
268 fast_file_copy($srcfile, $file, $srcfd, @_);
277 my $dir=dirname($file);
278 while (rmdir($dir)) {
283 sub srcdir_check () {
284 # security check, avoid following symlinks in the srcdir path by default
285 my $test=$config{srcdir};
286 while (length $test) {
287 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
288 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
290 unless ($test=~s/\/+$//) {
291 $test=dirname($test);
297 sub find_src_files () {
300 eval q{use File::Find};
305 my $origdir=getcwd();
306 my $abssrcdir=Cwd::abs_path($config{srcdir});
308 my ($page, $underlay);
310 my $file=decode_utf8($_);
312 return if -l $file || -d _;
314 return if ! length $file;
315 $page = pagename($file);
316 if (! exists $pagesources{$page} &&
317 file_pruned($file)) {
318 $File::Find::prune=1;
322 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
324 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
329 # avoid underlaydir override attacks; see security.mdwn
330 if (! -l "$abssrcdir/$f" && ! -e _) {
331 if (! $pages{$page}) {
340 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
346 chdir($config{srcdir}) || die "chdir $config{srcdir}: $!";
351 chdir($origdir) || die "chdir $origdir: $!";
354 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
360 chdir($origdir) || die "chdir: $!";
364 return \@files, \%pages;
367 sub find_new_files ($) {
374 foreach my $file (@$files) {
375 my $page=pagename($file);
377 if ($config{rcs} && $config{gettime} &&
378 -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($file);
387 $pagectime{$page}=$ctime;
395 $mtime=rcs_getmtime($file);
401 utime($mtime, $mtime, "$config{srcdir}/$file");
405 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
406 # the page has changed its type
407 $forcerebuild{$page}=1;
409 $pagesources{$page}=$file;
410 if (! $pagemtime{$page}) {
411 if (isinternal($page)) {
412 push @internal_new, $file;
417 $pagecase{lc $page}=$page;
418 if (! exists $pagectime{$page}) {
419 $pagectime{$page}=(srcfile_stat($file))[10];
424 return \@new, \@internal_new;
427 sub find_del_files ($) {
432 foreach my $page (keys %pagesources) {
433 if (! $pages->{$page}) {
434 if (isinternal($page)) {
435 push @internal_del, $pagesources{$page};
438 push @del, $pagesources{$page};
441 delete $typedlinks{$page};
442 $renderedfiles{$page}=[];
447 return \@del, \@internal_del;
451 foreach my $file (@_) {
452 my $page=pagename($file);
453 if (! isinternal($page)) {
454 debug(sprintf(gettext("removing obsolete %s"), $page));
457 foreach my $old (@{$oldrenderedfiles{$page}}) {
458 prune($config{destdir}."/".$old);
461 foreach my $source (keys %destsources) {
462 if ($destsources{$source} eq $page) {
463 delete $destsources{$source};
467 delete $pagecase{lc $page};
468 $delpagesources{$page}=$pagesources{$page};
469 delete $pagesources{$page};
473 sub find_changed ($) {
476 my @internal_changed;
477 foreach my $file (@$files) {
478 my $page=pagename($file);
479 my ($srcfile, @stat)=srcfile_stat($file);
480 if (! exists $pagemtime{$page} ||
481 $stat[9] > $pagemtime{$page} ||
482 $forcerebuild{$page}) {
483 $pagemtime{$page}=$stat[9];
485 if (isinternal($page)) {
486 # Preprocess internal page in scan-only mode.
487 preprocess($page, $page, readfile($srcfile), 1);
488 push @internal_changed, $file;
491 push @changed, $file;
495 return \@changed, \@internal_changed;
498 sub calculate_old_links ($$) {
499 my ($changed, $del)=@_;
501 foreach my $file (@$changed, @$del) {
502 my $page=pagename($file);
503 if (exists $oldlinks{$page}) {
504 foreach my $l (@{$oldlinks{$page}}) {
505 $oldlink_targets{$page}{$l}=bestlink($page, $l);
509 return \%oldlink_targets;
512 sub derender_internal ($) {
514 my $page=pagename($file);
515 delete $depends{$page};
516 delete $depends_simple{$page};
517 foreach my $old (@{$renderedfiles{$page}}) {
518 delete $destsources{$old};
520 $renderedfiles{$page}=[];
523 sub render_linkers ($) {
526 foreach my $page (keys %{$backlinks{$p}}) {
527 my $file=$pagesources{$page};
528 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
532 sub remove_unrendered () {
533 foreach my $src (keys %rendered) {
534 my $page=pagename($src);
535 foreach my $file (@{$oldrenderedfiles{$page}}) {
536 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
537 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
538 prune($config{destdir}."/".$file);
544 sub link_types_changed ($$) {
545 # each is of the form { type => { link => 1 } }
549 return 0 if !defined $new && !defined $old;
550 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
552 while (my ($type, $links) = each %$new) {
553 foreach my $link (keys %$links) {
554 return 1 unless exists $old->{$type}{$link};
558 while (my ($type, $links) = each %$old) {
559 foreach my $link (keys %$links) {
560 return 1 unless exists $new->{$type}{$link};
567 sub calculate_changed_links ($$$) {
568 my ($changed, $del, $oldlink_targets)=@_;
570 my (%backlinkchanged, %linkchangers);
572 foreach my $file (@$changed, @$del) {
573 my $page=pagename($file);
575 if (exists $links{$page}) {
576 foreach my $l (@{$links{$page}}) {
577 my $target=bestlink($page, $l);
578 if (! exists $oldlink_targets->{$page}{$l} ||
579 $target ne $oldlink_targets->{$page}{$l}) {
580 $backlinkchanged{$target}=1;
581 $linkchangers{lc($page)}=1;
583 delete $oldlink_targets->{$page}{$l};
586 if (exists $oldlink_targets->{$page} &&
587 %{$oldlink_targets->{$page}}) {
588 foreach my $target (values %{$oldlink_targets->{$page}}) {
589 $backlinkchanged{$target}=1;
591 $linkchangers{lc($page)}=1;
594 # we currently assume that changing the type of a link doesn't
596 if (!exists $linkchangers{lc($page)}) {
597 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
598 $linkchangers{lc($page)}=1;
603 return \%backlinkchanged, \%linkchangers;
606 sub render_dependent ($$$$$$$) {
607 my ($files, $new, $internal_new, $del, $internal_del,
608 $internal_changed, $linkchangers)=@_;
610 my @changed=(keys %rendered, @$del);
611 my @exists_changed=(@$new, @$del);
613 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
614 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
616 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
617 if ($rendered{$p} || grep { $_ eq $p } @$del) {
618 foreach my $f (@$files) {
619 next if $rendered{$f};
620 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
626 foreach my $f (@$files) {
627 next if $rendered{$f};
631 if (exists $depends_simple{$p} && ! defined $reason) {
632 foreach my $d (keys %{$depends_simple{$p}}) {
633 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
636 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
637 $lc_exists_changed{$d})
639 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
648 if (exists $depends{$p} && ! defined $reason) {
649 foreach my $dep (keys %{$depends{$p}}) {
650 my $sub=pagespec_translate($dep);
651 next unless defined $sub;
653 # only consider internal files
654 # if the page explicitly depends
656 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
661 foreach my $file (@$list) {
663 my $page=pagename($file);
664 if ($sub->($page, location => $p)) {
665 if ($type == $IkiWiki::DEPEND_LINKS) {
666 next unless $linkchangers->{lc($page)};
675 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
676 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
677 last if $internal_dep && (
678 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
679 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
680 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
683 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
684 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
685 last if $internal_dep && (
686 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
687 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
690 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
691 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
692 last if $internal_dep && (
693 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
694 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
695 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
701 if (defined $reason) {
702 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
710 sub render_backlinks ($) {
711 my $backlinkchanged=shift;
712 foreach my $link (keys %$backlinkchanged) {
713 my $linkfile=$pagesources{$link};
714 if (defined $linkfile) {
715 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
720 sub gen_autofile ($$$) {
725 if (file_pruned($autofile)) {
729 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
730 if (! defined $file) {
734 # Remember autofiles that were tried, and never try them again later.
735 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
738 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
740 if (srcfile($autofile, 1) || file_pruned($autofile)) {
744 if (-l $file || -d _ || -e _) {
748 my $page = pagename($file);
749 if ($pages->{$page}) {
753 if (grep { $_ eq $autofile } @$del) {
757 $autofiles{$autofile}{generator}->();
765 run_hooks(refresh => sub { shift->() });
766 my ($files, $pages)=find_src_files();
767 my ($new, $internal_new)=find_new_files($files);
768 my ($del, $internal_del)=find_del_files($pages);
769 my ($changed, $internal_changed)=find_changed($files);
770 run_hooks(needsbuild => sub { shift->($changed) });
771 my $oldlink_targets=calculate_old_links($changed, $del);
773 foreach my $file (@$changed) {
777 foreach my $autofile (keys %autofiles) {
778 if (gen_autofile($autofile, $pages, $del)) {
779 push @{$files}, $autofile;
780 push @{$new}, $autofile if find_new_files([$autofile]);
781 push @{$changed}, $autofile if find_changed([$autofile]);
789 remove_del(@$del, @$internal_del);
791 foreach my $file (@$changed) {
792 render($file, sprintf(gettext("building %s"), $file));
794 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
795 derender_internal($file);
798 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
799 $del, $oldlink_targets);
801 foreach my $file (@$new, @$del) {
802 render_linkers($file);
805 if (@$changed || @$internal_changed ||
806 @$del || @$internal_del || @$internal_new) {
807 1 while render_dependent($files, $new, $internal_new,
808 $del, $internal_del, $internal_changed,
812 render_backlinks($backlinkchanged);
815 if (@$del || @$internal_del) {
816 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
819 run_hooks(change => sub { shift->(keys %rendered) });
827 foreach my $page (keys %oldrenderedfiles) {
828 foreach my $file (@{$oldrenderedfiles{$page}}) {
829 prune($config{destdir}."/".$file);
834 sub commandline_render () {
839 my $srcfile=possibly_foolish_untaint($config{render});
841 $file=~s/\Q$config{srcdir}\E\/?//;
843 my $type=pagetype($file);
844 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
845 my $content=readfile($srcfile);
846 my $page=pagename($file);
847 $pagesources{$page}=$file;
848 $content=filter($page, $page, $content);
849 $content=preprocess($page, $page, $content);
850 $content=linkify($page, $page, $content);
851 $content=htmlize($page, $page, $type, $content);
852 $pagemtime{$page}=(stat($srcfile))[9];
853 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
855 print genpage($page, $content);