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 verify_src_file ($$) {
285 my $file=decode_utf8(shift);
288 return if -l $file || -d _;
289 $file=~s/^\Q$dir\E\/?//;
290 return if ! length $file;
291 my $page = pagename($file);
292 if (! exists $pagesources{$page} &&
293 file_pruned($file)) {
294 $File::Find::prune=1;
298 my ($file_untainted) = $file =~ /$config{wiki_file_regexp}/; # untaint
299 if (! defined $file_untainted) {
300 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
302 return ($file_untainted, $page);
305 sub find_src_files () {
308 eval q{use File::Find};
313 my ($file, $page) = verify_src_file($_, $config{srcdir});
317 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
323 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
327 my ($file, $page) = verify_src_file($_, $dir);
329 # avoid underlaydir override
330 # attacks; see security.mdwn
331 if (! -l "$config{srcdir}/$file" &&
333 if (! $pages{$page}) {
342 return \@files, \%pages;
345 sub find_new_files ($) {
352 foreach my $file (@$files) {
353 my $page=pagename($file);
354 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
355 # the page has changed its type
356 $forcerebuild{$page}=1;
358 $pagesources{$page}=$file;
359 if (! $pagemtime{$page}) {
360 if (isinternal($page)) {
361 push @internal_new, $file;
363 elsif ($config{rcs}) {
364 if (! $times_noted) {
365 debug(sprintf(gettext("querying %s for file creation and modification times.."), $config{rcs}));
370 if ($config{gettime} && -e "$config{srcdir}/$file") {
372 my $ctime=rcs_getctime("$config{srcdir}/$file");
374 $pagectime{$page}=$ctime;
382 $mtime=rcs_getmtime("$config{srcdir}/$file");
388 utime($mtime, $mtime, "$config{srcdir}/$file");
392 $pagecase{lc $page}=$page;
393 if (! exists $pagectime{$page}) {
394 $pagectime{$page}=(srcfile_stat($file))[10];
399 return \@new, \@internal_new;
402 sub find_del_files ($) {
407 foreach my $page (keys %pagemtime) {
408 if (! $pages->{$page}) {
409 if (isinternal($page)) {
410 push @internal_del, $pagesources{$page};
413 push @del, $pagesources{$page};
416 delete $typedlinks{$page};
417 $renderedfiles{$page}=[];
422 return \@del, \@internal_del;
426 foreach my $file (@_) {
427 my $page=pagename($file);
428 if (! isinternal($page)) {
429 debug(sprintf(gettext("removing old page %s"), $page));
432 foreach my $old (@{$oldrenderedfiles{$page}}) {
433 prune($config{destdir}."/".$old);
436 foreach my $source (keys %destsources) {
437 if ($destsources{$source} eq $page) {
438 delete $destsources{$source};
442 delete $pagecase{lc $page};
443 delete $pagesources{$page};
447 sub find_changed ($) {
450 my @internal_changed;
451 foreach my $file (@$files) {
452 my $page=pagename($file);
453 my ($srcfile, @stat)=srcfile_stat($file);
454 if (! exists $pagemtime{$page} ||
455 $stat[9] > $pagemtime{$page} ||
456 $forcerebuild{$page}) {
457 $pagemtime{$page}=$stat[9];
459 if (isinternal($page)) {
460 # Preprocess internal page in scan-only mode.
461 preprocess($page, $page, readfile($srcfile), 1);
462 push @internal_changed, $file;
465 push @changed, $file;
469 return \@changed, \@internal_changed;
472 sub calculate_old_links ($$) {
473 my ($changed, $del)=@_;
475 foreach my $file (@$changed, @$del) {
476 my $page=pagename($file);
477 if (exists $oldlinks{$page}) {
478 foreach my $l (@{$oldlinks{$page}}) {
479 $oldlink_targets{$page}{$l}=bestlink($page, $l);
483 return \%oldlink_targets;
486 sub derender_internal ($) {
488 my $page=pagename($file);
489 delete $depends{$page};
490 delete $depends_simple{$page};
491 foreach my $old (@{$renderedfiles{$page}}) {
492 delete $destsources{$old};
494 $renderedfiles{$page}=[];
497 sub render_linkers ($) {
500 foreach my $page (keys %{$backlinks{$p}}) {
501 my $file=$pagesources{$page};
502 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
506 sub remove_unrendered () {
507 foreach my $src (keys %rendered) {
508 my $page=pagename($src);
509 foreach my $file (@{$oldrenderedfiles{$page}}) {
510 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
511 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
512 prune($config{destdir}."/".$file);
518 sub link_types_changed ($$) {
519 # each is of the form { type => { link => 1 } }
523 return 0 if !defined $new && !defined $old;
524 return 1 if !defined $new || !defined $old;
526 while (my ($type, $links) = each %$new) {
527 foreach my $link (keys %$links) {
528 return 1 unless exists $old->{$type}{$link};
532 while (my ($type, $links) = each %$old) {
533 foreach my $link (keys %$links) {
534 return 1 unless exists $new->{$type}{$link};
541 sub calculate_changed_links ($$$) {
542 my ($changed, $del, $oldlink_targets)=@_;
544 my (%backlinkchanged, %linkchangers);
546 foreach my $file (@$changed, @$del) {
547 my $page=pagename($file);
549 if (exists $links{$page}) {
550 foreach my $l (@{$links{$page}}) {
551 my $target=bestlink($page, $l);
552 if (! exists $oldlink_targets->{$page}{$l} ||
553 $target ne $oldlink_targets->{$page}{$l}) {
554 $backlinkchanged{$target}=1;
555 $linkchangers{lc($page)}=1;
557 delete $oldlink_targets->{$page}{$l};
560 if (exists $oldlink_targets->{$page} &&
561 %{$oldlink_targets->{$page}}) {
562 foreach my $target (values %{$oldlink_targets->{$page}}) {
563 $backlinkchanged{$target}=1;
565 $linkchangers{lc($page)}=1;
568 # we currently assume that changing the type of a link doesn't
570 if (!exists $linkchangers{lc($page)}) {
571 if (link_types_changed($typedlinks{$page}, $oldtypedlinks{$page})) {
572 $linkchangers{lc($page)}=1;
577 return \%backlinkchanged, \%linkchangers;
580 sub render_dependent ($$$$$$$) {
581 my ($files, $new, $internal_new, $del, $internal_del,
582 $internal_changed, $linkchangers)=@_;
584 my @changed=(keys %rendered, @$del);
585 my @exists_changed=(@$new, @$del);
587 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
588 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
590 foreach my $f (@$files) {
591 next if $rendered{$f};
595 if (exists $depends_simple{$p}) {
596 foreach my $d (keys %{$depends_simple{$p}}) {
597 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
600 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
601 $lc_exists_changed{$d})
603 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
612 if (exists $depends{$p} && ! defined $reason) {
613 foreach my $dep (keys %{$depends{$p}}) {
614 my $sub=pagespec_translate($dep);
615 next unless defined $sub;
617 # only consider internal files
618 # if the page explicitly depends
620 my $internal_dep=$dep =~ /internal\(/;
625 foreach my $file (@$list) {
627 my $page=pagename($file);
628 if ($sub->($page, location => $p)) {
629 if ($type == $IkiWiki::DEPEND_LINKS) {
630 next unless $linkchangers->{lc($page)};
638 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
640 $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
641 last if $internal_dep && ($reason =
642 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
643 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
644 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
646 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
648 $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
649 last if $internal_dep && ($reason =
650 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
651 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
653 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
655 $in->(\@changed, $IkiWiki::DEPEND_LINKS);
656 last if $internal_dep && ($reason =
657 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
658 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
659 $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
664 if (defined $reason) {
665 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
673 sub render_backlinks ($) {
674 my $backlinkchanged=shift;
675 foreach my $link (keys %$backlinkchanged) {
676 my $linkfile=$pagesources{$link};
677 if (defined $linkfile) {
678 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
685 run_hooks(refresh => sub { shift->() });
686 my ($files, $pages)=find_src_files();
687 my ($new, $internal_new)=find_new_files($files);
688 my ($del, $internal_del)=find_del_files($pages);
689 my ($changed, $internal_changed)=find_changed($files);
690 run_hooks(needsbuild => sub { shift->($changed) });
691 my $oldlink_targets=calculate_old_links($changed, $del);
692 %del_hash = map { $_ => 1 } @{$del};
694 foreach my $file (@$changed) {
698 while (my $autofile = shift @{[keys %autofiles]}) {
699 my $plugin=$autofiles{$autofile};
700 my $page=pagename($autofile);
701 if ($pages->{$page}) {
702 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
706 push @{$files}, $autofile;
707 push @{$new}, $autofile if find_new_files([$autofile]);
708 push @{$changed}, $autofile if find_changed([$autofile]);
711 delete $autofiles{$autofile};
716 remove_del(@$del, @$internal_del);
718 foreach my $file (@$changed) {
719 render($file, sprintf(gettext("building %s"), $file));
721 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
722 derender_internal($file);
725 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
726 $del, $oldlink_targets);
728 foreach my $file (@$new, @$del) {
729 render_linkers($file);
732 if (@$changed || @$internal_changed ||
733 @$del || @$internal_del || @$internal_new) {
734 1 while render_dependent($files, $new, $internal_new,
735 $del, $internal_del, $internal_changed,
739 render_backlinks($backlinkchanged);
743 run_hooks(delete => sub { shift->(@$del) });
746 run_hooks(change => sub { shift->(keys %rendered) });
754 foreach my $page (keys %oldrenderedfiles) {
755 foreach my $file (@{$oldrenderedfiles{$page}}) {
756 prune($config{destdir}."/".$file);
761 sub commandline_render () {
766 my $srcfile=possibly_foolish_untaint($config{render});
768 $file=~s/\Q$config{srcdir}\E\/?//;
770 my $type=pagetype($file);
771 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
772 my $content=readfile($srcfile);
773 my $page=pagename($file);
774 $pagesources{$page}=$file;
775 $content=filter($page, $page, $content);
776 $content=preprocess($page, $page, $content);
777 $content=linkify($page, $page, $content);
778 $content=htmlize($page, $page, $type, $content);
779 $pagemtime{$page}=(stat($srcfile))[9];
780 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
782 print genpage($page, $content);