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) };
61 sub find_changed_links (@_) {
64 foreach my $file (@_) {
65 my $page=pagename($file);
67 if (exists $links{$page}) {
68 foreach my $l (@{$links{$page}}) {
69 my $link=bestlink($page, $l);
71 if (! exists $oldlinks{$page} ||
72 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}}) {
73 $linkchanged{$link}=1;
74 $linkchangers{lc($page)}=1;
78 if (! grep { lc $_ eq lc $l } @{$oldlinks{$page}}) {
79 $linkchangers{lc($page)}=1
85 if (exists $oldlinks{$page}) {
86 foreach my $l (@{$oldlinks{$page}}) {
87 my $link=bestlink($page, $l);
89 if (! exists $links{$page} ||
90 ! grep { bestlink($page, $_) eq $link } @{$links{$page}}) {
91 $linkchanged{$link}=1;
92 $linkchangers{lc($page)}=1;
96 if (! grep { lc $_ eq lc $l } @{$links{$page}}) {
97 $linkchangers{lc($page)}=1
104 return \%linkchanged, \%linkchangers;
112 run_hooks(templatefile => sub {
113 return if defined $templatefile;
114 my $file=shift->(page => $page);
115 if (defined $file && defined template_file($file)) {
119 my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
122 if (length $config{cgiurl}) {
123 $template->param(editurl => cgiurl(do => "edit", page => $page))
124 if IkiWiki->can("cgi_editpage");
125 $template->param(prefsurl => cgiurl(do => "prefs"))
126 if exists $hooks{auth};
130 if (defined $config{historyurl} && length $config{historyurl}) {
131 my $u=$config{historyurl};
132 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
133 $template->param(historyurl => $u);
136 if ($config{discussion}) {
137 if ($page !~ /.*\/\Q$config{discussionpage}\E$/ &&
138 (length $config{cgiurl} ||
139 exists $links{$page."/".$config{discussionpage}})) {
140 $template->param(discussionlink => htmllink($page, $page, $config{discussionpage}, noimageinline => 1, forcesubpage => 1));
146 $template->param(have_actions => 1);
149 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
150 my ($backlinks, $more_backlinks);
151 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
152 $backlinks=\@backlinks;
156 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
157 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
161 title => $page eq 'index'
163 : pagetitle(basename($page)),
164 wikiname => $config{wikiname},
166 backlinks => $backlinks,
167 more_backlinks => $more_backlinks,
168 mtime => displaytime($pagemtime{$page}),
169 ctime => displaytime($pagectime{$page}),
170 baseurl => baseurl($page),
173 run_hooks(pagetemplate => sub {
174 shift->(page => $page, destpage => $page, template => $template);
177 $content=$template->output;
179 run_hooks(postscan => sub {
180 shift->(page => $page, content => $content);
183 run_hooks(format => sub {
196 my $type=pagetype($file);
198 my $srcfile=srcfile($file);
199 my $content=readfile($srcfile);
200 my $page=pagename($file);
201 will_render($page, htmlpage($page), 1);
203 if ($config{discussion}) {
204 # Discussion links are a special case since they're
205 # not in the text of the page, but on its template.
206 $links{$page}=[ $page."/".lc($config{discussionpage}) ];
212 run_hooks(scan => sub {
219 # Preprocess in scan-only mode.
220 preprocess($page, $page, $content, 1);
223 will_render($file, $file, 1);
227 sub fast_file_copy (@) {
235 my ($len, $buf, $written);
236 while ($len = sysread $srcfd, $buf, $blksize) {
237 if (! defined $len) {
238 next if $! =~ /^Interrupted/;
239 error("failed to read $srcfile: $!", $cleanup);
243 defined($written = syswrite $destfd, $buf, $len, $offset)
244 or error("failed to write $destfile: $!", $cleanup);
254 my $type=pagetype($file);
255 my $srcfile=srcfile($file);
257 my $page=pagename($file);
258 delete $depends{$page};
259 delete $depends_simple{$page};
260 will_render($page, htmlpage($page), 1);
261 return if $type=~/^_/;
263 my $content=htmlize($page, $page, $type,
264 linkify($page, $page,
265 preprocess($page, $page,
267 readfile($srcfile)))));
269 my $output=htmlpage($page);
270 writefile($output, $config{destdir}, genpage($page, $content));
273 delete $depends{$file};
274 delete $depends_simple{$file};
275 will_render($file, $file, 1);
277 if ($config{hardlink}) {
278 # only hardlink if owned by same user
279 my @stat=stat($srcfile);
280 if ($stat[4] == $>) {
281 prep_writefile($file, $config{destdir});
282 unlink($config{destdir}."/".$file);
283 if (link($srcfile, $config{destdir}."/".$file)) {
287 # if hardlink fails, fall back to copying
290 my $srcfd=readfile($srcfile, 1, 1);
291 writefile($file, $config{destdir}, undef, 1, sub {
292 fast_file_copy($srcfile, $file, $srcfd, @_);
301 my $dir=dirname($file);
302 while (rmdir($dir)) {
307 sub srcdir_check () {
308 # security check, avoid following symlinks in the srcdir path by default
309 my $test=$config{srcdir};
310 while (length $test) {
311 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
312 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
314 unless ($test=~s/\/+$//) {
315 $test=dirname($test);
321 sub find_src_files () {
323 eval q{use File::Find};
329 if (file_pruned($_, $config{srcdir})) {
330 $File::Find::prune=1;
332 elsif (! -l $_ && ! -d _) {
333 my ($f)=/$config{wiki_file_regexp}/; # untaint
335 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
338 $f=~s/^\Q$config{srcdir}\E\/?//;
340 my $pagename = pagename($f);
341 if ($pages{$pagename}) {
342 debug(sprintf(gettext("%s has multiple possible source pages"), $pagename));
349 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
354 if (file_pruned($_, $dir)) {
355 $File::Find::prune=1;
357 elsif (! -l $_ && ! -d _) {
358 my ($f)=/$config{wiki_file_regexp}/; # untaint
360 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
363 $f=~s/^\Q$dir\E\/?//;
365 # override attacks; see
367 if (! -l "$config{srcdir}/$f" &&
369 my $page=pagename($f);
370 if (! $pages{$page}) {
381 # Returns a list of all source files found, and a hash of
382 # the corresponding page names.
383 return \@files, \%pages;
388 run_hooks(refresh => sub { shift->() });
389 my ($files, $exists)=find_src_files();
391 my (%rendered, @add, @del, @internal, @internal_change);
392 # check for added or removed pages
393 foreach my $file (@$files) {
394 my $page=pagename($file);
395 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
396 # the page has changed its type
397 $forcerebuild{$page}=1;
399 $pagesources{$page}=$file;
400 if (! $pagemtime{$page}) {
401 if (isinternal($page)) {
402 push @internal, $file;
406 if ($config{getctime} && -e "$config{srcdir}/$file") {
408 my $time=rcs_getctime("$config{srcdir}/$file");
409 $pagectime{$page}=$time;
416 $pagecase{lc $page}=$page;
417 if (! exists $pagectime{$page}) {
418 $pagectime{$page}=(srcfile_stat($file))[10];
422 foreach my $page (keys %pagemtime) {
423 if (! $exists->{$page}) {
424 if (isinternal($page)) {
425 push @internal, $pagesources{$page};
428 debug(sprintf(gettext("removing old page %s"), $page));
429 push @del, $pagesources{$page};
432 $renderedfiles{$page}=[];
434 foreach my $old (@{$oldrenderedfiles{$page}}) {
435 prune($config{destdir}."/".$old);
437 delete $pagesources{$page};
438 foreach my $source (keys %destsources) {
439 if ($destsources{$source} eq $page) {
440 delete $destsources{$source};
446 # find changed and new files
448 foreach my $file (@$files) {
449 my $page=pagename($file);
450 my ($srcfile, @stat)=srcfile_stat($file);
451 if (! exists $pagemtime{$page} ||
452 $stat[9] > $pagemtime{$page} ||
453 $forcerebuild{$page}) {
454 $pagemtime{$page}=$stat[9];
455 if (isinternal($page)) {
456 push @internal_change, $file;
457 # Preprocess internal page in scan-only mode.
458 preprocess($page, $page, readfile($srcfile), 1);
461 push @needsbuild, $file;
465 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
467 # scan and render files
468 foreach my $file (@needsbuild) {
469 debug(sprintf(gettext("scanning %s"), $file));
473 foreach my $file (@needsbuild) {
474 debug(sprintf(gettext("building %s"), $file));
478 foreach my $file (@internal, @internal_change) {
479 # internal pages are not rendered
480 my $page=pagename($file);
481 delete $depends{$page};
482 delete $depends_simple{$page};
483 foreach my $old (@{$renderedfiles{$page}}) {
484 delete $destsources{$old};
486 $renderedfiles{$page}=[];
489 # rebuild pages that link to added or removed pages
491 foreach my $f (@add, @del) {
493 foreach my $page (keys %{$backlinks{$p}}) {
494 my $file=$pagesources{$page};
495 next if $rendered{$file};
496 debug(sprintf(gettext("building %s, which links to %s"), $file, $p));
503 if (%rendered || @del || @internal || @internal_change) {
504 my @changed=(keys %rendered, @del);
505 my ($linkchanged, $linkchangers)=find_changed_links(@changed);
510 @changed=(keys %rendered, @del);
511 my @exists_changed=(@add, @del);
513 my %lc_changed = map { lc(pagename($_)) => 1 } @changed;
514 my %lc_exists_changed = map { lc(pagename($_)) => 1 } @exists_changed;
516 # rebuild dependant pages
517 foreach my $f (@$files) {
518 next if $rendered{$f};
522 if (exists $depends_simple{$p}) {
523 foreach my $d (keys %{$depends_simple{$p}}) {
524 if (($depends_simple{$p}{$d} & $IkiWiki::DEPEND_CONTENT &&
527 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_PRESENCE &&
528 $lc_exists_changed{$d})
530 ($depends_simple{$p}{$d} & $IkiWiki::DEPEND_LINKS &&
539 if (exists $depends{$p} && ! defined $reason) {
540 D: foreach my $d (keys %{$depends{$p}}) {
541 my $sub=pagespec_translate($d);
542 next if $@ || ! defined $sub;
544 # only consider internal files
545 # if the page explicitly depends
547 my $internal_dep=$d =~ /internal\(/;
550 if ($depends{$p}{$d} & $IkiWiki::DEPEND_PRESENCE) {
551 @candidates=@exists_changed;
552 push @candidates, @internal
555 if (($depends{$p}{$d} & ($IkiWiki::DEPEND_CONTENT | $IkiWiki::DEPEND_LINKS))) {
556 @candidates=@changed;
557 push @candidates, @internal, @internal_change
561 foreach my $file (@candidates) {
563 my $page=pagename($file);
564 if ($sub->($page, location => $p)) {
565 if ($depends{$p}{$d} & $IkiWiki::DEPEND_LINKS) {
566 next unless $linkchangers->{lc($page)};
575 if (defined $reason) {
576 debug(sprintf(gettext("building %s, which depends on %s"), $f, $reason));
585 # update backlinks at end
586 foreach my $link (keys %{$linkchanged}) {
587 my $linkfile=$pagesources{$link};
588 if (defined $linkfile) {
589 next if $rendered{$linkfile};
590 debug(sprintf(gettext("building %s, to update its backlinks"), $linkfile));
592 $rendered{$linkfile}=1;
597 # remove no longer rendered files
598 foreach my $src (keys %rendered) {
599 my $page=pagename($src);
600 foreach my $file (@{$oldrenderedfiles{$page}}) {
601 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
602 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
603 prune($config{destdir}."/".$file);
609 run_hooks(delete => sub { shift->(@del) });
612 run_hooks(change => sub { shift->(keys %rendered) });
616 sub commandline_render () {
621 my $srcfile=possibly_foolish_untaint($config{render});
623 $file=~s/\Q$config{srcdir}\E\/?//;
625 my $type=pagetype($file);
626 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
627 my $content=readfile($srcfile);
628 my $page=pagename($file);
629 $pagesources{$page}=$file;
630 $content=filter($page, $page, $content);
631 $content=preprocess($page, $page, $content);
632 $content=linkify($page, $page, $content);
633 $content=htmlize($page, $page, $type, $content);
634 $pagemtime{$page}=(stat($srcfile))[9];
635 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
637 print genpage($page, $content);