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}),
140 baseurl => baseurl($page),
143 run_hooks(pagetemplate => sub {
144 shift->(page => $page, destpage => $page, template => $template);
147 $content=$template->output;
149 run_hooks(format => sub {
162 debug(sprintf(gettext("scanning %s"), $file));
164 my $type=pagetype($file);
166 my $srcfile=srcfile($file);
167 my $content=readfile($srcfile);
168 my $page=pagename($file);
169 will_render($page, htmlpage($page), 1);
171 if ($config{discussion}) {
172 # Discussion links are a special case since they're
173 # not in the text of the page, but on its template.
174 $links{$page}=[ $page."/".lc($config{discussionpage}) ];
179 delete $typedlinks{$page};
181 run_hooks(scan => sub {
188 # Preprocess in scan-only mode.
189 preprocess($page, $page, $content, 1);
192 will_render($file, $file, 1);
196 sub fast_file_copy (@) {
204 my ($len, $buf, $written);
205 while ($len = sysread $srcfd, $buf, $blksize) {
206 if (! defined $len) {
207 next if $! =~ /^Interrupted/;
208 error("failed to read $srcfile: $!", $cleanup);
212 defined($written = syswrite $destfd, $buf, $len, $offset)
213 or error("failed to write $destfile: $!", $cleanup);
222 return if $rendered{$file};
226 my $type=pagetype($file);
227 my $srcfile=srcfile($file);
229 my $page=pagename($file);
230 delete $depends{$page};
231 delete $depends_simple{$page};
232 will_render($page, htmlpage($page), 1);
233 return if $type=~/^_/;
235 my $content=htmlize($page, $page, $type,
236 linkify($page, $page,
237 preprocess($page, $page,
239 readfile($srcfile)))));
241 my $output=htmlpage($page);
242 writefile($output, $config{destdir}, genpage($page, $content));
245 delete $depends{$file};
246 delete $depends_simple{$file};
247 will_render($file, $file, 1);
249 if ($config{hardlink}) {
250 # only hardlink if owned by same user
251 my @stat=stat($srcfile);
252 if ($stat[4] == $>) {
253 prep_writefile($file, $config{destdir});
254 unlink($config{destdir}."/".$file);
255 if (link($srcfile, $config{destdir}."/".$file)) {
259 # if hardlink fails, fall back to copying
262 my $srcfd=readfile($srcfile, 1, 1);
263 writefile($file, $config{destdir}, undef, 1, sub {
264 fast_file_copy($srcfile, $file, $srcfd, @_);
273 my $dir=dirname($file);
274 while (rmdir($dir)) {
279 sub srcdir_check () {
280 # security check, avoid following symlinks in the srcdir path by default
281 my $test=$config{srcdir};
282 while (length $test) {
283 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
284 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
286 unless ($test=~s/\/+$//) {
287 $test=dirname($test);
293 sub find_src_files () {
296 eval q{use File::Find};
299 my ($page, $dir, $underlay);
301 my $file=decode_utf8($_);
303 return if -l $file || -d _;
304 $file=~s/^\Q$dir\E\/?//;
305 return if ! length $file;
306 $page = pagename($file);
307 if (! exists $pagesources{$page} &&
308 file_pruned($file)) {
309 $File::Find::prune=1;
313 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
315 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
319 # avoid underlaydir override attacks; see security.mdwn
320 if (! -l "$config{srcdir}/$f" && ! -e _) {
321 if (! $pages{$page}) {
330 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
339 }, $dir=$config{srcdir});
341 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
347 return \@files, \%pages;
350 sub find_new_files ($) {
357 foreach my $file (@$files) {
358 my $page=pagename($file);
359 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
360 # the page has changed its type
361 $forcerebuild{$page}=1;
363 $pagesources{$page}=$file;
364 if (! $pagemtime{$page}) {
365 if (isinternal($page)) {
366 push @internal_new, $file;
368 elsif ($config{rcs}) {
370 if ($config{gettime} && -e "$config{srcdir}/$file") {
371 if (! $times_noted) {
372 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
377 my $ctime=rcs_getctime("$config{srcdir}/$file");
379 $pagectime{$page}=$ctime;
387 $mtime=rcs_getmtime("$config{srcdir}/$file");
393 utime($mtime, $mtime, "$config{srcdir}/$file");
397 $pagecase{lc $page}=$page;
398 if (! exists $pagectime{$page}) {
399 $pagectime{$page}=(srcfile_stat($file))[10];
404 return \@new, \@internal_new;
407 sub find_del_files ($) {
412 foreach my $page (keys %pagemtime) {
413 if (! $pages->{$page}) {
414 if (isinternal($page)) {
415 push @internal_del, $pagesources{$page};
418 push @del, $pagesources{$page};
421 delete $typedlinks{$page};
422 $renderedfiles{$page}=[];
427 return \@del, \@internal_del;
431 foreach my $file (@_) {
432 my $page=pagename($file);
433 if (! isinternal($page)) {
434 debug(sprintf(gettext("removing obsolete %s"), $page));
437 foreach my $old (@{$oldrenderedfiles{$page}}) {
438 prune($config{destdir}."/".$old);
441 foreach my $source (keys %destsources) {
442 if ($destsources{$source} eq $page) {
443 delete $destsources{$source};
447 delete $pagecase{lc $page};
448 delete $pagesources{$page};
452 sub find_changed ($) {
455 my @internal_changed;
456 foreach my $file (@$files) {
457 my $page=pagename($file);
458 my ($srcfile, @stat)=srcfile_stat($file);
459 if (! exists $pagemtime{$page} ||
460 $stat[9] > $pagemtime{$page} ||
461 $forcerebuild{$page}) {
462 $pagemtime{$page}=$stat[9];
464 if (isinternal($page)) {
465 # Preprocess internal page in scan-only mode.
466 preprocess($page, $page, readfile($srcfile), 1);
467 push @internal_changed, $file;
470 push @changed, $file;
474 return \@changed, \@internal_changed;
477 sub calculate_old_links ($$) {
478 my ($changed, $del)=@_;
480 foreach my $file (@$changed, @$del) {
481 my $page=pagename($file);
482 if (exists $oldlinks{$page}) {
483 foreach my $l (@{$oldlinks{$page}}) {
484 $oldlink_targets{$page}{$l}=bestlink($page, $l);
488 return \%oldlink_targets;
491 sub derender_internal ($) {
493 my $page=pagename($file);
494 delete $depends{$page};
495 delete $depends_simple{$page};
496 foreach my $old (@{$renderedfiles{$page}}) {
497 delete $destsources{$old};
499 $renderedfiles{$page}=[];
502 sub render_linkers ($) {
505 foreach my $page (keys %{$backlinks{$p}}) {
506 my $file=$pagesources{$page};
507 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
511 sub remove_unrendered () {
512 foreach my $src (keys %rendered) {
513 my $page=pagename($src);
514 foreach my $file (@{$oldrenderedfiles{$page}}) {
515 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
516 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
517 prune($config{destdir}."/".$file);
523 sub link_types_changed ($$) {
524 # each is of the form { type => { link => 1 } }
528 return 0 if !defined $new && !defined $old;
529 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
531 while (my ($type, $links) = each %$new) {
532 foreach my $link (keys %$links) {
533 return 1 unless exists $old->{$type}{$link};
537 while (my ($type, $links) = each %$old) {
538 foreach my $link (keys %$links) {
539 return 1 unless exists $new->{$type}{$link};
546 sub calculate_changed_links ($$$) {
547 my ($changed, $del, $oldlink_targets)=@_;
549 my (%backlinkchanged, %linkchangers);
551 foreach my $file (@$changed, @$del) {
552 my $page=pagename($file);
554 if (exists $links{$page}) {
555 foreach my $l (@{$links{$page}}) {
556 my $target=bestlink($page, $l);
557 if (! exists $oldlink_targets->{$page}{$l} ||
558 $target ne $oldlink_targets->{$page}{$l}) {
559 $backlinkchanged{$target}=1;
560 $linkchangers{lc($page)}=1;
562 delete $oldlink_targets->{$page}{$l};
565 if (exists $oldlink_targets->{$page} &&
566 %{$oldlink_targets->{$page}}) {
567 foreach my $target (values %{$oldlink_targets->{$page}}) {
568 $backlinkchanged{$target}=1;
570 $linkchangers{lc($page)}=1;
573 # we currently assume that changing the type of a link doesn't
575 if (!exists $linkchangers{lc($page)}) {
576 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
577 $linkchangers{lc($page)}=1;
582 return \%backlinkchanged, \%linkchangers;
585 sub render_dependent ($$$$$$$) {
586 my ($files, $new, $internal_new, $del, $internal_del,
587 $internal_changed, $linkchangers)=@_;
589 my @changed=(keys %rendered, @$del);
590 my @exists_changed=(@$new, @$del);
592 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
593 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
595 foreach my $f (@$files) {
596 next if $rendered{$f};
600 if (exists $depends_simple{$p}) {
601 foreach my $d (keys %{$depends_simple{$p}}) {
602 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
605 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
606 $lc_exists_changed{$d})
608 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
617 if (exists $depends{$p} && ! defined $reason) {
618 foreach my $dep (keys %{$depends{$p}}) {
619 my $sub=pagespec_translate($dep);
620 next unless defined $sub;
622 # only consider internal files
623 # if the page explicitly depends
625 my $internal_dep=$dep =~ /internal\(/;
630 foreach my $file (@$list) {
632 my $page=pagename($file);
633 if ($sub->($page, location => $p)) {
634 if ($type == $IkiWiki::DEPEND_LINKS) {
635 next unless $linkchangers->{lc($page)};
644 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
645 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
646 last if $internal_dep && (
647 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
648 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
649 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
652 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
653 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
654 last if $internal_dep && (
655 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
656 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
659 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
660 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
661 last if $internal_dep && (
662 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
663 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
664 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
670 if (defined $reason) {
671 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
679 sub render_backlinks ($) {
680 my $backlinkchanged=shift;
681 foreach my $link (keys %$backlinkchanged) {
682 my $linkfile=$pagesources{$link};
683 if (defined $linkfile) {
684 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
689 sub gen_autofile ($$$) {
694 if (file_pruned($autofile)) {
698 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
699 if (! defined $file) {
703 # Remember autofiles that were tried, and never try them again later.
704 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
707 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
709 if (srcfile($autofile, 1) || file_pruned($autofile)) {
713 if (-l $file || -d _ || -e _) {
717 my $page = pagename($file);
718 if ($pages->{$page}) {
722 if (grep { $_ eq $autofile } @$del) {
726 $autofiles{$autofile}{generator}->();
734 run_hooks(refresh => sub { shift->() });
735 my ($files, $pages)=find_src_files();
736 my ($new, $internal_new)=find_new_files($files);
737 my ($del, $internal_del)=find_del_files($pages);
738 my ($changed, $internal_changed)=find_changed($files);
739 run_hooks(needsbuild => sub { shift->($changed) });
740 my $oldlink_targets=calculate_old_links($changed, $del);
742 foreach my $file (@$changed) {
746 foreach my $autofile (keys %autofiles) {
747 if (gen_autofile($autofile, $pages, $del)) {
748 push @{$files}, $autofile;
749 push @{$new}, $autofile if find_new_files([$autofile]);
750 push @{$changed}, $autofile if find_changed([$autofile]);
758 remove_del(@$del, @$internal_del);
760 foreach my $file (@$changed) {
761 render($file, sprintf(gettext("building %s"), $file));
763 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
764 derender_internal($file);
767 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
768 $del, $oldlink_targets);
770 foreach my $file (@$new, @$del) {
771 render_linkers($file);
774 if ($rendered{"templates/page.tmpl"}) {
775 foreach my $f (@$files) {
776 next if $f eq "templates/page.tmpl";
777 render($f, sprintf(gettext("building %s, which depends on %s"), $f, "templates/page.tmpl"));
780 elsif (@$changed || @$internal_changed ||
781 @$del || @$internal_del || @$internal_new) {
782 1 while render_dependent($files, $new, $internal_new,
783 $del, $internal_del, $internal_changed,
787 render_backlinks($backlinkchanged);
791 run_hooks(delete => sub { shift->(@$del) });
794 run_hooks(change => sub { shift->(keys %rendered) });
802 foreach my $page (keys %oldrenderedfiles) {
803 foreach my $file (@{$oldrenderedfiles{$page}}) {
804 prune($config{destdir}."/".$file);
809 sub commandline_render () {
814 my $srcfile=possibly_foolish_untaint($config{render});
816 $file=~s/\Q$config{srcdir}\E\/?//;
818 my $type=pagetype($file);
819 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
820 my $content=readfile($srcfile);
821 my $page=pagename($file);
822 $pagesources{$page}=$file;
823 $content=filter($page, $page, $content);
824 $content=preprocess($page, $page, $content);
825 $content=linkify($page, $page, $content);
826 $content=htmlize($page, $page, $type, $content);
827 $pagemtime{$page}=(stat($srcfile))[9];
828 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
830 print genpage($page, $content);