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;
27 sub backlinks ($) { #{{{
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) };
50 sub parentlinks ($) { #{{{
56 my $title=$config{wikiname};
58 foreach my $dir (split("/", $page)) {
59 next if $dir eq 'index';
60 push @ret, { url => urlto($path, $page), page => $title };
62 $title=pagetitle($dir);
67 sub genpage ($$) { #{{{
72 run_hooks(templatefile => sub {
73 return if defined $templatefile;
74 my $file=shift->(page => $page);
75 if (defined $file && defined template_file($file)) {
79 my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
82 if (length $config{cgiurl}) {
83 $template->param(editurl => cgiurl(do => "edit", page => pagetitle($page, 1)));
84 $template->param(prefsurl => cgiurl(do => "prefs"));
88 if (length $config{historyurl}) {
89 my $u=$config{historyurl};
90 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
91 $template->param(historyurl => $u);
94 if ($config{discussion}) {
95 my $discussionlink=gettext("discussion");
96 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
97 (length $config{cgiurl} ||
98 exists $links{$page."/".$discussionlink})) {
99 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
105 $template->param(have_actions => 1);
108 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
109 my ($backlinks, $more_backlinks);
110 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
111 $backlinks=\@backlinks;
115 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
116 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
120 title => $page eq 'index'
122 : pagetitle(basename($page)),
123 wikiname => $config{wikiname},
124 parentlinks => [parentlinks($page)],
126 backlinks => $backlinks,
127 more_backlinks => $more_backlinks,
128 mtime => displaytime($pagemtime{$page}),
129 ctime => displaytime($pagectime{$page}),
130 baseurl => baseurl($page),
133 run_hooks(pagetemplate => sub {
134 shift->(page => $page, destpage => $page, template => $template);
137 $content=$template->output;
139 run_hooks(format => sub {
152 my $type=pagetype($file);
154 my $srcfile=srcfile($file);
155 my $content=readfile($srcfile);
156 my $page=pagename($file);
157 will_render($page, htmlpage($page), 1);
159 if ($config{discussion}) {
160 # Discussion links are a special case since they're
161 # not in the text of the page, but on its template.
162 $links{$page}=[ $page."/".gettext("discussion") ];
168 run_hooks(scan => sub {
175 # Preprocess in scan-only mode.
176 preprocess($page, $page, $content, 1);
179 will_render($file, $file, 1);
183 sub fast_file_copy (@) { #{{{
191 my ($len, $buf, $written);
192 while ($len = sysread $srcfd, $buf, $blksize) {
193 if (! defined $len) {
194 next if $! =~ /^Interrupted/;
195 error("failed to read $srcfile: $!", $cleanup);
199 defined($written = syswrite $destfd, $buf, $len, $offset)
200 or error("failed to write $destfile: $!", $cleanup);
207 sub render ($) { #{{{
210 my $type=pagetype($file);
211 my $srcfile=srcfile($file);
213 my $page=pagename($file);
214 delete $depends{$page};
215 will_render($page, htmlpage($page), 1);
216 return if $type=~/^_/;
218 my $content=htmlize($page, $page, $type,
219 linkify($page, $page,
220 preprocess($page, $page,
222 readfile($srcfile)))));
224 my $output=htmlpage($page);
225 writefile($output, $config{destdir}, genpage($page, $content));
228 delete $depends{$file};
229 will_render($file, $file, 1);
231 if ($config{hardlink}) {
232 prep_writefile($file, $config{destdir});
233 unlink($config{destdir}."/".$file);
234 if (link($srcfile, $config{destdir}."/".$file)) {
237 # if hardlink fails, fall back to copying
240 my $srcfd=readfile($srcfile, 1, 1);
241 writefile($file, $config{destdir}, undef, 1, sub {
242 fast_file_copy($srcfile, $file, $srcfd, @_);
251 my $dir=dirname($file);
252 while (rmdir($dir)) {
257 sub refresh () { #{{{
258 # security check, avoid following symlinks in the srcdir path
259 my $test=$config{srcdir};
260 while (length $test) {
262 error("symlink found in srcdir path ($test)");
264 unless ($test=~s/\/+$//) {
265 $test=dirname($test);
269 run_hooks(refresh => sub { shift->() });
271 # find existing pages
274 eval q{use File::Find};
280 if (file_pruned($_, $config{srcdir})) {
281 $File::Find::prune=1;
283 elsif (! -l $_ && ! -d _) {
284 my ($f)=/$config{wiki_file_regexp}/; # untaint
286 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
289 $f=~s/^\Q$config{srcdir}\E\/?//;
291 $exists{pagename($f)}=1;
296 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
301 if (file_pruned($_, $dir)) {
302 $File::Find::prune=1;
304 elsif (! -l $_ && ! -d _) {
305 my ($f)=/$config{wiki_file_regexp}/; # untaint
307 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
310 $f=~s/^\Q$dir\E\/?//;
312 # override attacks; see
314 if (! -l "$config{srcdir}/$f" &&
316 my $page=pagename($f);
317 if (! $exists{$page}) {
328 my (%rendered, @add, @del, @internal);
329 # check for added or removed pages
330 foreach my $file (@files) {
331 my $page=pagename($file);
332 $pagesources{$page}=$file;
333 if (! $pagemtime{$page}) {
334 if (isinternal($page)) {
335 push @internal, $file;
339 if ($config{getctime} && -e "$config{srcdir}/$file") {
341 my $time=rcs_getctime("$config{srcdir}/$file");
342 $pagectime{$page}=$time;
349 $pagecase{lc $page}=$page;
350 if (! exists $pagectime{$page}) {
351 $pagectime{$page}=(srcfile_stat($file))[10];
355 foreach my $page (keys %pagemtime) {
356 if (! $exists{$page}) {
357 if (isinternal($page)) {
358 push @internal, $pagesources{$page};
361 debug(sprintf(gettext("removing old page %s"), $page));
362 push @del, $pagesources{$page};
365 $renderedfiles{$page}=[];
367 prune($config{destdir}."/".$_)
368 foreach @{$oldrenderedfiles{$page}};
369 delete $pagesources{$page};
370 foreach (keys %destsources) {
371 if ($destsources{$_} eq $page) {
372 delete $destsources{$_};
378 # find changed and new files
380 foreach my $file (@files) {
381 my $page=pagename($file);
382 my ($srcfile, @stat)=srcfile_stat($file);
383 if (! exists $pagemtime{$page} ||
384 $stat[9] > $pagemtime{$page} ||
385 $forcerebuild{$page}) {
386 $pagemtime{$page}=$stat[9];
387 if (isinternal($page)) {
388 push @internal, $file;
389 # Preprocess internal page in scan-only mode.
390 preprocess($page, $page, readfile($srcfile), 1);
393 push @needsbuild, $file;
397 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
399 # scan and render files
400 foreach my $file (@needsbuild) {
401 debug(sprintf(gettext("scanning %s"), $file));
404 calculate_backlinks();
405 foreach my $file (@needsbuild) {
406 debug(sprintf(gettext("rendering %s"), $file));
410 foreach my $file (@internal) {
411 # internal pages are not rendered
412 my $page=pagename($file);
413 delete $depends{$page};
414 foreach my $old (@{$renderedfiles{$page}}) {
415 delete $destsources{$old};
417 $renderedfiles{$page}=[];
420 # rebuild pages that link to added or removed pages
422 foreach my $f (@add, @del) {
424 foreach my $page (keys %{$backlinks{$p}}) {
425 my $file=$pagesources{$page};
426 next if $rendered{$file};
427 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
434 if (%rendered || @del || @internal) {
435 my @changed=(keys %rendered, @del);
437 # rebuild dependant pages
438 foreach my $f (@files) {
439 next if $rendered{$f};
441 if (exists $depends{$p}) {
442 # only consider internal files
443 # if the page explicitly depends on such files
444 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
446 my $page=pagename($file);
447 if (pagespec_match($page, $depends{$p}, location => $p)) {
448 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
457 # handle backlinks; if a page has added/removed links,
458 # update the pages it links to
460 foreach my $file (@changed) {
461 my $page=pagename($file);
463 if (exists $links{$page}) {
464 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
466 (! exists $oldlinks{$page} ||
467 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
468 $linkchanged{$link}=1;
472 if (exists $oldlinks{$page}) {
473 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
475 (! exists $links{$page} ||
476 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
477 $linkchanged{$link}=1;
483 foreach my $link (keys %linkchanged) {
484 my $linkfile=$pagesources{$link};
485 if (defined $linkfile) {
486 next if $rendered{$linkfile};
487 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
489 $rendered{$linkfile}=1;
494 # remove no longer rendered files
495 foreach my $src (keys %rendered) {
496 my $page=pagename($src);
497 foreach my $file (@{$oldrenderedfiles{$page}}) {
498 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
499 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
500 prune($config{destdir}."/".$file);
506 run_hooks(delete => sub { shift->(@del) });
509 run_hooks(change => sub { shift->(keys %rendered) });
513 sub commandline_render () { #{{{
518 my $srcfile=possibly_foolish_untaint($config{render});
520 $file=~s/\Q$config{srcdir}\E\/?//;
522 my $type=pagetype($file);
523 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
524 my $content=readfile($srcfile);
525 my $page=pagename($file);
526 $pagesources{$page}=$file;
527 $content=filter($page, $page, $content);
528 $content=preprocess($page, $page, $content);
529 $content=linkify($page, $page, $content);
530 $content=htmlize($page, $page, $type, $content);
531 $pagemtime{$page}=(stat($srcfile))[9];
533 print genpage($page, $content);