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 $template->param(editurl => cgiurl(do => "edit", page => $page))
82 if IkiWiki->can("cgi_editpage");
83 $template->param(prefsurl => cgiurl(do => "prefs"))
84 if exists $hooks{auth};
88 if (defined $config{historyurl} && length $config{historyurl}) {
89 my $u=$config{historyurl};
90 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
91 $template->param(historyurl => $u);
94 if ($config{discussion}) {
95 if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
96 (length $config{cgiurl} ||
97 exists $links{$page."/".$config{discussionpage}})) {
98 $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
104 $template->param(have_actions => 1);
107 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
108 my ($backlinks, $more_backlinks);
109 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
110 $backlinks=\@backlinks;
114 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
115 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
119 title => $page eq 'index'
121 : pagetitle(basename($page)),
122 wikiname => $config{wikiname},
124 backlinks => $backlinks,
125 more_backlinks => $more_backlinks,
126 mtime => displaytime($pagemtime{$page}),
127 ctime => displaytime($pagectime{$page}),
128 baseurl => baseurl($page),
131 run_hooks(pagetemplate => sub {
132 shift->(page => $page, destpage => $page, template => $template);
135 $content=$template->output;
137 run_hooks(format => sub {
150 debug(sprintf(gettext("scanning %s"), $file));
152 my $type=pagetype($file);
154 my $srcfile=srcfile($file);
155 my $content=readfile($srcfile);
156 my $page=pagename($file);
157 will_render($page, htmlpage($page), 1);
159 if ($config{discussion}) {
160 # Discussion links are a special case since they're
161 # not in the text of the page, but on its template.
162 $links{$page}=[ $page."/".lc($config{discussionpage}) ];
168 run_hooks(scan => sub {
175 # Preprocess in scan-only mode.
176 preprocess($page, $page, $content, 1);
179 will_render($file, $file, 1);
183 sub fast_file_copy (@) {
191 my ($len, $buf, $written);
192 while ($len = sysread $srcfd, $buf, $blksize) {
193 if (! defined $len) {
194 next if $! =~ /^Interrupted/;
195 error("failed to read $srcfile: $!", $cleanup);
199 defined($written = syswrite $destfd, $buf, $len, $offset)
200 or error("failed to write $destfile: $!", $cleanup);
209 return if $rendered{$file};
213 my $type=pagetype($file);
214 my $srcfile=srcfile($file);
216 my $page=pagename($file);
217 delete $depends{$page};
218 delete $depends_simple{$page};
219 will_render($page, htmlpage($page), 1);
220 return if $type=~/^_/;
222 my $content=htmlize($page, $page, $type,
223 linkify($page, $page,
224 preprocess($page, $page,
226 readfile($srcfile)))));
228 my $output=htmlpage($page);
229 writefile($output, $config{destdir}, genpage($page, $content));
232 delete $depends{$file};
233 delete $depends_simple{$file};
234 will_render($file, $file, 1);
236 if ($config{hardlink}) {
237 # only hardlink if owned by same user
238 my @stat=stat($srcfile);
239 if ($stat[4] == $>) {
240 prep_writefile($file, $config{destdir});
241 unlink($config{destdir}."/".$file);
242 if (link($srcfile, $config{destdir}."/".$file)) {
246 # if hardlink fails, fall back to copying
249 my $srcfd=readfile($srcfile, 1, 1);
250 writefile($file, $config{destdir}, undef, 1, sub {
251 fast_file_copy($srcfile, $file, $srcfd, @_);
260 my $dir=dirname($file);
261 while (rmdir($dir)) {
266 sub srcdir_check () {
267 # security check, avoid following symlinks in the srcdir path by default
268 my $test=$config{srcdir};
269 while (length $test) {
270 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
271 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
273 unless ($test=~s/\/+$//) {
274 $test=dirname($test);
280 sub find_src_files () {
283 eval q{use File::Find};
288 my $file=decode_utf8($_);
289 $file=~s/^\Q$config{srcdir}\E\/?//;
290 return if -l $_ || -d _ || ! length $file;
291 my $page = pagename($file);
292 if (! exists $pagesources{$page} &&
293 file_pruned($file)) {
294 $File::Find::prune=1;
298 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
300 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
305 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
311 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
315 my $file=decode_utf8($_);
316 $file=~s/^\Q$dir\E\/?//;
317 return if -l $_ || -d _ || ! length $file;
318 my $page=pagename($file);
319 if (! exists $pagesources{$page} &&
320 file_pruned($file)) {
321 $File::Find::prune=1;
325 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
327 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
330 # avoid underlaydir override
331 # attacks; see security.mdwn
332 if (! -l "$config{srcdir}/$f" &&
334 if (! $pages{$page}) {
343 return \@files, \%pages;
346 sub find_new_files ($) {
351 foreach my $file (@$files) {
352 my $page=pagename($file);
353 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
354 # the page has changed its type
355 $forcerebuild{$page}=1;
357 $pagesources{$page}=$file;
358 if (! $pagemtime{$page}) {
359 if (isinternal($page)) {
360 push @internal_new, $file;
364 if ($config{getctime} && -e "$config{srcdir}/$file") {
366 my $time=rcs_getctime("$config{srcdir}/$file");
367 $pagectime{$page}=$time;
374 $pagecase{lc $page}=$page;
375 if (! exists $pagectime{$page}) {
376 $pagectime{$page}=(srcfile_stat($file))[10];
381 return \@new, \@internal_new;
384 sub find_del_files ($) {
389 foreach my $page (keys %pagemtime) {
390 if (! $pages->{$page}) {
391 if (isinternal($page)) {
392 push @internal_del, $pagesources{$page};
395 debug(sprintf(gettext("removing old page %s"), $page));
396 push @del, $pagesources{$page};
399 $renderedfiles{$page}=[];
401 foreach my $old (@{$oldrenderedfiles{$page}}) {
402 prune($config{destdir}."/".$old);
404 delete $pagesources{$page};
405 foreach my $source (keys %destsources) {
406 if ($destsources{$source} eq $page) {
407 delete $destsources{$source};
413 return \@del, \@internal_del;
416 sub find_changed ($) {
419 my @internal_changed;
420 foreach my $file (@$files) {
421 my $page=pagename($file);
422 my ($srcfile, @stat)=srcfile_stat($file);
423 if (! exists $pagemtime{$page} ||
424 $stat[9] > $pagemtime{$page} ||
425 $forcerebuild{$page}) {
426 $pagemtime{$page}=$stat[9];
428 if (isinternal($page)) {
429 # Preprocess internal page in scan-only mode.
430 preprocess($page, $page, readfile($srcfile), 1);
431 push @internal_changed, $file;
434 push @changed, $file;
438 return \@changed, \@internal_changed;
441 sub calculate_old_links ($$) {
442 my ($changed, $del)=@_;
444 foreach my $file (@$changed, @$del) {
445 my $page=pagename($file);
446 if (exists $oldlinks{$page}) {
447 foreach my $l (@{$oldlinks{$page}}) {
448 $oldlink_targets{$page}{$l}=bestlink($page, $l);
452 return \%oldlink_targets;
455 sub derender_internal ($) {
457 my $page=pagename($file);
458 delete $depends{$page};
459 delete $depends_simple{$page};
460 foreach my $old (@{$renderedfiles{$page}}) {
461 delete $destsources{$old};
463 $renderedfiles{$page}=[];
466 sub render_linkers ($) {
469 foreach my $page (keys %{$backlinks{$p}}) {
470 my $file=$pagesources{$page};
471 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
475 sub remove_unrendered () {
476 foreach my $src (keys %rendered) {
477 my $page=pagename($src);
478 foreach my $file (@{$oldrenderedfiles{$page}}) {
479 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
480 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
481 prune($config{destdir}."/".$file);
487 sub calculate_changed_links ($$$) {
488 my ($changed, $del, $oldlink_targets)=@_;
490 my (%backlinkchanged, %linkchangers);
492 foreach my $file (@$changed, @$del) {
493 my $page=pagename($file);
495 if (exists $links{$page}) {
496 foreach my $l (@{$links{$page}}) {
497 my $target=bestlink($page, $l);
498 if (! exists $oldlink_targets->{$page}{$l} ||
499 $target ne $oldlink_targets->{$page}{$l}) {
500 $backlinkchanged{$target}=1;
501 $linkchangers{lc($page)}=1;
503 delete $oldlink_targets->{$page}{$l};
506 if (exists $oldlink_targets->{$page} &&
507 %{$oldlink_targets->{$page}}) {
508 foreach my $target (values %{$oldlink_targets->{$page}}) {
509 $backlinkchanged{$target}=1;
511 $linkchangers{lc($page)}=1;
515 return \%backlinkchanged, \%linkchangers;
518 sub render_dependent ($$$$$$$) {
519 my ($files, $new, $internal_new, $del, $internal_del,
520 $internal_changed, $linkchangers)=@_;
522 my @changed=(keys %rendered, @$del);
523 my @exists_changed=(@$new, @$del);
525 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
526 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
528 foreach my $f (@$files) {
529 next if $rendered{$f};
533 if (exists $depends_simple{$p}) {
534 foreach my $d (keys %{$depends_simple{$p}}) {
535 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
538 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
539 $lc_exists_changed{$d})
541 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
550 if (exists $depends{$p} && ! defined $reason) {
551 foreach my $dep (keys %{$depends{$p}}) {
552 my $sub=pagespec_translate($dep);
553 next if $@ || ! defined $sub;
555 # only consider internal files
556 # if the page explicitly depends
558 my $internal_dep=$dep =~ /internal\(/;
563 foreach my $file (@$list) {
565 my $page=pagename($file);
566 if ($sub->($page, location => $p)) {
567 if ($type == $IkiWiki::DEPEND_LINKS) {
568 next unless $linkchangers->{lc($page)};
576 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
578 $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
579 last if $internal_dep && ($reason =
580 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
581 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
582 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
584 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
586 $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
587 last if $internal_dep && ($reason =
588 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
589 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
591 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
593 $in->(\@changed, $IkiWiki::DEPEND_LINKS);
594 last if $internal_dep && ($reason =
595 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
596 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
597 $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
602 if (defined $reason) {
603 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
611 sub render_backlinks ($) {
612 my $backlinkchanged=shift;
613 foreach my $link (keys %$backlinkchanged) {
614 my $linkfile=$pagesources{$link};
615 if (defined $linkfile) {
616 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
623 run_hooks(refresh => sub { shift->() });
624 my ($files, $pages)=find_src_files();
625 my ($new, $internal_new)=find_new_files($files);
626 my ($del, $internal_del)=find_del_files($pages);
627 my ($changed, $internal_changed)=find_changed($files);
628 run_hooks(needsbuild => sub { shift->($changed) });
629 my $oldlink_targets=calculate_old_links($changed, $del);
631 foreach my $file (@$changed) {
637 foreach my $file (@$changed) {
638 render($file, sprintf(gettext("building %s"), $file));
640 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
641 derender_internal($file);
644 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
645 $del, $oldlink_targets);
647 foreach my $file (@$new, @$del) {
648 render_linkers($file);
651 if (@$changed || @$internal_changed ||
652 @$del || @$internal_del || @$internal_new) {
653 1 while render_dependent($files, $new, $internal_new,
654 $del, $internal_del, $internal_changed,
658 render_backlinks($backlinkchanged);
662 run_hooks(delete => sub { shift->(@$del) });
665 run_hooks(change => sub { shift->(keys %rendered) });
669 sub commandline_render () {
674 my $srcfile=possibly_foolish_untaint($config{render});
676 $file=~s/\Q$config{srcdir}\E\/?//;
678 my $type=pagetype($file);
679 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
680 my $content=readfile($srcfile);
681 my $page=pagename($file);
682 $pagesources{$page}=$file;
683 $content=filter($page, $page, $content);
684 $content=preprocess($page, $page, $content);
685 $content=linkify($page, $page, $content);
686 $content=htmlize($page, $page, $type, $content);
687 $pagemtime{$page}=(stat($srcfile))[9];
688 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
690 print genpage($page, $content);