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(postscan => sub {
66 shift->(page => $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));
94 if (exists $hooks{auth}) {
95 $template->param(prefsurl => cgiurl(do => "prefs"));
100 if (defined $config{historyurl} && length $config{historyurl}) {
101 my $u=$config{historyurl};
102 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
103 $template->param(historyurl => $u);
106 if ($config{discussion}) {
107 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
108 (length $config{cgiurl} ||
109 exists $links{$page."/".$config{discussionpage}})) {
110 $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
116 $template->param(have_actions => 1);
119 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
120 my ($backlinks, $more_backlinks);
121 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
122 $backlinks=\@backlinks;
126 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
127 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
131 title => $page eq 'index'
133 : pagetitle(basename($page)),
134 wikiname => $config{wikiname},
136 backlinks => $backlinks,
137 more_backlinks => $more_backlinks,
138 mtime => displaytime($pagemtime{$page}),
139 ctime => displaytime($pagectime{$page}, undef, 1),
140 baseurl => baseurl($page),
141 html5 => $config{html5},
144 run_hooks(pagetemplate => sub {
145 shift->(page => $page, destpage => $page, template => $template);
148 $content=$template->output;
150 run_hooks(format => sub {
163 debug(sprintf(gettext("scanning %s"), $file));
165 my $type=pagetype($file);
167 my $srcfile=srcfile($file);
168 my $content=readfile($srcfile);
169 my $page=pagename($file);
170 will_render($page, htmlpage($page), 1);
172 if ($config{discussion}) {
173 # Discussion links are a special case since they're
174 # not in the text of the page, but on its template.
175 $links{$page}=[ $page."/".lc($config{discussionpage}) ];
180 delete $typedlinks{$page};
182 run_hooks(scan => sub {
189 # Preprocess in scan-only mode.
190 preprocess($page, $page, $content, 1);
193 will_render($file, $file, 1);
197 sub fast_file_copy (@) {
205 my ($len, $buf, $written);
206 while ($len = sysread $srcfd, $buf, $blksize) {
207 if (! defined $len) {
208 next if $! =~ /^Interrupted/;
209 error("failed to read $srcfile: $!", $cleanup);
213 defined($written = syswrite $destfd, $buf, $len, $offset)
214 or error("failed to write $destfile: $!", $cleanup);
223 return if $rendered{$file};
227 my $type=pagetype($file);
228 my $srcfile=srcfile($file);
230 my $page=pagename($file);
231 delete $depends{$page};
232 delete $depends_simple{$page};
233 will_render($page, htmlpage($page), 1);
234 return if $type=~/^_/;
236 my $content=htmlize($page, $page, $type,
237 linkify($page, $page,
238 preprocess($page, $page,
240 readfile($srcfile)))));
242 my $output=htmlpage($page);
243 writefile($output, $config{destdir}, genpage($page, $content));
246 delete $depends{$file};
247 delete $depends_simple{$file};
248 will_render($file, $file, 1);
250 if ($config{hardlink}) {
251 # only hardlink if owned by same user
252 my @stat=stat($srcfile);
253 if ($stat[4] == $>) {
254 prep_writefile($file, $config{destdir});
255 unlink($config{destdir}."/".$file);
256 if (link($srcfile, $config{destdir}."/".$file)) {
260 # if hardlink fails, fall back to copying
263 my $srcfd=readfile($srcfile, 1, 1);
264 writefile($file, $config{destdir}, undef, 1, sub {
265 fast_file_copy($srcfile, $file, $srcfd, @_);
274 my $dir=dirname($file);
275 while (rmdir($dir)) {
280 sub srcdir_check () {
281 # security check, avoid following symlinks in the srcdir path by default
282 my $test=$config{srcdir};
283 while (length $test) {
284 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
285 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
287 unless ($test=~s/\/+$//) {
288 $test=dirname($test);
294 sub find_src_files () {
297 eval q{use File::Find};
300 my ($page, $dir, $underlay);
302 my $file=decode_utf8($_);
304 return if -l $file || -d _;
305 $file=~s/^\Q$dir\E\/?//;
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");
320 # avoid underlaydir override attacks; see security.mdwn
321 if (! -l "$config{srcdir}/$f" && ! -e _) {
322 if (! $pages{$page}) {
331 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
340 }, $dir=$config{srcdir});
342 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
348 return \@files, \%pages;
351 sub find_new_files ($) {
358 foreach my $file (@$files) {
359 my $page=pagename($file);
360 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
361 # the page has changed its type
362 $forcerebuild{$page}=1;
364 $pagesources{$page}=$file;
365 if (! $pagemtime{$page}) {
366 if (isinternal($page)) {
367 push @internal_new, $file;
369 elsif ($config{rcs}) {
371 if ($config{gettime} && -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("$config{srcdir}/$file");
380 $pagectime{$page}=$ctime;
388 $mtime=rcs_getmtime("$config{srcdir}/$file");
394 utime($mtime, $mtime, "$config{srcdir}/$file");
398 $pagecase{lc $page}=$page;
399 if (! exists $pagectime{$page}) {
400 $pagectime{$page}=(srcfile_stat($file))[10];
405 return \@new, \@internal_new;
408 sub find_del_files ($) {
413 foreach my $page (keys %pagesources) {
414 if (! $pages->{$page}) {
415 if (isinternal($page)) {
416 push @internal_del, $pagesources{$page};
419 push @del, $pagesources{$page};
422 delete $typedlinks{$page};
423 $renderedfiles{$page}=[];
428 return \@del, \@internal_del;
432 foreach my $file (@_) {
433 my $page=pagename($file);
434 if (! isinternal($page)) {
435 debug(sprintf(gettext("removing obsolete %s"), $page));
438 foreach my $old (@{$oldrenderedfiles{$page}}) {
439 prune($config{destdir}."/".$old);
442 foreach my $source (keys %destsources) {
443 if ($destsources{$source} eq $page) {
444 delete $destsources{$source};
448 delete $pagecase{lc $page};
449 delete $pagesources{$page};
453 sub find_changed ($) {
456 my @internal_changed;
457 foreach my $file (@$files) {
458 my $page=pagename($file);
459 my ($srcfile, @stat)=srcfile_stat($file);
460 if (! exists $pagemtime{$page} ||
461 $stat[9] > $pagemtime{$page} ||
462 $forcerebuild{$page}) {
463 $pagemtime{$page}=$stat[9];
465 if (isinternal($page)) {
466 # Preprocess internal page in scan-only mode.
467 preprocess($page, $page, readfile($srcfile), 1);
468 push @internal_changed, $file;
471 push @changed, $file;
475 return \@changed, \@internal_changed;
478 sub calculate_old_links ($$) {
479 my ($changed, $del)=@_;
481 foreach my $file (@$changed, @$del) {
482 my $page=pagename($file);
483 if (exists $oldlinks{$page}) {
484 foreach my $l (@{$oldlinks{$page}}) {
485 $oldlink_targets{$page}{$l}=bestlink($page, $l);
489 return \%oldlink_targets;
492 sub derender_internal ($) {
494 my $page=pagename($file);
495 delete $depends{$page};
496 delete $depends_simple{$page};
497 foreach my $old (@{$renderedfiles{$page}}) {
498 delete $destsources{$old};
500 $renderedfiles{$page}=[];
503 sub render_linkers ($) {
506 foreach my $page (keys %{$backlinks{$p}}) {
507 my $file=$pagesources{$page};
508 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
512 sub remove_unrendered () {
513 foreach my $src (keys %rendered) {
514 my $page=pagename($src);
515 foreach my $file (@{$oldrenderedfiles{$page}}) {
516 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
517 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
518 prune($config{destdir}."/".$file);
524 sub link_types_changed ($$) {
525 # each is of the form { type => { link => 1 } }
529 return 0 if !defined $new && !defined $old;
530 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
532 while (my ($type, $links) = each %$new) {
533 foreach my $link (keys %$links) {
534 return 1 unless exists $old->{$type}{$link};
538 while (my ($type, $links) = each %$old) {
539 foreach my $link (keys %$links) {
540 return 1 unless exists $new->{$type}{$link};
547 sub calculate_changed_links ($$$) {
548 my ($changed, $del, $oldlink_targets)=@_;
550 my (%backlinkchanged, %linkchangers);
552 foreach my $file (@$changed, @$del) {
553 my $page=pagename($file);
555 if (exists $links{$page}) {
556 foreach my $l (@{$links{$page}}) {
557 my $target=bestlink($page, $l);
558 if (! exists $oldlink_targets->{$page}{$l} ||
559 $target ne $oldlink_targets->{$page}{$l}) {
560 $backlinkchanged{$target}=1;
561 $linkchangers{lc($page)}=1;
563 delete $oldlink_targets->{$page}{$l};
566 if (exists $oldlink_targets->{$page} &&
567 %{$oldlink_targets->{$page}}) {
568 foreach my $target (values %{$oldlink_targets->{$page}}) {
569 $backlinkchanged{$target}=1;
571 $linkchangers{lc($page)}=1;
574 # we currently assume that changing the type of a link doesn't
576 if (!exists $linkchangers{lc($page)}) {
577 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
578 $linkchangers{lc($page)}=1;
583 return \%backlinkchanged, \%linkchangers;
586 sub render_dependent ($$$$$$$) {
587 my ($files, $new, $internal_new, $del, $internal_del,
588 $internal_changed, $linkchangers)=@_;
590 my @changed=(keys %rendered, @$del);
591 my @exists_changed=(@$new, @$del);
593 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
594 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
596 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
597 if ($rendered{$p} || grep { $_ eq $p } @$del) {
598 foreach my $f (@$files) {
599 next if $rendered{$f};
600 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
606 foreach my $f (@$files) {
607 next if $rendered{$f};
611 if (exists $depends_simple{$p} && ! defined $reason) {
612 foreach my $d (keys %{$depends_simple{$p}}) {
613 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
616 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
617 $lc_exists_changed{$d})
619 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
628 if (exists $depends{$p} && ! defined $reason) {
629 foreach my $dep (keys %{$depends{$p}}) {
630 my $sub=pagespec_translate($dep);
631 next unless defined $sub;
633 # only consider internal files
634 # if the page explicitly depends
636 my $internal_dep=$dep =~ /internal\(/;
641 foreach my $file (@$list) {
643 my $page=pagename($file);
644 if ($sub->($page, location => $p)) {
645 if ($type == $IkiWiki::DEPEND_LINKS) {
646 next unless $linkchangers->{lc($page)};
655 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
656 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
657 last if $internal_dep && (
658 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
659 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
660 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
663 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
664 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
665 last if $internal_dep && (
666 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
667 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
670 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
671 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
672 last if $internal_dep && (
673 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
674 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
675 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
681 if (defined $reason) {
682 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
690 sub render_backlinks ($) {
691 my $backlinkchanged=shift;
692 foreach my $link (keys %$backlinkchanged) {
693 my $linkfile=$pagesources{$link};
694 if (defined $linkfile) {
695 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
700 sub gen_autofile ($$$) {
705 if (file_pruned($autofile)) {
709 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
710 if (! defined $file) {
714 # Remember autofiles that were tried, and never try them again later.
715 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
718 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
720 if (srcfile($autofile, 1) || file_pruned($autofile)) {
724 if (-l $file || -d _ || -e _) {
728 my $page = pagename($file);
729 if ($pages->{$page}) {
733 if (grep { $_ eq $autofile } @$del) {
737 $autofiles{$autofile}{generator}->();
745 run_hooks(refresh => sub { shift->() });
746 my ($files, $pages)=find_src_files();
747 my ($new, $internal_new)=find_new_files($files);
748 my ($del, $internal_del)=find_del_files($pages);
749 my ($changed, $internal_changed)=find_changed($files);
750 run_hooks(needsbuild => sub { shift->($changed) });
751 my $oldlink_targets=calculate_old_links($changed, $del);
753 foreach my $file (@$changed) {
757 foreach my $autofile (keys %autofiles) {
758 if (gen_autofile($autofile, $pages, $del)) {
759 push @{$files}, $autofile;
760 push @{$new}, $autofile if find_new_files([$autofile]);
761 push @{$changed}, $autofile if find_changed([$autofile]);
769 remove_del(@$del, @$internal_del);
771 foreach my $file (@$changed) {
772 render($file, sprintf(gettext("building %s"), $file));
774 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
775 derender_internal($file);
778 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
779 $del, $oldlink_targets);
781 foreach my $file (@$new, @$del) {
782 render_linkers($file);
785 if (@$changed || @$internal_changed ||
786 @$del || @$internal_del || @$internal_new) {
787 1 while render_dependent($files, $new, $internal_new,
788 $del, $internal_del, $internal_changed,
792 render_backlinks($backlinkchanged);
796 run_hooks(delete => sub { shift->(@$del) });
799 run_hooks(change => sub { shift->(keys %rendered) });
807 foreach my $page (keys %oldrenderedfiles) {
808 foreach my $file (@{$oldrenderedfiles{$page}}) {
809 prune($config{destdir}."/".$file);
814 sub commandline_render () {
819 my $srcfile=possibly_foolish_untaint($config{render});
821 $file=~s/\Q$config{srcdir}\E\/?//;
823 my $type=pagetype($file);
824 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
825 my $content=readfile($srcfile);
826 my $page=pagename($file);
827 $pagesources{$page}=$file;
828 $content=filter($page, $page, $content);
829 $content=preprocess($page, $page, $content);
830 $content=linkify($page, $page, $content);
831 $content=htmlize($page, $page, $type, $content);
832 $pagemtime{$page}=(stat($srcfile))[9];
833 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
835 print genpage($page, $content);