11 my $backlinks_calculated=0;
13 sub calculate_backlinks () {
14 return if $backlinks_calculated;
16 foreach my $page (keys %links) {
17 foreach my $link (@{$links{$page}}) {
18 my $bestlink=bestlink($page, $link);
19 if (length $bestlink && $bestlink ne $page) {
20 $backlinks{$bestlink}{$page}=1;
24 $backlinks_calculated=1;
30 calculate_backlinks();
33 foreach my $p (keys %{$backlinks{$page}}) {
34 my $href=urlto($p, $page);
36 # Trim common dir prefixes from both pages.
38 my $page_trimmed=$page;
40 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
42 $p_trimmed=~s/^\Q$dir\E// &&
43 $page_trimmed=~s/^\Q$dir\E//;
45 push @links, { url => $href, page => pagetitle($p_trimmed) };
55 run_hooks(templatefile => sub {
56 return if defined $templatefile;
57 my $file=shift->(page => $page);
58 if (defined $file && defined template_file($file)) {
62 my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
65 if (length $config{cgiurl}) {
66 $template->param(editurl => cgiurl(do => "edit", page => $page))
67 if IkiWiki->can("cgi_editpage");
68 $template->param(prefsurl => cgiurl(do => "prefs"));
72 if (defined $config{historyurl} && length $config{historyurl}) {
73 my $u=$config{historyurl};
74 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
75 $template->param(historyurl => $u);
78 if ($config{discussion}) {
79 my $discussionlink=gettext("discussion");
80 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
81 (length $config{cgiurl} ||
82 exists $links{$page."/".$discussionlink})) {
83 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
89 $template->param(have_actions => 1);
92 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
93 my ($backlinks, $more_backlinks);
94 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
95 $backlinks=\@backlinks;
99 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
100 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
104 title => $page eq 'index'
106 : pagetitle(basename($page)),
107 wikiname => $config{wikiname},
109 backlinks => $backlinks,
110 more_backlinks => $more_backlinks,
111 mtime => displaytime($pagemtime{$page}),
112 ctime => displaytime($pagectime{$page}),
113 baseurl => baseurl($page),
116 run_hooks(pagetemplate => sub {
117 shift->(page => $page, destpage => $page, template => $template);
120 $content=$template->output;
122 run_hooks(postscan => sub {
123 shift->(page => $page, content => $content);
126 run_hooks(format => sub {
139 my $type=pagetype($file);
141 my $srcfile=srcfile($file);
142 my $content=readfile($srcfile);
143 my $page=pagename($file);
144 will_render($page, htmlpage($page), 1);
146 if ($config{discussion}) {
147 # Discussion links are a special case since they're
148 # not in the text of the page, but on its template.
149 $links{$page}=[ $page."/".gettext("discussion") ];
155 run_hooks(scan => sub {
162 # Preprocess in scan-only mode.
163 preprocess($page, $page, $content, 1);
166 will_render($file, $file, 1);
170 sub fast_file_copy (@) {
178 my ($len, $buf, $written);
179 while ($len = sysread $srcfd, $buf, $blksize) {
180 if (! defined $len) {
181 next if $! =~ /^Interrupted/;
182 error("failed to read $srcfile: $!", $cleanup);
186 defined($written = syswrite $destfd, $buf, $len, $offset)
187 or error("failed to write $destfile: $!", $cleanup);
197 my $type=pagetype($file);
198 my $srcfile=srcfile($file);
200 my $page=pagename($file);
201 delete $depends{$page};
202 will_render($page, htmlpage($page), 1);
203 return if $type=~/^_/;
205 my $content=htmlize($page, $page, $type,
206 linkify($page, $page,
207 preprocess($page, $page,
209 readfile($srcfile)))));
211 my $output=htmlpage($page);
212 writefile($output, $config{destdir}, genpage($page, $content));
215 delete $depends{$file};
216 will_render($file, $file, 1);
218 if ($config{hardlink}) {
219 # only hardlink if owned by same user
220 my @stat=stat($srcfile);
221 if ($stat[4] == $>) {
222 prep_writefile($file, $config{destdir});
223 unlink($config{destdir}."/".$file);
224 if (link($srcfile, $config{destdir}."/".$file)) {
228 # if hardlink fails, fall back to copying
231 my $srcfd=readfile($srcfile, 1, 1);
232 writefile($file, $config{destdir}, undef, 1, sub {
233 fast_file_copy($srcfile, $file, $srcfd, @_);
242 my $dir=dirname($file);
243 while (rmdir($dir)) {
249 # security check, avoid following symlinks in the srcdir path by default
250 my $test=$config{srcdir};
251 while (length $test) {
252 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
253 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
255 unless ($test=~s/\/+$//) {
256 $test=dirname($test);
260 run_hooks(refresh => sub { shift->() });
262 # find existing pages
265 eval q{use File::Find};
271 if (file_pruned($_, $config{srcdir})) {
272 $File::Find::prune=1;
274 elsif (! -l $_ && ! -d _) {
275 my ($f)=/$config{wiki_file_regexp}/; # untaint
277 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
280 $f=~s/^\Q$config{srcdir}\E\/?//;
282 my $pagename = pagename($f);
283 if ($exists{$pagename}) {
284 debug(sprintf(gettext("%s has multiple possible source pages"), $pagename));
286 $exists{$pagename}=1;
291 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
296 if (file_pruned($_, $dir)) {
297 $File::Find::prune=1;
299 elsif (! -l $_ && ! -d _) {
300 my ($f)=/$config{wiki_file_regexp}/; # untaint
302 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
305 $f=~s/^\Q$dir\E\/?//;
307 # override attacks; see
309 if (! -l "$config{srcdir}/$f" &&
311 my $page=pagename($f);
312 if (! $exists{$page}) {
323 my (%rendered, @add, @del, @internal);
324 # check for added or removed pages
325 foreach my $file (@files) {
326 my $page=pagename($file);
327 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
328 # the page has changed its type
329 $forcerebuild{$page}=1;
331 $pagesources{$page}=$file;
332 if (! $pagemtime{$page}) {
333 if (isinternal($page)) {
334 push @internal, $file;
338 if ($config{getctime} && -e "$config{srcdir}/$file") {
340 my $time=rcs_getctime("$config{srcdir}/$file");
341 $pagectime{$page}=$time;
348 $pagecase{lc $page}=$page;
349 if (! exists $pagectime{$page}) {
350 $pagectime{$page}=(srcfile_stat($file))[10];
354 foreach my $page (keys %pagemtime) {
355 if (! $exists{$page}) {
356 if (isinternal($page)) {
357 push @internal, $pagesources{$page};
360 debug(sprintf(gettext("removing old page %s"), $page));
361 push @del, $pagesources{$page};
364 $renderedfiles{$page}=[];
366 prune($config{destdir}."/".$_)
367 foreach @{$oldrenderedfiles{$page}};
368 delete $pagesources{$page};
369 foreach (keys %destsources) {
370 if ($destsources{$_} eq $page) {
371 delete $destsources{$_};
377 # find changed and new files
379 foreach my $file (@files) {
380 my $page=pagename($file);
381 my ($srcfile, @stat)=srcfile_stat($file);
382 if (! exists $pagemtime{$page} ||
383 $stat[9] > $pagemtime{$page} ||
384 $forcerebuild{$page}) {
385 $pagemtime{$page}=$stat[9];
386 if (isinternal($page)) {
387 push @internal, $file;
388 # Preprocess internal page in scan-only mode.
389 preprocess($page, $page, readfile($srcfile), 1);
392 push @needsbuild, $file;
396 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
398 # scan and render files
399 foreach my $file (@needsbuild) {
400 debug(sprintf(gettext("scanning %s"), $file));
403 calculate_backlinks();
404 foreach my $file (@needsbuild) {
405 debug(sprintf(gettext("rendering %s"), $file));
409 foreach my $file (@internal) {
410 # internal pages are not rendered
411 my $page=pagename($file);
412 delete $depends{$page};
413 foreach my $old (@{$renderedfiles{$page}}) {
414 delete $destsources{$old};
416 $renderedfiles{$page}=[];
419 # rebuild pages that link to added or removed pages
421 foreach my $f (@add, @del) {
423 foreach my $page (keys %{$backlinks{$p}}) {
424 my $file=$pagesources{$page};
425 next if $rendered{$file};
426 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
433 if (%rendered || @del || @internal) {
434 my @changed=(keys %rendered, @del);
436 # rebuild dependant pages
437 foreach my $f (@files) {
438 next if $rendered{$f};
440 if (exists $depends{$p}) {
441 # only consider internal files
442 # if the page explicitly depends on such files
443 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
445 my $page=pagename($file);
446 if (pagespec_match($page, $depends{$p}, location => $p)) {
447 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
456 # handle backlinks; if a page has added/removed links,
457 # update the pages it links to
459 foreach my $file (@changed) {
460 my $page=pagename($file);
462 if (exists $links{$page}) {
463 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
465 (! exists $oldlinks{$page} ||
466 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
467 $linkchanged{$link}=1;
471 if (exists $oldlinks{$page}) {
472 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
474 (! exists $links{$page} ||
475 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
476 $linkchanged{$link}=1;
482 foreach my $link (keys %linkchanged) {
483 my $linkfile=$pagesources{$link};
484 if (defined $linkfile) {
485 next if $rendered{$linkfile};
486 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
488 $rendered{$linkfile}=1;
493 # remove no longer rendered files
494 foreach my $src (keys %rendered) {
495 my $page=pagename($src);
496 foreach my $file (@{$oldrenderedfiles{$page}}) {
497 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
498 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
499 prune($config{destdir}."/".$file);
505 run_hooks(delete => sub { shift->(@del) });
508 run_hooks(change => sub { shift->(keys %rendered) });
512 sub commandline_render () {
517 my $srcfile=possibly_foolish_untaint($config{render});
519 $file=~s/\Q$config{srcdir}\E\/?//;
521 my $type=pagetype($file);
522 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
523 my $content=readfile($srcfile);
524 my $page=pagename($file);
525 $pagesources{$page}=$file;
526 $content=filter($page, $page, $content);
527 $content=preprocess($page, $page, $content);
528 $content=linkify($page, $page, $content);
529 $content=htmlize($page, $page, $type, $content);
530 $pagemtime{$page}=(stat($srcfile))[9];
531 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
533 print genpage($page, $content);