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)),
141 wikiname => $config{wikiname},
143 backlinks => $backlinks,
144 more_backlinks => $more_backlinks,
145 mtime => displaytime($pagemtime{$page}),
146 ctime => displaytime($pagectime{$page}, undef, 1),
147 baseurl => baseurl($page),
148 html5 => $config{html5},
151 run_hooks(pagetemplate => sub {
152 shift->(page => $page, destpage => $page, template => $template);
155 $content=$template->output;
157 run_hooks(format => sub {
170 debug(sprintf(gettext("scanning %s"), $file));
172 my $type=pagetype($file);
174 my $srcfile=srcfile($file);
175 my $content=readfile($srcfile);
176 my $page=pagename($file);
177 will_render($page, htmlpage($page), 1);
179 if ($config{discussion}) {
180 # Discussion links are a special case since they're
181 # not in the text of the page, but on its template.
182 $links{$page}=[ $page."/".lc($config{discussionpage}) ];
187 delete $typedlinks{$page};
189 run_hooks(scan => sub {
196 # Preprocess in scan-only mode.
197 preprocess($page, $page, $content, 1);
200 will_render($file, $file, 1);
204 sub fast_file_copy (@) {
212 my ($len, $buf, $written);
213 while ($len = sysread $srcfd, $buf, $blksize) {
214 if (! defined $len) {
215 next if $! =~ /^Interrupted/;
216 error("failed to read $srcfile: $!", $cleanup);
220 defined($written = syswrite $destfd, $buf, $len, $offset)
221 or error("failed to write $destfile: $!", $cleanup);
230 return if $rendered{$file};
234 my $type=pagetype($file);
235 my $srcfile=srcfile($file);
237 my $page=pagename($file);
238 delete $depends{$page};
239 delete $depends_simple{$page};
240 will_render($page, htmlpage($page), 1);
241 return if $type=~/^_/;
243 my $content=htmlize($page, $page, $type,
244 linkify($page, $page,
245 preprocess($page, $page,
247 readfile($srcfile)))));
249 my $output=htmlpage($page);
250 writefile($output, $config{destdir}, genpage($page, $content));
253 delete $depends{$file};
254 delete $depends_simple{$file};
255 will_render($file, $file, 1);
257 if ($config{hardlink}) {
258 # only hardlink if owned by same user
259 my @stat=stat($srcfile);
260 if ($stat[4] == $>) {
261 prep_writefile($file, $config{destdir});
262 unlink($config{destdir}."/".$file);
263 if (link($srcfile, $config{destdir}."/".$file)) {
267 # if hardlink fails, fall back to copying
270 my $srcfd=readfile($srcfile, 1, 1);
271 writefile($file, $config{destdir}, undef, 1, sub {
272 fast_file_copy($srcfile, $file, $srcfd, @_);
281 my $dir=dirname($file);
282 while (rmdir($dir)) {
287 sub srcdir_check () {
288 # security check, avoid following symlinks in the srcdir path by default
289 my $test=$config{srcdir};
290 while (length $test) {
291 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
292 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
294 unless ($test=~s/\/+$//) {
295 $test=dirname($test);
301 sub find_src_files () {
304 eval q{use File::Find};
307 my ($page, $dir, $underlay);
309 my $file=decode_utf8($_);
311 return if -l $file || -d _;
312 $file=~s/^\Q$dir\E\/?//;
313 return if ! length $file;
314 $page = pagename($file);
315 if (! exists $pagesources{$page} &&
316 file_pruned($file)) {
317 $File::Find::prune=1;
321 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
323 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
327 # avoid underlaydir override attacks; see security.mdwn
328 if (! -l "$config{srcdir}/$f" && ! -e _) {
329 if (! $pages{$page}) {
338 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
347 }, $dir=$config{srcdir});
349 foreach (@{$config{underlaydirs}}, $config{underlaydir}) {
355 return \@files, \%pages;
358 sub find_new_files ($) {
365 foreach my $file (@$files) {
366 my $page=pagename($file);
367 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
368 # the page has changed its type
369 $forcerebuild{$page}=1;
371 $pagesources{$page}=$file;
372 if (! $pagemtime{$page}) {
373 if (isinternal($page)) {
374 push @internal_new, $file;
376 elsif ($config{rcs}) {
378 if ($config{gettime} && -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("$config{srcdir}/$file");
387 $pagectime{$page}=$ctime;
395 $mtime=rcs_getmtime("$config{srcdir}/$file");
401 utime($mtime, $mtime, "$config{srcdir}/$file");
405 $pagecase{lc $page}=$page;
406 if (! exists $pagectime{$page}) {
407 $pagectime{$page}=(srcfile_stat($file))[10];
412 return \@new, \@internal_new;
415 sub find_del_files ($) {
420 foreach my $page (keys %pagesources) {
421 if (! $pages->{$page}) {
422 if (isinternal($page)) {
423 push @internal_del, $pagesources{$page};
426 push @del, $pagesources{$page};
429 delete $typedlinks{$page};
430 $renderedfiles{$page}=[];
435 return \@del, \@internal_del;
439 foreach my $file (@_) {
440 my $page=pagename($file);
441 if (! isinternal($page)) {
442 debug(sprintf(gettext("removing obsolete %s"), $page));
445 foreach my $old (@{$oldrenderedfiles{$page}}) {
446 prune($config{destdir}."/".$old);
449 foreach my $source (keys %destsources) {
450 if ($destsources{$source} eq $page) {
451 delete $destsources{$source};
455 delete $pagecase{lc $page};
456 delete $pagesources{$page};
460 sub find_changed ($) {
463 my @internal_changed;
464 foreach my $file (@$files) {
465 my $page=pagename($file);
466 my ($srcfile, @stat)=srcfile_stat($file);
467 if (! exists $pagemtime{$page} ||
468 $stat[9] > $pagemtime{$page} ||
469 $forcerebuild{$page}) {
470 $pagemtime{$page}=$stat[9];
472 if (isinternal($page)) {
473 # Preprocess internal page in scan-only mode.
474 preprocess($page, $page, readfile($srcfile), 1);
475 push @internal_changed, $file;
478 push @changed, $file;
482 return \@changed, \@internal_changed;
485 sub calculate_old_links ($$) {
486 my ($changed, $del)=@_;
488 foreach my $file (@$changed, @$del) {
489 my $page=pagename($file);
490 if (exists $oldlinks{$page}) {
491 foreach my $l (@{$oldlinks{$page}}) {
492 $oldlink_targets{$page}{$l}=bestlink($page, $l);
496 return \%oldlink_targets;
499 sub derender_internal ($) {
501 my $page=pagename($file);
502 delete $depends{$page};
503 delete $depends_simple{$page};
504 foreach my $old (@{$renderedfiles{$page}}) {
505 delete $destsources{$old};
507 $renderedfiles{$page}=[];
510 sub render_linkers ($) {
513 foreach my $page (keys %{$backlinks{$p}}) {
514 my $file=$pagesources{$page};
515 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
519 sub remove_unrendered () {
520 foreach my $src (keys %rendered) {
521 my $page=pagename($src);
522 foreach my $file (@{$oldrenderedfiles{$page}}) {
523 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
524 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
525 prune($config{destdir}."/".$file);
531 sub link_types_changed ($$) {
532 # each is of the form { type => { link => 1 } }
536 return 0 if !defined $new && !defined $old;
537 return 1 if (!defined $new && %$old) || (!defined $old && %$new);
539 while (my ($type, $links) = each %$new) {
540 foreach my $link (keys %$links) {
541 return 1 unless exists $old->{$type}{$link};
545 while (my ($type, $links) = each %$old) {
546 foreach my $link (keys %$links) {
547 return 1 unless exists $new->{$type}{$link};
554 sub calculate_changed_links ($$$) {
555 my ($changed, $del, $oldlink_targets)=@_;
557 my (%backlinkchanged, %linkchangers);
559 foreach my $file (@$changed, @$del) {
560 my $page=pagename($file);
562 if (exists $links{$page}) {
563 foreach my $l (@{$links{$page}}) {
564 my $target=bestlink($page, $l);
565 if (! exists $oldlink_targets->{$page}{$l} ||
566 $target ne $oldlink_targets->{$page}{$l}) {
567 $backlinkchanged{$target}=1;
568 $linkchangers{lc($page)}=1;
570 delete $oldlink_targets->{$page}{$l};
573 if (exists $oldlink_targets->{$page} &&
574 %{$oldlink_targets->{$page}}) {
575 foreach my $target (values %{$oldlink_targets->{$page}}) {
576 $backlinkchanged{$target}=1;
578 $linkchangers{lc($page)}=1;
581 # we currently assume that changing the type of a link doesn't
583 if (!exists $linkchangers{lc($page)}) {
584 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
585 $linkchangers{lc($page)}=1;
590 return \%backlinkchanged, \%linkchangers;
593 sub render_dependent ($$$$$$$) {
594 my ($files, $new, $internal_new, $del, $internal_del,
595 $internal_changed, $linkchangers)=@_;
597 my @changed=(keys %rendered, @$del);
598 my @exists_changed=(@$new, @$del);
600 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
601 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
603 foreach my $p ("templates/page.tmpl", keys %{$depends_simple{""}}) {
604 if ($rendered{$p} || grep { $_ eq $p } @$del) {
605 foreach my $f (@$files) {
606 next if $rendered{$f};
607 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $p));
613 foreach my $f (@$files) {
614 next if $rendered{$f};
618 if (exists $depends_simple{$p} && ! defined $reason) {
619 foreach my $d (keys %{$depends_simple{$p}}) {
620 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
623 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
624 $lc_exists_changed{$d})
626 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
635 if (exists $depends{$p} && ! defined $reason) {
636 foreach my $dep (keys %{$depends{$p}}) {
637 my $sub=pagespec_translate($dep);
638 next unless defined $sub;
640 # only consider internal files
641 # if the page explicitly depends
643 my $internal_dep=$dep =~ /internal\(/;
648 foreach my $file (@$list) {
650 my $page=pagename($file);
651 if ($sub->($page, location => $p)) {
652 if ($type == $IkiWiki::DEPEND_LINKS) {
653 next unless $linkchangers->{lc($page)};
662 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
663 last if $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
664 last if $internal_dep && (
665 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
666 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
667 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT)
670 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
671 last if $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
672 last if $internal_dep && (
673 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
674 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE)
677 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
678 last if $in->(\@changed, $IkiWiki::DEPEND_LINKS);
679 last if $internal_dep && (
680 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
681 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
682 $in->($internal_changed, $IkiWiki::DEPEND_LINKS)
688 if (defined $reason) {
689 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
697 sub render_backlinks ($) {
698 my $backlinkchanged=shift;
699 foreach my $link (keys %$backlinkchanged) {
700 my $linkfile=$pagesources{$link};
701 if (defined $linkfile) {
702 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
707 sub gen_autofile ($$$) {
712 if (file_pruned($autofile)) {
716 my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint
717 if (! defined $file) {
721 # Remember autofiles that were tried, and never try them again later.
722 if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) {
725 $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1;
727 if (srcfile($autofile, 1) || file_pruned($autofile)) {
731 if (-l $file || -d _ || -e _) {
735 my $page = pagename($file);
736 if ($pages->{$page}) {
740 if (grep { $_ eq $autofile } @$del) {
744 $autofiles{$autofile}{generator}->();
752 run_hooks(refresh => sub { shift->() });
753 my ($files, $pages)=find_src_files();
754 my ($new, $internal_new)=find_new_files($files);
755 my ($del, $internal_del)=find_del_files($pages);
756 my ($changed, $internal_changed)=find_changed($files);
757 run_hooks(needsbuild => sub { shift->($changed) });
758 my $oldlink_targets=calculate_old_links($changed, $del);
760 foreach my $file (@$changed) {
764 foreach my $autofile (keys %autofiles) {
765 if (gen_autofile($autofile, $pages, $del)) {
766 push @{$files}, $autofile;
767 push @{$new}, $autofile if find_new_files([$autofile]);
768 push @{$changed}, $autofile if find_changed([$autofile]);
776 remove_del(@$del, @$internal_del);
778 foreach my $file (@$changed) {
779 render($file, sprintf(gettext("building %s"), $file));
781 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
782 derender_internal($file);
785 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
786 $del, $oldlink_targets);
788 foreach my $file (@$new, @$del) {
789 render_linkers($file);
792 if (@$changed || @$internal_changed ||
793 @$del || @$internal_del || @$internal_new) {
794 1 while render_dependent($files, $new, $internal_new,
795 $del, $internal_del, $internal_changed,
799 render_backlinks($backlinkchanged);
803 run_hooks(delete => sub { shift->(@$del) });
806 run_hooks(change => sub { shift->(keys %rendered) });
814 foreach my $page (keys %oldrenderedfiles) {
815 foreach my $file (@{$oldrenderedfiles{$page}}) {
816 prune($config{destdir}."/".$file);
821 sub commandline_render () {
826 my $srcfile=possibly_foolish_untaint($config{render});
828 $file=~s/\Q$config{srcdir}\E\/?//;
830 my $type=pagetype($file);
831 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
832 my $content=readfile($srcfile);
833 my $page=pagename($file);
834 $pagesources{$page}=$file;
835 $content=filter($page, $page, $content);
836 $content=preprocess($page, $page, $content);
837 $content=linkify($page, $page, $content);
838 $content=htmlize($page, $page, $type, $content);
839 $pagemtime{$page}=(stat($srcfile))[9];
840 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
842 print genpage($page, $content);