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 my $type=pagetype($file);
152 my $srcfile=srcfile($file);
153 my $content=readfile($srcfile);
154 my $page=pagename($file);
155 will_render($page, htmlpage($page), 1);
157 if ($config{discussion}) {
158 # Discussion links are a special case since they're
159 # not in the text of the page, but on its template.
160 $links{$page}=[ $page."/".lc($config{discussionpage}) ];
166 run_hooks(scan => sub {
173 # Preprocess in scan-only mode.
174 preprocess($page, $page, $content, 1);
177 will_render($file, $file, 1);
181 sub fast_file_copy (@) {
189 my ($len, $buf, $written);
190 while ($len = sysread $srcfd, $buf, $blksize) {
191 if (! defined $len) {
192 next if $! =~ /^Interrupted/;
193 error("failed to read $srcfile: $!", $cleanup);
197 defined($written = syswrite $destfd, $buf, $len, $offset)
198 or error("failed to write $destfile: $!", $cleanup);
208 my $type=pagetype($file);
209 my $srcfile=srcfile($file);
211 my $page=pagename($file);
212 delete $depends{$page};
213 delete $depends_simple{$page};
214 will_render($page, htmlpage($page), 1);
215 return if $type=~/^_/;
217 my $content=htmlize($page, $page, $type,
218 linkify($page, $page,
219 preprocess($page, $page,
221 readfile($srcfile)))));
223 my $output=htmlpage($page);
224 writefile($output, $config{destdir}, genpage($page, $content));
227 delete $depends{$file};
228 delete $depends_simple{$file};
229 will_render($file, $file, 1);
231 if ($config{hardlink}) {
232 # only hardlink if owned by same user
233 my @stat=stat($srcfile);
234 if ($stat[4] == $>) {
235 prep_writefile($file, $config{destdir});
236 unlink($config{destdir}."/".$file);
237 if (link($srcfile, $config{destdir}."/".$file)) {
241 # if hardlink fails, fall back to copying
244 my $srcfd=readfile($srcfile, 1, 1);
245 writefile($file, $config{destdir}, undef, 1, sub {
246 fast_file_copy($srcfile, $file, $srcfd, @_);
255 my $dir=dirname($file);
256 while (rmdir($dir)) {
261 sub srcdir_check () {
262 # security check, avoid following symlinks in the srcdir path by default
263 my $test=$config{srcdir};
264 while (length $test) {
265 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
266 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
268 unless ($test=~s/\/+$//) {
269 $test=dirname($test);
275 sub find_src_files () {
277 eval q{use File::Find};
283 if (file_pruned($_, $config{srcdir})) {
284 $File::Find::prune=1;
286 elsif (! -l $_ && ! -d _) {
287 my ($f)=/$config{wiki_file_regexp}/; # untaint
289 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
292 $f=~s/^\Q$config{srcdir}\E\/?//;
294 my $pagename = pagename($f);
295 if ($pages{$pagename}) {
296 debug(sprintf(gettext("%s has multiple possible source pages"), $pagename));
303 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
308 if (file_pruned($_, $dir)) {
309 $File::Find::prune=1;
311 elsif (! -l $_ && ! -d _) {
312 my ($f)=/$config{wiki_file_regexp}/; # untaint
314 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
317 $f=~s/^\Q$dir\E\/?//;
319 # override attacks; see
321 if (! -l "$config{srcdir}/$f" &&
323 my $page=pagename($f);
324 if (! $pages{$page}) {
335 # Returns a list of all source files found, and a hash of
336 # the corresponding page names.
337 return \@files, \%pages;
342 run_hooks(refresh => sub { shift->() });
343 my ($files, $exists)=find_src_files();
345 my (%rendered, @add, @del, @internal, @internal_change);
346 # check for added or removed pages
347 foreach my $file (@$files) {
348 my $page=pagename($file);
349 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
350 # the page has changed its type
351 $forcerebuild{$page}=1;
353 $pagesources{$page}=$file;
354 if (! $pagemtime{$page}) {
355 if (isinternal($page)) {
356 push @internal, $file;
360 if ($config{getctime} && -e "$config{srcdir}/$file") {
362 my $time=rcs_getctime("$config{srcdir}/$file");
363 $pagectime{$page}=$time;
370 $pagecase{lc $page}=$page;
371 if (! exists $pagectime{$page}) {
372 $pagectime{$page}=(srcfile_stat($file))[10];
376 foreach my $page (keys %pagemtime) {
377 if (! $exists->{$page}) {
378 if (isinternal($page)) {
379 push @internal, $pagesources{$page};
382 debug(sprintf(gettext("removing old page %s"), $page));
383 push @del, $pagesources{$page};
386 $renderedfiles{$page}=[];
388 foreach my $old (@{$oldrenderedfiles{$page}}) {
389 prune($config{destdir}."/".$old);
391 delete $pagesources{$page};
392 foreach my $source (keys %destsources) {
393 if ($destsources{$source} eq $page) {
394 delete $destsources{$source};
400 # find changed and new files
402 foreach my $file (@$files) {
403 my $page=pagename($file);
404 my ($srcfile, @stat)=srcfile_stat($file);
405 if (! exists $pagemtime{$page} ||
406 $stat[9] > $pagemtime{$page} ||
407 $forcerebuild{$page}) {
408 $pagemtime{$page}=$stat[9];
409 if (isinternal($page)) {
410 push @internal_change, $file;
411 # Preprocess internal page in scan-only mode.
412 preprocess($page, $page, readfile($srcfile), 1);
415 push @needsbuild, $file;
419 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
421 # scan and render files
422 foreach my $file (@needsbuild) {
423 debug(sprintf(gettext("scanning %s"), $file));
427 foreach my $file (@needsbuild) {
428 debug(sprintf(gettext("building %s"), $file));
432 foreach my $file (@internal, @internal_change) {
433 # internal pages are not rendered
434 my $page=pagename($file);
435 delete $depends{$page};
436 delete $depends_simple{$page};
437 foreach my $old (@{$renderedfiles{$page}}) {
438 delete $destsources{$old};
440 $renderedfiles{$page}=[];
443 # rebuild pages that link to added or removed pages
445 foreach my $f (@add, @del) {
447 foreach my $page (keys %{$backlinks{$p}}) {
448 my $file=$pagesources{$page};
449 next if $rendered{$file};
450 debug(sprintf(gettext("building %s, which links to %s"), $file, $p));
457 if (%rendered || @del || @internal || @internal_change) {
462 @changed=(keys %rendered, @del);
463 my @exists_changed=(@add, @del);
465 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
466 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
468 # rebuild dependant pages
469 foreach my $f (@$files) {
470 next if $rendered{$f};
474 if (exists $depends_simple{$p}) {
475 foreach my $d (keys %{$depends_simple{$p}}) {
476 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
477 exists $lc_changed{$d})
479 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
480 exists $lc_exists_changed{$d})) {
487 if (exists $depends{$p} && ! defined $reason) {
488 D: foreach my $d (keys %{$depends{$p}}) {
489 my $sub=pagespec_translate($d);
490 next if $@ || ! defined $sub;
493 if ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT) {
494 @candidates=@changed;
496 elsif ($depends{$p}{$d} & $IkiWiki::DEPEND_PRESENCE) {
497 @candidates=@exists_changed;
499 # only consider internal files
500 # if the page explicitly depends
502 if ($d =~ /internal\(/) {
503 if ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT) {
504 push @candidates, @internal, @internal_change;
506 elsif ($depends{$p}{$d} & $IkiWiki::DEPEND_PRESENCE) {
507 push @candidates, @internal;
511 foreach my $file (@candidates) {
513 my $page=pagename($file);
514 if ($sub->($page, location => $p)) {
522 if (defined $reason) {
523 debug(sprintf(gettext("building %s, which depends on %s"), $f, $reason));
532 # handle backlinks; if a page has added/removed links,
533 # update the pages it links to
535 foreach my $file (@changed) {
536 my $page=pagename($file);
538 if (exists $links{$page}) {
539 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
541 (! exists $oldlinks{$page} ||
542 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
543 $linkchanged{$link}=1;
547 if (exists $oldlinks{$page}) {
548 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
550 (! exists $links{$page} ||
551 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
552 $linkchanged{$link}=1;
558 foreach my $link (keys %linkchanged) {
559 my $linkfile=$pagesources{$link};
560 if (defined $linkfile) {
561 next if $rendered{$linkfile};
562 debug(sprintf(gettext("building %s, to update its backlinks"), $linkfile));
564 $rendered{$linkfile}=1;
569 # remove no longer rendered files
570 foreach my $src (keys %rendered) {
571 my $page=pagename($src);
572 foreach my $file (@{$oldrenderedfiles{$page}}) {
573 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
574 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
575 prune($config{destdir}."/".$file);
581 run_hooks(delete => sub { shift->(@del) });
584 run_hooks(change => sub { shift->(keys %rendered) });
588 sub commandline_render () {
593 my $srcfile=possibly_foolish_untaint($config{render});
595 $file=~s/\Q$config{srcdir}\E\/?//;
597 my $type=pagetype($file);
598 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
599 my $content=readfile($srcfile);
600 my $page=pagename($file);
601 $pagesources{$page}=$file;
602 $content=filter($page, $page, $content);
603 $content=preprocess($page, $page, $content);
604 $content=linkify($page, $page, $content);
605 $content=htmlize($page, $page, $type, $content);
606 $pagemtime{$page}=(stat($srcfile))[9];
607 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
609 print genpage($page, $content);