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)) {
77 my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
80 if (length $config{cgiurl}) {
81 if (IkiWiki->can("cgi_editpage")) {
82 $template->param(editurl => cgiurl(do => "edit", page => $page));
85 if (exists $hooks{auth}) {
86 $template->param(prefsurl => cgiurl(do => "prefs"));
91 if (defined $config{historyurl} && length $config{historyurl}) {
92 my $u=$config{historyurl};
93 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
94 $template->param(historyurl => $u);
97 if ($config{discussion}) {
98 if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
99 (length $config{cgiurl} ||
100 exists $links{$page."/".$config{discussionpage}})) {
101 $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
107 $template->param(have_actions => 1);
110 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
111 my ($backlinks, $more_backlinks);
112 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
113 $backlinks=\@backlinks;
117 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
118 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
122 title => $page eq 'index'
124 : pagetitle(basename($page)),
125 wikiname => $config{wikiname},
127 backlinks => $backlinks,
128 more_backlinks => $more_backlinks,
129 mtime => displaytime($pagemtime{$page}),
130 ctime => displaytime($pagectime{$page}),
131 baseurl => baseurl($page),
134 run_hooks(pagetemplate => sub {
135 shift->(page => $page, destpage => $page, template => $template);
138 $content=$template->output;
140 run_hooks(format => sub {
153 debug(sprintf(gettext("scanning %s"), $file));
155 my $type=pagetype($file);
157 my $srcfile=srcfile($file);
158 my $content=readfile($srcfile);
159 my $page=pagename($file);
160 will_render($page, htmlpage($page), 1);
162 if ($config{discussion}) {
163 # Discussion links are a special case since they're
164 # not in the text of the page, but on its template.
165 $links{$page}=[ $page."/".lc($config{discussionpage}) ];
170 delete $typedlinks{$page};
172 run_hooks(scan => sub {
179 # Preprocess in scan-only mode.
180 preprocess($page, $page, $content, 1);
183 will_render($file, $file, 1);
187 sub fast_file_copy (@) {
195 my ($len, $buf, $written);
196 while ($len = sysread $srcfd, $buf, $blksize) {
197 if (! defined $len) {
198 next if $! =~ /^Interrupted/;
199 error("failed to read $srcfile: $!", $cleanup);
203 defined($written = syswrite $destfd, $buf, $len, $offset)
204 or error("failed to write $destfile: $!", $cleanup);
213 return if $rendered{$file};
217 my $type=pagetype($file);
218 my $srcfile=srcfile($file);
220 my $page=pagename($file);
221 delete $depends{$page};
222 delete $depends_simple{$page};
223 will_render($page, htmlpage($page), 1);
224 return if $type=~/^_/;
226 my $content=htmlize($page, $page, $type,
227 linkify($page, $page,
228 preprocess($page, $page,
230 readfile($srcfile)))));
232 my $output=htmlpage($page);
233 writefile($output, $config{destdir}, genpage($page, $content));
236 delete $depends{$file};
237 delete $depends_simple{$file};
238 will_render($file, $file, 1);
240 if ($config{hardlink}) {
241 # only hardlink if owned by same user
242 my @stat=stat($srcfile);
243 if ($stat[4] == $>) {
244 prep_writefile($file, $config{destdir});
245 unlink($config{destdir}."/".$file);
246 if (link($srcfile, $config{destdir}."/".$file)) {
250 # if hardlink fails, fall back to copying
253 my $srcfd=readfile($srcfile, 1, 1);
254 writefile($file, $config{destdir}, undef, 1, sub {
255 fast_file_copy($srcfile, $file, $srcfd, @_);
264 my $dir=dirname($file);
265 while (rmdir($dir)) {
270 sub srcdir_check () {
271 # security check, avoid following symlinks in the srcdir path by default
272 my $test=$config{srcdir};
273 while (length $test) {
274 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
275 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
277 unless ($test=~s/\/+$//) {
278 $test=dirname($test);
284 sub find_src_files () {
287 eval q{use File::Find};
292 my $file=decode_utf8($_);
293 $file=~s/^\Q$config{srcdir}\E\/?//;
294 return if -l $_ || -d _ || ! length $file;
295 my $page = pagename($file);
296 if (! exists $pagesources{$page} &&
297 file_pruned($file)) {
298 $File::Find::prune=1;
302 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
304 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
309 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
315 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
319 my $file=decode_utf8($_);
320 $file=~s/^\Q$dir\E\/?//;
321 return if -l $_ || -d _ || ! length $file;
322 my $page=pagename($file);
323 if (! exists $pagesources{$page} &&
324 file_pruned($file)) {
325 $File::Find::prune=1;
329 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
331 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
334 # avoid underlaydir override
335 # attacks; see security.mdwn
336 if (! -l "$config{srcdir}/$f" &&
338 if (! $pages{$page}) {
347 return \@files, \%pages;
350 sub find_new_files ($) {
355 foreach my $file (@$files) {
356 my $page=pagename($file);
357 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
358 # the page has changed its type
359 $forcerebuild{$page}=1;
361 $pagesources{$page}=$file;
362 if (! $pagemtime{$page}) {
363 if (isinternal($page)) {
364 push @internal_new, $file;
368 if ($config{gettime} && -e "$config{srcdir}/$file") {
370 my $ctime=rcs_getctime("$config{srcdir}/$file");
372 $pagectime{$page}=$ctime;
380 my $mtime=rcs_getmtime("$config{srcdir}/$file");
386 utime($mtime, $mtime, "$config{srcdir}/$file");
390 $pagecase{lc $page}=$page;
391 if (! exists $pagectime{$page}) {
392 $pagectime{$page}=(srcfile_stat($file))[10];
397 return \@new, \@internal_new;
400 sub find_del_files ($) {
405 foreach my $page (keys %pagemtime) {
406 if (! $pages->{$page}) {
407 if (isinternal($page)) {
408 push @internal_del, $pagesources{$page};
411 push @del, $pagesources{$page};
414 delete $typedlinks{$page};
415 $renderedfiles{$page}=[];
420 return \@del, \@internal_del;
424 foreach my $file (@_) {
425 my $page=pagename($file);
426 if (! isinternal($page)) {
427 debug(sprintf(gettext("removing old page %s"), $page));
430 foreach my $old (@{$oldrenderedfiles{$page}}) {
431 prune($config{destdir}."/".$old);
434 foreach my $source (keys %destsources) {
435 if ($destsources{$source} eq $page) {
436 delete $destsources{$source};
440 delete $pagecase{lc $page};
441 delete $pagesources{$page};
445 sub find_changed ($) {
448 my @internal_changed;
449 foreach my $file (@$files) {
450 my $page=pagename($file);
451 my ($srcfile, @stat)=srcfile_stat($file);
452 if (! exists $pagemtime{$page} ||
453 $stat[9] > $pagemtime{$page} ||
454 $forcerebuild{$page}) {
455 $pagemtime{$page}=$stat[9];
457 if (isinternal($page)) {
458 # Preprocess internal page in scan-only mode.
459 preprocess($page, $page, readfile($srcfile), 1);
460 push @internal_changed, $file;
463 push @changed, $file;
467 return \@changed, \@internal_changed;
470 sub calculate_old_links ($$) {
471 my ($changed, $del)=@_;
473 foreach my $file (@$changed, @$del) {
474 my $page=pagename($file);
475 if (exists $oldlinks{$page}) {
476 foreach my $l (@{$oldlinks{$page}}) {
477 $oldlink_targets{$page}{$l}=bestlink($page, $l);
481 return \%oldlink_targets;
484 sub derender_internal ($) {
486 my $page=pagename($file);
487 delete $depends{$page};
488 delete $depends_simple{$page};
489 foreach my $old (@{$renderedfiles{$page}}) {
490 delete $destsources{$old};
492 $renderedfiles{$page}=[];
495 sub render_linkers ($) {
498 foreach my $page (keys %{$backlinks{$p}}) {
499 my $file=$pagesources{$page};
500 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
504 sub remove_unrendered () {
505 foreach my $src (keys %rendered) {
506 my $page=pagename($src);
507 foreach my $file (@{$oldrenderedfiles{$page}}) {
508 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
509 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
510 prune($config{destdir}."/".$file);
516 sub link_types_changed ($$) {
517 # each is of the form { type => { link => 1 } }
521 return 0 if !defined $new && !defined $old;
522 return 1 if !defined $new || !defined $old;
524 while (my ($type, $links) = each %$new) {
525 foreach my $link (keys %$links) {
526 return 1 unless exists $old->{$type}{$link};
530 while (my ($type, $links) = each %$old) {
531 foreach my $link (keys %$links) {
532 return 1 unless exists $new->{$type}{$link};
539 sub calculate_changed_links ($$$) {
540 my ($changed, $del, $oldlink_targets)=@_;
542 my (%backlinkchanged, %linkchangers);
544 foreach my $file (@$changed, @$del) {
545 my $page=pagename($file);
547 if (exists $links{$page}) {
548 foreach my $l (@{$links{$page}}) {
549 my $target=bestlink($page, $l);
550 if (! exists $oldlink_targets->{$page}{$l} ||
551 $target ne $oldlink_targets->{$page}{$l}) {
552 $backlinkchanged{$target}=1;
553 $linkchangers{lc($page)}=1;
555 delete $oldlink_targets->{$page}{$l};
558 if (exists $oldlink_targets->{$page} &&
559 %{$oldlink_targets->{$page}}) {
560 foreach my $target (values %{$oldlink_targets->{$page}}) {
561 $backlinkchanged{$target}=1;
563 $linkchangers{lc($page)}=1;
566 # we currently assume that changing the type of a link doesn't
568 if (!exists $linkchangers{lc($page)}) {
569 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
570 $linkchangers{lc($page)}=1;
575 return \%backlinkchanged, \%linkchangers;
578 sub render_dependent ($$$$$$$) {
579 my ($files, $new, $internal_new, $del, $internal_del,
580 $internal_changed, $linkchangers)=@_;
582 my @changed=(keys %rendered, @$del);
583 my @exists_changed=(@$new, @$del);
585 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
586 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
588 foreach my $f (@$files) {
589 next if $rendered{$f};
593 if (exists $depends_simple{$p}) {
594 foreach my $d (keys %{$depends_simple{$p}}) {
595 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
598 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
599 $lc_exists_changed{$d})
601 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
610 if (exists $depends{$p} && ! defined $reason) {
611 foreach my $dep (keys %{$depends{$p}}) {
612 my $sub=pagespec_translate($dep);
613 next unless defined $sub;
615 # only consider internal files
616 # if the page explicitly depends
618 my $internal_dep=$dep =~ /internal\(/;
623 foreach my $file (@$list) {
625 my $page=pagename($file);
626 if ($sub->($page, location => $p)) {
627 if ($type == $IkiWiki::DEPEND_LINKS) {
628 next unless $linkchangers->{lc($page)};
636 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
638 $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
639 last if $internal_dep && ($reason =
640 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
641 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
642 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
644 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
646 $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
647 last if $internal_dep && ($reason =
648 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
649 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
651 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
653 $in->(\@changed, $IkiWiki::DEPEND_LINKS);
654 last if $internal_dep && ($reason =
655 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
656 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
657 $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
662 if (defined $reason) {
663 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
671 sub render_backlinks ($) {
672 my $backlinkchanged=shift;
673 foreach my $link (keys %$backlinkchanged) {
674 my $linkfile=$pagesources{$link};
675 if (defined $linkfile) {
676 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
683 run_hooks(refresh => sub { shift->() });
684 my ($files, $pages)=find_src_files();
685 my ($new, $internal_new)=find_new_files($files);
686 my ($del, $internal_del)=find_del_files($pages);
687 my ($changed, $internal_changed)=find_changed($files);
688 run_hooks(needsbuild => sub { shift->($changed) });
689 my $oldlink_targets=calculate_old_links($changed, $del);
691 foreach my $file (@$changed) {
697 remove_del(@$del, @$internal_del);
699 foreach my $file (@$changed) {
700 render($file, sprintf(gettext("building %s"), $file));
702 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
703 derender_internal($file);
706 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
707 $del, $oldlink_targets);
709 foreach my $file (@$new, @$del) {
710 render_linkers($file);
713 if (@$changed || @$internal_changed ||
714 @$del || @$internal_del || @$internal_new) {
715 1 while render_dependent($files, $new, $internal_new,
716 $del, $internal_del, $internal_changed,
720 render_backlinks($backlinkchanged);
724 run_hooks(delete => sub { shift->(@$del) });
727 run_hooks(change => sub { shift->(keys %rendered) });
735 foreach my $page (keys %oldrenderedfiles) {
736 foreach my $file (@{$oldrenderedfiles{$page}}) {
737 prune($config{destdir}."/".$file);
742 sub commandline_render () {
747 my $srcfile=possibly_foolish_untaint($config{render});
749 $file=~s/\Q$config{srcdir}\E\/?//;
751 my $type=pagetype($file);
752 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
753 my $content=readfile($srcfile);
754 my $page=pagename($file);
755 $pagesources{$page}=$file;
756 $content=filter($page, $page, $content);
757 $content=preprocess($page, $page, $content);
758 $content=linkify($page, $page, $content);
759 $content=htmlize($page, $page, $type, $content);
760 $pagemtime{$page}=(stat($srcfile))[9];
761 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
763 print genpage($page, $content);