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 return (stat($file))[9];
158 my $type=pagetype($file);
160 my $srcfile=srcfile($file);
161 my $content=readfile($srcfile);
162 my $page=pagename($file);
163 will_render($page, htmlpage($page), 1);
165 # Always needs to be done, since filters might add links
167 $content=filter($page, $page, $content);
169 if ($config{discussion}) {
170 # Discussion links are a special case since they're
171 # not in the text of the page, but on its template.
172 $links{$page}=[ $page."/".gettext("discussion") ];
178 run_hooks(scan => sub {
185 # Preprocess in scan-only mode.
186 preprocess($page, $page, $content, 1);
189 will_render($file, $file, 1);
193 sub render ($) { #{{{
196 my $type=pagetype($file);
197 my $srcfile=srcfile($file);
199 my $page=pagename($file);
200 delete $depends{$page};
201 will_render($page, htmlpage($page), 1);
202 return if $type=~/^_/;
204 my $content=htmlize($page, $type,
205 linkify($page, $page,
206 preprocess($page, $page,
208 readfile($srcfile)))));
210 my $output=htmlpage($page);
211 writefile($output, $config{destdir}, genpage($page, $content));
214 delete $depends{$file};
215 will_render($file, $file, 1);
217 if ($config{hardlink}) {
218 prep_writefile($file, $config{destdir});
219 unlink($config{destdir}."/".$file);
220 if (link($srcfile, $config{destdir}."/".$file)) {
223 # if hardlink fails, fall back top copying
226 my $srcfd=readfile($srcfile, 1, 1);
227 writefile($file, $config{destdir}, undef, 1, sub {
232 my ($len, $buf, $written);
233 while ($len = sysread $srcfd, $buf, $blksize) {
234 if (! defined $len) {
235 next if $! =~ /^Interrupted/;
236 error("failed to read $srcfile: $!", $cleanup);
240 defined($written = syswrite $destfd, $buf, $len, $offset)
241 or error("failed to write $file: $!", $cleanup);
254 my $dir=dirname($file);
255 while (rmdir($dir)) {
260 sub refresh () { #{{{
261 # security check, avoid following symlinks in the srcdir path
262 my $test=$config{srcdir};
263 while (length $test) {
265 error("symlink found in srcdir path ($test)");
267 unless ($test=~s/\/+$//) {
268 $test=dirname($test);
272 run_hooks(refresh => sub { shift->() });
274 # find existing pages
277 eval q{use File::Find};
283 if (file_pruned($_, $config{srcdir})) {
284 $File::Find::prune=1;
286 elsif (! -d $_ && ! -l $_) {
287 my ($f)=/$config{wiki_file_regexp}/; # untaint
289 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
292 $f=~s/^\Q$config{srcdir}\E\/?//;
294 $exists{pagename($f)}=1;
299 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
304 if (file_pruned($_, $dir)) {
305 $File::Find::prune=1;
307 elsif (! -d $_ && ! -l $_) {
308 my ($f)=/$config{wiki_file_regexp}/; # untaint
310 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
313 $f=~s/^\Q$dir\E\/?//;
315 # override attacks; see
317 if (! -e "$config{srcdir}/$f" &&
318 ! -l "$config{srcdir}/$f") {
319 my $page=pagename($f);
320 if (! $exists{$page}) {
331 my (%rendered, @add, @del, @internal);
333 # check for added or removed pages
334 foreach my $file (@files) {
335 my $page=pagename($file);
336 $pagesources{$page}=$file;
337 if (! $pagemtime{$page}) {
338 if (isinternal($page)) {
339 push @internal, $file;
343 if ($config{getctime} && -e "$config{srcdir}/$file") {
345 my $time=rcs_getctime("$config{srcdir}/$file");
346 $pagectime{$page}=$time;
353 $pagecase{lc $page}=$page;
354 if (! exists $pagectime{$page}) {
355 $pagectime{$page}=mtime(srcfile($file));
359 foreach my $page (keys %pagemtime) {
360 if (! $exists{$page}) {
361 if (isinternal($page)) {
362 push @internal, $pagesources{$page};
365 debug(sprintf(gettext("removing old page %s"), $page));
366 push @del, $pagesources{$page};
369 $renderedfiles{$page}=[];
371 prune($config{destdir}."/".$_)
372 foreach @{$oldrenderedfiles{$page}};
373 delete $pagesources{$page};
374 foreach (keys %destsources) {
375 if ($destsources{$_} eq $page) {
376 delete $destsources{$_};
382 # find changed and new files
384 foreach my $file (@files) {
385 my $page=pagename($file);
387 my $mtime=mtime(srcfile($file));
388 if (! exists $pagemtime{$page} ||
389 $mtime > $pagemtime{$page} ||
390 $forcerebuild{$page}) {
391 $pagemtime{$page}=$mtime;
392 if (isinternal($page)) {
393 push @internal, $file;
394 # Preprocess internal page in scan-only mode.
395 preprocess($page, $page, readfile(srcfile($file)), 1);
398 push @needsbuild, $file;
402 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
404 # scan and render files
405 foreach my $file (@needsbuild) {
406 debug(sprintf(gettext("scanning %s"), $file));
409 calculate_backlinks();
410 foreach my $file (@needsbuild) {
411 debug(sprintf(gettext("rendering %s"), $file));
415 foreach my $file (@internal) {
416 # internal pages are not rendered
417 my $page=pagename($file);
418 delete $depends{$page};
419 foreach my $old (@{$renderedfiles{$page}}) {
420 delete $destsources{$old};
422 $renderedfiles{$page}=[];
425 # rebuild pages that link to added or removed pages
427 foreach my $f (@add, @del) {
429 foreach my $page (keys %{$backlinks{$p}}) {
430 my $file=$pagesources{$page};
431 next if $rendered{$file};
432 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
439 if (%rendered || @del || @internal) {
440 my @changed=(keys %rendered, @del);
442 # rebuild dependant pages
443 foreach my $f (@files) {
444 next if $rendered{$f};
446 if (exists $depends{$p}) {
447 # only consider internal files
448 # if the page explicitly depends on such files
449 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
451 my $page=pagename($file);
452 if (pagespec_match($page, $depends{$p}, location => $p)) {
453 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
462 # handle backlinks; if a page has added/removed links,
463 # update the pages it links to
465 foreach my $file (@changed) {
466 my $page=pagename($file);
468 if (exists $links{$page}) {
469 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
471 (! exists $oldlinks{$page} ||
472 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
473 $linkchanged{$link}=1;
477 if (exists $oldlinks{$page}) {
478 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
480 (! exists $links{$page} ||
481 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
482 $linkchanged{$link}=1;
488 foreach my $link (keys %linkchanged) {
489 my $linkfile=$pagesources{$link};
490 if (defined $linkfile) {
491 next if $rendered{$linkfile};
492 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
494 $rendered{$linkfile}=1;
499 # remove no longer rendered files
500 foreach my $src (keys %rendered) {
501 my $page=pagename($src);
502 foreach my $file (@{$oldrenderedfiles{$page}}) {
503 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
504 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
505 prune($config{destdir}."/".$file);
511 run_hooks(delete => sub { shift->(@del) });
514 run_hooks(change => sub { shift->(keys %rendered) });
518 sub commandline_render () { #{{{
525 my $srcfile=possibly_foolish_untaint($config{render});
527 $file=~s/\Q$config{srcdir}\E\/?//;
529 my $type=pagetype($file);
530 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
531 my $content=readfile($srcfile);
532 my $page=pagename($file);
533 $pagesources{$page}=$file;
534 $content=filter($page, $page, $content);
535 $content=preprocess($page, $page, $content);
536 $content=linkify($page, $page, $content);
537 $content=htmlize($page, $type, $content);
538 $pagemtime{$page}=mtime($srcfile);
540 print genpage($page, $content);