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) };
66 run_hooks(templatefile => sub {
67 return if defined $templatefile;
68 my $file=shift->(page => $page);
69 if (defined $file && defined template_file($file)) {
73 my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
76 if (length $config{cgiurl}) {
77 $template->param(editurl => cgiurl(do => "edit", page => $page))
78 if IkiWiki->can("cgi_editpage");
79 $template->param(prefsurl => cgiurl(do => "prefs"))
80 if exists $hooks{auth};
84 if (defined $config{historyurl} && length $config{historyurl}) {
85 my $u=$config{historyurl};
86 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
87 $template->param(historyurl => $u);
90 if ($config{discussion}) {
91 if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
92 (length $config{cgiurl} ||
93 exists $links{$page."/".$config{discussionpage}})) {
94 $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
100 $template->param(have_actions => 1);
103 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
104 my ($backlinks, $more_backlinks);
105 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
106 $backlinks=\@backlinks;
110 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
111 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
115 title => $page eq 'index'
117 : pagetitle(basename($page)),
118 wikiname => $config{wikiname},
120 backlinks => $backlinks,
121 more_backlinks => $more_backlinks,
122 mtime => displaytime($pagemtime{$page}),
123 ctime => displaytime($pagectime{$page}),
124 baseurl => baseurl($page),
127 run_hooks(pagetemplate => sub {
128 shift->(page => $page, destpage => $page, template => $template);
131 $content=$template->output;
133 run_hooks(postscan => sub {
134 shift->(page => $page, content => $content);
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};
289 if (file_pruned($_, $config{srcdir})) {
290 $File::Find::prune=1;
292 elsif (! -l $_ && ! -d _) {
293 my ($f)=/$config{wiki_file_regexp}/; # untaint
295 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
298 $f=~s/^\Q$config{srcdir}\E\/?//;
300 my $page = pagename($f);
302 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
309 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
314 if (file_pruned($_, $dir)) {
315 $File::Find::prune=1;
317 elsif (! -l $_ && ! -d _) {
318 my ($f)=/$config{wiki_file_regexp}/; # untaint
320 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
323 $f=~s/^\Q$dir\E\/?//;
325 # override attacks; see
327 if (! -l "$config{srcdir}/$f" &&
329 my $page=pagename($f);
330 if (! $pages{$page}) {
340 return \@files, \%pages;
343 sub find_new_files ($) {
348 foreach my $file (@$files) {
349 my $page=pagename($file);
350 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
351 # the page has changed its type
352 $forcerebuild{$page}=1;
354 $pagesources{$page}=$file;
355 if (! $pagemtime{$page}) {
356 if (isinternal($page)) {
357 push @internal_new, $file;
361 if ($config{getctime} && -e "$config{srcdir}/$file") {
363 my $time=rcs_getctime("$config{srcdir}/$file");
364 $pagectime{$page}=$time;
371 $pagecase{lc $page}=$page;
372 if (! exists $pagectime{$page}) {
373 $pagectime{$page}=(srcfile_stat($file))[10];
378 return \@new, \@internal_new;
381 sub find_del_files ($) {
386 foreach my $page (keys %pagemtime) {
387 if (! $pages->{$page}) {
388 if (isinternal($page)) {
389 push @internal_del, $pagesources{$page};
392 debug(sprintf(gettext("removing old page %s"), $page));
393 push @del, $pagesources{$page};
396 $renderedfiles{$page}=[];
398 foreach my $old (@{$oldrenderedfiles{$page}}) {
399 prune($config{destdir}."/".$old);
401 delete $pagesources{$page};
402 foreach my $source (keys %destsources) {
403 if ($destsources{$source} eq $page) {
404 delete $destsources{$source};
410 return \@del, \@internal_del;
413 sub find_changed ($) {
416 my @internal_changed;
417 foreach my $file (@$files) {
418 my $page=pagename($file);
419 my ($srcfile, @stat)=srcfile_stat($file);
420 if (! exists $pagemtime{$page} ||
421 $stat[9] > $pagemtime{$page} ||
422 $forcerebuild{$page}) {
423 $pagemtime{$page}=$stat[9];
425 if (isinternal($page)) {
426 # Preprocess internal page in scan-only mode.
427 preprocess($page, $page, readfile($srcfile), 1);
428 push @internal_changed, $file;
431 push @changed, $file;
435 return \@changed, \@internal_changed;
438 sub calculate_old_links ($$) {
439 my ($changed, $del)=@_;
441 foreach my $file (@$changed, @$del) {
442 my $page=pagename($file);
443 if (exists $oldlinks{$page}) {
444 foreach my $l (@{$oldlinks{$page}}) {
445 $oldlink_targets{$page}{$l}=bestlink($page, $l);
449 return \%oldlink_targets;
452 sub derender_internal ($) {
454 my $page=pagename($file);
455 delete $depends{$page};
456 delete $depends_simple{$page};
457 foreach my $old (@{$renderedfiles{$page}}) {
458 delete $destsources{$old};
460 $renderedfiles{$page}=[];
463 sub render_linkers ($) {
466 foreach my $page (keys %{$backlinks{$p}}) {
467 my $file=$pagesources{$page};
468 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
472 sub remove_unrendered () {
473 foreach my $src (keys %rendered) {
474 my $page=pagename($src);
475 foreach my $file (@{$oldrenderedfiles{$page}}) {
476 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
477 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
478 prune($config{destdir}."/".$file);
484 sub calculate_changed_links ($$$) {
485 my ($changed, $del, $oldlink_targets)=@_;
487 my (%backlinkchanged, %linkchangers);
489 foreach my $file (@$changed, @$del) {
490 my $page=pagename($file);
492 if (exists $links{$page}) {
493 foreach my $l (@{$links{$page}}) {
494 my $target=bestlink($page, $l);
495 if (! exists $oldlink_targets->{$page}{$l} ||
496 $target ne $oldlink_targets->{$page}{$l}) {
497 $backlinkchanged{$l}=1;
498 $linkchangers{lc($page)}=1;
500 delete $oldlink_targets->{$page}{$l};
503 if (exists $oldlink_targets->{$page} &&
504 %{$oldlink_targets->{$page}}) {
505 foreach my $target (keys %{$oldlink_targets->{$page}}) {
506 $backlinkchanged{$target}=1;
508 $linkchangers{lc($page)}=1;
512 return \%backlinkchanged, \%linkchangers;
515 sub render_dependent ($$$$$$$) {
516 my ($files, $new, $internal_new, $del, $internal_del,
517 $internal_changed, $linkchangers)=@_;
519 my @changed=(keys %rendered, @$del);
520 my @exists_changed=(@$new, @$del);
522 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
523 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
525 foreach my $f (@$files) {
526 next if $rendered{$f};
530 if (exists $depends_simple{$p}) {
531 foreach my $d (keys %{$depends_simple{$p}}) {
532 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
535 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
536 $lc_exists_changed{$d})
538 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
547 if (exists $depends{$p} && ! defined $reason) {
548 D: foreach my $d (keys %{$depends{$p}}) {
549 my $sub=pagespec_translate($d);
550 next if $@ || ! defined $sub;
552 # only consider internal files
553 # if the page explicitly depends
555 my $internal_dep=$d =~ /internal\(/;
558 if ($depends{$p}{$d} & $IkiWiki::DEPEND_PRESENCE) {
559 @candidates=@exists_changed;
560 push @candidates, @$internal_new, @$internal_del
563 if (($depends{$p}{$d} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS))) {
564 @candidates=@changed;
565 push @candidates, @$internal_new, @$internal_del, @$internal_changed
569 foreach my $file (@candidates) {
571 my $page=pagename($file);
572 if ($sub->($page, location => $p)) {
573 if ($depends{$p}{$d} & $IkiWiki::DEPEND_LINKS) {
574 next unless $linkchangers->{lc($page)};
583 if (defined $reason) {
584 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
592 sub render_backlinks ($) {
593 my $backlinkchanged=shift;
594 foreach my $link (keys %$backlinkchanged) {
595 my $linkfile=$pagesources{$link};
596 if (defined $linkfile) {
597 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
604 run_hooks(refresh => sub { shift->() });
605 my ($files, $pages)=find_src_files();
606 my ($new, $internal_new)=find_new_files($files);
607 my ($del, $internal_del)=find_del_files($pages);
608 my ($changed, $internal_changed)=find_changed($files);
609 run_hooks(needsbuild => sub { shift->($changed) });
610 my $oldlink_targets=calculate_old_links($changed, $del);
612 foreach my $file (@$changed) {
618 foreach my $file (@$changed) {
619 render($file, sprintf(gettext("building %s"), $file));
621 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
622 derender_internal($file);
625 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
626 $del, $oldlink_targets);
628 foreach my $file (@$new, @$del) {
629 render_linkers($file);
632 if (@$changed || @$internal_changed ||
633 @$del || @$internal_del || @$internal_new) {
634 1 while render_dependent($files, $new, $internal_new,
635 $del, $internal_del, $internal_changed,
639 render_backlinks($backlinkchanged);
643 run_hooks(delete => sub { shift->(@$del) });
646 run_hooks(change => sub { shift->(keys %rendered) });
650 sub commandline_render () {
655 my $srcfile=possibly_foolish_untaint($config{render});
657 $file=~s/\Q$config{srcdir}\E\/?//;
659 my $type=pagetype($file);
660 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
661 my $content=readfile($srcfile);
662 my $page=pagename($file);
663 $pagesources{$page}=$file;
664 $content=filter($page, $page, $content);
665 $content=preprocess($page, $page, $content);
666 $content=linkify($page, $page, $content);
667 $content=htmlize($page, $page, $type, $content);
668 $pagemtime{$page}=(stat($srcfile))[9];
669 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
671 print genpage($page, $content);