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 genpage ($$) { #{{{
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 $template->param(prefsurl => cgiurl(do => "prefs"));
71 if (length $config{historyurl}) {
72 my $u=$config{historyurl};
73 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
74 $template->param(historyurl => $u);
77 if ($config{discussion}) {
78 my $discussionlink=gettext("discussion");
79 if ($page !~ /.*\/\Q$discussionlink\E$/ &&
80 (length $config{cgiurl} ||
81 exists $links{$page."/".$discussionlink})) {
82 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), noimageinline => 1, forcesubpage => 1));
88 $template->param(have_actions => 1);
91 my @backlinks=sort { $a->{page} cmp $b->{page} } backlinks($page);
92 my ($backlinks, $more_backlinks);
93 if (@backlinks <= $config{numbacklinks} || ! $config{numbacklinks}) {
94 $backlinks=\@backlinks;
98 $backlinks=[@backlinks[0..$config{numbacklinks}-1]];
99 $more_backlinks=[@backlinks[$config{numbacklinks}..$#backlinks]];
103 title => $page eq 'index'
105 : pagetitle(basename($page)),
106 wikiname => $config{wikiname},
108 backlinks => $backlinks,
109 more_backlinks => $more_backlinks,
110 mtime => displaytime($pagemtime{$page}),
111 ctime => displaytime($pagectime{$page}),
112 baseurl => baseurl($page),
115 run_hooks(pagetemplate => sub {
116 shift->(page => $page, destpage => $page, template => $template);
119 $content=$template->output;
121 run_hooks(format => sub {
134 my $type=pagetype($file);
136 my $srcfile=srcfile($file);
137 my $content=readfile($srcfile);
138 my $page=pagename($file);
139 will_render($page, htmlpage($page), 1);
141 if ($config{discussion}) {
142 # Discussion links are a special case since they're
143 # not in the text of the page, but on its template.
144 $links{$page}=[ $page."/".gettext("discussion") ];
150 run_hooks(scan => sub {
157 # Preprocess in scan-only mode.
158 preprocess($page, $page, $content, 1);
161 will_render($file, $file, 1);
165 sub fast_file_copy (@) { #{{{
173 my ($len, $buf, $written);
174 while ($len = sysread $srcfd, $buf, $blksize) {
175 if (! defined $len) {
176 next if $! =~ /^Interrupted/;
177 error("failed to read $srcfile: $!", $cleanup);
181 defined($written = syswrite $destfd, $buf, $len, $offset)
182 or error("failed to write $destfile: $!", $cleanup);
189 sub render ($) { #{{{
192 my $type=pagetype($file);
193 my $srcfile=srcfile($file);
195 my $page=pagename($file);
196 delete $depends{$page};
197 will_render($page, htmlpage($page), 1);
198 return if $type=~/^_/;
200 my $content=htmlize($page, $page, $type,
201 linkify($page, $page,
202 preprocess($page, $page,
204 readfile($srcfile)))));
206 my $output=htmlpage($page);
207 writefile($output, $config{destdir}, genpage($page, $content));
210 delete $depends{$file};
211 will_render($file, $file, 1);
213 if ($config{hardlink}) {
214 # only hardlink if owned by same user
215 my @stat=stat($srcfile);
216 if ($stat[4] == $>) {
217 prep_writefile($file, $config{destdir});
218 unlink($config{destdir}."/".$file);
219 if (link($srcfile, $config{destdir}."/".$file)) {
223 # if hardlink fails, fall back to copying
226 my $srcfd=readfile($srcfile, 1, 1);
227 writefile($file, $config{destdir}, undef, 1, sub {
228 fast_file_copy($srcfile, $file, $srcfd, @_);
237 my $dir=dirname($file);
238 while (rmdir($dir)) {
243 sub refresh () { #{{{
244 # security check, avoid following symlinks in the srcdir path
245 my $test=$config{srcdir};
246 while (length $test) {
248 error("symlink found in srcdir path ($test)");
250 unless ($test=~s/\/+$//) {
251 $test=dirname($test);
255 run_hooks(refresh => sub { shift->() });
257 # find existing pages
260 eval q{use File::Find};
266 if (file_pruned($_, $config{srcdir})) {
267 $File::Find::prune=1;
269 elsif (! -l $_ && ! -d _) {
270 my ($f)=/$config{wiki_file_regexp}/; # untaint
272 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
275 $f=~s/^\Q$config{srcdir}\E\/?//;
277 $exists{pagename($f)}=1;
282 foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) {
287 if (file_pruned($_, $dir)) {
288 $File::Find::prune=1;
290 elsif (! -l $_ && ! -d _) {
291 my ($f)=/$config{wiki_file_regexp}/; # untaint
293 warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
296 $f=~s/^\Q$dir\E\/?//;
298 # override attacks; see
300 if (! -l "$config{srcdir}/$f" &&
302 my $page=pagename($f);
303 if (! $exists{$page}) {
314 my (%rendered, @add, @del, @internal);
315 # check for added or removed pages
316 foreach my $file (@files) {
317 my $page=pagename($file);
318 $pagesources{$page}=$file;
319 if (! $pagemtime{$page}) {
320 if (isinternal($page)) {
321 push @internal, $file;
325 if ($config{getctime} && -e "$config{srcdir}/$file") {
327 my $time=rcs_getctime("$config{srcdir}/$file");
328 $pagectime{$page}=$time;
335 $pagecase{lc $page}=$page;
336 if (! exists $pagectime{$page}) {
337 $pagectime{$page}=(srcfile_stat($file))[10];
341 foreach my $page (keys %pagemtime) {
342 if (! $exists{$page}) {
343 if (isinternal($page)) {
344 push @internal, $pagesources{$page};
347 debug(sprintf(gettext("removing old page %s"), $page));
348 push @del, $pagesources{$page};
351 $renderedfiles{$page}=[];
353 prune($config{destdir}."/".$_)
354 foreach @{$oldrenderedfiles{$page}};
355 delete $pagesources{$page};
356 foreach (keys %destsources) {
357 if ($destsources{$_} eq $page) {
358 delete $destsources{$_};
364 # find changed and new files
366 foreach my $file (@files) {
367 my $page=pagename($file);
368 my ($srcfile, @stat)=srcfile_stat($file);
369 if (! exists $pagemtime{$page} ||
370 $stat[9] > $pagemtime{$page} ||
371 $forcerebuild{$page}) {
372 $pagemtime{$page}=$stat[9];
373 if (isinternal($page)) {
374 push @internal, $file;
375 # Preprocess internal page in scan-only mode.
376 preprocess($page, $page, readfile($srcfile), 1);
379 push @needsbuild, $file;
383 run_hooks(needsbuild => sub { shift->(\@needsbuild) });
385 # scan and render files
386 foreach my $file (@needsbuild) {
387 debug(sprintf(gettext("scanning %s"), $file));
390 calculate_backlinks();
391 foreach my $file (@needsbuild) {
392 debug(sprintf(gettext("rendering %s"), $file));
396 foreach my $file (@internal) {
397 # internal pages are not rendered
398 my $page=pagename($file);
399 delete $depends{$page};
400 foreach my $old (@{$renderedfiles{$page}}) {
401 delete $destsources{$old};
403 $renderedfiles{$page}=[];
406 # rebuild pages that link to added or removed pages
408 foreach my $f (@add, @del) {
410 foreach my $page (keys %{$backlinks{$p}}) {
411 my $file=$pagesources{$page};
412 next if $rendered{$file};
413 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
420 if (%rendered || @del || @internal) {
421 my @changed=(keys %rendered, @del);
423 # rebuild dependant pages
424 foreach my $f (@files) {
425 next if $rendered{$f};
427 if (exists $depends{$p}) {
428 # only consider internal files
429 # if the page explicitly depends on such files
430 foreach my $file (@changed, $depends{$p}=~/internal\(/ ? @internal : ()) {
432 my $page=pagename($file);
433 if (pagespec_match($page, $depends{$p}, location => $p)) {
434 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
443 # handle backlinks; if a page has added/removed links,
444 # update the pages it links to
446 foreach my $file (@changed) {
447 my $page=pagename($file);
449 if (exists $links{$page}) {
450 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
452 (! exists $oldlinks{$page} ||
453 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
454 $linkchanged{$link}=1;
458 if (exists $oldlinks{$page}) {
459 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
461 (! exists $links{$page} ||
462 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
463 $linkchanged{$link}=1;
469 foreach my $link (keys %linkchanged) {
470 my $linkfile=$pagesources{$link};
471 if (defined $linkfile) {
472 next if $rendered{$linkfile};
473 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
475 $rendered{$linkfile}=1;
480 # remove no longer rendered files
481 foreach my $src (keys %rendered) {
482 my $page=pagename($src);
483 foreach my $file (@{$oldrenderedfiles{$page}}) {
484 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
485 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
486 prune($config{destdir}."/".$file);
492 run_hooks(delete => sub { shift->(@del) });
495 run_hooks(change => sub { shift->(keys %rendered) });
499 sub commandline_render () { #{{{
504 my $srcfile=possibly_foolish_untaint($config{render});
506 $file=~s/\Q$config{srcdir}\E\/?//;
508 my $type=pagetype($file);
509 die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
510 my $content=readfile($srcfile);
511 my $page=pagename($file);
512 $pagesources{$page}=$file;
513 $content=filter($page, $page, $content);
514 $content=preprocess($page, $page, $content);
515 $content=linkify($page, $page, $content);
516 $content=htmlize($page, $page, $type, $content);
517 $pagemtime{$page}=(stat($srcfile))[9];
519 print genpage($page, $content);