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}) ];
171 run_hooks(scan => sub {
178 # Preprocess in scan-only mode.
179 preprocess($page, $page, $content, 1);
182 will_render($file, $file, 1);
186 sub fast_file_copy (@) {
194 my ($len, $buf, $written);
195 while ($len = sysread $srcfd, $buf, $blksize) {
196 if (! defined $len) {
197 next if $! =~ /^Interrupted/;
198 error("failed to read $srcfile: $!", $cleanup);
202 defined($written = syswrite $destfd, $buf, $len, $offset)
203 or error("failed to write $destfile: $!", $cleanup);
212 return if $rendered{$file};
216 my $type=pagetype($file);
217 my $srcfile=srcfile($file);
219 my $page=pagename($file);
220 delete $depends{$page};
221 delete $depends_simple{$page};
222 will_render($page, htmlpage($page), 1);
223 return if $type=~/^_/;
225 my $content=htmlize($page, $page, $type,
226 linkify($page, $page,
227 preprocess($page, $page,
229 readfile($srcfile)))));
231 my $output=htmlpage($page);
232 writefile($output, $config{destdir}, genpage($page, $content));
235 delete $depends{$file};
236 delete $depends_simple{$file};
237 will_render($file, $file, 1);
239 if ($config{hardlink}) {
240 # only hardlink if owned by same user
241 my @stat=stat($srcfile);
242 if ($stat[4] == $>) {
243 prep_writefile($file, $config{destdir});
244 unlink($config{destdir}."/".$file);
245 if (link($srcfile, $config{destdir}."/".$file)) {
249 # if hardlink fails, fall back to copying
252 my $srcfd=readfile($srcfile, 1, 1);
253 writefile($file, $config{destdir}, undef, 1, sub {
254 fast_file_copy($srcfile, $file, $srcfd, @_);
263 my $dir=dirname($file);
264 while (rmdir($dir)) {
269 sub srcdir_check () {
270 # security check, avoid following symlinks in the srcdir path by default
271 my $test=$config{srcdir};
272 while (length $test) {
273 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
274 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
276 unless ($test=~s/\/+$//) {
277 $test=dirname($test);
283 sub verify_src_file ($$) {
284 my $file=decode_utf8(shift);
287 return if -l $file || -d $file;
288 $file=~s/^\Q$dir\E\/?//;
289 return if ! length $file;
290 my $page = pagename($file);
291 if (! exists $pagesources{$page} &&
292 file_pruned($file)) {
293 $File::Find::prune=1;
297 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
299 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
301 return ($file,$page,$f);
304 sub find_src_files () {
307 eval q{use File::Find};
312 my ($file,$page,$f) = verify_src_file($_,$config{srcdir});
316 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
322 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
326 my ($file,$page,$f) = verify_src_file($_,$dir);
328 # avoid underlaydir override
329 # attacks; see security.mdwn
330 if (! -l "$config{srcdir}/$f" &&
332 if (! $pages{$page}) {
341 return \@files, \%pages;
344 sub find_new_files ($) {
349 foreach my $file (@$files) {
350 my $page=pagename($file);
351 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
352 # the page has changed its type
353 $forcerebuild{$page}=1;
355 $pagesources{$page}=$file;
356 if (! $pagemtime{$page}) {
357 if (isinternal($page)) {
358 push @internal_new, $file;
362 if ($config{getctime} && -e "$config{srcdir}/$file") {
364 my $time=rcs_getctime("$config{srcdir}/$file");
365 $pagectime{$page}=$time;
372 $pagecase{lc $page}=$page;
373 if (! exists $pagectime{$page}) {
374 $pagectime{$page}=(srcfile_stat($file))[10];
379 return \@new, \@internal_new;
382 sub find_del_files ($) {
387 foreach my $page (keys %pagemtime) {
388 if (! $pages->{$page}) {
389 if (isinternal($page)) {
390 push @internal_del, $pagesources{$page};
393 push @del, $pagesources{$page};
395 $dellinks{$page}= $links{$page};
397 $delrenderedfiles{$page}= $renderedfiles{$page};
398 $renderedfiles{$page}=[];
403 return \@del, \@internal_del;
407 foreach my $file (@_) {
408 my $page=pagename($file);
409 if (! isinternal($page)) {
410 debug(sprintf(gettext("removing old page %s"), $page));
413 foreach my $old (@{$oldrenderedfiles{$page}}) {
414 prune($config{destdir}."/".$old);
417 foreach my $source (keys %destsources) {
418 if ($destsources{$source} eq $page) {
419 delete $destsources{$source};
423 delete $pagecase{lc $page};
424 delete $pagesources{$page};
428 sub find_changed ($) {
431 my @internal_changed;
432 foreach my $file (@$files) {
433 my $page=pagename($file);
434 my ($srcfile, @stat)=srcfile_stat($file);
435 if (! exists $pagemtime{$page} ||
436 $stat[9] > $pagemtime{$page} ||
437 $forcerebuild{$page}) {
438 $pagemtime{$page}=$stat[9];
440 if (isinternal($page)) {
441 # Preprocess internal page in scan-only mode.
442 preprocess($page, $page, readfile($srcfile), 1);
443 push @internal_changed, $file;
446 push @changed, $file;
450 return \@changed, \@internal_changed;
453 sub calculate_old_links ($$) {
454 my ($changed, $del)=@_;
456 foreach my $file (@$changed, @$del) {
457 my $page=pagename($file);
458 if (exists $oldlinks{$page}) {
459 foreach my $l (@{$oldlinks{$page}}) {
460 $oldlink_targets{$page}{$l}=bestlink($page, $l);
464 return \%oldlink_targets;
467 sub derender_internal ($) {
469 my $page=pagename($file);
470 delete $depends{$page};
471 delete $depends_simple{$page};
472 foreach my $old (@{$renderedfiles{$page}}) {
473 delete $destsources{$old};
475 $renderedfiles{$page}=[];
478 sub render_linkers ($) {
481 foreach my $page (keys %{$backlinks{$p}}) {
482 my $file=$pagesources{$page};
483 render($file, sprintf(gettext("building %s, which links to %s"), $file, $p));
487 sub remove_unrendered () {
488 foreach my $src (keys %rendered) {
489 my $page=pagename($src);
490 foreach my $file (@{$oldrenderedfiles{$page}}) {
491 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
492 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
493 prune($config{destdir}."/".$file);
499 sub calculate_changed_links ($$$) {
500 my ($changed, $del, $oldlink_targets)=@_;
502 my (%backlinkchanged, %linkchangers);
504 foreach my $file (@$changed, @$del) {
505 my $page=pagename($file);
507 if (exists $links{$page}) {
508 foreach my $l (@{$links{$page}}) {
509 my $target=bestlink($page, $l);
510 if (! exists $oldlink_targets->{$page}{$l} ||
511 $target ne $oldlink_targets->{$page}{$l}) {
512 $backlinkchanged{$target}=1;
513 $linkchangers{lc($page)}=1;
515 delete $oldlink_targets->{$page}{$l};
518 if (exists $oldlink_targets->{$page} &&
519 %{$oldlink_targets->{$page}}) {
520 foreach my $target (values %{$oldlink_targets->{$page}}) {
521 $backlinkchanged{$target}=1;
523 $linkchangers{lc($page)}=1;
527 return \%backlinkchanged, \%linkchangers;
530 sub render_dependent ($$$$$$$) {
531 my ($files, $new, $internal_new, $del, $internal_del,
532 $internal_changed, $linkchangers)=@_;
534 my @changed=(keys %rendered, @$del);
535 my @exists_changed=(@$new, @$del);
537 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
538 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
540 foreach my $f (@$files) {
541 next if $rendered{$f};
545 if (exists $depends_simple{$p}) {
546 foreach my $d (keys %{$depends_simple{$p}}) {
547 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
550 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
551 $lc_exists_changed{$d})
553 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
562 if (exists $depends{$p} && ! defined $reason) {
563 foreach my $dep (keys %{$depends{$p}}) {
564 my $sub=pagespec_translate($dep);
565 next if $@ || ! defined $sub;
567 # only consider internal files
568 # if the page explicitly depends
570 my $internal_dep=$dep =~ /internal\(/;
575 foreach my $file (@$list) {
577 my $page=pagename($file);
578 if ($sub->($page, location => $p)) {
579 if ($type == $IkiWiki::DEPEND_LINKS) {
580 next unless $linkchangers->{lc($page)};
588 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_CONTENT) {
590 $in->(\@changed, $IkiWiki::DEPEND_CONTENT);
591 last if $internal_dep && ($reason =
592 $in->($internal_new, $IkiWiki::DEPEND_CONTENT) ||
593 $in->($internal_del, $IkiWiki::DEPEND_CONTENT) ||
594 $in->($internal_changed, $IkiWiki::DEPEND_CONTENT));
596 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_PRESENCE) {
598 $in->(\@exists_changed, $IkiWiki::DEPEND_PRESENCE);
599 last if $internal_dep && ($reason =
600 $in->($internal_new, $IkiWiki::DEPEND_PRESENCE) ||
601 $in->($internal_del, $IkiWiki::DEPEND_PRESENCE));
603 if ($depends{$p}{$dep} & $IkiWiki::DEPEND_LINKS) {
605 $in->(\@changed, $IkiWiki::DEPEND_LINKS);
606 last if $internal_dep && ($reason =
607 $in->($internal_new, $IkiWiki::DEPEND_LINKS) ||
608 $in->($internal_del, $IkiWiki::DEPEND_LINKS) ||
609 $in->($internal_changed, $IkiWiki::DEPEND_LINKS));
614 if (defined $reason) {
615 render($f, sprintf(gettext("building %s, which depends on %s"), $f, $reason));
623 sub render_backlinks ($) {
624 my $backlinkchanged=shift;
625 foreach my $link (keys %$backlinkchanged) {
626 my $linkfile=$pagesources{$link};
627 if (defined $linkfile) {
628 render($linkfile, sprintf(gettext("building %s, to update its backlinks"), $linkfile));
635 run_hooks(refresh => sub { shift->() });
636 my ($files, $pages)=find_src_files();
637 my ($new, $internal_new)=find_new_files($files);
638 my ($del, $internal_del)=find_del_files($pages);
639 my ($changed, $internal_changed)=find_changed($files);
640 run_hooks(needsbuild => sub { shift->($changed) });
641 my $oldlink_targets=calculate_old_links($changed, $del);
643 foreach my $file (@$changed) {
647 my %del_hash = map {$_, 1} @$del;
648 while (my $autofile = shift (@autofiles)) {
649 my $page=pagename($autofile);
650 if (exists $del_hash{$page}) {
651 $links{$page}= $dellinks{$page};
652 $renderedfiles{$page}= $delrenderedfiles{$page};
653 delete $del_hash{$page};
655 if ($pages->{$page}) {
656 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
660 push @{$files}, $autofile;
661 push @{$new}, $autofile if find_new_files([$autofile]);
662 push @{$changed}, $autofile if find_changed([$autofile]);
666 $del = [keys %del_hash];
670 remove_del(@$del, @$internal_del);
672 foreach my $file (@$changed) {
673 render($file, sprintf(gettext("building %s"), $file));
675 foreach my $file (@$internal_new, @$internal_del, @$internal_changed) {
676 derender_internal($file);
679 my ($backlinkchanged, $linkchangers)=calculate_changed_links($changed,
680 $del, $oldlink_targets);
682 foreach my $file (@$new, @$del) {
683 render_linkers($file);
686 if (@$changed || @$internal_changed ||
687 @$del || @$internal_del || @$internal_new) {
688 1 while render_dependent($files, $new, $internal_new,
689 $del, $internal_del, $internal_changed,
693 render_backlinks($backlinkchanged);
697 run_hooks(delete => sub { shift->(@$del) });
700 run_hooks(change => sub { shift->(keys %rendered) });
704 sub commandline_render () {
709 my $srcfile=possibly_foolish_untaint($config{render});
711 $file=~s/\Q$config{srcdir}\E\/?//;
713 my $type=pagetype($file);
714 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
715 my $content=readfile($srcfile);
716 my $page=pagename($file);
717 $pagesources{$page}=$file;
718 $content=filter($page, $page, $content);
719 $content=preprocess($page, $page, $content);
720 $content=linkify($page, $page, $content);
721 $content=htmlize($page, $page, $type, $content);
722 $pagemtime{$page}=(stat($srcfile))[9];
723 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
725 print genpage($page, $content);