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};
282 my $file=decode_utf8($_);
283 $file=~s/^\Q$config{srcdir}\E\/?//;
284 my $page = pagename($file);
285 if (! exists $pagesources{$page} &&
286 file_pruned($file)) {
287 $File::Find::prune=1;
290 return if -l $_ || -d _ || ! length $file;
292 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
294 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
299 debug(sprintf(gettext("%s has multiple possible source pages"), $page));
305 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
309 my $file=decode_utf8($_);
310 $file=~s/^\Q$dir\E\/?//;
311 my $page=pagename($file);
312 if (! exists $pagesources{$page} &&
313 file_pruned($file)) {
314 $File::Find::prune=1;
317 return if -l $_ || -d _ || ! length $file;
319 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
321 warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
324 # avoid underlaydir override
325 # attacks; see security.mdwn
326 if (! -l "$config{srcdir}/$f" &&
328 if (! $pages{$page}) {
338 # Returns a list of all source files found, and a hash of
339 # the corresponding page names.
340 return \@files, \%pages;
345 run_hooks(refresh => sub { shift->() });
346 my ($files, $exists)=find_src_files();
348 my (%rendered, @add, @del, @internal);
349 # check for added or removed pages
350 foreach my $file (@$files) {
351 my $page=pagename($file);
352 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
353 # the page has changed its type
354 $forcerebuild{$page}=1;
356 $pagesources{$page}=$file;
357 if (! $pagemtime{$page}) {
358 if (isinternal($page)) {
359 push @internal, $file;
363 if ($config{getctime} && -e "$config{srcdir}/$file") {
365 my $time=rcs_getctime("$config{srcdir}/$file");
366 $pagectime{$page}=$time;
373 $pagecase{lc $page}=$page;
374 if (! exists $pagectime{$page}) {
375 $pagectime{$page}=(srcfile_stat($file))[10];
379 foreach my $page (keys %pagemtime) {
380 if (! $exists->{$page}) {
381 if (isinternal($page)) {
382 push @internal, $pagesources{$page};
385 debug(sprintf(gettext("removing old page %s"), $page));
386 push @del, $pagesources{$page};
389 $renderedfiles{$page}=[];
391 foreach my $old (@{$oldrenderedfiles{$page}}) {
392 prune($config{destdir}."/".$old);
394 delete $pagesources{$page};
395 foreach my $source (keys %destsources) {
396 if ($destsources{$source} eq $page) {
397 delete $destsources{$source};
403 # find changed and new files
405 foreach my $file (@$files) {
406 my $page=pagename($file);
407 my ($srcfile, @stat)=srcfile_stat($file);
408 if (! exists $pagemtime{$page} ||
409 $stat[9] > $pagemtime{$page} ||
410 $forcerebuild{$page}) {
411 $pagemtime{$page}=$stat[9];
412 if (isinternal($page)) {
413 push @internal, $file;
414 # Preprocess internal page in scan-only mode.
415 preprocess($page, $page, readfile($srcfile), 1);
418 push @needsbuild, $file;
422 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
424 # scan and render files
425 foreach my $file (@needsbuild) {
426 debug(sprintf(gettext("scanning %s"), $file));
430 foreach my $file (@needsbuild) {
431 debug(sprintf(gettext("building %s"), $file));
435 foreach my $file (@internal) {
436 # internal pages are not rendered
437 my $page=pagename($file);
438 delete $depends{$page};
439 delete $depends_simple{$page};
440 foreach my $old (@{$renderedfiles{$page}}) {
441 delete $destsources{$old};
443 $renderedfiles{$page}=[];
446 # rebuild pages that link to added or removed pages
448 foreach my $f (@add, @del) {
450 foreach my $page (keys %{$backlinks{$p}}) {
451 my $file=$pagesources{$page};
452 next if $rendered{$file};
453 debug(sprintf(gettext("building %s, which links to %s"), $file, $p));
460 if (%rendered || @del || @internal) {
461 my @changed=(keys %rendered, @del);
463 my %lcchanged = map { lc(pagename($_)) => 1 } @changed;
465 # rebuild dependant pages
466 foreach my $f (@$files) {
467 next if $rendered{$f};
471 if (exists $depends_simple{$p}) {
472 foreach my $d (keys %{$depends_simple{$p}}) {
473 if (exists $lcchanged{$d}) {
480 if (exists $depends{$p} && ! defined $reason) {
481 D: foreach my $d (keys %{$depends{$p}}) {
482 my $sub=pagespec_translate($d);
483 next if $@ || ! defined $sub;
485 # only consider internal files
486 # if the page explicitly depends
488 foreach my $file (@changed, $d =~ /internal\(/ ? @internal : ()) {
490 my $page=pagename($file);
491 if ($sub->($page, location => $p)) {
499 if (defined $reason) {
500 debug(sprintf(gettext("building %s, which depends on %s"), $f, $reason));
506 # handle backlinks; if a page has added/removed links,
507 # update the pages it links to
509 foreach my $file (@changed) {
510 my $page=pagename($file);
512 if (exists $links{$page}) {
513 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
515 (! exists $oldlinks{$page} ||
516 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
517 $linkchanged{$link}=1;
521 if (exists $oldlinks{$page}) {
522 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
524 (! exists $links{$page} ||
525 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
526 $linkchanged{$link}=1;
532 foreach my $link (keys %linkchanged) {
533 my $linkfile=$pagesources{$link};
534 if (defined $linkfile) {
535 next if $rendered{$linkfile};
536 debug(sprintf(gettext("building %s, to update its backlinks"), $linkfile));
538 $rendered{$linkfile}=1;
543 # remove no longer rendered files
544 foreach my $src (keys %rendered) {
545 my $page=pagename($src);
546 foreach my $file (@{$oldrenderedfiles{$page}}) {
547 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
548 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
549 prune($config{destdir}."/".$file);
555 run_hooks(delete => sub { shift->(@del) });
558 run_hooks(change => sub { shift->(keys %rendered) });
562 sub commandline_render () {
567 my $srcfile=possibly_foolish_untaint($config{render});
569 $file=~s/\Q$config{srcdir}\E\/?//;
571 my $type=pagetype($file);
572 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
573 my $content=readfile($srcfile);
574 my $page=pagename($file);
575 $pagesources{$page}=$file;
576 $content=filter($page, $page, $content);
577 $content=preprocess($page, $page, $content);
578 $content=linkify($page, $page, $content);
579 $content=htmlize($page, $page, $type, $content);
580 $pagemtime{$page}=(stat($srcfile))[9];
581 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
583 print genpage($page, $content);