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 run_hooks(pageactions => sub {
117 push @actions, map { { action => $_ } }
118 grep { defined } shift->(page => $page);
120 $template->param(actions => \@actions);
122 if ($actions || @actions) {
123 $template->param(have_actions => 1);
126 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
127 my ($backlinks, $more_backlinks);
128 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
129 $backlinks=\@backlinks;
133 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
134 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
138 title => $page eq 'index'
140 : pagetitle(basename($page)),
142 wikiname => $config{wikiname},
144 backlinks => $backlinks,
145 more_backlinks => $more_backlinks,
146 mtime => displaytime($pagemtime{$page}),
147 ctime => displaytime($pagectime{$page}, undef, 1),
148 baseurl => baseurl($page),
150 html5 => $config{html5},
153 run_hooks(pagetemplate => sub {
154 shift->(page => $page, destpage => $page, template => $template);
157 $content=$template->output;
159 run_hooks(format => sub {
172 debug(sprintf(gettext("scanning %s"), $file));
174 my $type=pagetype($file);
176 my $srcfile=srcfile($file);
177 my $content=readfile($srcfile);
178 my $page=pagename($file);
179 will_render($page, htmlpage($page), 1);
181 if ($config{discussion}) {
182 # Discussion links are a special case since they're
183 # not in the text of the page, but on its template.
184 $links{$page}=[ $page."/".lc($config{discussionpage}) ];
189 delete $typedlinks{$page};
191 run_hooks(scan => sub {
198 # Preprocess in scan-only mode.
199 preprocess($page, $page, $content, 1);
202 will_render($file, $file, 1);
206 sub fast_file_copy (@) {
214 my ($len, $buf, $written);
215 while ($len = sysread $srcfd, $buf, $blksize) {
216 if (! defined $len) {
217 next if $! =~ /^Interrupted/;
218 error("failed to read $srcfile: $!", $cleanup);
222 defined($written = syswrite $destfd, $buf, $len, $offset)
223 or error("failed to write $destfile: $!", $cleanup);
232 return if $rendered{$file};
236 my $type=pagetype($file);
237 my $srcfile=srcfile($file);
239 my $page=pagename($file);
240 delete $depends{$page};
241 delete $depends_simple{$page};
242 will_render($page, htmlpage($page), 1);
243 return if $type=~/^_/;
245 my $content=htmlize($page, $page, $type,
246 linkify($page, $page,
247 preprocess($page, $page,
249 readfile($srcfile)))));
251 my $output=htmlpage($page);
252 writefile($output, $config{destdir}, genpage($page, $content));
255 delete $depends{$file};
256 delete $depends_simple{$file};
257 will_render($file, $file, 1);
259 if ($config{hardlink}) {
260 # only hardlink if owned by same user
261 my @stat=stat($srcfile);
262 if ($stat[4] == $>) {
263 prep_writefile($file, $config{destdir});
264 unlink($config{destdir}."/".$file);
265 if (link($srcfile, $config{destdir}."/".$file)) {
269 # if hardlink fails, fall back to copying
272 my $srcfd=readfile($srcfile, 1, 1);
273 writefile($file, $config{destdir}, undef, 1, sub {
274 fast_file_copy($srcfile, $file, $srcfd, @_);
283 my $dir=dirname($file);
284 while (rmdir($dir)) {
289 sub srcdir_check () {
290 # security check, avoid following symlinks in the srcdir path by default
291 my $test=$config{srcdir};
292 while (length $test) {
293 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
294 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
296 unless ($test=~s/\/+$//) {
297 $test=dirname($test);
303 sub find_src_files () {
306 eval q{use File::Find};
309 my ($page, $dir, $underlay);
311 my $file=decode_utf8($_);
313 return if -l $file || -d _;
314 $file=~s/^\Q$dir\E\/?//;
315 return if ! length $file;
316 $page = pagename($file);
317 if (! exists $pagesources{$page} &&
318 file_pruned($file)) {
319 $File::Find::prune=1;
323 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
325 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
330 # avoid underlaydir override attacks; see security.mdwn
331 if (! -l "$config{srcdir}/$f" && ! -e _) {
332 if (! $pages{$page}) {
341 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
350 }, $dir=$config{srcdir});
352 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
358 return \@files, \%pages;
361 sub find_new_files ($) {
368 foreach my $file (@$files) {
369 my $page=pagename($file);
370 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
371 # the page has changed its type
372 $forcerebuild{$page}=1;
374 $pagesources{$page}=$file;
375 if (! $pagemtime{$page}) {
376 if (isinternal($page)) {
377 push @internal_new, $file;
379 elsif ($config{rcs}) {
381 if ($config{gettime} && -e "$config{srcdir}/$file") {
382 if (! $times_noted) {
383 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
388 my $ctime=rcs_getctime("$config{srcdir}/$file");
390 $pagectime{$page}=$ctime;
398 $mtime=rcs_getmtime("$config{srcdir}/$file");
404 utime($mtime, $mtime, "$config{srcdir}/$file");
408 $pagecase{lc $page}=$page;
409 if (! exists $pagectime{$page}) {
410 $pagectime{$page}=(srcfile_stat($file))[10];
415 return \@new, \@internal_new;
418 sub find_del_files ($) {
423 foreach my $page (keys %pagesources) {
424 if (! $pages->{$page}) {
425 if (isinternal($page)) {
426 push @internal_del, $pagesources{$page};
429 push @del, $pagesources{$page};
432 delete $typedlinks{$page};
433 $renderedfiles{$page}=[];
438 return \@del, \@internal_del;
442 foreach my $file (@_) {
443 my $page=pagename($file);
444 if (! isinternal($page)) {
445 debug(sprintf(gettext("removing obsolete %s"), $page));
448 foreach my $old (@{$oldrenderedfiles{$page}}) {
449 prune($config{destdir}."/".$old);
452 foreach my $source (keys %destsources) {
453 if ($destsources{$source} eq $page) {
454 delete $destsources{$source};
458 delete $pagecase{lc $page};
459 delete $pagesources{$page};
463 sub find_changed ($) {
466 my @internal_changed;
467 foreach my $file (@$files) {
468 my $page=pagename($file);
469 my ($srcfile, @stat)=srcfile_stat($file);
470 if (! exists $pagemtime{$page} ||
471 $stat[9] > $pagemtime{$page} ||
472 $forcerebuild{$page}) {
473 $pagemtime{$page}=$stat[9];
475 if (isinternal($page)) {
476 # Preprocess internal page in scan-only mode.
477 preprocess($page, $page, readfile($srcfile), 1);
478 push @internal_changed, $file;
481 push @changed, $file;
485 return \@changed, \@internal_changed;
488 sub calculate_old_links ($$) {
489 my ($changed, $del)=@_;
491 foreach my $file (@$changed, @$del) {
492 my $page=pagename($file);
493 if (exists $oldlinks{$page}) {
494 foreach my $l (@{$oldlinks{$page}}) {
495 $oldlink_targets{$page}{$l}=bestlink($page, $l);
499 return \%oldlink_targets;
502 sub derender_internal ($) {
504 my $page=pagename($file);
505 delete $depends{$page};
506 delete $depends_simple{$page};
507 foreach my $old (@{$renderedfiles{$page}}) {
508 delete $destsources{$old};
510 $renderedfiles{$page}=[];
513 sub render_linkers ($) {
516 foreach my $page (keys %{$backlinks{$p}}) {
517 my $file=$pagesources{$page};
518 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
522 sub remove_unrendered () {
523 foreach my $src (keys %rendered) {
524 my $page=pagename($src);
525 foreach my $file (@{$oldrenderedfiles{$page}}) {
526 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
527 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
528 prune($config{destdir}."/".$file);
534 sub link_types_changed ($$) {
535 # each is of the form { type => { link => 1 } }
539 return 0 if !defined $new && !defined $old;
540 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
542 while (my ($type, $links) = each %$new) {
543 foreach my $link (keys %$links) {
544 return 1 unless exists $old->{$type}{$link};
548 while (my ($type, $links) = each %$old) {
549 foreach my $link (keys %$links) {
550 return 1 unless exists $new->{$type}{$link};
557 sub calculate_changed_links ($$$) {
558 my ($changed, $del, $oldlink_targets)=@_;
560 my (%backlinkchanged, %linkchangers);
562 foreach my $file (@$changed, @$del) {
563 my $page=pagename($file);
565 if (exists $links{$page}) {
566 foreach my $l (@{$links{$page}}) {
567 my $target=bestlink($page, $l);
568 if (! exists $oldlink_targets->{$page}{$l} ||
569 $target ne $oldlink_targets->{$page}{$l}) {
570 $backlinkchanged{$target}=1;
571 $linkchangers{lc($page)}=1;
573 delete $oldlink_targets->{$page}{$l};
576 if (exists $oldlink_targets->{$page} &&
577 %{$oldlink_targets->{$page}}) {
578 foreach my $target (values %{$oldlink_targets->{$page}}) {
579 $backlinkchanged{$target}=1;
581 $linkchangers{lc($page)}=1;
584 # we currently assume that changing the type of a link doesn't
586 if (!exists $linkchangers{lc($page)}) {
587 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
588 $linkchangers{lc($page)}=1;
593 return \%backlinkchanged, \%linkchangers;
596 sub render_dependent ($$$$$$$) {
597 my ($files, $new, $internal_new, $del, $internal_del,
598 $internal_changed, $linkchangers)=@_;
600 my @changed=(keys %rendered, @$del);
601 my @exists_changed=(@$new, @$del);
603 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
604 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
606 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
607 if ($rendered{$p} || grep { $_ eq $p } @$del) {
608 foreach my $f (@$files) {
609 next if $rendered{$f};
610 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
616 foreach my $f (@$files) {
617 next if $rendered{$f};
621 if (exists $depends_simple{$p} && ! defined $reason) {
622 foreach my $d (keys %{$depends_simple{$p}}) {
623 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
626 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
627 $lc_exists_changed{$d})
629 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
638 if (exists $depends{$p} && ! defined $reason) {
639 foreach my $dep (keys %{$depends{$p}}) {
640 my $sub=pagespec_translate($dep);
641 next unless defined $sub;
643 # only consider internal files
644 # if the page explicitly depends
646 my $internal_dep=$dep =~ /internal\(/;
651 foreach my $file (@$list) {
653 my $page=pagename($file);
654 if ($sub->($page, location => $p)) {
655 if ($type == $IkiWiki::DEPEND_LINKS) {
656 next unless $linkchangers->{lc($page)};
665 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
666 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
667 last if $internal_dep && (
668 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
669 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
670 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
673 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
674 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
675 last if $internal_dep && (
676 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
677 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
680 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
681 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
682 last if $internal_dep && (
683 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
684 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
685 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
691 if (defined $reason) {
692 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
700 sub render_backlinks ($) {
701 my $backlinkchanged=shift;
702 foreach my $link (keys %$backlinkchanged) {
703 my $linkfile=$pagesources{$link};
704 if (defined $linkfile) {
705 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
710 sub gen_autofile ($$$) {
715 if (file_pruned($autofile)) {
719 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
720 if (! defined $file) {
724 # Remember autofiles that were tried, and never try them again later.
725 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
728 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
730 if (srcfile($autofile, 1) || file_pruned($autofile)) {
734 if (-l $file || -d _ || -e _) {
738 my $page = pagename($file);
739 if ($pages->{$page}) {
743 if (grep { $_ eq $autofile } @$del) {
747 $autofiles{$autofile}{generator}->();
755 run_hooks(refresh => sub { shift->() });
756 my ($files, $pages)=find_src_files();
757 my ($new, $internal_new)=find_new_files($files);
758 my ($del, $internal_del)=find_del_files($pages);
759 my ($changed, $internal_changed)=find_changed($files);
760 run_hooks(needsbuild => sub { shift->($changed) });
761 my $oldlink_targets=calculate_old_links($changed, $del);
763 foreach my $file (@$changed) {
767 foreach my $autofile (keys %autofiles) {
768 if (gen_autofile($autofile, $pages, $del)) {
769 push @{$files}, $autofile;
770 push @{$new}, $autofile if find_new_files([$autofile]);
771 push @{$changed}, $autofile if find_changed([$autofile]);
779 remove_del(@$del, @$internal_del);
781 foreach my $file (@$changed) {
782 render($file, sprintf(gettext("building %s"), $file));
784 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
785 derender_internal($file);
788 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
789 $del, $oldlink_targets);
791 foreach my $file (@$new, @$del) {
792 render_linkers($file);
795 if (@$changed || @$internal_changed ||
796 @$del || @$internal_del || @$internal_new) {
797 1 while render_dependent($files, $new, $internal_new,
798 $del, $internal_del, $internal_changed,
802 render_backlinks($backlinkchanged);
806 run_hooks(delete => sub { shift->(@$del) });
809 run_hooks(change => sub { shift->(keys %rendered) });
817 foreach my $page (keys %oldrenderedfiles) {
818 foreach my $file (@{$oldrenderedfiles{$page}}) {
819 prune($config{destdir}."/".$file);
824 sub commandline_render () {
829 my $srcfile=possibly_foolish_untaint($config{render});
831 $file=~s/\Q$config{srcdir}\E\/?//;
833 my $type=pagetype($file);
834 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
835 my $content=readfile($srcfile);
836 my $page=pagename($file);
837 $pagesources{$page}=$file;
838 $content=filter($page, $page, $content);
839 $content=preprocess($page, $page, $content);
840 $content=linkify($page, $page, $content);
841 $content=htmlize($page, $page, $type, $content);
842 $pagemtime{$page}=(stat($srcfile))[9];
843 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
845 print genpage($page, $content);