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};
295 my ($page, $dir, $underlay);
297 my $file=decode_utf8($_);
299 return if -l $file || -d _;
300 $file=~s/^\Q$dir\E\/?//;
301 return if ! length $file;
302 $page = pagename($file);
303 if (! exists $pagesources{$page} &&
304 file_pruned($file)) {
305 $File::Find::prune=1;
309 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
311 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
316 # avoid underlaydir override attacks; see security.mdwn
317 if (! -l "$config{srcdir}/$f" && ! -e _) {
318 if (! $pages{$page}) {
327 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
336 }, $dir=$config{srcdir});
338 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
344 return \@files, \%pages;
347 sub find_new_files ($) {
354 foreach my $file (@$files) {
355 my $page=pagename($file);
356 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
357 # the page has changed its type
358 $forcerebuild{$page}=1;
360 $pagesources{$page}=$file;
361 if (! $pagemtime{$page}) {
362 if (isinternal($page)) {
363 push @internal_new, $file;
365 elsif ($config{rcs}) {
367 if ($config{gettime} && -e "$config{srcdir}/$file") {
368 if (! $times_noted) {
369 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
374 my $ctime=rcs_getctime("$config{srcdir}/$file");
376 $pagectime{$page}=$ctime;
384 $mtime=rcs_getmtime("$config{srcdir}/$file");
390 utime($mtime, $mtime, "$config{srcdir}/$file");
394 $pagecase{lc $page}=$page;
395 if (! exists $pagectime{$page}) {
396 $pagectime{$page}=(srcfile_stat($file))[10];
401 return \@new, \@internal_new;
404 sub find_del_files ($) {
409 foreach my $page (keys %pagesources) {
410 if (! $pages->{$page}) {
411 if (isinternal($page)) {
412 push @internal_del, $pagesources{$page};
415 push @del, $pagesources{$page};
418 delete $typedlinks{$page};
419 $renderedfiles{$page}=[];
424 return \@del, \@internal_del;
428 foreach my $file (@_) {
429 my $page=pagename($file);
430 if (! isinternal($page)) {
431 debug(sprintf(gettext("removing obsolete %s"), $page));
434 foreach my $old (@{$oldrenderedfiles{$page}}) {
435 prune($config{destdir}."/".$old);
438 foreach my $source (keys %destsources) {
439 if ($destsources{$source} eq $page) {
440 delete $destsources{$source};
444 delete $pagecase{lc $page};
445 delete $pagesources{$page};
449 sub find_changed ($) {
452 my @internal_changed;
453 foreach my $file (@$files) {
454 my $page=pagename($file);
455 my ($srcfile, @stat)=srcfile_stat($file);
456 if (! exists $pagemtime{$page} ||
457 $stat[9] > $pagemtime{$page} ||
458 $forcerebuild{$page}) {
459 $pagemtime{$page}=$stat[9];
461 if (isinternal($page)) {
462 # Preprocess internal page in scan-only mode.
463 preprocess($page, $page, readfile($srcfile), 1);
464 push @internal_changed, $file;
467 push @changed, $file;
471 return \@changed, \@internal_changed;
474 sub calculate_old_links ($$) {
475 my ($changed, $del)=@_;
477 foreach my $file (@$changed, @$del) {
478 my $page=pagename($file);
479 if (exists $oldlinks{$page}) {
480 foreach my $l (@{$oldlinks{$page}}) {
481 $oldlink_targets{$page}{$l}=bestlink($page, $l);
485 return \%oldlink_targets;
488 sub derender_internal ($) {
490 my $page=pagename($file);
491 delete $depends{$page};
492 delete $depends_simple{$page};
493 foreach my $old (@{$renderedfiles{$page}}) {
494 delete $destsources{$old};
496 $renderedfiles{$page}=[];
499 sub render_linkers ($) {
502 foreach my $page (keys %{$backlinks{$p}}) {
503 my $file=$pagesources{$page};
504 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
508 sub remove_unrendered () {
509 foreach my $src (keys %rendered) {
510 my $page=pagename($src);
511 foreach my $file (@{$oldrenderedfiles{$page}}) {
512 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
513 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
514 prune($config{destdir}."/".$file);
520 sub link_types_changed ($$) {
521 # each is of the form { type => { link => 1 } }
525 return 0 if !defined $new && !defined $old;
526 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
528 while (my ($type, $links) = each %$new) {
529 foreach my $link (keys %$links) {
530 return 1 unless exists $old->{$type}{$link};
534 while (my ($type, $links) = each %$old) {
535 foreach my $link (keys %$links) {
536 return 1 unless exists $new->{$type}{$link};
543 sub calculate_changed_links ($$$) {
544 my ($changed, $del, $oldlink_targets)=@_;
546 my (%backlinkchanged, %linkchangers);
548 foreach my $file (@$changed, @$del) {
549 my $page=pagename($file);
551 if (exists $links{$page}) {
552 foreach my $l (@{$links{$page}}) {
553 my $target=bestlink($page, $l);
554 if (! exists $oldlink_targets->{$page}{$l} ||
555 $target ne $oldlink_targets->{$page}{$l}) {
556 $backlinkchanged{$target}=1;
557 $linkchangers{lc($page)}=1;
559 delete $oldlink_targets->{$page}{$l};
562 if (exists $oldlink_targets->{$page} &&
563 %{$oldlink_targets->{$page}}) {
564 foreach my $target (values %{$oldlink_targets->{$page}}) {
565 $backlinkchanged{$target}=1;
567 $linkchangers{lc($page)}=1;
570 # we currently assume that changing the type of a link doesn't
572 if (!exists $linkchangers{lc($page)}) {
573 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
574 $linkchangers{lc($page)}=1;
579 return \%backlinkchanged, \%linkchangers;
582 sub render_dependent ($$$$$$$) {
583 my ($files, $new, $internal_new, $del, $internal_del,
584 $internal_changed, $linkchangers)=@_;
586 my @changed=(keys %rendered, @$del);
587 my @exists_changed=(@$new, @$del);
589 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
590 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
592 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
593 if ($rendered{$p} || grep { $_ eq $p } @$del) {
594 foreach my $f (@$files) {
595 next if $rendered{$f};
596 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
602 foreach my $f (@$files) {
603 next if $rendered{$f};
607 if (exists $depends_simple{$p} && ! defined $reason) {
608 foreach my $d (keys %{$depends_simple{$p}}) {
609 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
612 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
613 $lc_exists_changed{$d})
615 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
624 if (exists $depends{$p} && ! defined $reason) {
625 foreach my $dep (keys %{$depends{$p}}) {
626 my $sub=pagespec_translate($dep);
627 next unless defined $sub;
629 # only consider internal files
630 # if the page explicitly depends
632 my $internal_dep=$dep =~ /(?:internal|comment|comment_pending)\(/;
637 foreach my $file (@$list) {
639 my $page=pagename($file);
640 if ($sub->($page, location => $p)) {
641 if ($type == $IkiWiki::DEPEND_LINKS) {
642 next unless $linkchangers->{lc($page)};
651 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
652 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
653 last if $internal_dep && (
654 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
655 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
656 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
659 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
660 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
661 last if $internal_dep && (
662 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
663 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
666 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
667 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
668 last if $internal_dep && (
669 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
670 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
671 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
677 if (defined $reason) {
678 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
686 sub render_backlinks ($) {
687 my $backlinkchanged=shift;
688 foreach my $link (keys %$backlinkchanged) {
689 my $linkfile=$pagesources{$link};
690 if (defined $linkfile) {
691 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
696 sub gen_autofile ($$$) {
701 if (file_pruned($autofile)) {
705 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
706 if (! defined $file) {
710 # Remember autofiles that were tried, and never try them again later.
711 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
714 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
716 if (srcfile($autofile, 1) || file_pruned($autofile)) {
720 if (-l $file || -d _ || -e _) {
724 my $page = pagename($file);
725 if ($pages->{$page}) {
729 if (grep { $_ eq $autofile } @$del) {
733 $autofiles{$autofile}{generator}->();
741 run_hooks(refresh => sub { shift->() });
742 my ($files, $pages)=find_src_files();
743 my ($new, $internal_new)=find_new_files($files);
744 my ($del, $internal_del)=find_del_files($pages);
745 my ($changed, $internal_changed)=find_changed($files);
746 run_hooks(needsbuild => sub { shift->($changed) });
747 my $oldlink_targets=calculate_old_links($changed, $del);
749 foreach my $file (@$changed) {
753 foreach my $autofile (keys %autofiles) {
754 if (gen_autofile($autofile, $pages, $del)) {
755 push @{$files}, $autofile;
756 push @{$new}, $autofile if find_new_files([$autofile]);
757 push @{$changed}, $autofile if find_changed([$autofile]);
765 remove_del(@$del, @$internal_del);
767 foreach my $file (@$changed) {
768 render($file, sprintf(gettext("building %s"), $file));
770 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
771 derender_internal($file);
774 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
775 $del, $oldlink_targets);
777 foreach my $file (@$new, @$del) {
778 render_linkers($file);
781 if (@$changed || @$internal_changed ||
782 @$del || @$internal_del || @$internal_new) {
783 1 while render_dependent($files, $new, $internal_new,
784 $del, $internal_del, $internal_changed,
788 render_backlinks($backlinkchanged);
791 if (@$del || @$internal_del) {
792 run_hooks(delete => sub { shift->(@$del, @$internal_del) });
795 run_hooks(change => sub { shift->(keys %rendered) });
803 foreach my $page (keys %oldrenderedfiles) {
804 foreach my $file (@{$oldrenderedfiles{$page}}) {
805 prune($config{destdir}."/".$file);
810 sub commandline_render () {
815 my $srcfile=possibly_foolish_untaint($config{render});
817 $file=~s/\Q$config{srcdir}\E\/?//;
819 my $type=pagetype($file);
820 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
821 my $content=readfile($srcfile);
822 my $page=pagename($file);
823 $pagesources{$page}=$file;
824 $content=filter($page, $page, $content);
825 $content=preprocess($page, $page, $content);
826 $content=linkify($page, $page, $content);
827 $content=htmlize($page, $page, $type, $content);
828 $pagemtime{$page}=(stat($srcfile))[9];
829 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
831 print genpage($page, $content);