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 will_render($page, htmlpage($page), 1);
214 return if $type=~/^_/;
216 my $content=htmlize($page, $page, $type,
217 linkify($page, $page,
218 preprocess($page, $page,
220 readfile($srcfile)))));
222 my $output=htmlpage($page);
223 writefile($output, $config{destdir}, genpage($page, $content));
226 delete $depends{$file};
227 will_render($file, $file, 1);
229 if ($config{hardlink}) {
230 # only hardlink if owned by same user
231 my @stat=stat($srcfile);
232 if ($stat[4] == $>) {
233 prep_writefile($file, $config{destdir});
234 unlink($config{destdir}."/".$file);
235 if (link($srcfile, $config{destdir}."/".$file)) {
239 # if hardlink fails, fall back to copying
242 my $srcfd=readfile($srcfile, 1, 1);
243 writefile($file, $config{destdir}, undef, 1, sub {
244 fast_file_copy($srcfile, $file, $srcfd, @_);
253 my $dir=dirname($file);
254 while (rmdir($dir)) {
259 sub srcdir_check () {
260 # security check, avoid following symlinks in the srcdir path by default
261 my $test=$config{srcdir};
262 while (length $test) {
263 if (-l $test && ! $config{allow_symlinks_before_srcdir}) {
264 error(sprintf(gettext("symlink found in srcdir path (%s) -- set allow_symlinks_before_srcdir to allow this"), $test));
266 unless ($test=~s/\/+$//) {
267 $test=dirname($test);
273 sub find_src_files () {
275 eval q{use File::Find};
281 if (file_pruned($_, $config{srcdir})) {
282 $File::Find::prune=1;
284 elsif (! -l $_ && ! -d _) {
285 my ($f)=/$config{wiki_file_regexp}/; # untaint
287 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
290 $f=~s/^\Q$config{srcdir}\E\/?//;
292 my $pagename = pagename($f);
293 if ($pages{$pagename}) {
294 debug(sprintf(gettext("%s has multiple possible source pages"), $pagename));
301 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
306 if (file_pruned($_, $dir)) {
307 $File::Find::prune=1;
309 elsif (! -l $_ && ! -d _) {
310 my ($f)=/$config{wiki_file_regexp}/; # untaint
312 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
315 $f=~s/^\Q$dir\E\/?//;
317 # override attacks; see
319 if (! -l "$config{srcdir}/$f" &&
321 my $page=pagename($f);
322 if (! $pages{$page}) {
333 # Returns a list of all source files found, and a hash of
334 # the corresponding page names.
335 return \@files, \%pages;
340 run_hooks(refresh => sub { shift->() });
341 my ($files, $exists)=find_src_files();
343 my (%rendered, @add, @del, @internal);
344 # check for added or removed pages
345 foreach my $file (@$files) {
346 my $page=pagename($file);
347 if (exists $pagesources{$page} && $pagesources{$page} ne $file) {
348 # the page has changed its type
349 $forcerebuild{$page}=1;
351 $pagesources{$page}=$file;
352 if (! $pagemtime{$page}) {
353 if (isinternal($page)) {
354 push @internal, $file;
358 if ($config{getctime} && -e "$config{srcdir}/$file") {
360 my $time=rcs_getctime("$config{srcdir}/$file");
361 $pagectime{$page}=$time;
368 $pagecase{lc $page}=$page;
369 if (! exists $pagectime{$page}) {
370 $pagectime{$page}=(srcfile_stat($file))[10];
374 foreach my $page (keys %pagemtime) {
375 if (! $exists->{$page}) {
376 if (isinternal($page)) {
377 push @internal, $pagesources{$page};
380 debug(sprintf(gettext("removing old page %s"), $page));
381 push @del, $pagesources{$page};
384 $renderedfiles{$page}=[];
386 foreach my $old (@{$oldrenderedfiles{$page}}) {
387 prune($config{destdir}."/".$old);
389 delete $pagesources{$page};
390 foreach my $source (keys %destsources) {
391 if ($destsources{$source} eq $page) {
392 delete $destsources{$source};
398 # find changed and new files
400 foreach my $file (@$files) {
401 my $page=pagename($file);
402 my ($srcfile, @stat)=srcfile_stat($file);
403 if (! exists $pagemtime{$page} ||
404 $stat[9] > $pagemtime{$page} ||
405 $forcerebuild{$page}) {
406 $pagemtime{$page}=$stat[9];
407 if (isinternal($page)) {
408 push @internal, $file;
409 # Preprocess internal page in scan-only mode.
410 preprocess($page, $page, readfile($srcfile), 1);
413 push @needsbuild, $file;
417 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
419 # scan and render files
420 foreach my $file (@needsbuild) {
421 debug(sprintf(gettext("scanning %s"), $file));
425 foreach my $file (@needsbuild) {
426 debug(sprintf(gettext("building %s"), $file));
430 foreach my $file (@internal) {
431 # internal pages are not rendered
432 my $page=pagename($file);
433 delete $depends{$page};
434 foreach my $old (@{$renderedfiles{$page}}) {
435 delete $destsources{$old};
437 $renderedfiles{$page}=[];
440 # rebuild pages that link to added or removed pages
442 foreach my $f (@add, @del) {
444 foreach my $page (keys %{$backlinks{$p}}) {
445 my $file=$pagesources{$page};
446 next if $rendered{$file};
447 debug(sprintf(gettext("building %s, which links to %s"), $file, $p));
454 if (%rendered || @del || @internal) {
455 my @changed=(keys %rendered, @del);
457 # rebuild dependant pages
458 F: foreach my $f (@$files) {
459 next if $rendered{$f};
461 if (exists $depends{$p}) {
462 foreach my $d (keys %{$depends{$p}}) {
463 # only consider internal files
464 # if the page explicitly depends on such files
469 } (@changed, $d =~ /internal\(/ ? @internal : ());
470 @pages = pagespec_match_list(\@pages, $d, location => $p);
472 debug(sprintf(gettext("building %s, which depends on %s"), $f, $pages[0]));
481 # handle backlinks; if a page has added/removed links,
482 # update the pages it links to
484 foreach my $file (@changed) {
485 my $page=pagename($file);
487 if (exists $links{$page}) {
488 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
490 (! exists $oldlinks{$page} ||
491 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
492 $linkchanged{$link}=1;
496 if (exists $oldlinks{$page}) {
497 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
499 (! exists $links{$page} ||
500 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
501 $linkchanged{$link}=1;
507 foreach my $link (keys %linkchanged) {
508 my $linkfile=$pagesources{$link};
509 if (defined $linkfile) {
510 next if $rendered{$linkfile};
511 debug(sprintf(gettext("building %s, to update its backlinks"), $linkfile));
513 $rendered{$linkfile}=1;
518 # remove no longer rendered files
519 foreach my $src (keys %rendered) {
520 my $page=pagename($src);
521 foreach my $file (@{$oldrenderedfiles{$page}}) {
522 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
523 debug(sprintf(gettext("removing %s, no longer built by %s"), $file, $page));
524 prune($config{destdir}."/".$file);
530 run_hooks(delete => sub { shift->(@del) });
533 run_hooks(change => sub { shift->(keys %rendered) });
537 sub commandline_render () {
542 my $srcfile=possibly_foolish_untaint($config{render});
544 $file=~s/\Q$config{srcdir}\E\/?//;
546 my $type=pagetype($file);
547 die sprintf(gettext("ikiwiki: cannot build %s"), $srcfile)."\n" unless defined $type;
548 my $content=readfile($srcfile);
549 my $page=pagename($file);
550 $pagesources{$page}=$file;
551 $content=filter($page, $page, $content);
552 $content=preprocess($page, $page, $content);
553 $content=linkify($page, $page, $content);
554 $content=htmlize($page, $page, $type, $content);
555 $pagemtime{$page}=(stat($srcfile))[9];
556 $pagectime{$page}=$pagemtime{$page} if ! exists $pagectime{$page};
558 print genpage($page, $content);